annotate flys-backend/src/main/java/de/intevation/flys/importer/ImporterSession.java @ 499:cce054f27dac

Importer: Only accept main value types 'Q', 'W', 'D' and 'T' by default. flys-backend/trunk@1853 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 08 May 2011 21:34:43 +0000
parents 8ab04de0b879
children d50cd3a632e0
rev   line source
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.importer;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import java.util.Iterator;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 import java.util.Map;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import java.util.HashMap;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 import java.math.BigDecimal;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import org.hibernate.SessionFactory;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 import org.hibernate.Session;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 import org.hibernate.Query;
499
cce054f27dac Importer: Only accept main value types 'Q', 'W', 'D' and 'T' by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 498
diff changeset
12 import org.hibernate.FlushMode;
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 import de.intevation.flys.backend.SessionFactoryProvider;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import de.intevation.flys.model.WstColumnValue;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import de.intevation.flys.model.WstColumn;
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
18 import de.intevation.flys.model.DischargeTableValue;
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
19 import de.intevation.flys.model.DischargeTable;
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 public class ImporterSession
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 private static final ThreadLocal<ImporterSession> SESSION =
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 new ThreadLocal<ImporterSession>() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 @Override
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 protected ImporterSession initialValue() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 return new ImporterSession();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 };
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 protected Session databaseSession;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
33 protected Map<IdValueKey, WstColumnValue> wstColumnValues;
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
34
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
35 protected Map<IdValueKey, DischargeTableValue> dischargeTableValues;
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 public static ImporterSession getInstance() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 return SESSION.get();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 public ImporterSession() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 SessionFactory sessionFactory =
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 SessionFactoryProvider.createSessionFactory();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 databaseSession = sessionFactory.openSession();
499
cce054f27dac Importer: Only accept main value types 'Q', 'W', 'D' and 'T' by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 498
diff changeset
45 databaseSession.setFlushMode(FlushMode.MANUAL);
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 public Session getDatabaseSession() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 return databaseSession;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 public WstColumnValue getWstColumnValue(
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 WstColumn column,
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 BigDecimal position,
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 BigDecimal w
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 ) {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 if (wstColumnValues == null) {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 loadWstColumnValues();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
61 IdValueKey key = new IdValueKey(
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 column.getId(),
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 position.doubleValue(),
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 w.doubleValue());
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 WstColumnValue wcv = wstColumnValues.get(key);
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 if (wcv != null) {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 return wcv;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 wcv = new WstColumnValue(column, position, w);
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 databaseSession.save(wcv);
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 wstColumnValues.put(key, wcv);
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 return wcv;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 protected void loadWstColumnValues() {
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
82 wstColumnValues = new HashMap<IdValueKey, WstColumnValue>();
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 Query query = databaseSession.createQuery("from WstColumnValue");
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 for (Iterator iter = query.iterate(); iter.hasNext();) {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 WstColumnValue wcv = (WstColumnValue)iter.next();
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
88 wstColumnValues.put(new IdValueKey(wcv), wcv);
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
89 }
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
90 }
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
91
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
92 public DischargeTableValue getDischargeTableValue(
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
93 DischargeTable table,
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
94 BigDecimal q,
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
95 BigDecimal w
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
96 ) {
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
97 if (dischargeTableValues == null) {
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
98 loadDischargeTableValues();
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
99 }
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
100
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
101 IdValueKey key = new IdValueKey(
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
102 table.getId(),
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
103 q.doubleValue(),
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
104 w.doubleValue());
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
105
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
106 DischargeTableValue dtv = dischargeTableValues.get(key);
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
107
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
108 if (dtv != null) {
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
109 return dtv;
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
110 }
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
111
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
112 dtv = new DischargeTableValue(table, q, w);
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
113
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
114 databaseSession.save(dtv);
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
115
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
116 dischargeTableValues.put(key, dtv);
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
117
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
118 return dtv;
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
119 }
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
120
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
121 protected void loadDischargeTableValues() {
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
122 dischargeTableValues = new HashMap<IdValueKey, DischargeTableValue>();
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
123
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
124 Query query = databaseSession.createQuery("from DischargeTableValue");
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
125
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
126 for (Iterator iter = query.iterate(); iter.hasNext();) {
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
127 DischargeTableValue dtv = (DischargeTableValue)iter.next();
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
128 dischargeTableValues.put(new IdValueKey(dtv), dtv);
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org