Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/ImportEdge.java @ 759:9f2204ed79ed
Import edges to database.
flys-backend/trunk@2107 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 14 Jun 2011 13:13:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
758:bf16268629d9 | 759:9f2204ed79ed |
---|---|
1 package de.intevation.flys.importer; | |
2 | |
3 import de.intevation.flys.model.Edge; | |
4 | |
5 import org.hibernate.Session; | |
6 import org.hibernate.Query; | |
7 | |
8 import java.util.List; | |
9 | |
10 import java.math.BigDecimal; | |
11 | |
12 public class ImportEdge | |
13 implements Comparable<ImportEdge> | |
14 { | |
15 protected BigDecimal top; | |
16 protected BigDecimal bottom; | |
17 | |
18 protected Edge peer; | |
19 | |
20 public ImportEdge() { | |
21 } | |
22 | |
23 public ImportEdge(BigDecimal top, BigDecimal bottom) { | |
24 this.top = top; | |
25 this.bottom = bottom; | |
26 } | |
27 | |
28 public BigDecimal getTop() { | |
29 return top; | |
30 } | |
31 | |
32 public void setTop(BigDecimal top) { | |
33 this.top = top; | |
34 } | |
35 | |
36 public BigDecimal getBottom() { | |
37 return bottom; | |
38 } | |
39 | |
40 public void setBottom(BigDecimal bottom) { | |
41 this.bottom = bottom; | |
42 } | |
43 | |
44 private static final int compare(BigDecimal a, BigDecimal b) { | |
45 if (a == null && b != null) return -1; | |
46 if (a != null && b == null) return +1; | |
47 if (a == null && b == null) return 0; | |
48 return a.compareTo(b); | |
49 } | |
50 | |
51 public int compareTo(ImportEdge other) { | |
52 int cmp = compare(top, other.top); | |
53 return cmp != 0 ? cmp : compare(bottom, other.bottom); | |
54 } | |
55 | |
56 public Edge getPeer() { | |
57 if (peer == null) { | |
58 Session session = ImporterSession.getInstance().getDatabaseSession(); | |
59 Query query = session.createQuery( | |
60 "from Edge where top=:top and bottom=:bottom"); | |
61 query.setParameter("top", top); | |
62 query.setParameter("bottom", bottom); | |
63 List<Edge> edges = query.list(); | |
64 if (edges.isEmpty()) { | |
65 peer = new Edge(top, bottom); | |
66 session.save(peer); | |
67 } | |
68 else { | |
69 peer = edges.get(0); | |
70 } | |
71 } | |
72 return peer; | |
73 } | |
74 } | |
75 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |