comparison flys-backend/src/main/java/de/intevation/flys/importer/ImportCrossSectionLine.java @ 1204:22858e7cca79

Integrated PRF parsing into importer. Needs testing! flys-backend/trunk@2309 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 07 Jul 2011 22:25:38 +0000
parents
children 5f1506fc7636
comparison
equal deleted inserted replaced
1203:3c01bef43a98 1204:22858e7cca79
1 package de.intevation.flys.importer;
2
3 import de.intevation.flys.model.CrossSection;
4 import de.intevation.flys.model.CrossSectionPoint;
5 import de.intevation.flys.model.CrossSectionLine;
6
7 import org.hibernate.Session;
8 import org.hibernate.Query;
9
10 import java.math.BigDecimal;
11
12 import java.util.List;
13 import java.util.Comparator;
14 import java.util.Map;
15 import java.util.TreeMap;
16
17 public class ImportCrossSectionLine
18 {
19 public static final Comparator<CrossSectionPoint> INDEX_CMP =
20 new Comparator<CrossSectionPoint>() {
21 public int compare(CrossSectionPoint a, CrossSectionPoint b) {
22 return a.getColPos().compareTo(b.getColPos());
23 }
24 };
25
26 protected BigDecimal km;
27 protected ImportCrossSection crossSection;
28 protected List<XY> points;
29
30 protected CrossSectionLine peer;
31
32 public ImportCrossSectionLine() {
33 }
34
35 public ImportCrossSectionLine(BigDecimal km, List<XY> points) {
36 this.km = km;
37 this.points = points;
38 }
39
40 public ImportCrossSection getCrossSection() {
41 return crossSection;
42 }
43
44 public void setCrossSection(ImportCrossSection crossSection) {
45 this.crossSection = crossSection;
46 }
47
48 public BigDecimal getKm() {
49 return km;
50 }
51
52 public void setKm(BigDecimal km) {
53 this.km = km;
54 }
55
56 public void storeDependencies() {
57 storePoints();
58 }
59
60 protected void storePoints() {
61 CrossSectionLine csl = getPeer();
62
63 Map<CrossSectionPoint, CrossSectionPoint> map =
64 new TreeMap<CrossSectionPoint, CrossSectionPoint>(INDEX_CMP);
65
66 // build index for faster collision lookup
67 for (CrossSectionPoint point: csl.getPoints()) {
68 map.put(point, point);
69 }
70
71 Session session =
72 ImporterSession.getInstance().getDatabaseSession();
73
74 CrossSectionPoint key = new CrossSectionPoint();
75
76 for (XY xy: points) {
77 key.setColPos(xy.getIndex());
78 CrossSectionPoint csp = map.get(key);
79 if (csp == null) { // create new
80 csp = new CrossSectionPoint(
81 csl, key.getColPos(),
82 new BigDecimal(xy.getX()),
83 new BigDecimal(xy.getY()));
84 }
85 else { // update old
86 csp.setX(new BigDecimal(xy.getX()));
87 csp.setY(new BigDecimal(xy.getY()));
88 }
89 session.save(csp);
90 }
91 }
92
93 public CrossSectionLine getPeer() {
94 if (peer == null) {
95 CrossSection cs = crossSection.getPeer();
96
97 Session session =
98 ImporterSession.getInstance().getDatabaseSession();
99
100 Query query = session.createQuery(
101 "from CrossSectionLine where crossSection=:cs and km=:km");
102 query.setParameter("cs", cs);
103 query.setParameter("km", km);
104
105 List<CrossSectionLine> lines = query.list();
106 if (lines.isEmpty()) {
107 peer = new CrossSectionLine(cs, km);
108 session.save(peer);
109 }
110 else {
111 peer = lines.get(0);
112 }
113 }
114 return peer;
115 }
116 }
117 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org