teichmann@8028: package org.dive4elements.river.importer; teichmann@8028: teichmann@8028: import java.util.List; tom@8056: import java.util.ArrayList; teichmann@8028: teichmann@8028: import org.dive4elements.river.model.GrainFraction; teichmann@8028: import org.dive4elements.river.model.SedimentLoad; teichmann@8028: import org.dive4elements.river.model.TimeInterval; teichmann@8028: import org.hibernate.Query; teichmann@8028: import org.hibernate.Session; teichmann@8028: teichmann@8028: public class ImportSedimentLoad teichmann@8028: { teichmann@8028: private SedimentLoad peer; teichmann@8028: teichmann@8028: private ImportGrainFraction grainFraction; teichmann@8028: private ImportTimeInterval timeInterval; teichmann@8028: private ImportTimeInterval sqTimeInterval; teichmann@8028: private String description; teichmann@8028: private Integer kind; teichmann@8028: tom@8056: private List values; tom@8056: teichmann@8028: public ImportSedimentLoad() { tom@8056: this.values = new ArrayList(); tom@8056: } tom@8056: tom@8056: public ImportSedimentLoad ( tom@8056: ImportGrainFraction grainFraction, tom@8056: ImportTimeInterval timeInterval, tom@8059: ImportTimeInterval sqTimeInterval, tom@8056: String description, tom@8056: Integer kind tom@8056: ) { tom@8059: this.grainFraction = grainFraction; tom@8059: this.timeInterval = timeInterval; tom@8059: this.sqTimeInterval = sqTimeInterval; tom@8059: this.description = description; tom@8059: this.kind = kind; tom@8056: tom@8056: this.values = new ArrayList(); tom@8056: } tom@8056: tom@8056: public void addValue(ImportSedimentLoadValue value) { tom@8056: values.add(value); teichmann@8028: } teichmann@8028: teichmann@8028: public void storeDependencies() { teichmann@8028: grainFraction.getPeer(); teichmann@8028: timeInterval.getPeer(); tom@8056: tom@8056: if (sqTimeInterval != null) { tom@8056: sqTimeInterval.getPeer(); tom@8056: } teichmann@8028: teichmann@8028: getPeer(); tom@8056: tom@8056: for (ImportSedimentLoadValue value : values) { tom@8056: value.storeDependencies(peer); tom@8056: } tom@8056: teichmann@8028: } teichmann@8028: teichmann@8028: public SedimentLoad getPeer() { teichmann@8028: teichmann@8028: if (peer == null) { teichmann@8028: Session session = ImporterSession.getInstance().getDatabaseSession(); tom@8056: tom@8056: String sqtquery = sqTimeInterval == null ? tom@8056: "sq_time_interval_id is null" : tom@8056: "sqTimeInterval = :sqTimeInterval"; teichmann@8028: Query query = session.createQuery( teichmann@8028: "from SedimentLoad where " + teichmann@8028: " grainFraction = :grainFraction and " + teichmann@8028: " timeInterval = :timeInterval and " + teichmann@8028: " description = :description and " + tom@8056: " kind = :kind and " + tom@8056: sqtquery); teichmann@8028: teichmann@8028: GrainFraction gf = grainFraction.getPeer(); teichmann@8028: TimeInterval ti = timeInterval.getPeer(); teichmann@8028: teichmann@8028: TimeInterval sqti = sqTimeInterval != null teichmann@8028: ? sqTimeInterval.getPeer() teichmann@8028: : null; teichmann@8028: teichmann@8028: query.setParameter("grainFraction", gf); teichmann@8028: query.setParameter("timeInterval", ti); tom@8056: tom@8056: if (sqti != null) { tom@8056: query.setParameter("sqTimeInterval", sqti); tom@8056: } teichmann@8028: query.setParameter("description", description); teichmann@8028: query.setParameter("kind", kind); teichmann@8028: teichmann@8028: List loads = query.list(); teichmann@8028: if (loads.isEmpty()) { teichmann@8028: peer = new SedimentLoad(gf, ti, sqti, description, kind); teichmann@8028: session.save(peer); teichmann@8028: } teichmann@8028: else { teichmann@8028: peer = loads.get(0); teichmann@8028: } teichmann@8028: } teichmann@8028: teichmann@8028: return peer; teichmann@8028: } teichmann@8028: } teichmann@8028: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :