comparison backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/SelectedAdditionalParser.java @ 8988:ae76f618d990

Checks added for missing import directory
author mschaefer
date Sun, 08 Apr 2018 18:09:32 +0200
parents bf8a9df86f32
children
comparison
equal deleted inserted replaced
8987:5ff8ce9a2e06 8988:ae76f618d990
15 import java.io.IOException; 15 import java.io.IOException;
16 import java.io.InputStreamReader; 16 import java.io.InputStreamReader;
17 import java.io.LineNumberReader; 17 import java.io.LineNumberReader;
18 import java.io.Serializable; 18 import java.io.Serializable;
19 import java.util.ArrayList; 19 import java.util.ArrayList;
20 import java.util.HashMap;
20 import java.util.List; 21 import java.util.List;
21 22
22 import org.apache.log4j.Logger; 23 import org.apache.log4j.Logger;
23 import org.dive4elements.river.importer.Config; 24 import org.dive4elements.river.importer.Config;
24 import org.dive4elements.river.importer.ImportRiver; 25 import org.dive4elements.river.importer.ImportRiver;
41 42
42 /***** FIELDS *****/ 43 /***** FIELDS *****/
43 44
44 private static final Logger log = Logger.getLogger(SelectedAdditionalParser.class); 45 private static final Logger log = Logger.getLogger(SelectedAdditionalParser.class);
45 46
46 private static final String IMPORT_Q_FILENAME = "Mit_Abflussdaten.txt"; 47 private static final String IMPORT_FILENAME = "Zus_Laengsschnitte.txt";
47
48 private static final String IMPORT_W_FILENAME = "Ohne_Abflussdaten.txt";
49 48
50 private enum SelectionType { 49 private enum SelectionType {
51 WITH_Q("Q", "with discharge"), // 50 WITH_Q("Q", "with discharge"), //
52 WITHOUT_Q("W", "without discharge"); 51 WITHOUT_Q("W", "without discharge");
53 52
64 } 63 }
65 64
66 public String getLogText() { 65 public String getLogText() {
67 return this.logText; 66 return this.logText;
68 } 67 }
68
69 public static SelectionType parse(final String path) {
70 if (path.toLowerCase().endsWith(".wst"))
71 return WITH_Q;
72 else
73 return WITHOUT_Q;
74 }
69 } 75 }
70 76
71 private final File importPath; 77 private final File importPath;
72 78
73 private final File rootRelativePath; 79 private final File rootRelativePath;
74 80
75 private final ImportRiver river; 81 private final ImportRiver river;
76 82
77 private final SelectionType selectionType; 83 private final HashMap<String, SelectionType> links;
78
79 private final List<String> links;
80 84
81 85
82 /***** CONSTRUCTORS *****/ 86 /***** CONSTRUCTORS *****/
83 87
84 public SelectedAdditionalParser(final File importPath, final File rootRelativePath, final ImportRiver river, final SelectionType selectionType) { 88 public SelectedAdditionalParser(final File importPath, final File rootRelativePath, final ImportRiver river) {
85 this.importPath = importPath; 89 this.importPath = importPath;
86 this.rootRelativePath = rootRelativePath; 90 this.rootRelativePath = rootRelativePath;
87 this.river = river; 91 this.river = river;
88 this.selectionType = selectionType; 92 this.links = new HashMap<>();
89 this.links = new ArrayList<>();
90 } 93 }
91 94
92 95
93 /***** METHODS *****/ 96 /***** METHODS *****/
94 97
102 /** 105 /**
103 * Creates a list of parsers for all selected additionals import files in a directory 106 * Creates a list of parsers for all selected additionals import files in a directory
104 */ 107 */
105 public static List<SelectedAdditionalParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) { 108 public static List<SelectedAdditionalParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) {
106 final List<SelectedAdditionalParser> parsers = new ArrayList<>(); 109 final List<SelectedAdditionalParser> parsers = new ArrayList<>();
107 parsers.add(new SelectedAdditionalParser(new File(importDir, IMPORT_Q_FILENAME), new File(relativeDir, IMPORT_Q_FILENAME), 110 final File importFile = new File(importDir, IMPORT_FILENAME);
108 river, SelectionType.WITH_Q)); 111 if (importFile.exists())
109 parsers.add(new SelectedAdditionalParser(new File(importDir, IMPORT_W_FILENAME), new File(relativeDir, IMPORT_W_FILENAME), 112 parsers.add(new SelectedAdditionalParser(importFile, new File(relativeDir, IMPORT_FILENAME), river));
110 river, SelectionType.WITHOUT_Q));
111 return parsers; 113 return parsers;
112 } 114 }
113 115
114 @Override 116 @Override
115 public void parse() throws IOException { 117 public void parse() throws IOException {
121 String line; 123 String line;
122 while (true) { 124 while (true) {
123 line = in.readLine(); 125 line = in.readLine();
124 if (line == null) 126 if (line == null)
125 break; 127 break;
126 if (!line.trim().isEmpty() && !line.trim().startsWith(AbstractParser.START_META_CHAR)) 128 if (!line.trim().isEmpty() && !line.trim().startsWith(AbstractParser.START_META_CHAR) && !this.links.containsKey(line.trim()))
127 this.links.add(line.trim()); 129 this.links.put(line.trim(), SelectionType.parse(line.trim()));
128 } 130 }
129 log.info("Number of file links found: " + this.links.size()); 131 log.info("Number of file links found: " + this.links.size());
130 } 132 }
131 finally { 133 finally {
132 if (in != null) 134 if (in != null)
136 138
137 @Override 139 @Override
138 public void store() { 140 public void store() {
139 final Session session = ImporterSession.getInstance().getDatabaseSession(); 141 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)" 142 final SQLQuery reset = session.createSQLQuery("UPDATE wsts SET sinfo_selection = NULL WHERE (river_id=:river_id) AND (kind=1)"
141 + " AND (sinfo_selection=:seltype)"); 143 + " AND (sinfo_selection IS NOT NULL)");
142 reset.setParameter("river_id", this.river.getPeer().getId()); 144 reset.setParameter("river_id", this.river.getPeer().getId());
143 reset.setParameter("seltype", this.selectionType.getKey());
144 reset.executeUpdate(); 145 reset.executeUpdate();
145 final Query query = session.createQuery("FROM Wst WHERE (river=:river) AND (kind=1) AND (lower(description) LIKE :path)"); 146 final Query query = session.createQuery("FROM Wst WHERE (river=:river) AND (kind=1) AND (lower(description) LIKE :path)");
146 query.setParameter("river", this.river.getPeer()); 147 query.setParameter("river", this.river.getPeer());
147 int count = 0; 148 int count = 0;
148 for (final String wstfile : this.links) { 149 for (final String wstfile : this.links.keySet()) {
149 count += updateWst(session, query, this.river.getPeer(), wstfile, this.selectionType); 150 count += updateWst(session, query, this.river.getPeer(), wstfile, this.links.get(wstfile));
150 } 151 }
151 log.info("Updated " + count + " wsts for selected additionals " + this.selectionType.getLogText()); 152 log.info("Updated " + count + " wsts for selected additionals");
152 } 153 }
153 154
154 private int updateWst(final Session session, final Query query, final River river, final String path, final SelectionType selectionType) { 155 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 final String pathPattern = path.toLowerCase().replace('/', '_').replace('\\', '_');
156 query.setParameter("path", pathPattern); 157 query.setParameter("path", pathPattern);

http://dive4elements.wald.intevation.org