Skip to content
Snippets Groups Projects
Commit d9648693 authored by Lyn Elisa Goltz's avatar Lyn Elisa Goltz
Browse files

XPLANBOX-2591 - removed BereichUpdateTool and SortDateUpdateTool

parent 163df6bc
No related branches found
No related tags found
No related merge requests found
Showing with 0 additions and 542 deletions
/*-
* #%L
* xplan-update-data-cli - update of database
* %%
* Copyright (C) 2008 - 2023 Freie und Hansestadt Hamburg, developed by lat/lon gesellschaft für raumbezogene Informationssysteme mbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package de.latlon.xplanbox.cli.admin.artefacts;
import de.latlon.xplan.commons.XPlanVersion;
import de.latlon.xplan.commons.feature.XPlanGmlParserBuilder;
import de.latlon.xplan.commons.reference.ExternalReference;
import de.latlon.xplan.commons.reference.ExternalReferenceInfo;
import de.latlon.xplan.commons.reference.ExternalReferenceScanner;
import de.latlon.xplan.core.manager.db.model.Artefact;
import de.latlon.xplan.core.manager.db.model.ArtefactType;
import de.latlon.xplan.core.manager.db.model.Plan;
import de.latlon.xplan.core.manager.db.repository.PlanRepository;
import de.latlon.xplan.manager.web.shared.XPlan;
import org.apache.commons.io.IOUtils;
import org.deegree.feature.FeatureCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.transaction.Transactional;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;
import static de.latlon.xplan.core.manager.db.model.ArtefactType.RASTERBASIS;
import static de.latlon.xplan.core.manager.db.model.ArtefactType.XPLANGML;
/**
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz </a>
*/
public class ArtefactsTableUpdater {
private final Logger LOG = LoggerFactory.getLogger(ArtefactsTableUpdater.class);
private PlanRepository planRepository;
private final ExternalReferenceScanner externalReferenceScanner = new ExternalReferenceScanner();
public ArtefactsTableUpdater(PlanRepository planRepository) {
this.planRepository = planRepository;
}
@Transactional(rollbackOn = Exception.class)
public void update(XPlan xPlan) throws Exception {
LOG.info("Update plan with id {}, version {}, type {}", xPlan.getId(), xPlan.getVersion(), xPlan.getType());
int planId = Integer.parseInt(xPlan.getId());
Optional<Plan> planCandidate = planRepository.findById(planId);
if (!planCandidate.isPresent()) {
LOG.warn("Plan with id {} not found.", planId);
return;
}
Plan plan = planCandidate.get();
Set<Artefact> artefacts = plan.getArtefacts();
FeatureCollection featureCollection = retrieveFeatureCollection(xPlan, artefacts);
if (featureCollection == null || featureCollection.isEmpty()) {
LOG.warn("FeatureCollection is not available! Plan with id {} is skipped.", planId);
return;
}
List<String> rasterFileNames = scanRasterReferenceFileNames(featureCollection);
if (rasterFileNames.isEmpty()) {
LOG.info("Plan with id {} has no rasterfile.", planId);
}
for (Artefact artefact : artefacts) {
LOG.info("Update artefact {}", artefact.getId().getFilename());
long length = detectLength(artefact.getData());
ArtefactType artefactType = detectArtefactType(artefact, rasterFileNames);
artefact.artefacttype(artefactType).length(length);
}
planRepository.save(plan);
}
private ArtefactType detectArtefactType(Artefact artefact, List<String> rasterFileNames) {
String filename = artefact.getId().getFilename();
if ("xplan.gml".equals(filename))
return XPLANGML;
if (rasterFileNames.contains(filename))
return RASTERBASIS;
return null;
}
private long detectLength(byte[] zippedData) throws IOException {
byte[] bytes = unzipArtefact(zippedData);
return bytes.length;
}
private byte[] unzipArtefact(byte[] zippedData) throws IOException {
try (ByteArrayInputStream bis = new ByteArrayInputStream(zippedData);
GZIPInputStream is = new GZIPInputStream(bis);
ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
IOUtils.copy(is, bos);
return bos.toByteArray();
}
}
private FeatureCollection retrieveFeatureCollection(XPlan plan, Set<Artefact> artefacts) throws Exception {
Optional<Artefact> xplanGmlArtefact = artefacts.stream()
.filter(artefact -> "xplan.gml".equals(artefact.getId().getFilename()))
.findFirst();
if (!xplanGmlArtefact.isPresent())
return null;
XPlanVersion version = XPlanVersion.valueOf(plan.getVersion());
InputStream originalPlan = new ByteArrayInputStream(unzipArtefact(xplanGmlArtefact.get().getData()));
return XPlanGmlParserBuilder.newBuilder().build().parseFeatureCollection(originalPlan, version);
}
private List<String> scanRasterReferenceFileNames(FeatureCollection featureCollection) {
ExternalReferenceInfo scan = externalReferenceScanner.scan(featureCollection);
List<ExternalReference> rasterPlanBaseAndUpdateScans = scan.getRasterPlanBaseAndUpdateScans();
return rasterPlanBaseAndUpdateScans.stream()
.map(externalReference -> externalReference.getReferenzUrl())
.collect(Collectors.toList());
}
}
/*-
* #%L
* xplan-update-data-cli - update of database
* %%
* Copyright (C) 2008 - 2023 Freie und Hansestadt Hamburg, developed by lat/lon gesellschaft für raumbezogene Informationssysteme mbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package de.latlon.xplanbox.cli.admin.config;
import de.latlon.xplan.core.manager.db.repository.PlanRepository;
import de.latlon.xplanbox.cli.admin.artefacts.ArtefactsTableUpdater;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
/**
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz </a>
*/
@Import(ApplicationContext.class)
public class ArtefactsTableUpdaterApplicationApplicationContext {
@Autowired
private PlanRepository planRepository;
@Bean
public ArtefactsTableUpdater artefactsTableUpdater() {
return new ArtefactsTableUpdater(planRepository);
}
}
/*-
* #%L
* xplan-update-data-cli - update of database
* %%
* Copyright (C) 2008 - 2023 Freie und Hansestadt Hamburg, developed by lat/lon gesellschaft für raumbezogene Informationssysteme mbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package de.latlon.xplanbox.cli.admin.tool;
import de.latlon.xplanbox.cli.admin.updater.ArtefactsTableUpdaterApplicationRunner;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Update tool to update the column artefacttype of xplanmgr.artefacts.
*
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz</a>
*/
@SpringBootApplication
public class ArtefactsTableUpdateTool {
/**
* CLI entry method.
* @param args command line arguments
*/
public static void main(String[] args) {
if (args.length > 0 && ("--help".equals(args[0]) || "-help".equals(args[0]) || "-h".equals(args[0]))) {
printUsage();
}
else {
SpringApplication app = new SpringApplication(ArtefactsTableUpdaterApplicationRunner.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args).close();
}
}
private static void printUsage() {
System.out.println("Update columns artefacttype and length (new in xPlanBox v7.0) of xplanmgr.artefacts.");
System.out.println();
System.out.println("Usage: artefactsTableUpdate");
System.out.println();
System.out.println("Allgemeine Hinweise:");
System.out.println(
" Das Verzeichnis in dem die Konfigurationsdatei managerConfiguration.properties liegt, muss durch Angabe des Verzeichnis in der Datei etc/application.properties oder durch Setzen der Umgebungsvariablen _XPLANBOX_CONFIG_ erfolgen. Andernfalls wird die Konfiguration aus etc/managerConfiguration.properties verwendet.");
System.out.println(
" Der Workspace `xplan-manager-workspace` muss im Verzeichnis _.deegree/_ des Home-Verzeichnis des Nutzers liegen, der das Tool aufruft. Alternativ kann das Verzeichnis, in dem der Workspace liegt, durch Angabe der Umgebungsvariablen _DEEGREE_WORKSPACE_ROOT_ gesetzt werden.");
System.exit(0);
}
}
/*-
* #%L
* xplan-update-data-cli - update of database
* %%
* Copyright (C) 2008 - 2023 Freie und Hansestadt Hamburg, developed by lat/lon gesellschaft für raumbezogene Informationssysteme mbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package de.latlon.xplanbox.cli.admin.tool;
import de.latlon.xplanbox.cli.admin.updater.BereichUpdateApplicationRunner;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Update tool to update the bereich of all plans.
*
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz</a>
*/
@SpringBootApplication
public class BereichUpdateTool {
/**
* CLI entry method.
* @param args command line arguments
*/
public static void main(String[] args) {
if (args.length > 0 && ("--help".equals(args[0]) || "-help".equals(args[0]) || "-h".equals(args[0]))) {
printUsage();
}
else {
SpringApplication app = new SpringApplication(BereichUpdateApplicationRunner.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args).close();
}
}
private static void printUsage() {
System.out.println("Update Bereiche of plans.");
System.out.println();
System.out.println("Usage: bereichUpdate");
System.out.println();
System.out.println("Allgemeine Hinweise:");
System.out.println(
" Das Verzeichnis in dem die Konfigurationsdatei managerConfiguration.properties liegt, muss durch Angabe des Verzeichnis in der Datei etc/application.properties oder durch Setzen der Umgebungsvariablen _XPLANBOX_CONFIG_ erfolgen. Andernfalls wird die Konfiguration aus etc/managerConfiguration.properties verwendet.");
System.out.println(
" Der Workspace `xplan-manager-workspace` muss im Verzeichnis _.deegree/_ des Home-Verzeichnis des Nutzers liegen, der das Tool aufruft. Alternativ kann das Verzeichnis, in dem der Workspace liegt, durch Angabe der Umgebungsvariablen _DEEGREE_WORKSPACE_ROOT_ gesetzt werden.");
System.exit(0);
}
}
/*-
* #%L
* xplan-update-data-cli - update of database
* %%
* Copyright (C) 2008 - 2023 Freie und Hansestadt Hamburg, developed by lat/lon gesellschaft für raumbezogene Informationssysteme mbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package de.latlon.xplanbox.cli.admin.tool;
import de.latlon.xplanbox.cli.admin.SortdateUpdateSubcommand;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Main entry point to update the sort date in databases.
*
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz</a>
* @version $Revision: $, $Date: $
*/
@SpringBootApplication
public final class SortDateUpdateTool {
/**
* CLI entry method.
* @param args command line arguments
*/
public static void main(String[] args) {
if (args.length > 0 && ("--help".equals(args[0]) || "-help".equals(args[0]) || "-h".equals(args[0]))) {
printUsage();
}
else {
SpringApplication app = new SpringApplication(SortdateUpdateSubcommand.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args).close();
}
}
private static void printUsage() {
System.out.println("Update sort date.");
System.out.println();
System.out.println("Usage: sortDateUpdate");
System.out.println();
System.out.println("Allgemeine Hinweise:");
System.out.println(
" Das Verzeichnis in dem die Konfigurationsdatei managerConfiguration.properties liegt, muss durch Angabe des Verzeichnis in der Datei etc/application.properties oder durch Setzen der Umgebungsvariablen _XPLANBOX_CONFIG_ erfolgen. Andernfalls wird die Konfiguration aus etc/managerConfiguration.properties verwendet.");
System.out.println(
" Der Workspace `xplan-manager-workspace` muss im Verzeichnis _.deegree/_ des Home-Verzeichnis des Nutzers liegen, der das Tool aufruft. Alternativ kann das Verzeichnis, in dem der Workspace liegt, durch Angabe der Umgebungsvariablen _DEEGREE_WORKSPACE_ROOT_ gesetzt werden.");
System.exit(0);
}
}
/*-
* #%L
* xplan-update-data-cli - update of database
* %%
* Copyright (C) 2008 - 2023 Freie und Hansestadt Hamburg, developed by lat/lon gesellschaft für raumbezogene Informationssysteme mbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package de.latlon.xplanbox.cli.admin.updater;
import de.latlon.xplan.manager.database.XPlanDao;
import de.latlon.xplan.manager.web.shared.XPlan;
import de.latlon.xplanbox.cli.admin.artefacts.ArtefactsTableUpdater;
import de.latlon.xplanbox.cli.admin.config.ArtefactsTableUpdaterApplicationApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Updates the data from version 6.0 to 7.0: Inserts data to
* xplanmgr.artefacts.artefacttype
*
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz </a>
*/
@Component
@Import(ArtefactsTableUpdaterApplicationApplicationContext.class)
public class ArtefactsTableUpdaterApplicationRunner implements ApplicationRunner {
private final Logger LOG = LoggerFactory.getLogger(ArtefactsTableUpdaterApplicationRunner.class);
@Autowired
private XPlanDao xplanDao;
@Autowired
private ArtefactsTableUpdater artefactsTableUpdater;
@Override
public void run(ApplicationArguments args) throws Exception {
int noOfSuccessfullyUpdatedPlanIds = 0;
Map<String, String> failedPlans = new HashMap<>();
List<XPlan> plans = xplanDao.getXPlanList();
for (XPlan plan : plans) {
String planId = plan.getId();
try {
artefactsTableUpdater.update(plan);
noOfSuccessfullyUpdatedPlanIds++;
}
catch (Exception e) {
failedPlans.put(planId, e.getMessage());
LOG.error("Plan with id " + planId + " could not be updated!", e);
}
}
logResult(plans.size(), noOfSuccessfullyUpdatedPlanIds, failedPlans);
}
private void logResult(int noOfExecutedPlans, int noOfSuccessfullyUpdatedPlanIds, Map<String, String> failedPlans) {
LOG.info("ArtefactsTableUpdateTool completely executed!");
LOG.info("Updated {} of {} plans successfully.", noOfSuccessfullyUpdatedPlanIds, noOfExecutedPlans);
if (!failedPlans.isEmpty()) {
LOG.warn("{} of {} plans could not be updated:", failedPlans.size(), noOfExecutedPlans);
failedPlans.forEach((id, msg) -> LOG.warn(" - ID: {}, failure: {}", id, msg));
}
}
}
/*-
* #%L
* xplan-update-data-cli - update of database
* %%
* Copyright (C) 2008 - 2023 Freie und Hansestadt Hamburg, developed by lat/lon gesellschaft für raumbezogene Informationssysteme mbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package de.latlon.xplanbox.cli.admin.updater;
import de.latlon.xplan.commons.util.FeatureCollectionUtils;
import de.latlon.xplan.manager.database.XPlanDao;
import de.latlon.xplan.manager.web.shared.Bereich;
import de.latlon.xplan.manager.web.shared.XPlan;
import de.latlon.xplanbox.cli.admin.config.ApplicationContext;
import org.deegree.feature.FeatureCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
import java.util.List;
/**
* Updates the data from version 5.0 to 5.0.2: Inserts data to xplanmgr.bereiche
*
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz</a>
*/
@Component
@Import(ApplicationContext.class)
public class BereichUpdateApplicationRunner implements ApplicationRunner {
private final Logger LOG = LoggerFactory.getLogger(BereichUpdateApplicationRunner.class);
@Autowired
private XPlanDao xplanDao;
/**
* Updates data. Schema must be up to date already.
* @throws Exception if an error occurred during update
*/
@Override
@Transactional(rollbackOn = Exception.class)
public void run(ApplicationArguments args) throws Exception {
List<XPlan> plans = xplanDao.getXPlanList();
for (XPlan plan : plans) {
update(plan);
}
LOG.info("BereichUpdateTool successfully executed!");
}
private void update(XPlan plan) throws Exception {
LOG.info("Update plan with id {}, version {}, type {}", plan.getId(), plan.getVersion(), plan.getType());
FeatureCollection featureCollection = xplanDao.retrieveFeatureCollection(plan);
if (featureCollection.isEmpty()) {
LOG.warn("FeatureCollection is not available! Plan with id {} is skipped.", plan.getId());
return;
}
List<Bereich> bereiche = FeatureCollectionUtils.retrieveBereiche(featureCollection);
if (bereiche.isEmpty()) {
LOG.info("Plan with id {} has no bereiche.", plan.getId());
}
xplanDao.updateBereiche(plan, bereiche);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment

Consent

On this website, we use the web analytics service Matomo to analyze and review the use of our website. Through the collected statistics, we can improve our offerings and make them more appealing for you. Here, you can decide whether to allow us to process your data and set corresponding cookies for these purposes, in addition to technically necessary cookies. Further information on data protection—especially regarding "cookies" and "Matomo"—can be found in our privacy policy. You can withdraw your consent at any time.