annotate flys-backend/src/main/java/de/intevation/flys/importer/ImporterSession.java @ 498:8ab04de0b879

Importer: Cache the discharge table values, too. flys-backend/trunk@1852 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 08 May 2011 17:55:49 +0000
parents 67fd63e4ef66
children cce054f27dac
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;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 import de.intevation.flys.backend.SessionFactoryProvider;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 import de.intevation.flys.model.WstColumnValue;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 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
17 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
18 import de.intevation.flys.model.DischargeTable;
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 public class ImporterSession
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 private static final ThreadLocal<ImporterSession> SESSION =
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 new ThreadLocal<ImporterSession>() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 @Override
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 protected ImporterSession initialValue() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 return new ImporterSession();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 }
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 protected Session databaseSession;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
32 protected Map<IdValueKey, WstColumnValue> wstColumnValues;
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
33
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
34 protected Map<IdValueKey, DischargeTableValue> dischargeTableValues;
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 public static ImporterSession getInstance() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 return SESSION.get();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 }
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 public ImporterSession() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 SessionFactory sessionFactory =
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 SessionFactoryProvider.createSessionFactory();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 databaseSession = sessionFactory.openSession();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 public Session getDatabaseSession() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 return databaseSession;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 public WstColumnValue getWstColumnValue(
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 WstColumn column,
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 BigDecimal position,
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 BigDecimal w
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 ) {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 if (wstColumnValues == null) {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 loadWstColumnValues();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
59 IdValueKey key = new IdValueKey(
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 column.getId(),
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 position.doubleValue(),
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 w.doubleValue());
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 WstColumnValue wcv = wstColumnValues.get(key);
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 if (wcv != null) {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 return wcv;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 wcv = new WstColumnValue(column, position, w);
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 databaseSession.save(wcv);
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 wstColumnValues.put(key, 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 return 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
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 protected void loadWstColumnValues() {
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
80 wstColumnValues = new HashMap<IdValueKey, WstColumnValue>();
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 Query query = databaseSession.createQuery("from WstColumnValue");
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 for (Iterator iter = query.iterate(); iter.hasNext();) {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 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
86 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
87 }
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
88 }
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 public DischargeTableValue getDischargeTableValue(
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
91 DischargeTable table,
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
92 BigDecimal q,
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
93 BigDecimal w
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
94 ) {
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
95 if (dischargeTableValues == null) {
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
96 loadDischargeTableValues();
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
97 }
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
98
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
99 IdValueKey key = new IdValueKey(
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
100 table.getId(),
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
101 q.doubleValue(),
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
102 w.doubleValue());
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
103
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
104 DischargeTableValue dtv = dischargeTableValues.get(key);
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 if (dtv != null) {
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
107 return dtv;
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
108 }
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
109
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
110 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
111
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
112 databaseSession.save(dtv);
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 dischargeTableValues.put(key, 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 return 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
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
119 protected void loadDischargeTableValues() {
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
120 dischargeTableValues = new HashMap<IdValueKey, DischargeTableValue>();
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
121
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
122 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
123
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
124 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
125 DischargeTableValue dtv = (DischargeTableValue)iter.next();
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
126 dischargeTableValues.put(new IdValueKey(dtv), dtv);
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 }
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 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org