java-8odataentityolingo

Adding custom logic in Odata Create Entity call in java code


In the project am using olingo 2.0.12 jar in the java code.

During the create Entity service call ,

  1. Is there a way to check for which entity data insert requested and,
  2. Alter column values / append new column values before data persisted?

Is there a way to add above?

Code snippet given below,

public class A extends ODataJPADefaultProcessor{
          @Override
public ODataResponse createEntity(final PostUriInfo uriParserResultView, final InputStream content,
        final String requestContentType, final String contentType) throws ODataJPAModelException,
        ODataJPARuntimeException, ODataNotFoundException, EdmException, EntityProviderException {
        
        // Need to check the entity name and need to alter/add column values
        }
                  }

Solution

  • Yes one of the possible ways would be to create your own CustomODataJPAProcessor which extends ODataJPADefaultProcessor.

    You will have to register this in JPAServiceFactory by overriding the method

        @Override
        public ODataSingleProcessor createCustomODataProcessor(ODataJPAContext oDataJPAContext) {
            return new CustomODataJPAProcessor(this.oDataJPAContext);
        }
    

    Now Olingo will use CustomODataJPAProcessor which can implement the following code to check the entities and transform them if needed

    Sample code of CustomODataJPAProcessor

    public class CustomODataJPAProcessor extends ODataJPADefaultProcessor {
    
        Logger LOG = LoggerFactory.getLogger(this.getClass());
    
        public CustomODataJPAProcessor(ODataJPAContext oDataJPAContext) {
            super(oDataJPAContext);
    
        }
    
        @Override
        public ODataResponse createEntity(final PostUriInfo uriParserResultView, final InputStream content,
                final String requestContentType, final String contentType) throws ODataException {
            ODataResponse oDataResponse = null;
            oDataJPAContext.setODataContext(getContext());
            InputStream forwardedInputStream = content;
            try {
                if (uriParserResultView.getTargetEntitySet().getName().equals("Students")) {
                    LOG.info("Students Entity Set Executed");
    
                    if (requestContentType.equalsIgnoreCase(ContentType.APPLICATION_JSON.toContentTypeString())) {
                        @SuppressWarnings("deprecation")
                        JsonElement elem = new JsonParser().parse(new InputStreamReader(content));
                        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
                        Student s = gson.fromJson(elem, Student.class);
    
                        // Change some values
                        s.setStudentID("Test" + s.getStudentID());
                        forwardedInputStream = new ByteArrayInputStream(gson.toJson(s).getBytes());
    
                    }
                }
                Object createdJpaEntity = jpaProcessor.process(uriParserResultView, forwardedInputStream,
                        requestContentType);
                oDataResponse = responseBuilder.build(uriParserResultView, createdJpaEntity, contentType);
            } catch (JsonIOException | JsonSyntaxException e) {
                throw new RuntimeException(e);
            } finally {
                close();
            }
    
            return oDataResponse;
        }
    }
    

    In Summery

    1. Register your custom org.apache.olingo.odata2.service.factory Code Link
    2. Create your own CustomODataJPAProcessor Code Link
    3. Override createCustomODataProcessor in JPAServiceFactory to use the custom processor Code Link