comparison flys-backend/src/main/java/org/dive4elements/river/importer/ImportEdge.java @ 5828:dfb26b03b179

Moved directories to org.dive4elements.river
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:53:11 +0200
parents flys-backend/src/main/java/de/intevation/flys/importer/ImportEdge.java@9f2204ed79ed
children 18619c1e7c2a
comparison
equal deleted inserted replaced
5827:e308d4ecd35a 5828:dfb26b03b179
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 :

http://dive4elements.wald.intevation.org