PersistTransactionCommand implements CommandHasRequest<PersistTransactionRequest, PersistTransactionResponse> { @Override public void execute(PersistTransactionRequest request, PersistTransactionResponse response) throws Exception { if (null == request.getFeatureTransaction()) { throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "featureTransaction"); } if (null == request.getFeatureTransaction().getLayerId()) { throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "featureTransaction.layerId"); } if (null == request.getCrs()) { throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "crs"); } FeatureTransaction featureTransaction = request.getFeatureTransaction(); if (featureTransaction == null) { return; } List<InternalFeature> oldFeatures = new ArrayList<InternalFeature>(); List<InternalFeature> newFeatures = new ArrayList<InternalFeature>(); if (featureTransaction.getOldFeatures() != null) { for (int i = 0; i < featureTransaction.getOldFeatures().length; i++) { oldFeatures.add(converter.toInternal(featureTransaction.getOldFeatures()[i])); } } if (featureTransaction.getNewFeatures() != null) { for (int i = 0; i < featureTransaction.getNewFeatures().length; i++) { newFeatures.add(converter.toInternal(featureTransaction.getNewFeatures()[i])); } } layerService.saveOrUpdate(featureTransaction.getLayerId(), geoService.getCrs(request.getCrs()), oldFeatures, newFeatures); if (featureTransaction.getNewFeatures() != null) { Feature[] resultFeatures = new Feature[newFeatures.size()]; for (int i = 0; i < newFeatures.size(); i++) { InternalFeature internalFeature = newFeatures.get(i); resultFeatures[i] = converter.toDto(internalFeature); } featureTransaction.setNewFeatures(resultFeatures); } response.setFeatureTransaction(featureTransaction); }  @Override PersistTransactionRequest getEmptyCommandRequest(); @Override PersistTransactionResponse getEmptyCommandResponse(); @Override void execute(PersistTransactionRequest request, PersistTransactionResponse response);  }
@Test public void testPersistEmptyTransaction() throws Exception { PersistTransactionRequest request = new PersistTransactionRequest(); request.setCrs(CRS); FeatureTransaction featureTransaction = new FeatureTransaction(); featureTransaction.setLayerId(LAYER_ID); request.setFeatureTransaction(featureTransaction); PersistTransactionResponse response = (PersistTransactionResponse) dispatcher.execute( PersistTransactionRequest.COMMAND, request, null, "en"); if (response.isError()) { response.getErrors().get(0).printStackTrace(); } Assert.assertFalse(response.isError()); Assert.assertNotNull(response.getFeatureTransaction()); }
@Test @DirtiesContext public void testPersistAddFeatureTransaction() throws Exception { PersistTransactionRequest request = new PersistTransactionRequest(); request.setCrs(CRS); FeatureTransaction featureTransaction = new FeatureTransaction(); featureTransaction.setLayerId(LAYER_ID); Feature feature = new Feature(); GeometryFactory factory = new GeometryFactory(); Geometry circle = dtoConverter.toDto(geoService.createCircle(factory.createPoint(new Coordinate(0, 0)), 10, 10)); feature.setGeometry(circle); featureTransaction.setNewFeatures(new Feature[] {feature}); request.setFeatureTransaction(featureTransaction); PersistTransactionResponse response = (PersistTransactionResponse) dispatcher.execute( PersistTransactionRequest.COMMAND, request, null, "en"); if (response.isError()) { response.getErrors().get(0).printStackTrace(); } Assert.assertFalse(response.isError()); Assert.assertNotNull(response.getFeatureTransaction()); Feature[] newFeatures = response.getFeatureTransaction().getNewFeatures(); Assert.assertEquals(1, newFeatures.length); Assert.assertNotNull(newFeatures[0].getId()); }
@Test @DirtiesContext public void testCreateWithoutGeometry() throws Exception { PersistTransactionRequest request = new PersistTransactionRequest(); request.setCrs(CRS); FeatureTransaction featureTransaction = new FeatureTransaction(); featureTransaction.setLayerId(LAYER_ID); Feature feature = new Feature(); featureTransaction.setNewFeatures(new Feature[] {feature}); request.setFeatureTransaction(featureTransaction); PersistTransactionResponse response = (PersistTransactionResponse) dispatcher.execute( PersistTransactionRequest.COMMAND, request, null, "en"); assertThat(response.isError()).isTrue(); Throwable error = response.getErrors().get(0); assertThat(error).isInstanceOf(LayerException.class); assertThat(((LayerException) error).getExceptionCode()).isEqualTo( ExceptionCode.LAYER_EMPTY_GEOMETRY_NOT_ALLOWED); }