Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/ImportSQRelation.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/ImportSQRelation.java@18619c1e7c2a |
children | 4dd33b86dc61 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.importer; | |
2 | |
3 import java.sql.SQLException; | |
4 import java.util.ArrayList; | |
5 import java.util.List; | |
6 | |
7 import org.apache.log4j.Logger; | |
8 | |
9 import org.hibernate.Query; | |
10 import org.hibernate.Session; | |
11 import org.hibernate.exception.ConstraintViolationException; | |
12 | |
13 import org.dive4elements.river.model.River; | |
14 import org.dive4elements.river.model.SQRelation; | |
15 import org.dive4elements.river.model.TimeInterval; | |
16 | |
17 | |
18 public class ImportSQRelation { | |
19 | |
20 private static Logger log = Logger.getLogger(ImportSQRelation.class); | |
21 | |
22 private ImportTimeInterval timeInterval; | |
23 | |
24 private String description; | |
25 | |
26 private List<ImportSQRelationValue> values; | |
27 | |
28 private SQRelation peer; | |
29 | |
30 public ImportSQRelation() { | |
31 this.values = new ArrayList<ImportSQRelationValue>(); | |
32 } | |
33 | |
34 public void storeDependencies(River river) { | |
35 log.info("store dependencies"); | |
36 | |
37 SQRelation peer = getPeer(river); | |
38 | |
39 if (peer != null) { | |
40 int count = 0; | |
41 | |
42 for (ImportSQRelationValue value : values) { | |
43 try { | |
44 value.storeDependencies(peer); | |
45 count++; | |
46 } | |
47 catch (SQLException sqle) { | |
48 log.warn("ISQ: Unable to store sq relation value.", sqle); | |
49 } | |
50 catch (ConstraintViolationException cve) { | |
51 log.warn("ISQ: Unable to store sq relation value.", cve); | |
52 } | |
53 } | |
54 | |
55 log.info("stored " + count + " sq relation values."); | |
56 } | |
57 } | |
58 | |
59 public SQRelation getPeer(River river) { | |
60 log.debug("getPeer()"); | |
61 | |
62 if (peer == null) { | |
63 TimeInterval timeInter = timeInterval.getPeer(); | |
64 | |
65 if (timeInter == null) { | |
66 log.warn("ISQ: Cannot determine sq relation without time interval"); | |
67 return null; | |
68 } | |
69 | |
70 Session session = ImporterSession.getInstance() | |
71 .getDatabaseSession(); | |
72 | |
73 Query query = session | |
74 .createQuery("FROM SQRelation WHERE river=:river AND timeInterval=:timeInter"); | |
75 | |
76 query.setParameter("river", river); | |
77 query.setParameter("timeInter", timeInter); | |
78 | |
79 List<SQRelation> sq = query.list(); | |
80 | |
81 if (sq.isEmpty()) { | |
82 log.info("create new SQ relation '" + description + "'"); | |
83 | |
84 peer = new SQRelation(river, timeInter, description); | |
85 session.save(peer); | |
86 } | |
87 else { | |
88 peer = sq.get(0); | |
89 } | |
90 } | |
91 | |
92 return peer; | |
93 } | |
94 | |
95 public void setDescription(String description) { | |
96 this.description = description; | |
97 } | |
98 | |
99 public void setTimeInterval(ImportTimeInterval timeInterval) { | |
100 this.timeInterval = timeInterval; | |
101 } | |
102 | |
103 public void addValue(ImportSQRelationValue value) { | |
104 if (value != null) { | |
105 this.values.add(value); | |
106 } | |
107 } | |
108 } | |
109 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |