PageRenderTime 65ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/source/commons-aerius/aerius-commons-imaer/src/main/java/nl/overheid/aerius/controller/ImaerController.java

https://gitlab.com/AERIUS/AERIUS
Java | 179 lines | 137 code | 20 blank | 22 comment | 9 complexity | 154466e91d8e5738dbbbd0256b362c01 MD5 | raw file
  1. /*
  2. * Copyright the State of the Netherlands
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see http://www.gnu.org/licenses/.
  16. */
  17. package nl.overheid.aerius.controller;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.sql.SQLException;
  21. import java.util.List;
  22. import java.util.Locale;
  23. import java.util.Map;
  24. import java.util.Map.Entry;
  25. import java.util.Set;
  26. import java.util.function.Function;
  27. import java.util.stream.Collectors;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import nl.overheid.aerius.db.PMF;
  31. import nl.overheid.aerius.db.common.ReceptorGridSettingsRepository;
  32. import nl.overheid.aerius.db.common.sector.SectorRepository;
  33. import nl.overheid.aerius.gml.GMLWriter;
  34. import nl.overheid.aerius.importer.ImportOption;
  35. import nl.overheid.aerius.importer.ImporterFacade;
  36. import nl.overheid.aerius.importer.PermitReferenceGenerator;
  37. import nl.overheid.aerius.io.ImportableFile;
  38. import nl.overheid.aerius.schematron.EmissionSourceSchematronVisitor;
  39. import nl.overheid.aerius.schematron.ImaerSchematronValidator;
  40. import nl.overheid.aerius.schematron.SchematronValidators;
  41. import nl.overheid.aerius.shared.domain.importer.ImportProperties;
  42. import nl.overheid.aerius.shared.domain.sector.category.SectorCategories;
  43. import nl.overheid.aerius.shared.domain.v2.importer.ImportParcel;
  44. import nl.overheid.aerius.shared.domain.v2.nsl.NSLCorrection;
  45. import nl.overheid.aerius.shared.domain.v2.nsl.NSLDispersionLineFeature;
  46. import nl.overheid.aerius.shared.domain.v2.nsl.NSLMeasureFeature;
  47. import nl.overheid.aerius.shared.domain.v2.point.CalculationPointFeature;
  48. import nl.overheid.aerius.shared.domain.v2.source.EmissionSourceFeature;
  49. import nl.overheid.aerius.shared.exception.AeriusException;
  50. import nl.overheid.aerius.shared.exception.AeriusExceptionReason;
  51. import nl.overheid.aerius.util.LocaleUtils;
  52. public class ImaerController {
  53. private static final Logger LOG = LoggerFactory.getLogger(ImaerController.class);
  54. private final PMF pmf;
  55. private final GMLWriter gmlWriter;
  56. private final SchematronValidators validators;
  57. private final Map<Locale, SectorCategories> sectorCategoriesMap;
  58. public ImaerController(final PMF pmf) throws AeriusException, IOException {
  59. this.pmf = pmf;
  60. sectorCategoriesMap = LocaleUtils.getLocales().stream()
  61. .collect(Collectors.toMap(Function.identity(), locale -> {
  62. try {
  63. return SectorRepository.getSectorCategories(pmf, locale);
  64. } catch (final SQLException e) {
  65. throw new RuntimeException(e);
  66. }
  67. }));
  68. validators = new SchematronValidators();
  69. try {
  70. gmlWriter = new GMLWriter(ReceptorGridSettingsRepository.getReceptorGridSettings(pmf), new PermitReferenceGenerator());
  71. } catch (final SQLException e) {
  72. LOG.error("Failed creating GMLWriter", e);
  73. throw new AeriusException(AeriusExceptionReason.SQL_ERROR);
  74. }
  75. }
  76. public List<ImportParcel> processFile(final ImportableFile inputFile, final Set<ImportOption> options,
  77. final ImportProperties importProperties, final Locale locale) throws AeriusException {
  78. try {
  79. final List<ImportParcel> importParcels = importParcel(inputFile, importProperties, options, locale);
  80. for (final ImportParcel importParcel : importParcels) {
  81. processEmissionSources(options, importParcel);
  82. processCalculationPoints(options, importParcel);
  83. processDispersionLines(options, importParcel);
  84. processMeasures(options, importParcel);
  85. processCorrections(options, importParcel);
  86. }
  87. return importParcels;
  88. } catch (final IOException e) {
  89. LOG.error("Error during processing file", e);
  90. throw new AeriusException(AeriusExceptionReason.INTERNAL_ERROR);
  91. }
  92. }
  93. private void processEmissionSources(final Set<ImportOption> options, final ImportParcel importParcel) throws AeriusException {
  94. // TODO: do we want to keep schematron validation?
  95. // It seems like much overhead (first generating GML snippets before schematron validation) for little gain
  96. // Benefit of it is that in theory you could bundle the schematron template files for external validation
  97. // If we do want to keep it, the GML for each object should be referenced outside of the domain objects.
  98. //
  99. if (ImportOption.VALIDATE_WITH_SCHEMATRON.in(options)) {
  100. final Map<EmissionSourceFeature, String> gmlPerSource = gmlWriter.getGmlByEmissionSource(importParcel.getSituation().getEmissionSourcesList());
  101. validateWithSchematron(gmlPerSource, importParcel);
  102. }
  103. if (ImportOption.PERSIST_INPUT.in(options)) {
  104. store(importParcel.getSituation().getEmissionSourcesList());
  105. }
  106. }
  107. private void processCalculationPoints(final Set<ImportOption> options, final ImportParcel importParcel) throws AeriusException {
  108. if (ImportOption.VALIDATE_WITH_SCHEMATRON.in(options)) {
  109. final List<CalculationPointFeature> points = importParcel.getCalculationPointsList();
  110. final Map<CalculationPointFeature, String> gmlPerCalculationPoint = gmlWriter.getGMLByCalculationPoint(points);
  111. validateWithSchematron(validators.getNslCalculationPointValidator(), points, gmlPerCalculationPoint::get, importParcel);
  112. }
  113. }
  114. private void processDispersionLines(final Set<ImportOption> options, final ImportParcel importParcel) throws AeriusException {
  115. if (ImportOption.VALIDATE_WITH_SCHEMATRON.in(options)) {
  116. final List<NSLDispersionLineFeature> dispersionLines = importParcel.getSituation().getNslDispersionLinesList();
  117. final Map<NSLDispersionLineFeature, String> gmlByDispersionLine = gmlWriter.getGmlByNslDispersionLines(dispersionLines);
  118. validateWithSchematron(validators.getDispersionLineValidator(), dispersionLines, gmlByDispersionLine::get, importParcel);
  119. }
  120. }
  121. private void processMeasures(final Set<ImportOption> options, final ImportParcel importParcel) throws AeriusException {
  122. if (options.contains(ImportOption.VALIDATE_WITH_SCHEMATRON)) {
  123. final List<NSLMeasureFeature> measures = importParcel.getSituation().getNslMeasuresList();
  124. final Map<NSLMeasureFeature, String> gmlByMeasure = gmlWriter.getGmlByNslMeasures(measures);
  125. validateWithSchematron(validators.getMeasureAreaValidator(), measures, gmlByMeasure::get, importParcel);
  126. }
  127. }
  128. private void processCorrections(final Set<ImportOption> options, final ImportParcel importParcel) throws AeriusException {
  129. if (ImportOption.VALIDATE_WITH_SCHEMATRON.in(options)) {
  130. final List<NSLCorrection> corrections = importParcel.getSituation().getNslCorrections();
  131. final Map<NSLCorrection, String> gmlByCorrection = gmlWriter.getGmlByNslCorrections(corrections);
  132. validateWithSchematron(validators.getCalculationPointCorrectionValidator(), corrections, gmlByCorrection::get, importParcel);
  133. }
  134. }
  135. private List<ImportParcel> importParcel(final ImportableFile inputFile, final ImportProperties importProperties,
  136. final Set<ImportOption> options, final Locale locale) throws IOException, AeriusException {
  137. final ImporterFacade facade = new ImporterFacade(pmf, sectorCategoriesMap.get(locale));
  138. facade.setImportOptions(options);
  139. try (InputStream is = inputFile.asInputStream()) {
  140. return facade.convertInputStream2ImportResult(inputFile.getFileName(), is, importProperties);
  141. }
  142. }
  143. private void validateWithSchematron(final Map<EmissionSourceFeature, String> gmlPerSource, final ImportParcel importParcel) throws AeriusException {
  144. for (final Entry<EmissionSourceFeature, String> entry : gmlPerSource.entrySet()) {
  145. final EmissionSourceSchematronVisitor schematronVisitor = new EmissionSourceSchematronVisitor(entry.getValue(), validators,
  146. importParcel.getExceptions(), importParcel.getWarnings());
  147. entry.getKey().accept(schematronVisitor);
  148. }
  149. }
  150. private <T> void validateWithSchematron(final ImaerSchematronValidator validator, final List<T> objects, final Function<T, String> gmlFunction,
  151. final ImportParcel importParcel) throws AeriusException {
  152. for (final T object : objects) {
  153. validator.validate(() -> gmlFunction.apply(object), importParcel.getExceptions(), importParcel.getWarnings());
  154. }
  155. }
  156. private void store(final List<EmissionSourceFeature> esl) {
  157. // TODO Auto-generated method stub
  158. }
  159. }