Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/ImportCrossSectionLine.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-backend/src/main/java/org/dive4elements/river/importer/ImportCrossSectionLine.java@18619c1e7c2a |
children | 4dd33b86dc61 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.importer; | |
2 | |
3 import org.dive4elements.river.model.CrossSection; | |
4 import org.dive4elements.river.model.CrossSectionPoint; | |
5 import org.dive4elements.river.model.CrossSectionLine; | |
6 | |
7 import org.hibernate.Session; | |
8 import org.hibernate.Query; | |
9 | |
10 import java.util.List; | |
11 import java.util.Comparator; | |
12 import java.util.Map; | |
13 import java.util.TreeMap; | |
14 | |
15 /** | |
16 * A CrossSectionLine (containing points) ready to be transformed into a mapped | |
17 * object and written to db (used in importer). | |
18 */ | |
19 public class ImportCrossSectionLine | |
20 { | |
21 public static final Comparator<CrossSectionPoint> INDEX_CMP = | |
22 new Comparator<CrossSectionPoint>() { | |
23 public int compare(CrossSectionPoint a, CrossSectionPoint b) { | |
24 return a.getColPos().compareTo(b.getColPos()); | |
25 } | |
26 }; | |
27 | |
28 protected Double km; | |
29 protected ImportCrossSection crossSection; | |
30 protected List<XY> points; | |
31 | |
32 protected CrossSectionLine peer; | |
33 | |
34 public ImportCrossSectionLine() { | |
35 } | |
36 | |
37 public ImportCrossSectionLine(Double km, List<XY> points) { | |
38 this.km = km; | |
39 this.points = points; | |
40 } | |
41 | |
42 public ImportCrossSection getCrossSection() { | |
43 return crossSection; | |
44 } | |
45 | |
46 public void setCrossSection(ImportCrossSection crossSection) { | |
47 this.crossSection = crossSection; | |
48 } | |
49 | |
50 public Double getKm() { | |
51 return km; | |
52 } | |
53 | |
54 public void setKm(Double km) { | |
55 this.km = km; | |
56 } | |
57 | |
58 public void storeDependencies() { | |
59 storePoints(); | |
60 } | |
61 | |
62 | |
63 /** Write a line and its points. */ | |
64 protected void storePoints() { | |
65 CrossSectionLine csl = getPeer(); | |
66 | |
67 Map<CrossSectionPoint, CrossSectionPoint> map = | |
68 new TreeMap<CrossSectionPoint, CrossSectionPoint>(INDEX_CMP); | |
69 | |
70 // Build index for faster (index) collision lookup. | |
71 List<CrossSectionPoint> ps = csl.getPoints(); | |
72 if (ps != null) { | |
73 for (CrossSectionPoint point: ps) { | |
74 map.put(point, point); | |
75 } | |
76 } | |
77 | |
78 Session session = | |
79 ImporterSession.getInstance().getDatabaseSession(); | |
80 | |
81 CrossSectionPoint key = new CrossSectionPoint(); | |
82 | |
83 // Somehow it looks as if even with the map it is still possible that | |
84 // multiple points with same id enter hibernate (and then violate a | |
85 // constraint). -> TODO | |
86 for (XY xy: points) { | |
87 key.setColPos(xy.getIndex()); | |
88 CrossSectionPoint csp = map.get(key); | |
89 if (csp == null) { // create new | |
90 csp = new CrossSectionPoint( | |
91 csl, key.getColPos(), | |
92 Double.valueOf(xy.getX()), | |
93 Double.valueOf(xy.getY())); | |
94 } | |
95 else { // update old | |
96 csp.setX(Double.valueOf(xy.getX())); | |
97 csp.setY(Double.valueOf(xy.getY())); | |
98 } | |
99 session.save(csp); | |
100 } | |
101 } | |
102 | |
103 public CrossSectionLine getPeer() { | |
104 if (peer == null) { | |
105 CrossSection cs = crossSection.getPeer(); | |
106 | |
107 Session session = | |
108 ImporterSession.getInstance().getDatabaseSession(); | |
109 | |
110 Query query = session.createQuery( | |
111 "from CrossSectionLine where crossSection=:cs and km=:km"); | |
112 query.setParameter("cs", cs); | |
113 query.setParameter("km", km); | |
114 | |
115 List<CrossSectionLine> lines = query.list(); | |
116 if (lines.isEmpty()) { | |
117 peer = new CrossSectionLine(cs, km); | |
118 session.save(peer); | |
119 } | |
120 else { | |
121 peer = lines.get(0); | |
122 } | |
123 } | |
124 return peer; | |
125 } | |
126 } | |
127 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |