teichmann@5829: package org.dive4elements.river.importer; sascha@1204: teichmann@5829: import org.dive4elements.river.model.CrossSection; teichmann@5829: import org.dive4elements.river.model.CrossSectionPoint; teichmann@5829: import org.dive4elements.river.model.CrossSectionLine; sascha@1204: sascha@1204: import org.hibernate.Session; sascha@1204: import org.hibernate.Query; sascha@1204: sascha@1204: import java.util.List; sascha@1204: import java.util.Comparator; sascha@1204: import java.util.Map; sascha@1204: import java.util.TreeMap; sascha@1204: felix@4705: /** felix@4705: * A CrossSectionLine (containing points) ready to be transformed into a mapped felix@4705: * object and written to db (used in importer). felix@4705: */ sascha@1204: public class ImportCrossSectionLine sascha@1204: { sascha@1204: public static final Comparator INDEX_CMP = sascha@1204: new Comparator() { sascha@1204: public int compare(CrossSectionPoint a, CrossSectionPoint b) { sascha@1204: return a.getColPos().compareTo(b.getColPos()); sascha@1204: } sascha@1204: }; sascha@1204: felix@5047: protected Double km; sascha@1204: protected ImportCrossSection crossSection; felix@5047: protected List points; sascha@1204: sascha@1204: protected CrossSectionLine peer; sascha@1204: sascha@1204: public ImportCrossSectionLine() { sascha@1204: } sascha@1204: sascha@2860: public ImportCrossSectionLine(Double km, List points) { sascha@1204: this.km = km; sascha@1204: this.points = points; sascha@1204: } sascha@1204: sascha@1204: public ImportCrossSection getCrossSection() { sascha@1204: return crossSection; sascha@1204: } sascha@1204: sascha@1204: public void setCrossSection(ImportCrossSection crossSection) { sascha@1204: this.crossSection = crossSection; sascha@1204: } sascha@1204: sascha@2860: public Double getKm() { sascha@1204: return km; sascha@1204: } sascha@1204: sascha@2860: public void setKm(Double km) { sascha@1204: this.km = km; sascha@1204: } sascha@1204: sascha@1204: public void storeDependencies() { sascha@1204: storePoints(); sascha@1204: } sascha@1204: felix@4705: felix@4705: /** Write a line and its points. */ sascha@1204: protected void storePoints() { sascha@1204: CrossSectionLine csl = getPeer(); sascha@1204: sascha@1204: Map map = sascha@1204: new TreeMap(INDEX_CMP); sascha@1204: felix@4705: // Build index for faster (index) collision lookup. sascha@1205: List ps = csl.getPoints(); sascha@1205: if (ps != null) { sascha@1205: for (CrossSectionPoint point: ps) { sascha@1205: map.put(point, point); sascha@1205: } sascha@1204: } sascha@1204: sascha@1204: Session session = sascha@1204: ImporterSession.getInstance().getDatabaseSession(); sascha@1204: sascha@1204: CrossSectionPoint key = new CrossSectionPoint(); sascha@1204: felix@4705: // Somehow it looks as if even with the map it is still possible that felix@4705: // multiple points with same id enter hibernate (and then violate a felix@4705: // constraint). -> TODO sascha@1204: for (XY xy: points) { sascha@1204: key.setColPos(xy.getIndex()); sascha@1204: CrossSectionPoint csp = map.get(key); sascha@1204: if (csp == null) { // create new sascha@1204: csp = new CrossSectionPoint( sascha@1204: csl, key.getColPos(), sascha@2860: Double.valueOf(xy.getX()), sascha@2860: Double.valueOf(xy.getY())); sascha@1204: } sascha@1204: else { // update old sascha@2860: csp.setX(Double.valueOf(xy.getX())); sascha@2860: csp.setY(Double.valueOf(xy.getY())); sascha@1204: } sascha@1204: session.save(csp); sascha@1204: } sascha@1204: } sascha@1204: sascha@1204: public CrossSectionLine getPeer() { sascha@1204: if (peer == null) { sascha@1204: CrossSection cs = crossSection.getPeer(); sascha@1204: sascha@1204: Session session = sascha@1204: ImporterSession.getInstance().getDatabaseSession(); sascha@1204: sascha@1204: Query query = session.createQuery( sascha@1204: "from CrossSectionLine where crossSection=:cs and km=:km"); sascha@1204: query.setParameter("cs", cs); sascha@1204: query.setParameter("km", km); sascha@1204: sascha@1204: List lines = query.list(); sascha@1204: if (lines.isEmpty()) { sascha@1204: peer = new CrossSectionLine(cs, km); sascha@1204: session.save(peer); sascha@1204: } sascha@1204: else { sascha@1204: peer = lines.get(0); sascha@1204: } sascha@1204: } sascha@1204: return peer; sascha@1204: } sascha@1204: } sascha@1204: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :