/
DataManager.java
162 lines (125 loc) · 5.78 KB
/
DataManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//@formatter:off
/*
* Data Manager - provides finder methods in this example
* Code-Beispiel zum Buch Patterns Kompakt, Verlag Springer Vieweg
* Copyright 2014 Karl Eilebrecht
*
* Licensed under the Apache License, Version 2.0 (the "License"):
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//@formatter:on
package de.calamanari.pk.identitymap;
import static de.calamanari.pk.util.LambdaSupportLoggerProxy.defer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.calamanari.pk.util.LambdaSupportLoggerProxy;
/**
* Data Manager - provides finder methods in this example, placeholder for whatever persistence framework/strategies may be in use. <br>
* If DAOs come into play, you would have to decide where to place the calls to the identity map. On the one hand logically the lookup belongs ON TOP of the DAO
* layer, introducing an extra layer (yuck!).<br>
* On the other hand INSIDE DAO would be convenient to reduce lines of code.<br>
* But with different DAOs (for XML, Oracle, MySQL), which is the primary reason for implementing a DAO-layer, the identity map lookup code inside the DAO would
* have to be duplicated (yuck! again).<br>
* One solution to this dilemma could be an abstract DAO base class containing a generic identity map lookup procedure.
*
* @author <a href="mailto:Karl.Eilebrecht(a/t)calamanari.de">Karl Eilebrecht</a>
*/
public final class DataManager {
private static final Logger LOGGER = LambdaSupportLoggerProxy.wrap(LoggerFactory.getLogger(DataManager.class));
/**
* Utility class
*/
private DataManager() {
// no instances
}
/**
* Customer finder method
*
* @param customerId identifier
* @return customer entity or null if not found
*/
public static CustomerEntity findCustomerById(String customerId) {
LOGGER.debug("{}.findCustomerById('{}') called", DataManager.class.getSimpleName(), customerId);
CustomerEntity res = null;
Session session = Session.getCurrentSession();
LOGGER.debug("Getting Identity Map for Type: {}", CustomerEntity.class.getSimpleName());
IdentityMap<String, CustomerEntity> identityMap = session.getIdentityMap(CustomerEntity.class);
LOGGER.debug("Performing lookup on {}", identityMap);
res = identityMap.get(customerId);
if (res == null) {
LOGGER.debug("Instance not in {} yet!", identityMap);
res = findCustomerByIdInDatabase(customerId);
final var resL = res;
LOGGER.debug("Putting Instance @{} into {}", defer(() -> Integer.toHexString(resL.hashCode())), identityMap);
identityMap.add(res);
}
else {
final var resL = res;
LOGGER.debug("Instance found: @{}", defer(() -> Integer.toHexString(resL.hashCode())));
}
return res;
}
/**
* loads the entity from the persistence layer
*
* @param customerId identifier
* @return entity or null if not found
*/
private static CustomerEntity findCustomerByIdInDatabase(String customerId) {
LOGGER.debug("{}.findCustomerByIdInDatabase('{}') called", DataManager.class.getSimpleName(), customerId);
CustomerEntity entity = Database.CUSTOMERS.get(customerId);
// The database must always returns a fresh entity, otherwise the example was a little useless :-)
return entity.shallowCopy();
}
// Note:
// the code could be written more generic (the code below is almost the same as above),
// but for demonstration purposes (logging, tracing) it's easier to duplicate the lines
/**
* Finder method for addresses
*
* @param addressId identifier
* @return address entity or null if not found
*/
public static AddressEntity findAddressById(String addressId) {
LOGGER.debug("{}.findAddressById('{}') called", DataManager.class.getSimpleName(), addressId);
AddressEntity res = null;
Session session = Session.getCurrentSession();
LOGGER.debug("Getting Identity Map for Type: {}", AddressEntity.class.getSimpleName());
IdentityMap<String, AddressEntity> identityMap = session.getIdentityMap(AddressEntity.class);
LOGGER.debug("Performing lookup on {}", identityMap);
res = identityMap.get(addressId);
if (res == null) {
LOGGER.debug("Instance not in {} yet!", identityMap);
res = findAddressByIdInDatabase(addressId);
final var resL = res;
LOGGER.debug("Putting Instance @{} into {}", defer(() -> Integer.toHexString(resL.hashCode())), identityMap);
identityMap.add(res);
}
else {
final var resL = res;
LOGGER.debug("Instance found: @{}", defer(() -> Integer.toHexString(resL.hashCode())));
}
return res;
}
/**
* loads the entity from the persistence layer
*
* @param addressId identifier
* @return entity or null if not found
*/
private static AddressEntity findAddressByIdInDatabase(String addressId) {
LOGGER.debug("{}.findAddressByIdInDatabase('{}') called", DataManager.class.getSimpleName(), addressId);
AddressEntity entity = Database.ADDRESSES.get(addressId);
// The database must always return a fresh entity, otherwise the example was a little useless :-)
return entity.shallowCopy();
}
}