sascha@177: package de.intevation.flys.importer; sascha@177: sascha@184: import java.util.List; sascha@482: import java.util.ArrayList; sascha@184: sascha@177: import java.io.File; sascha@184: import java.io.IOException; sascha@184: sascha@184: import org.apache.log4j.Logger; sascha@184: sascha@188: import de.intevation.flys.model.River; sascha@188: sascha@184: import de.intevation.flys.utils.FileTools; sascha@177: sascha@188: import org.hibernate.Session; sascha@188: import org.hibernate.Query; sascha@188: sascha@177: public class ImportRiver sascha@177: { sascha@184: private static Logger log = Logger.getLogger(ImportRiver.class); sascha@184: sascha@184: public static final String PEGEL_GLT = "PEGEL.GLT"; sascha@184: sascha@483: public static final String FIXATIONS = "Fixierungen"; sascha@483: sascha@482: public static final String EXTRA_LONGITUDINALS = sascha@482: "Zus.L\u00e4ngsschnitte"; sascha@482: sascha@177: protected String name; sascha@177: sascha@177: protected File wstFile; sascha@177: sascha@177: protected File bbInfoFile; sascha@177: sascha@184: protected List gauges; sascha@184: sascha@186: protected List annotations; sascha@186: sascha@482: protected List extraWsts; sascha@482: sascha@483: protected List fixations; sascha@483: sascha@201: protected ImportWst wst; sascha@201: sascha@188: protected River peer; sascha@188: sascha@177: public ImportRiver() { sascha@482: extraWsts = new ArrayList(); sascha@483: fixations = new ArrayList(); sascha@177: } sascha@177: sascha@177: public ImportRiver(String name, File wstFile, File bbInfoFile) { sascha@482: this(); sascha@177: this.name = name; sascha@177: this.wstFile = wstFile; sascha@177: this.bbInfoFile = bbInfoFile; sascha@177: } sascha@177: sascha@177: public String getName() { sascha@177: return name; sascha@177: } sascha@177: sascha@177: public void setName(String name) { sascha@177: this.name = name; sascha@177: } sascha@177: sascha@177: public File getWstFile() { sascha@177: return wstFile; sascha@177: } sascha@177: sascha@177: public void setWstFile(File wstFile) { sascha@177: this.wstFile = wstFile; sascha@177: } sascha@177: sascha@177: public File getBBInfo() { sascha@177: return bbInfoFile; sascha@177: } sascha@177: sascha@177: public void setBBInfo(File bbInfoFile) { sascha@177: this.bbInfoFile = bbInfoFile; sascha@177: } sascha@184: sascha@201: public ImportWst getWst() { sascha@201: return wst; sascha@201: } sascha@201: sascha@201: public void setWst(ImportWst wst) { sascha@201: this.wst = wst; sascha@201: } sascha@201: sascha@186: public void parseDependencies() throws IOException { sascha@186: parseGauges(); sascha@186: parseAnnotations(); sascha@197: parseWst(); sascha@482: parseExtraWsts(); sascha@483: parseFixations(); sascha@483: } sascha@483: sascha@483: public void parseFixations() throws IOException { sascha@483: log.info("Parse fixation wst files"); sascha@483: sascha@483: File riverDir = wstFile.getParentFile().getParentFile(); sascha@483: sascha@483: File fixDir = FileTools.repair( sascha@483: new File(riverDir, FIXATIONS)); sascha@483: sascha@483: if (!fixDir.isDirectory() || !fixDir.canRead()) { sascha@483: log.info("no fixation wst file directory found"); sascha@483: return; sascha@483: } sascha@483: sascha@483: File [] files = fixDir.listFiles(); sascha@483: sascha@483: if (files == null) { sascha@483: log.warn("cannot read fixations wst file directory"); sascha@483: return; sascha@483: } sascha@483: sascha@483: for (File file: files) { sascha@483: if (!file.isFile() || !file.canRead()) { sascha@483: continue; sascha@483: } sascha@483: String name = file.getName().toLowerCase(); sascha@483: if (!name.endsWith(".wst")) { sascha@483: continue; sascha@483: } sascha@483: log.debug("Found WST file: " + file); sascha@483: sascha@483: WstParser wstParser = new WstParser(); sascha@483: wstParser.parse(file); sascha@483: ImportWst iw = wstParser.getWst(); sascha@483: iw.setKind(2); sascha@483: iw.setDescription("Fix: " + iw.getDescription()); sascha@483: fixations.add(iw); sascha@483: } sascha@482: } sascha@482: sascha@482: public void parseExtraWsts() throws IOException { sascha@482: log.info("Parse extra longitudinal wst files"); sascha@482: sascha@482: File riverDir = wstFile.getParentFile().getParentFile(); sascha@482: sascha@482: File extraDir = FileTools.repair( sascha@482: new File(riverDir, EXTRA_LONGITUDINALS)); sascha@482: sascha@482: if (!extraDir.isDirectory() || !extraDir.canRead()) { sascha@482: log.info("no extra longitudinal wst file directory found"); sascha@482: return; sascha@482: } sascha@482: sascha@482: File [] files = extraDir.listFiles(); sascha@482: sascha@482: if (files == null) { sascha@482: log.warn("cannot read extra longitudinal wst file directory"); sascha@482: return; sascha@482: } sascha@482: sascha@482: for (File file: files) { sascha@482: if (!file.isFile() || !file.canRead()) { sascha@482: continue; sascha@482: } sascha@482: String name = file.getName().toLowerCase(); sascha@482: if (!(name.endsWith(".zus") || name.endsWith(".wst"))) { sascha@482: continue; sascha@482: } sascha@482: log.debug("Found WST file: " + file); sascha@482: sascha@482: WstParser wstParser = new WstParser(); sascha@482: wstParser.parse(file); sascha@482: ImportWst iw = wstParser.getWst(); sascha@482: iw.setKind(1); sascha@482: extraWsts.add(iw); sascha@482: } sascha@482: sascha@197: } sascha@197: sascha@197: public void parseWst() throws IOException { sascha@197: WstParser wstParser = new WstParser(); sascha@197: wstParser.parse(wstFile); sascha@201: wst = wstParser.getWst(); sascha@186: } sascha@186: sascha@184: public void parseGauges() throws IOException { sascha@184: File gltFile = new File(wstFile.getParentFile(), PEGEL_GLT); sascha@184: gltFile = FileTools.repair(gltFile); sascha@184: sascha@184: if (!gltFile.isFile() || !gltFile.canRead()) { sascha@184: log.warn("cannot read gauges from '" + gltFile + "'"); sascha@184: return; sascha@184: } sascha@184: sascha@184: PegelGltParser pgltp = new PegelGltParser(); sascha@184: pgltp.parse(gltFile); sascha@184: sascha@184: gauges = pgltp.getGauges(); sascha@189: sascha@189: for (ImportGauge gauge: gauges) { sascha@189: gauge.parseDependencies(); sascha@189: } sascha@184: } sascha@186: sascha@186: public void parseAnnotations() throws IOException { sascha@186: File riverDir = wstFile.getParentFile().getParentFile(); sascha@186: AnnotationsParser aparser = new AnnotationsParser(); sascha@186: aparser.parse(riverDir); sascha@186: sascha@186: annotations = aparser.getAnnotations(); sascha@186: } sascha@188: sascha@190: public void storeDependencies() { sascha@190: storeAnnotations(); sascha@190: storeGauges(); sascha@201: storeWst(); sascha@482: storeExtraWsts(); sascha@483: storeFixations(); sascha@201: } sascha@201: sascha@201: public void storeWst() { sascha@201: River river = getPeer(); sascha@201: wst.storeDependencies(river); sascha@190: } sascha@190: sascha@483: public void storeFixations() { sascha@483: log.info("store fixation wsts"); sascha@483: River river = getPeer(); sascha@483: for (ImportWst wst: fixations) { sascha@483: log.debug("name: " + wst.getDescription()); sascha@483: wst.storeDependencies(river); sascha@483: } sascha@483: } sascha@483: sascha@482: public void storeExtraWsts() { sascha@482: log.info("store extra wsts"); sascha@482: River river = getPeer(); sascha@482: for (ImportWst wst: extraWsts) { sascha@482: log.debug("name: " + wst.getDescription()); sascha@482: wst.storeDependencies(river); sascha@482: } sascha@482: } sascha@482: sascha@188: public void storeAnnotations() { sascha@188: River river = getPeer(); sascha@188: for (ImportAnnotation annotation: annotations) { sascha@188: annotation.getPeer(river); sascha@188: } sascha@188: } sascha@188: sascha@190: public void storeGauges() { sascha@194: log.info("store gauges:"); sascha@191: River river = getPeer(); sascha@191: for (ImportGauge gauge: gauges) { sascha@194: log.info("\tgauge: " + gauge.getName()); sascha@191: gauge.storeDependencies(river); sascha@191: } sascha@190: } sascha@190: sascha@188: public River getPeer() { sascha@188: if (peer == null) { sascha@188: Session session = Importer.sessionHolder.get(); sascha@188: Query query = session.createQuery("from River where name=:name"); sascha@188: query.setString("name", name); sascha@188: List rivers = query.list(); sascha@188: if (rivers.isEmpty()) { sascha@188: peer = new River(name); sascha@188: session.save(peer); sascha@188: } sascha@188: else { sascha@188: peer = rivers.get(0); sascha@188: } sascha@188: } sascha@188: return peer; sascha@188: } sascha@177: } sascha@177: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :