teichmann@5844: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5844: * Software engineering by Intevation GmbH teichmann@5844: * teichmann@5992: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5844: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5992: * documentation coming with Dive4Elements River for details. teichmann@5844: */ teichmann@5844: teichmann@5829: package org.dive4elements.river.importer; sascha@497: teichmann@8029: import java.util.ArrayList; teichmann@8027: import java.util.HashMap; sascha@497: import java.util.Iterator; teichmann@8029: import java.util.List; sascha@497: import java.util.Map; sascha@503: import java.util.TreeMap; sascha@497: import java.math.BigDecimal; sascha@497: sascha@497: import org.hibernate.SessionFactory; sascha@497: import org.hibernate.Session; sascha@497: import org.hibernate.Query; teichmann@5829: import org.dive4elements.river.backend.SessionFactoryProvider; teichmann@8027: import org.dive4elements.river.model.GrainFraction; teichmann@8029: import org.dive4elements.river.model.MeasurementStation; teichmann@5829: import org.dive4elements.river.model.WstColumnValue; teichmann@5829: import org.dive4elements.river.model.WstColumn; teichmann@5829: import org.dive4elements.river.model.DischargeTableValue; teichmann@5829: import org.dive4elements.river.model.DischargeTable; teichmann@5829: import org.dive4elements.river.model.Range; teichmann@5829: import org.dive4elements.river.model.River; teichmann@8187: import org.dive4elements.river.backend.utils.EpsilonComparator; teichmann@5829: import org.dive4elements.artifacts.common.utils.LRUCache; sascha@1227: sascha@497: public class ImporterSession sascha@497: { sascha@497: private static final ThreadLocal SESSION = sascha@497: new ThreadLocal() { sascha@497: @Override sascha@497: protected ImporterSession initialValue() { sascha@497: return new ImporterSession(); sascha@497: } sascha@497: }; sascha@497: sascha@497: protected Session databaseSession; sascha@497: teichmann@8029: private LRUCache> sascha@503: wstColumnValues; sascha@498: teichmann@8029: private LRUCache> sascha@501: dischargeTableValues; sascha@497: teichmann@8029: private LRUCache> sascha@503: ranges; sascha@500: teichmann@8027: private Map grainFractions; teichmann@8027: teichmann@8029: private Map>> teichmann@8029: riversToMeasurementStations; teichmann@8029: sascha@497: public static ImporterSession getInstance() { sascha@497: return SESSION.get(); sascha@497: } sascha@497: sascha@497: public ImporterSession() { sascha@497: SessionFactory sessionFactory = sascha@497: SessionFactoryProvider.createSessionFactory(); sascha@497: databaseSession = sessionFactory.openSession(); sascha@502: //databaseSession.setFlushMode(FlushMode.MANUAL); sascha@501: sascha@501: wstColumnValues = sascha@1227: new LRUCache>(); sascha@501: sascha@501: dischargeTableValues = sascha@1227: new LRUCache>(); sascha@503: sascha@1227: ranges = new LRUCache>(); sascha@497: } sascha@497: sascha@497: public Session getDatabaseSession() { sascha@497: return databaseSession; sascha@497: } sascha@497: sascha@497: public WstColumnValue getWstColumnValue( sascha@497: WstColumn column, sascha@497: BigDecimal position, sascha@497: BigDecimal w sascha@497: ) { sascha@501: Integer c = column.getId(); sascha@501: sascha@501: Map map = wstColumnValues.get(c); sascha@501: sascha@501: if (map == null) { sascha@503: map = new TreeMap( sascha@503: ValueKey.EPSILON_COMPARATOR); sascha@501: wstColumnValues.put(c, map); sascha@501: Query query = databaseSession.createQuery( sascha@501: "from WstColumnValue where wstColumn.id=:cid"); sascha@501: query.setParameter("cid", c); sascha@501: for (Iterator iter = query.iterate(); iter.hasNext();) { sascha@501: WstColumnValue wcv = (WstColumnValue)iter.next(); sascha@501: map.put(new ValueKey(wcv.getPosition(), wcv.getW()), wcv); sascha@501: } sascha@497: } sascha@497: sascha@501: ValueKey key = new ValueKey(position, w); sascha@497: sascha@501: WstColumnValue wcv = map.get(key); sascha@497: sascha@497: if (wcv != null) { sascha@497: return wcv; sascha@497: } sascha@497: sascha@497: wcv = new WstColumnValue(column, position, w); sascha@497: sascha@497: databaseSession.save(wcv); sascha@497: sascha@501: map.put(key, wcv); sascha@497: sascha@497: return wcv; sascha@497: } sascha@497: sascha@498: public DischargeTableValue getDischargeTableValue( sascha@498: DischargeTable table, sascha@498: BigDecimal q, sascha@498: BigDecimal w sascha@498: ) { sascha@501: Integer t = table.getId(); sascha@498: sascha@501: Map map = sascha@501: dischargeTableValues.get(t); sascha@498: sascha@501: if (map == null) { sascha@503: map = new TreeMap( sascha@503: ValueKey.EPSILON_COMPARATOR); sascha@501: dischargeTableValues.put(t, map); sascha@501: Query query = databaseSession.createQuery( sascha@501: "from DischargeTableValue where dischargeTable.id=:tid"); sascha@501: query.setParameter("tid", t); sascha@501: for (Iterator iter = query.iterate(); iter.hasNext();) { sascha@501: DischargeTableValue dctv = (DischargeTableValue)iter.next(); sascha@503: map.put(new ValueKey(dctv.getQ(), dctv.getW()), dctv); sascha@501: } sascha@498: } sascha@498: sascha@501: ValueKey key = new ValueKey(q, w); sascha@498: sascha@501: DischargeTableValue dctv = map.get(key); sascha@498: sascha@501: if (dctv != null) { sascha@501: return dctv; sascha@501: } sascha@498: sascha@501: dctv = new DischargeTableValue(table, q, w); sascha@501: sascha@501: databaseSession.save(dctv); sascha@501: sascha@501: map.put(key, dctv); sascha@501: sascha@501: return dctv; sascha@500: } sascha@500: teichmann@8027: public GrainFraction getGrainFraction(String name) { teichmann@8027: if (grainFractions == null) { teichmann@8027: grainFractions = new HashMap(); teichmann@8027: Query query = databaseSession.createQuery("from GrainFraction"); teichmann@8027: for (Iterator iter = query.iterate(); iter.hasNext();) { teichmann@8027: GrainFraction gf = (GrainFraction)iter.next(); teichmann@8027: grainFractions.put(gf.getName(), gf); teichmann@8027: } teichmann@8027: } teichmann@8027: return grainFractions.get(name); teichmann@8027: } teichmann@8027: sascha@500: public Range getRange(River river, BigDecimal a, BigDecimal b) { sascha@503: Integer r = river.getId(); sascha@500: sascha@503: Map map = ranges.get(r); sascha@503: sascha@503: if (map == null) { sascha@503: map = new TreeMap( sascha@503: ValueKey.EPSILON_COMPARATOR); sascha@503: ranges.put(r, map); sascha@503: Query query = databaseSession.createQuery( sascha@503: "from Range where river.id=:rid"); sascha@503: query.setParameter("rid", r); sascha@503: for (Iterator iter = query.iterate(); iter.hasNext();) { sascha@503: Range range = (Range)iter.next(); sascha@503: map.put(new ValueKey(range.getA(), range.getB()), range); sascha@503: } sascha@500: } sascha@500: sascha@503: ValueKey key = new ValueKey(a, b); sascha@500: sascha@503: Range range = map.get(key); sascha@500: sascha@502: if (range != null) { sascha@500: return range; sascha@500: } sascha@500: sascha@500: range = new Range(a, b, river); sascha@500: sascha@500: databaseSession.save(range); sascha@500: sascha@503: map.put(key, range); sascha@500: sascha@500: return range; sascha@500: } teichmann@8029: teichmann@8029: public List getMeasurementStations(String river, double station) { teichmann@8029: if (riversToMeasurementStations == null) { teichmann@8029: riversToMeasurementStations = teichmann@8029: new HashMap>>(); teichmann@8029: } teichmann@8029: teichmann@8029: Map> km2Stations = teichmann@8029: riversToMeasurementStations.get(river); teichmann@8029: if (km2Stations == null) { teichmann@8029: km2Stations = teichmann@8029: new TreeMap>(EpsilonComparator.CMP); teichmann@8029: riversToMeasurementStations.put(river, km2Stations); teichmann@8029: Query query = databaseSession.createQuery( tom@8054: "from MeasurementStation where river.name = :name"); teichmann@8029: query.setParameter("name", river); teichmann@8029: for (Iterator iter = query.iterate(); iter.hasNext();) { teichmann@8029: MeasurementStation st = (MeasurementStation)iter.next(); teichmann@8029: List ms = km2Stations.get(st.getStation()); teichmann@8029: if (ms == null) { teichmann@8029: ms = new ArrayList(2); teichmann@8029: km2Stations.put(st.getStation(), ms); teichmann@8029: } teichmann@8029: ms.add(st); teichmann@8029: } teichmann@8029: teichmann@8029: } teichmann@8029: return km2Stations.get(station); teichmann@8029: } sascha@497: } sascha@497: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :