class 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);

		// Apply the new set of InternalFeatures onto the transaction: (ID may be filled in now)
		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);
	}

}