comparison flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelation.java @ 3943:a5b003595d6c

Store minfo values into database only if their peer has been successfully stored. flys-backend/trunk@5520 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 19 Sep 2012 10:05:36 +0000
parents 0d27d02b1208
children 4ee97d914501
comparison
equal deleted inserted replaced
3942:43aa1ac8614b 3943:a5b003595d6c
17 17
18 public class ImportSQRelation { 18 public class ImportSQRelation {
19 19
20 private static Logger log = Logger.getLogger(ImportSQRelation.class); 20 private static Logger log = Logger.getLogger(ImportSQRelation.class);
21 21
22
23 private ImportTimeInterval timeInterval; 22 private ImportTimeInterval timeInterval;
24 23
25 private String description; 24 private String description;
26 25
27 private List<ImportSQRelationValue> values; 26 private List<ImportSQRelationValue> values;
28 27
29 private SQRelation peer; 28 private SQRelation peer;
30 29
31
32 public ImportSQRelation() { 30 public ImportSQRelation() {
33 this.values = new ArrayList<ImportSQRelationValue>(); 31 this.values = new ArrayList<ImportSQRelationValue>();
34 } 32 }
35 33
36 34 public void storeDependencies(River river) throws SQLException,
37 public void storeDependencies(River river) 35 ConstraintViolationException {
38 throws SQLException, ConstraintViolationException
39 {
40 log.info("store dependencies"); 36 log.info("store dependencies");
41 37
42 SQRelation peer = getPeer(river); 38 SQRelation peer = getPeer(river);
43 39
44 timeInterval.getPeer(); 40 timeInterval.getPeer();
45 41
46 int count = 0; 42 if (peer != null) {
43 int count = 0;
47 44
48 for (ImportSQRelationValue value: values) { 45 for (ImportSQRelationValue value : values) {
49 try { 46 try {
50 value.storeDependencies(peer); 47 value.storeDependencies(peer);
51 count++; 48 count++;
49 }
50 catch (SQLException sqle) {
51 log.warn("ISQ: Unable to store sq relation value.", sqle);
52 }
53 catch (ConstraintViolationException cve) {
54 log.warn("ISQ: Unable to store sq relation value.", cve);
55 }
52 } 56 }
53 catch (SQLException sqle) { 57
54 log.warn("ISQ: Unable to store sq relation value.", sqle); 58 log.info("stored " + count + " sq relation values.");
55 }
56 catch (ConstraintViolationException cve) {
57 log.warn("ISQ: Unable to store sq relation value.", cve);
58 }
59 } 59 }
60
61 log.info("stored " + count + " sq relation values.");
62 } 60 }
63
64 61
65 public SQRelation getPeer(River river) { 62 public SQRelation getPeer(River river) {
66 log.debug("getPeer()"); 63 log.debug("getPeer()");
67 64
68 if (peer == null) { 65 if (peer == null) {
71 if (timeInter == null) { 68 if (timeInter == null) {
72 log.warn("ISQ: Cannot determine sq relation without time interval"); 69 log.warn("ISQ: Cannot determine sq relation without time interval");
73 return null; 70 return null;
74 } 71 }
75 72
76 Session session = ImporterSession.getInstance().getDatabaseSession(); 73 Session session = ImporterSession.getInstance()
74 .getDatabaseSession();
77 75
78 Query query = session.createQuery( 76 Query query = session
79 "FROM SQRelation WHERE river=:river AND timeInterval=:timeInter" 77 .createQuery("FROM SQRelation WHERE river=:river AND timeInterval=:timeInter");
80 );
81 78
82 query.setParameter("river", river); 79 query.setParameter("river", river);
83 query.setParameter("timeInter", timeInter); 80 query.setParameter("timeInter", timeInter);
84 81
85 List<SQRelation> sq = query.list(); 82 List<SQRelation> sq = query.list();
86 83
87 if (sq.isEmpty()) { 84 if (sq.isEmpty()) {
88 log.info("create new SQ relation '" + description + "'"); 85 log.info("create new SQ relation '" + description + "'");
89 86
90 peer = new SQRelation( 87 peer = new SQRelation(river, timeInter, description);
91 river,
92 timeInter,
93 description
94 );
95 session.save(peer); 88 session.save(peer);
96 } 89 }
97 else { 90 else {
98 peer = sq.get(0); 91 peer = sq.get(0);
99 } 92 }
100 } 93 }
101 94
102 return peer; 95 return peer;
103 } 96 }
104 97
105
106 public void setDescription(String description) { 98 public void setDescription(String description) {
107 this.description = description; 99 this.description = description;
108 } 100 }
109 101
110
111 public void setTimeInterval(ImportTimeInterval timeInterval) { 102 public void setTimeInterval(ImportTimeInterval timeInterval) {
112 this.timeInterval = timeInterval; 103 this.timeInterval = timeInterval;
113 } 104 }
114
115 105
116 public void addValue(ImportSQRelationValue value) { 106 public void addValue(ImportSQRelationValue value) {
117 if (value != null) { 107 if (value != null) {
118 this.values.add(value); 108 this.values.add(value);
119 } 109 }

http://dive4elements.wald.intevation.org