ingo@2813: package de.intevation.flys.importer; ingo@2813: ingo@2817: import java.sql.SQLException; ingo@2813: import java.util.ArrayList; ingo@2813: import java.util.List; ingo@2813: ingo@2813: import org.apache.log4j.Logger; ingo@3943: import org.hibernate.Query; ingo@2817: import org.hibernate.Session; ingo@2817: import org.hibernate.exception.ConstraintViolationException; ingo@2817: ingo@2813: import de.intevation.flys.model.River; ingo@2813: import de.intevation.flys.model.SedimentDensity; ingo@2813: ingo@2813: ingo@2813: public class ImportSedimentDensity { ingo@2813: ingo@2813: private static Logger log = Logger.getLogger(ImportSedimentDensity.class); ingo@2813: ingo@2813: protected SedimentDensity peer; ingo@2813: ingo@2813: protected ImportDepth depth; ingo@2813: ingo@2816: protected ImportUnit unit; ingo@2816: ingo@2817: protected String description; ingo@2817: ingo@2813: protected List values; ingo@2813: ingo@2817: public ImportSedimentDensity(String description) { ingo@2817: this.description = description; ingo@3943: this.values = new ArrayList(); ingo@2817: } ingo@2817: ingo@2817: public String getDescription() { ingo@2817: return description; ingo@2813: } ingo@2813: ingo@2813: public void setDepth(ImportDepth depth) { ingo@2813: this.depth = depth; ingo@2813: } ingo@2813: ingo@2816: public void setUnit(ImportUnit unit) { ingo@2816: this.unit = unit; ingo@2816: } ingo@2816: ingo@2813: public void addValue(ImportSedimentDensityValue value) { ingo@2813: values.add(value); ingo@2813: } ingo@2813: ingo@3943: public void storeDependencies(River river) throws SQLException, ingo@3943: ConstraintViolationException { ingo@2813: log.info("store dependencies"); ingo@2817: ingo@2817: if (depth != null) { ingo@2817: depth.storeDependencies(); ingo@2817: } ingo@2817: ingo@2817: SedimentDensity peer = getPeer(river); ingo@2817: ingo@3943: if (peer != null) { ingo@3943: log.info("store sediment density values."); ingo@3943: for (ImportSedimentDensityValue value : values) { ingo@3943: value.storeDependencies(peer); ingo@3943: } ingo@2817: } ingo@2813: } ingo@2813: ingo@2813: public SedimentDensity getPeer(River river) { ingo@2813: log.info("get peer"); sascha@3942: ingo@3940: if (depth == null) { ingo@3943: log.warn("cannot store sediment density '" + description ingo@3943: + "': no depth"); ingo@3940: return null; ingo@3940: } sascha@3942: ingo@3940: if (unit == null) { ingo@3943: log.warn("cannot store sediment density '" + description ingo@3943: + "': no unit"); ingo@3940: return null; ingo@3940: } ingo@2813: ingo@2817: if (peer == null) { ingo@3943: Session session = ImporterSession.getInstance() ingo@3943: .getDatabaseSession(); ingo@2817: ingo@3943: Query query = session.createQuery("from SedimentDensity where " ingo@3943: + " river=:river and " + " depth=:depth and " ingo@3943: + " unit=:unit"); ingo@2817: ingo@2817: query.setParameter("river", river); ingo@2817: query.setParameter("depth", depth.getPeer()); ingo@2817: query.setParameter("unit", unit.getPeer()); ingo@2817: ingo@2817: List density = query.list(); ingo@2817: ingo@2817: if (density.isEmpty()) { ingo@2817: log.debug("Create new SedimentDensity DB instance."); ingo@2817: ingo@3943: peer = new SedimentDensity(river, depth.getPeer(), ingo@3943: unit.getPeer(), description); ingo@2817: ingo@2817: session.save(peer); ingo@2817: } ingo@2817: else { ingo@2817: peer = density.get(0); ingo@2817: } ingo@2817: } ingo@2817: ingo@2817: return peer; ingo@2813: } ingo@2813: } ingo@2813: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :