Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/SelectedAdditionalParser.java @ 8971:50416a0df385
Importer for the Schifffahrt (S-INFO) and Oekologie (U-INFO) files
author | mschaefer |
---|---|
date | Tue, 03 Apr 2018 10:18:30 +0200 |
parents | |
children | bf8a9df86f32 |
comparison
equal
deleted
inserted
replaced
8970:da5dc7446652 | 8971:50416a0df385 |
---|---|
1 /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by | |
3 * Björnsen Beratende Ingenieure GmbH | |
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt | |
5 * | |
6 * This file is Free Software under the GNU AGPL (>=v3) | |
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
8 * documentation coming with Dive4Elements River for details. | |
9 */ | |
10 | |
11 package org.dive4elements.river.importer.sinfo.parsers; | |
12 | |
13 import java.io.File; | |
14 import java.io.FileInputStream; | |
15 import java.io.IOException; | |
16 import java.io.InputStreamReader; | |
17 import java.io.LineNumberReader; | |
18 import java.io.Serializable; | |
19 import java.util.ArrayList; | |
20 import java.util.List; | |
21 | |
22 import org.apache.log4j.Logger; | |
23 import org.dive4elements.river.importer.Config; | |
24 import org.dive4elements.river.importer.ImportRiver; | |
25 import org.dive4elements.river.importer.ImporterSession; | |
26 import org.dive4elements.river.importer.common.AbstractParser; | |
27 import org.dive4elements.river.importer.common.ImportParser; | |
28 import org.dive4elements.river.model.River; | |
29 import org.dive4elements.river.model.Wst; | |
30 import org.hibernate.Query; | |
31 import org.hibernate.SQLQuery; | |
32 import org.hibernate.Session; | |
33 | |
34 /** | |
35 * Reads and parses a selected WST additionals link file | |
36 * | |
37 * @author Matthias Schäfer | |
38 * | |
39 */ | |
40 public class SelectedAdditionalParser implements ImportParser { | |
41 | |
42 /***** FIELDS *****/ | |
43 | |
44 private static final Logger log = Logger.getLogger(SelectedAdditionalParser.class); | |
45 | |
46 private static final String IMPORT_Q_FILENAME = "Mit_Abflussdaten.txt"; | |
47 | |
48 private static final String IMPORT_W_FILENAME = "Ohne_Abflussdaten.txt"; | |
49 | |
50 private enum SelectionType { | |
51 WITH_Q("Q", "with discharge"), // | |
52 WITHOUT_Q("W", "without discharge"); | |
53 | |
54 private final String key; | |
55 private final String logText; | |
56 | |
57 SelectionType(final String key, final String logText) { | |
58 this.key = key; | |
59 this.logText = logText; | |
60 } | |
61 | |
62 public String getKey() { | |
63 return this.key; | |
64 } | |
65 | |
66 public String getLogText() { | |
67 return this.logText; | |
68 } | |
69 } | |
70 | |
71 private final File importPath; | |
72 | |
73 private final File rootRelativePath; | |
74 | |
75 private final ImportRiver river; | |
76 | |
77 private final SelectionType selectionType; | |
78 | |
79 private final List<String> links; | |
80 | |
81 | |
82 /***** CONSTRUCTORS *****/ | |
83 | |
84 public SelectedAdditionalParser(final File importPath, final File rootRelativePath, final ImportRiver river, final SelectionType selectionType) { | |
85 this.importPath = importPath; | |
86 this.rootRelativePath = rootRelativePath; | |
87 this.river = river; | |
88 this.selectionType = selectionType; | |
89 this.links = new ArrayList<>(); | |
90 } | |
91 | |
92 | |
93 /***** METHODS *****/ | |
94 | |
95 /** | |
96 * Whether this import type shall be skipped | |
97 */ | |
98 public static boolean shallSkip() { | |
99 return Config.INSTANCE.skipSInfoSelectedAdditional(); | |
100 } | |
101 | |
102 /** | |
103 * Creates a list of parsers for all selected additionals import files in a directory | |
104 */ | |
105 public static List<SelectedAdditionalParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) { | |
106 final List<SelectedAdditionalParser> parsers = new ArrayList<>(); | |
107 parsers.add(new SelectedAdditionalParser(new File(importDir, IMPORT_Q_FILENAME), new File(relativeDir, IMPORT_Q_FILENAME), | |
108 river, SelectionType.WITH_Q)); | |
109 parsers.add(new SelectedAdditionalParser(new File(importDir, IMPORT_W_FILENAME), new File(relativeDir, IMPORT_W_FILENAME), | |
110 river, SelectionType.WITHOUT_Q)); | |
111 return parsers; | |
112 } | |
113 | |
114 @Override | |
115 public void parse() throws IOException { | |
116 this.links.clear(); | |
117 log.info("Parse '... " + this.rootRelativePath + "'"); | |
118 LineNumberReader in = null; | |
119 try { | |
120 in = new LineNumberReader(new InputStreamReader(new FileInputStream(this.importPath), AbstractParser.ENCODING)); | |
121 String line; | |
122 while (true) { | |
123 line = in.readLine(); | |
124 if (line == null) | |
125 break; | |
126 if (!line.trim().isEmpty() && !line.trim().startsWith(AbstractParser.START_META_CHAR)) | |
127 this.links.add(line.trim()); | |
128 } | |
129 log.info("Number of file links found: " + this.links.size()); | |
130 } | |
131 finally { | |
132 if (in != null) | |
133 in.close(); | |
134 } | |
135 } | |
136 | |
137 @Override | |
138 public void store() { | |
139 final Session session = ImporterSession.getInstance().getDatabaseSession(); | |
140 final SQLQuery reset = session.createSQLQuery("UPDATE wsts SET sinfo_selection = NULL WHERE (river_id=:river_id) AND (kind=1)" | |
141 + " AND (sinfo_selection=:seltype)"); | |
142 reset.setParameter("river_id", this.river.getPeer().getId()); | |
143 reset.setParameter("seltype", this.selectionType.getKey()); | |
144 reset.executeUpdate(); | |
145 final Query query = session.createQuery("FROM Wst WHERE (river=:river) AND (kind=1) AND (lower(description) LIKE :path)"); | |
146 query.setParameter("river", this.river); | |
147 int count = 0; | |
148 for (final String wstfile : this.links) { | |
149 count += updateWst(session, query, this.river.getPeer(), wstfile, this.selectionType); | |
150 } | |
151 log.info("Updated " + count + " wsts for selected additionals " + this.selectionType.getLogText()); | |
152 } | |
153 | |
154 private int updateWst(final Session session, final Query query, final River river, final String path, final SelectionType selectionType) { | |
155 final String pathPattern = path.toLowerCase().replace('/', '_').replace('\\', '_'); | |
156 query.setParameter("path", pathPattern); | |
157 final List<Wst> rows = query.list(); | |
158 if (rows.isEmpty()) { | |
159 log.warn("Wst not found for description '" + path + "'" + ";" + this.rootRelativePath); | |
160 return 0; | |
161 } else { | |
162 final Wst wst = rows.get(0); | |
163 wst.setSInfoSelection(selectionType.getKey()); | |
164 final Serializable id = session.save(wst); | |
165 return 1; | |
166 } | |
167 } | |
168 } |