comparison flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentDensity.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 43aa1ac8614b
children 4ee97d914501
comparison
equal deleted inserted replaced
3942:43aa1ac8614b 3943:a5b003595d6c
1 package de.intevation.flys.importer; 1 package de.intevation.flys.importer;
2 2
3 import java.sql.SQLException; 3 import java.sql.SQLException;
4
5 import java.util.ArrayList; 4 import java.util.ArrayList;
6 import java.util.List; 5 import java.util.List;
7 6
8 import org.apache.log4j.Logger; 7 import org.apache.log4j.Logger;
9 8 import org.hibernate.Query;
10 import org.hibernate.Session; 9 import org.hibernate.Session;
11 import org.hibernate.Query;
12 import org.hibernate.exception.ConstraintViolationException; 10 import org.hibernate.exception.ConstraintViolationException;
13 11
14 import de.intevation.flys.model.River; 12 import de.intevation.flys.model.River;
15 import de.intevation.flys.model.SedimentDensity; 13 import de.intevation.flys.model.SedimentDensity;
16 14
17 15
18 public class ImportSedimentDensity { 16 public class ImportSedimentDensity {
19 17
20 private static Logger log = Logger.getLogger(ImportSedimentDensity.class); 18 private static Logger log = Logger.getLogger(ImportSedimentDensity.class);
21
22 19
23 protected SedimentDensity peer; 20 protected SedimentDensity peer;
24 21
25 protected ImportDepth depth; 22 protected ImportDepth depth;
26 23
28 25
29 protected String description; 26 protected String description;
30 27
31 protected List<ImportSedimentDensityValue> values; 28 protected List<ImportSedimentDensityValue> values;
32 29
33
34 public ImportSedimentDensity(String description) { 30 public ImportSedimentDensity(String description) {
35 this.description = description; 31 this.description = description;
36 this.values = new ArrayList<ImportSedimentDensityValue>(); 32 this.values = new ArrayList<ImportSedimentDensityValue>();
37 } 33 }
38
39 34
40 public String getDescription() { 35 public String getDescription() {
41 return description; 36 return description;
42 } 37 }
43 38
44
45 public void setDepth(ImportDepth depth) { 39 public void setDepth(ImportDepth depth) {
46 this.depth = depth; 40 this.depth = depth;
47 } 41 }
48
49 42
50 public void setUnit(ImportUnit unit) { 43 public void setUnit(ImportUnit unit) {
51 this.unit = unit; 44 this.unit = unit;
52 } 45 }
53 46
54
55 public void addValue(ImportSedimentDensityValue value) { 47 public void addValue(ImportSedimentDensityValue value) {
56 values.add(value); 48 values.add(value);
57 } 49 }
58 50
59 51 public void storeDependencies(River river) throws SQLException,
60 public void storeDependencies(River river) 52 ConstraintViolationException {
61 throws SQLException, ConstraintViolationException
62 {
63 log.info("store dependencies"); 53 log.info("store dependencies");
64 54
65 if (depth != null) { 55 if (depth != null) {
66 depth.storeDependencies(); 56 depth.storeDependencies();
67 } 57 }
68 58
69 log.info("store sediment density values.");
70
71 SedimentDensity peer = getPeer(river); 59 SedimentDensity peer = getPeer(river);
72 60
73 for (ImportSedimentDensityValue value: values) { 61 if (peer != null) {
74 value.storeDependencies(peer); 62 log.info("store sediment density values.");
63 for (ImportSedimentDensityValue value : values) {
64 value.storeDependencies(peer);
65 }
75 } 66 }
76 } 67 }
77
78 68
79 public SedimentDensity getPeer(River river) { 69 public SedimentDensity getPeer(River river) {
80 log.info("get peer"); 70 log.info("get peer");
81 71
82 if (depth == null) { 72 if (depth == null) {
83 log.warn("cannot store sediment density '" + description + "': no depth"); 73 log.warn("cannot store sediment density '" + description
74 + "': no depth");
84 return null; 75 return null;
85 } 76 }
86 77
87 if (unit == null) { 78 if (unit == null) {
88 log.warn("cannot store sediment density '" + description + "': no unit"); 79 log.warn("cannot store sediment density '" + description
80 + "': no unit");
89 return null; 81 return null;
90 } 82 }
91 83
92 if (peer == null) { 84 if (peer == null) {
93 Session session = ImporterSession.getInstance().getDatabaseSession(); 85 Session session = ImporterSession.getInstance()
86 .getDatabaseSession();
94 87
95 Query query = session.createQuery( 88 Query query = session.createQuery("from SedimentDensity where "
96 "from SedimentDensity where " + 89 + " river=:river and " + " depth=:depth and "
97 " river=:river and " + 90 + " unit=:unit");
98 " depth=:depth and " +
99 " unit=:unit");
100 91
101 query.setParameter("river", river); 92 query.setParameter("river", river);
102 query.setParameter("depth", depth.getPeer()); 93 query.setParameter("depth", depth.getPeer());
103 query.setParameter("unit", unit.getPeer()); 94 query.setParameter("unit", unit.getPeer());
104 95
105 List<SedimentDensity> density = query.list(); 96 List<SedimentDensity> density = query.list();
106 97
107 if (density.isEmpty()) { 98 if (density.isEmpty()) {
108 log.debug("Create new SedimentDensity DB instance."); 99 log.debug("Create new SedimentDensity DB instance.");
109 100
110 peer = new SedimentDensity( 101 peer = new SedimentDensity(river, depth.getPeer(),
111 river, 102 unit.getPeer(), description);
112 depth.getPeer(),
113 unit.getPeer(),
114 description);
115 103
116 session.save(peer); 104 session.save(peer);
117 } 105 }
118 else { 106 else {
119 peer = density.get(0); 107 peer = density.get(0);

http://dive4elements.wald.intevation.org