# HG changeset patch # User mschaefer # Date 1524046299 -7200 # Node ID 201817aa7b1c1c39f0082400cbf1302d63591bf3 # Parent 1fec0a06d8335c109719215d6c95baaa4d445592 Added the missing vegetation import classes diff -r 1fec0a06d833 -r 201817aa7b1c backend/src/main/java/org/dive4elements/river/importer/uinfo/importitem/VegetationSeriesImport.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/src/main/java/org/dive4elements/river/importer/uinfo/importitem/VegetationSeriesImport.java Wed Apr 18 12:11:39 2018 +0200 @@ -0,0 +1,69 @@ +/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde + * Software engineering by + * Björnsen Beratende Ingenieure GmbH + * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ + +package org.dive4elements.river.importer.uinfo.importitem; + +import java.util.List; + +import org.apache.log4j.Logger; +import org.dive4elements.river.importer.common.AbstractSeriesImport; +import org.dive4elements.river.model.River; +import org.dive4elements.river.model.uinfo.Vegetation; +import org.dive4elements.river.model.uinfo.VegetationZone; +import org.hibernate.Query; +import org.hibernate.Session; + +/** + * Imported vegetation data series of a river + * + * @author Matthias Schäfer + * + */ +public class VegetationSeriesImport extends AbstractSeriesImport { + + /***** FIELDS *****/ + + private static Logger log = Logger.getLogger(VegetationSeriesImport.class); + + private String name; + + + /***** CONSTRUCTORS *****/ + + public VegetationSeriesImport(final String filename) { + super(filename); + } + + + /***** METHODS *****/ + + public void setName(final String name) { + this.name = name; + } + + @Override + public Logger getLog() { + return log; + } + + @Override + public List querySeriesItem(final Session session, final River river) { + final Query query = session.createQuery("FROM Vegetation WHERE river=:river AND lower(filename)=:filename"); + query.setParameter("river", river); + query.setParameter("filename", this.filename.toLowerCase()); + return query.list(); + } + + + @Override + public Vegetation createSeriesItem(final River river) { + return new Vegetation(river, this.filename, this.name, this.comment); + } +} \ No newline at end of file diff -r 1fec0a06d833 -r 201817aa7b1c backend/src/main/java/org/dive4elements/river/importer/uinfo/importitem/VegetationTypeImport.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/src/main/java/org/dive4elements/river/importer/uinfo/importitem/VegetationTypeImport.java Wed Apr 18 12:11:39 2018 +0200 @@ -0,0 +1,94 @@ +/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde + * Software engineering by + * Björnsen Beratende Ingenieure GmbH + * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ + +package org.dive4elements.river.importer.uinfo.importitem; + +import java.util.List; + +import org.apache.log4j.Logger; +import org.dive4elements.river.importer.ImporterSession; +import org.dive4elements.river.model.uinfo.VegetationType; +import org.hibernate.Query; +import org.hibernate.Session; + +/** + * Imported vegetation zone type + * + * @author Matthias Schäfer + * + */ +public class VegetationTypeImport implements Comparable { + + /***** FIELDS *****/ + + private static final Logger log = Logger.getLogger(VegetationTypeImport.class); + + protected Integer id; + + protected String name; + + protected VegetationType peer; + + /***** CONSTRUCTOR *****/ + + public VegetationTypeImport() { + } + + public VegetationTypeImport(final int id) { + this.id = Integer.valueOf(id); + } + + /***** METHODS *****/ + + @Override + public int compareTo(final VegetationTypeImport other) { + return this.id.compareTo(other.getId()); + } + + @Override + public int hashCode() { + return this.id; + } + + public Integer getId() { + return this.id; + } + + public void setId(final Integer id) { + this.id = id; + } + + public String getName() { + return this.name; + } + + public void setName(final String name) { + this.name = name; + } + + public VegetationType getPeer() { + if (this.peer != null) + return this.peer; + final Session session = ImporterSession.getInstance().getDatabaseSession(); + final Query query = session.createQuery("FROM VegetationType WHERE id=:id"); + query.setParameter("id", this.id); + final List types = query.list(); + if (types.isEmpty()) { + // Type table is not modifiable by the importer + // this.peer = new VegetationType(this.name); + // session.save(this.peer); + // log.info(String.format("Create new database instance: %d, '%s'", this.peer.getId(), this.name)); + } + else { + this.peer = types.get(0); + } + return this.peer; + } +} diff -r 1fec0a06d833 -r 201817aa7b1c backend/src/main/java/org/dive4elements/river/importer/uinfo/importitem/VegetationZoneImport.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/src/main/java/org/dive4elements/river/importer/uinfo/importitem/VegetationZoneImport.java Wed Apr 18 12:11:39 2018 +0200 @@ -0,0 +1,77 @@ +/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde + * Software engineering by + * Björnsen Beratende Ingenieure GmbH + * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ + +package org.dive4elements.river.importer.uinfo.importitem; + +import java.util.List; + +import org.apache.log4j.Logger; +import org.dive4elements.river.importer.common.AbstractKmLineImport; +import org.dive4elements.river.model.uinfo.Vegetation; +import org.dive4elements.river.model.uinfo.VegetationType; +import org.dive4elements.river.model.uinfo.VegetationZone; +import org.hibernate.Query; +import org.hibernate.Session; + +/** + * Imported vegetation values of a river station. + * + * @author Matthias Schäfer + * + */ +public class VegetationZoneImport extends AbstractKmLineImport { + + /***** FIELDS *****/ + + private static Logger log = Logger.getLogger(VegetationZoneImport.class); + + private final VegetationTypeImport vegetationType; + + private final Integer minOverflowDays; + + private final Integer maxOverflowDays; + + + /***** CONSTRUCTOR *****/ + + public VegetationZoneImport(final int vegetationTypeId, final Integer minOverflowDays, final Integer maxOverflowDays) { + super(Double.NaN); + this.vegetationType = new VegetationTypeImport(vegetationTypeId); + this.minOverflowDays = minOverflowDays; + this.maxOverflowDays = maxOverflowDays; + } + + + /***** METHODS *****/ + + @Override + public VegetationZone queryValueItem(final Session session, final Vegetation parent) { + final Query query = session.createQuery("FROM VegetationZone WHERE (vegetation=:parent)" + + " AND (vegetationType=:type)"); + query.setParameter("parent", parent); + query.setParameter("type", this.vegetationType); + final List rows = query.list(); + if (!rows.isEmpty()) + return (VegetationZone) rows.get(0); + else + return null; + } + + + @Override + public VegetationZone createValueItem(final Vegetation parent) { + final VegetationType type = this.vegetationType.getPeer(); + if (type == null) { + log.error("Unknown vegetation zone class " + this.vegetationType.getId()); + return null; + } + return new VegetationZone(parent, type, this.minOverflowDays, this.maxOverflowDays); + } +} diff -r 1fec0a06d833 -r 201817aa7b1c backend/src/main/java/org/dive4elements/river/importer/uinfo/parsers/VegetationParser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/src/main/java/org/dive4elements/river/importer/uinfo/parsers/VegetationParser.java Wed Apr 18 12:11:39 2018 +0200 @@ -0,0 +1,187 @@ +/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde + * Software engineering by + * Björnsen Beratende Ingenieure GmbH + * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ + +package org.dive4elements.river.importer.uinfo.parsers; + +import java.io.File; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.log4j.Logger; +import org.dive4elements.river.importer.Config; +import org.dive4elements.river.importer.ImportRiver; +import org.dive4elements.river.importer.common.AbstractParser; +import org.dive4elements.river.importer.common.ParsingState; +import org.dive4elements.river.importer.uinfo.importitem.VegetationSeriesImport; +import org.dive4elements.river.importer.uinfo.importitem.VegetationZoneImport; +import org.dive4elements.river.model.uinfo.Vegetation; +import org.dive4elements.river.model.uinfo.VegetationZone; + +/** + * Reads and parses a vegetation zones file + * + * @author Matthias Schäfer + * + */ +public class VegetationParser extends AbstractParser { + + /***** FIELDS *****/ + + private static final Logger log = Logger.getLogger(VegetationParser.class); + + private static final Pattern META_NAME = Pattern.compile("^#\\sEinteilung:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE); + + private static final Pattern META_COLUMNTITLES = Pattern.compile("^#*\\s*Vegetationstyp\\s*;.+", Pattern.CASE_INSENSITIVE); + + private enum ColTitlePattern { + OVERFLOW_LIMIT("((.)|(Ue))berflutungsdauer-bis\\s*\\[(.*)\\].*"), // + CLASSNO("Vegetationsklasse.*"); + + private final Pattern pattern; + + ColTitlePattern(final String regexp) { + this.pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE); + } + + public Pattern getPattern() { + return this.pattern; + } + } + + private final EnumMap cols = new EnumMap<>(ColTitlePattern.class); + + private int previousClassNo; + + private int previousDaysLimit; + + + /***** CONSTRUCTORS *****/ + + public VegetationParser(final File importPath, final File rootRelativePath, final ImportRiver river) { + super(importPath, rootRelativePath, river); + this.previousClassNo = 0; + this.previousDaysLimit = -1; + } + + + /***** METHODS *****/ + + @Override + protected Logger getLog() { + return log; + } + + /** + * Whether this import type shall be skipped + */ + public static boolean shallSkip() { + return Config.INSTANCE.skipUInfoVegetation(); + } + + /** + * Creates a list of parsers for all vegetation import files in a directory + */ + public static List createParsers(final File importDir, final File relativeDir, final ImportRiver river) { + final List parsers = new ArrayList<>(); + if (importDir.exists()) { + for (final File file : listFiles(importDir, ".csv")) + parsers.add(new VegetationParser(file, new File(relativeDir, file.getName()), river)); + } + return parsers; + } + + @Override + protected KmMode kmMode() { + return KmMode.NONE; + } + + @Override + protected VegetationSeriesImport createSeriesImport(final String filename) { + return new VegetationSeriesImport(filename); + } + + @Override + protected boolean handleMetaOther() { + if (handleMetaName()) + return true; + else + return false; + } + + private boolean handleMetaName() { + final Matcher m = META_NAME.matcher(this.currentLine); + if (m.matches()) { + this.metaPatternsMatched.add(META_NAME); + this.seriesHeader.setName(parseMetaInfo(m.group(1).trim())); + return true; + } + return false; + } + + @Override + protected boolean handleMetaColumnTitles() { + if (!META_COLUMNTITLES.matcher(this.currentLine).matches()) + return false; + this.metaPatternsMatched.add(META_COLUMNTITLES); + this.columnTitles.clear(); + final String[] titles = this.currentLine.split(SEPARATOR_CHAR, 0); + for (int i = 0; i <= titles.length - 1; i++) + this.columnTitles.add(titles[i].trim()); + for (final ColTitlePattern col : ColTitlePattern.values()) + this.cols.put(col, -1); + for (int i = 1; i <= this.columnTitles.size() - 1; i++) { + for (final ColTitlePattern col : ColTitlePattern.values()) { + if (col.getPattern().matcher(this.columnTitles.get(i)).matches()) { + this.cols.put(col, i); + break; + } + } + } + if ((this.cols.get(ColTitlePattern.OVERFLOW_LIMIT) < 0) || (this.cols.get(ColTitlePattern.CLASSNO) < 0)) { + logError("Column of the overflow duration limit and/or vegetation zone class could not be identified"); + this.headerParsingState = ParsingState.STOP; + return true; + } + this.previousClassNo = 0; + this.previousDaysLimit = 0; + return true; + } + + @Override + protected VegetationZoneImport createKmLineImport(final Double km, final String[] values) { + int daysLimit = 367; + int classNo = 0; + try { + if (!values[this.cols.get(ColTitlePattern.OVERFLOW_LIMIT)].trim().isEmpty()) + daysLimit = Integer.parseInt(values[this.cols.get(ColTitlePattern.OVERFLOW_LIMIT)]); + classNo = Integer.parseInt(values[this.cols.get(ColTitlePattern.CLASSNO)]); + } + catch (final Exception e) { + logError("Overflow days limit and/or vegetation zone class could not be parsed: line " + this.in.getLineNumber()); + return null; + } + // Check completeness of vegetation zone type set, if needed + // if (classNo != this.previousClassNo + 1) { + // logError("Wrong vegetation zone class number or wrong class order: line " + this.in.getLineNumber()); + // return null; + // } + // if (!this.types.containsKey(Integer.valueOf(classNo))) { + // logError("Unknown vegetation zone class: line " + this.in.getLineNumber()); + // return null; + // } + this.previousClassNo = classNo; + final Integer minDays = Integer.valueOf(this.previousDaysLimit); + this.previousDaysLimit = daysLimit + 1; + return new VegetationZoneImport(classNo, minDays, Integer.valueOf(this.previousDaysLimit)); + } +} diff -r 1fec0a06d833 -r 201817aa7b1c backend/src/main/java/org/dive4elements/river/model/uinfo/Vegetation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/src/main/java/org/dive4elements/river/model/uinfo/Vegetation.java Wed Apr 18 12:11:39 2018 +0200 @@ -0,0 +1,136 @@ +/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde + * Software engineering by + * Björnsen Beratende Ingenieure GmbH + * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ + +package org.dive4elements.river.model.uinfo; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; + +import org.dive4elements.river.model.River; + +/** + * Hibernate binding for the DB table vegetation + * + * @author Matthias Schäfer + * + */ + +@Entity +@Table(name = "vegetation") +public class Vegetation implements Serializable { + + /***** FIELDS *****/ + + private static final long serialVersionUID = -2264657956270172835L; + + private Integer id; + + private River river; + + private String filename; + + private String name; + + private String comment; + + private List values; + + + /***** CONSTRUCTORS *****/ + + public Vegetation() { + } + + + public Vegetation(final River river, final String filename, final String name, final String comment) { + this.river = river; + this.filename = filename; + this.name = name; + this.comment = comment; + this.values = new ArrayList<>(); + } + + /***** METHODS *****/ + + @Id + @SequenceGenerator(name = "SEQUENCE_VEGETATION_ID_SEQ", sequenceName = "VEGETATION_ID_SEQ", allocationSize = 1) + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_VEGETATION_ID_SEQ") + @Column(name = "id") + public Integer getId() { + return this.id; + } + + public void setId(final Integer id) { + this.id = id; + } + + @OneToOne + @JoinColumn(name = "river_id") + public River getRiver() { + return this.river; + } + + public void setRiver(final River river) { + this.river = river; + } + + @Column(name = "filename") + public String getFilename() { + return this.filename; + } + + public void setFilename(final String filename) { + this.filename = filename; + } + + @Column(name = "name") + public String getName() { + return this.name; + } + + public void setName(final String name) { + this.name = name; + } + + @Column(name = "comment") + public String getComment() { + return this.comment; + } + + public void setComment(final String comment) { + this.comment = comment; + } + + @OneToMany + @JoinColumn(name = "vegetation_id") + public List getValues() { + return this.values; + } + + public void setValues(final List values) { + this.values = values; + } + + public void addValue(final VegetationZone value) { + this.values.add(value); + } +} \ No newline at end of file diff -r 1fec0a06d833 -r 201817aa7b1c backend/src/main/java/org/dive4elements/river/model/uinfo/VegetationType.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/src/main/java/org/dive4elements/river/model/uinfo/VegetationType.java Wed Apr 18 12:11:39 2018 +0200 @@ -0,0 +1,82 @@ +/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde + * Software engineering by + * Björnsen Beratende Ingenieure GmbH + * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ + +package org.dive4elements.river.model.uinfo; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.dive4elements.river.backend.SessionHolder; +import org.hibernate.Query; +import org.hibernate.Session; + +/** + * Hibernate binding for the DB table vegetation_type + * + * @author Matthias Schäfer + * + */ +@Entity +@Table(name = "vegetation_type") +public class VegetationType implements Serializable { + + /***** FIELDS *****/ + + private static final long serialVersionUID = -845317173014273709L; + + private Integer id; + + private String name; + + + /***** CONSTRUCTORS *****/ + + public VegetationType() { + } + + public VegetationType(final String name) { + this.name = name; + } + + /***** METHODS *****/ + + @Id + @Column(name = "id") + public Integer getId() { + return this.id; + } + + public void setId(final Integer id) { + this.id = id; + } + + @Column(name = "name") + public String getName() { + return this.name; + } + + public void setName(final String name) { + this.name = name; + } + /** + * Queries all salix ranks from the database, orders by id + */ + public static List getTypes() { + final Session session = SessionHolder.HOLDER.get(); + final Query query = session.createQuery("FROM VegetationType ORDER BY id"); + return new ArrayList<>(query.list()); + } +} diff -r 1fec0a06d833 -r 201817aa7b1c backend/src/main/java/org/dive4elements/river/model/uinfo/VegetationZone.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/src/main/java/org/dive4elements/river/model/uinfo/VegetationZone.java Wed Apr 18 12:11:39 2018 +0200 @@ -0,0 +1,115 @@ +/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde + * Software engineering by + * Björnsen Beratende Ingenieure GmbH + * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ + +package org.dive4elements.river.model.uinfo; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; + + +/** + * Hibernate binding for the DB table vegetation_zone + * + * @author Matthias Schäfer + * + */ +@Entity +@Table(name = "vegetation_zone") +public class VegetationZone implements Serializable { + + /***** FIELDS *****/ + + private static final long serialVersionUID = -6579828019873800147L; + + private Integer id; + + private Vegetation vegetation; + + private VegetationType vegetationType; + + private Integer min_overflow_days; + + private Integer max_overflow_days; + + + /***** CONSTRUCTORS *****/ + + public VegetationZone() { + } + + public VegetationZone(final Vegetation vegetation, final VegetationType vegetationType, final Integer min_overflow_days, final Integer max_overflow_days) { + this.vegetation = vegetation; + this.vegetationType = vegetationType; + this.min_overflow_days = min_overflow_days; + this.max_overflow_days = max_overflow_days; + } + + + /***** METHODS *****/ + + @Id + @SequenceGenerator(name = "SEQUENCE_VEGETATION_ZONE_ID_SEQ", sequenceName = "VEGETATION_ZONE_ID_SEQ", allocationSize = 1) + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_VEGETATION_ZONE_ID_SEQ") + @Column(name = "id") + public Integer getId() { + return this.id; + } + + public void setId(final Integer id) { + this.id = id; + } + + @OneToOne + @JoinColumn(name = "vegetation_id") + public Vegetation getVegetation() { + return this.vegetation; + } + + public void setVegetation(final Vegetation vegetation) { + this.vegetation = vegetation; + } + + @OneToOne + @JoinColumn(name = "vegetation_type_id") + public VegetationType getVegetationType() { + return this.vegetationType; + } + + public void setVegetationType(final VegetationType vegetationType) { + this.vegetationType = vegetationType; + } + + @Column(name = "min_overflow_days") + public Integer getMin_overflow_days() { + return this.min_overflow_days; + } + + public void setMin_overflow_days(final Integer min_overflow_days) { + this.min_overflow_days = min_overflow_days; + } + + @Column(name = "max_overflow_days") + public Integer getMax_overflow_days() { + return this.max_overflow_days; + } + + public void setMax_overflow_days(final Integer max_overflow_days) { + this.max_overflow_days = max_overflow_days; + } +}