Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/ImportFlowVelocityModel.java @ 8986:392bbcd8a88b
Database inserts accelerated by suppressing unnecessary database queries for new data series
author | mschaefer |
---|---|
date | Sun, 08 Apr 2018 18:07:06 +0200 |
parents | 4c3ccf2b0304 |
children |
comparison
equal
deleted
inserted
replaced
8985:27851cfda84a | 8986:392bbcd8a88b |
---|---|
10 | 10 |
11 import java.util.ArrayList; | 11 import java.util.ArrayList; |
12 import java.util.List; | 12 import java.util.List; |
13 | 13 |
14 import org.apache.log4j.Logger; | 14 import org.apache.log4j.Logger; |
15 | 15 import org.dive4elements.river.importer.common.StoreMode; |
16 import org.hibernate.Session; | |
17 import org.hibernate.Query; | |
18 | |
19 import org.dive4elements.river.model.DischargeZone; | 16 import org.dive4elements.river.model.DischargeZone; |
20 import org.dive4elements.river.model.FlowVelocityModel; | 17 import org.dive4elements.river.model.FlowVelocityModel; |
21 import org.dive4elements.river.model.River; | 18 import org.dive4elements.river.model.River; |
19 import org.hibernate.Query; | |
20 import org.hibernate.Session; | |
22 | 21 |
23 | 22 |
24 public class ImportFlowVelocityModel { | 23 public class ImportFlowVelocityModel { |
25 | 24 |
26 private static final Logger log = Logger | 25 private static final Logger log = Logger |
27 .getLogger(ImportFlowVelocityModel.class); | 26 .getLogger(ImportFlowVelocityModel.class); |
28 | 27 |
29 private String description; | 28 private String description; |
30 | 29 |
31 private ImportDischargeZone dischargeZone; | 30 private ImportDischargeZone dischargeZone; |
32 | 31 |
33 private List<ImportFlowVelocityModelValue> values; | 32 private final List<ImportFlowVelocityModelValue> values; |
33 | |
34 protected StoreMode storeMode; | |
34 | 35 |
35 private FlowVelocityModel peer; | 36 private FlowVelocityModel peer; |
36 | 37 |
37 public ImportFlowVelocityModel() { | 38 public ImportFlowVelocityModel() { |
38 values = new ArrayList<ImportFlowVelocityModelValue>(); | 39 this.values = new ArrayList<>(); |
40 this.storeMode = StoreMode.NONE; | |
39 } | 41 } |
40 | 42 |
41 public ImportFlowVelocityModel(String description) { | 43 public ImportFlowVelocityModel(final String description) { |
42 this(); | 44 this(); |
43 | |
44 this.description = description; | 45 this.description = description; |
45 } | 46 } |
46 | 47 |
47 public ImportFlowVelocityModel(ImportDischargeZone dischargeZone, | 48 public ImportFlowVelocityModel(final ImportDischargeZone dischargeZone, |
48 String description) { | 49 final String description) { |
49 this(); | 50 this(); |
50 | |
51 this.dischargeZone = dischargeZone; | 51 this.dischargeZone = dischargeZone; |
52 this.description = description; | 52 this.description = description; |
53 } | 53 } |
54 | 54 |
55 public void setDischargeZone(ImportDischargeZone dischargeZone) { | 55 public void setDischargeZone(final ImportDischargeZone dischargeZone) { |
56 this.dischargeZone = dischargeZone; | 56 this.dischargeZone = dischargeZone; |
57 } | 57 } |
58 | 58 |
59 public void setDescription(String description) { | 59 public void setDescription(final String description) { |
60 this.description = description; | 60 this.description = description; |
61 } | 61 } |
62 | 62 |
63 public void addValue(ImportFlowVelocityModelValue value) { | 63 public void addValue(final ImportFlowVelocityModelValue value) { |
64 this.values.add(value); | 64 this.values.add(value); |
65 } | 65 } |
66 | 66 |
67 public void storeDependencies(River river) { | 67 public void storeDependencies(final River river) { |
68 log.debug("store dependencies"); | 68 log.debug("store dependencies"); |
69 | 69 |
70 if (dischargeZone == null) { | 70 if (this.dischargeZone == null) { |
71 log.warn("skip flow velocity model: No discharge zone specified."); | 71 log.warn("skip flow velocity model: No discharge zone specified."); |
72 return; | 72 return; |
73 } | 73 } |
74 | 74 |
75 dischargeZone.storeDependencies(river); | 75 this.dischargeZone.storeDependencies(river); |
76 | 76 |
77 FlowVelocityModel peer = getPeer(river); | 77 final FlowVelocityModel peer = getPeer(river); |
78 | 78 |
79 if (peer != null) { | 79 if (peer != null) { |
80 int i = 0; | 80 int i = 0; |
81 | 81 |
82 for (ImportFlowVelocityModelValue value : values) { | 82 for (final ImportFlowVelocityModelValue value : this.values) { |
83 value.storeDependencies(peer); | 83 value.storeDependencies(peer, this.storeMode); |
84 i++; | 84 i++; |
85 } | 85 } |
86 | 86 |
87 log.info("stored " + i + " flow velocity model values."); | 87 log.info("stored " + i + " flow velocity model values."); |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 public FlowVelocityModel getPeer(River river) { | 91 public FlowVelocityModel getPeer(final River river) { |
92 if (peer == null) { | 92 if (this.peer == null) { |
93 Session session = ImporterSession.getInstance() | 93 final Session session = ImporterSession.getInstance() |
94 .getDatabaseSession(); | 94 .getDatabaseSession(); |
95 | 95 |
96 DischargeZone zone = dischargeZone.getPeer(river); | 96 final DischargeZone zone = this.dischargeZone.getPeer(river); |
97 | 97 |
98 Query query = session.createQuery("from FlowVelocityModel where " | 98 final Query query = session.createQuery("from FlowVelocityModel where " |
99 + " dischargeZone=:dischargeZone"); | 99 + " dischargeZone=:dischargeZone"); |
100 | 100 |
101 query.setParameter("dischargeZone", zone); | 101 query.setParameter("dischargeZone", zone); |
102 | 102 |
103 List<FlowVelocityModel> model = query.list(); | 103 final List<FlowVelocityModel> model = query.list(); |
104 | 104 |
105 if (model.isEmpty()) { | 105 if (model.isEmpty()) { |
106 peer = new FlowVelocityModel(zone, description); | 106 this.peer = new FlowVelocityModel(zone, this.description); |
107 session.save(peer); | 107 session.save(this.peer); |
108 this.storeMode = StoreMode.INSERT; | |
108 } | 109 } |
109 else { | 110 else { |
110 peer = model.get(0); | 111 this.peer = model.get(0); |
112 this.storeMode = StoreMode.UPDATE; | |
111 } | 113 } |
112 } | 114 } |
113 | 115 |
114 return peer; | 116 return this.peer; |
115 } | 117 } |
116 } | 118 } |
117 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 119 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |