@Test public void checkPatchCreateBindingLinkBetweenTwoEntities() throws ODataJPAModelException, ODataJPAProcessException { final JPAEntityType et = helper.getJPAEntityType("AdministrativeDivisions"); final JPAAssociationPath path = et.getAssociationPath("Children"); final Map<JPAAssociationPath, List<JPARequestLink>> relationLinks = new HashMap<>(); final JPARequestLink link = mock(JPARequestLink.class); final Map<String, Object> childKeys = new HashMap<>(); final AdministrativeDivision parent = new AdministrativeDivision(new AdministrativeDivisionKey("Eurostat", "NUTS1", "DE5")); final AdministrativeDivision child = new AdministrativeDivision(new AdministrativeDivisionKey("Eurostat", "NUTS2", "DE51")); doReturn(et).when(requestEntity).getEntityType(); keys.put("divisionCode", "DE5"); keys.put("codeID", "NUTS1"); keys.put("codePublisher", "Eurostat"); doReturn(relationLinks).when(requestEntity).getRelationLinks(); relationLinks.put(path, Arrays.asList(link)); doReturn(et).when(link).getEntityType(); doReturn(childKeys).when(link).getRelatedKeys(); childKeys.put("divisionCode", "DE51"); childKeys.put("codeID", "NUTS2"); childKeys.put("codePublisher", "Eurostat"); doReturn(childKeys).when(link).getValues(); doReturn(parent).when(em).find(eq(et.getTypeClass()), eq(parent.getKey())); doReturn(child).when(em).find(eq(et.getTypeClass()), eq(child.getKey())); final JPAUpdateResult act = cut.updateEntity(requestEntity, em, HttpMethod.DELETE); assertNotNull(act); assertEquals(parent, act.getModifyedEntity()); assertEquals(1, parent.getChildren().size()); assertEquals("DE51", parent.getChildren().get(0).getDivisionCode()); }
@Override public JPAUpdateResult updateEntity(final JPARequestEntity requestEntity, final EntityManager em, final HttpMethod method) throws ODataJPAProcessException { if (method == HttpMethod.PATCH || method == HttpMethod.DELETE) { final Object instance = em.find(requestEntity.getEntityType().getTypeClass(), requestEntity.getModifyUtil() .createPrimaryKey(requestEntity.getEntityType(), requestEntity.getKeys(), requestEntity.getEntityType())); if (instance == null) { throw new JPAExampleModifyException(ENTITY_NOT_FOUND, HttpStatusCode.NOT_FOUND); } requestEntity.getModifyUtil().setAttributesDeep(requestEntity.getData(), instance, requestEntity.getEntityType()); updateLinks(requestEntity, em, instance); return new JPAUpdateResult(false, instance); } return super.updateEntity(requestEntity, em, method); }
JPAExampleCUDRequestHandler extends JPAAbstractCUDRequestHandler { @Override public JPAUpdateResult updateEntity(final JPARequestEntity requestEntity, final EntityManager em, final HttpMethod method) throws ODataJPAProcessException { if (method == HttpMethod.PATCH || method == HttpMethod.DELETE) { final Object instance = em.find(requestEntity.getEntityType().getTypeClass(), requestEntity.getModifyUtil() .createPrimaryKey(requestEntity.getEntityType(), requestEntity.getKeys(), requestEntity.getEntityType())); if (instance == null) { throw new JPAExampleModifyException(ENTITY_NOT_FOUND, HttpStatusCode.NOT_FOUND); } requestEntity.getModifyUtil().setAttributesDeep(requestEntity.getData(), instance, requestEntity.getEntityType()); updateLinks(requestEntity, em, instance); return new JPAUpdateResult(false, instance); } return super.updateEntity(requestEntity, em, method); } }
JPAExampleCUDRequestHandler extends JPAAbstractCUDRequestHandler { @Override public JPAUpdateResult updateEntity(final JPARequestEntity requestEntity, final EntityManager em, final HttpMethod method) throws ODataJPAProcessException { if (method == HttpMethod.PATCH || method == HttpMethod.DELETE) { final Object instance = em.find(requestEntity.getEntityType().getTypeClass(), requestEntity.getModifyUtil() .createPrimaryKey(requestEntity.getEntityType(), requestEntity.getKeys(), requestEntity.getEntityType())); if (instance == null) { throw new JPAExampleModifyException(ENTITY_NOT_FOUND, HttpStatusCode.NOT_FOUND); } requestEntity.getModifyUtil().setAttributesDeep(requestEntity.getData(), instance, requestEntity.getEntityType()); updateLinks(requestEntity, em, instance); return new JPAUpdateResult(false, instance); } return super.updateEntity(requestEntity, em, method); } JPAExampleCUDRequestHandler(); }
JPAExampleCUDRequestHandler extends JPAAbstractCUDRequestHandler { @Override public JPAUpdateResult updateEntity(final JPARequestEntity requestEntity, final EntityManager em, final HttpMethod method) throws ODataJPAProcessException { if (method == HttpMethod.PATCH || method == HttpMethod.DELETE) { final Object instance = em.find(requestEntity.getEntityType().getTypeClass(), requestEntity.getModifyUtil() .createPrimaryKey(requestEntity.getEntityType(), requestEntity.getKeys(), requestEntity.getEntityType())); if (instance == null) { throw new JPAExampleModifyException(ENTITY_NOT_FOUND, HttpStatusCode.NOT_FOUND); } requestEntity.getModifyUtil().setAttributesDeep(requestEntity.getData(), instance, requestEntity.getEntityType()); updateLinks(requestEntity, em, instance); return new JPAUpdateResult(false, instance); } return super.updateEntity(requestEntity, em, method); } JPAExampleCUDRequestHandler(); @Override Object createEntity(final JPARequestEntity requestEntity, final EntityManager em); @Override void deleteEntity(JPARequestEntity requestEntity, EntityManager em); @Override JPAUpdateResult updateEntity(final JPARequestEntity requestEntity, final EntityManager em,
      final HttpMethod method); @Override void validateChanges(EntityManager em); }
JPAExampleCUDRequestHandler extends JPAAbstractCUDRequestHandler { @Override public JPAUpdateResult updateEntity(final JPARequestEntity requestEntity, final EntityManager em, final HttpMethod method) throws ODataJPAProcessException { if (method == HttpMethod.PATCH || method == HttpMethod.DELETE) { final Object instance = em.find(requestEntity.getEntityType().getTypeClass(), requestEntity.getModifyUtil() .createPrimaryKey(requestEntity.getEntityType(), requestEntity.getKeys(), requestEntity.getEntityType())); if (instance == null) { throw new JPAExampleModifyException(ENTITY_NOT_FOUND, HttpStatusCode.NOT_FOUND); } requestEntity.getModifyUtil().setAttributesDeep(requestEntity.getData(), instance, requestEntity.getEntityType()); updateLinks(requestEntity, em, instance); return new JPAUpdateResult(false, instance); } return super.updateEntity(requestEntity, em, method); } JPAExampleCUDRequestHandler(); @Override Object createEntity(final JPARequestEntity requestEntity, final EntityManager em); @Override void deleteEntity(JPARequestEntity requestEntity, EntityManager em); @Override JPAUpdateResult updateEntity(final JPARequestEntity requestEntity, final EntityManager em,
      final HttpMethod method); @Override void validateChanges(EntityManager em);  }