1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.model.mapper;
19
20 import java.io.File;
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.SQLException;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.SortedMap;
29
30 import org.apache.log4j.Logger;
31 import org.eclipse.core.resources.IProject;
32 import org.eclipse.core.runtime.CoreException;
33
34 import pl.edu.agh.cast.model.base.BasePlugin;
35 import pl.edu.agh.cast.model.mapper.internal.DBLoader;
36 import pl.edu.agh.cast.model.mapper.internal.DBSaver;
37 import pl.edu.agh.cast.model.mapper.internal.XMLLoader;
38 import pl.edu.agh.cast.model.mapper.internal.XMLRemover;
39 import pl.edu.agh.cast.model.mapper.internal.XMLSaver;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public final class Mapper {
60
61 private static Logger log = BasePlugin.getLogger();
62
63 private final String[] mysqlProps = new String[] { "com.mysql.jdbc.Driver",
64 "jdbc:mysql://localhost/cast_test", //$NON-NLS-1$
65 "root",
66 "" };
67
68
69 private static Mapper instance = new Mapper();
70
71 private Connection conn;
72
73 private Map<Node, Mappable> objectCache = Collections.synchronizedMap(new HashMap<Node, Mappable>());
74
75 private boolean dbPresent = false;
76
77 private boolean useDb = false;
78
79
80
81
82 public static final String DEFAULT_ROOT = "target" + File.separator
83 + "temp";
84
85
86
87
88 public static final String DEFAULT_FOLDER = "model";
89
90
91
92
93 public static final String DEFAULT_PATH = DEFAULT_ROOT + File.separator + DEFAULT_FOLDER + File.separator;
94
95 private Mapper() {
96 if (useDb) {
97 try {
98 String[] props = mysqlProps;
99 Class.forName(props[0]);
100 String url = props[1];
101 conn = DriverManager.getConnection(url, props[2], props[3]);
102
103
104 dbPresent = true;
105 } catch (ClassNotFoundException e) {
106 log.info("Data Base is not present. Using XML persister.");
107 } catch (SQLException e) {
108 log.error(e);
109 log.info("Check the DB configuration");
110 log.info("See plugins/base/db/mysql_init.sql for details");
111 }
112 }
113 }
114
115
116
117
118
119
120 public static boolean isDBPresent() {
121 return getInstance().useDb && getInstance().dbPresent;
122 }
123
124
125
126
127
128
129 Connection getConnection() {
130 return conn;
131 }
132
133
134
135
136
137
138 public static Mapper getInstance() {
139 return instance;
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153 public boolean save(Mappable object, IProject project) {
154 return save(object, false, project);
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public synchronized boolean save(Mappable object, boolean force, IProject project) {
171 Saver saver;
172 if (dbPresent) {
173 saver = new DBSaver(object, force);
174 } else {
175 saver = new XMLSaver(object, force, project);
176 }
177 boolean result = saver.save();
178 if (project != null && result) {
179 try {
180 project.touch(null);
181 } catch (CoreException e) {
182 e.printStackTrace();
183 }
184 }
185 return result;
186 }
187
188
189
190
191 @Override
192 public void finalize() {
193 try {
194 if (conn != null) {
195 conn.close();
196 }
197 super.finalize();
198 } catch (SQLException e) {
199 System.err.println("Exception occured while closing connection to DB. "
200 + e);
201 e.printStackTrace();
202 } catch (Throwable e) {
203 e.printStackTrace();
204 }
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 @SuppressWarnings("unchecked")
237 public List find(Class klass, Map<String, Class> typeMap, SortedMap<String, Object> conditions, IProject project) {
238 Loader loader;
239 if (dbPresent) {
240 loader = new DBLoader(objectCache, klass, typeMap, conditions);
241 } else {
242 loader = new XMLLoader(objectCache, klass, typeMap, conditions, project);
243 }
244 return loader.find();
245 }
246
247
248
249
250
251 public void clearCache() {
252 objectCache.clear();
253 }
254
255
256
257
258
259
260
261
262 public void clearCache(boolean subLevel) {
263 clearCache();
264 if (subLevel && dbPresent) {
265 Node.clearCache();
266 Link.clearCache();
267 }
268 }
269
270
271
272
273 public void stats() {
274
275
276 Node.stats();
277 Link.stats();
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291 public synchronized boolean remove(Mappable object, IProject project) throws Exception {
292 if (dbPresent) {
293
294 throw new Exception("Not implemented object remover for DB persistence");
295 } else {
296 Remover remover = new XMLRemover(object, project);
297 return remover.remove();
298 }
299
300 }
301 }