teichmann@5844: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5844: * Software engineering by Intevation GmbH teichmann@5844: * teichmann@5844: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5844: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5844: * documentation coming with Dive4Elements River for details. teichmann@5844: */ teichmann@5844: teichmann@5829: package org.dive4elements.river.importer; sascha@486: teichmann@5829: import org.dive4elements.river.model.TimeInterval; sascha@486: sascha@486: import org.hibernate.Session; sascha@486: import org.hibernate.Query; sascha@486: sascha@486: import java.util.List; sascha@486: import java.util.Date; sascha@486: sascha@493: import org.apache.log4j.Logger; sascha@493: sascha@486: public class ImportTimeInterval sascha@486: { sascha@493: private static Logger log = Logger.getLogger(ImportTimeInterval.class); sascha@493: sascha@486: protected Date startTime; sascha@486: protected Date stopTime; sascha@486: sascha@486: protected TimeInterval peer; sascha@486: sascha@486: public ImportTimeInterval() { sascha@486: } sascha@486: sascha@1204: public ImportTimeInterval(Date startTime) { sascha@1204: this.startTime = startTime; felix@5811: this.stopTime = null; sascha@1204: } sascha@1204: sascha@486: public ImportTimeInterval(Date startTime, Date stopTime) { tom@5780: Date start; tom@5780: Date stop; tom@5780: if (startTime == null) { tom@5780: start = stopTime; tom@5780: stop = null; tom@5780: } tom@5780: else { tom@5780: start = startTime; tom@5780: stop = stopTime; tom@5780: } tom@5780: tom@5780: if (stop != null && start.after(stop)) { tom@5780: this.stopTime = start; tom@5780: this.startTime = stop; rrenkert@5775: } rrenkert@5775: else { tom@5780: this.startTime = start; tom@5780: this.stopTime = stop; rrenkert@5775: } sascha@486: } sascha@486: sascha@486: public Date getStartTime() { sascha@486: return startTime; sascha@486: } sascha@486: sascha@486: public void setStartTime(Date startTime) { sascha@486: this.startTime = startTime; sascha@486: } sascha@486: sascha@486: public Date getStopTime() { sascha@486: return stopTime; sascha@486: } sascha@486: sascha@486: public void setStopTime(Date stopTime) { sascha@486: this.stopTime = stopTime; sascha@486: } sascha@486: sascha@486: public TimeInterval getPeer() { sascha@493: if (peer == null) { sascha@497: Session session = ImporterSession.getInstance().getDatabaseSession(); felix@5811: if (startTime == null) { felix@5811: log.error("Null Start time will be ignored."); felix@5811: } felix@5811: Query query; felix@5811: if (stopTime == null) { felix@5811: query = session.createQuery( felix@5811: "from TimeInterval where startTime=:a and stopTime is null"); felix@5811: } felix@5811: else { felix@5811: query = session.createQuery( felix@5811: "from TimeInterval where startTime=:a and stopTime=:b"); felix@5811: query.setParameter("b", stopTime); felix@5811: } sascha@486: query.setParameter("a", startTime); felix@5812: sascha@486: List intervals = query.list(); sascha@486: if (intervals.isEmpty()) { sascha@486: peer = new TimeInterval(startTime, stopTime); sascha@486: session.save(peer); sascha@486: } sascha@487: else { sascha@487: peer = intervals.get(0); sascha@487: } sascha@486: } sascha@486: return peer; sascha@486: } sascha@486: } sascha@486: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :