1 /*
2 * @(#)$Id: StorageManager.java 777 2010-03-11 19:45:18Z bpuype $
3 *
4 * Author : Ueli Kurmann, igesture@uelikurmann.ch
5 *
6 * Purpose : The front-end of the storage system. The storage
7 * manager uses a storage engine which provides
8 * functionality to access different data sources.
9 *
10 * -----------------------------------------------------------------------
11 *
12 * Revision Information:
13 *
14 * Date Who Reason
15 *
16 * Dec 26, 2006 ukurmann Initial Release
17 * Mar 22, 2007 bsigner Cleanup
18 *
19 * -----------------------------------------------------------------------
20 *
21 * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
22 *
23 * This software is the proprietary information of ETH Zurich.
24 * Use is subject to license terms.
25 *
26 */
27
28 package org.ximtec.igesture.storage;
29
30 import java.io.File;
31 import java.util.List;
32 import java.util.logging.Logger;
33
34 import org.sigtec.util.Constant;
35 import org.ximtec.igesture.core.DataObject;
36
37 /**
38 * The front-end of the storage system. The storage manager uses a storage
39 * engine which provides functionality to access different data sources.
40 *
41 * @version 1.0, Dec 2006
42 * @author Ueli Kurmann, igesture@uelikurmann.ch
43 * @author Beat Signer, signer@inf.ethz.ch
44 */
45 public class StorageManager implements IStorageManager {
46
47 private static final Logger LOGGER = Logger.getLogger(StorageManager.class.getName());
48
49 private static final String LOADING_DB = "Loading database from '";
50
51 private StorageEngine storageEngine;
52
53
54
55 public enum StorageEngineType {
56 igd, igdc, igx, igz
57 }
58
59 /**
60 * Instantiates the storage manager with the given storage engine.
61 *
62 * @param engine
63 * the storage engine to be used.
64 */
65 public StorageManager(StorageEngine engine) {
66 this.storageEngine = engine;
67 }
68
69 /**
70 * Loads the data object of the given type with the given id.
71 *
72 * @param clazz
73 * the type of the data object to be retrieved.
74 * @param id
75 * the id of the data object.
76 * @return the data object with the given id.
77 */
78 @Override
79 public <T extends DataObject> T load(Class<T> clazz, String id) {
80 return storageEngine.load(clazz, id);
81 } // load
82
83 /**
84 * Returns a typed list of data objects. All objects available in the
85 * collection with the given type are returned.
86 *
87 * @param clazz
88 * the type of the data objects.
89 * @return a list of data objects of the given type.
90 */
91 @Override
92 public <T extends DataObject> List<T> load(Class<T> clazz) {
93 return storageEngine.load(clazz);
94 } // load
95
96 /**
97 * Removes the given data object from the storage.
98 */
99 @Override
100 public void remove(DataObject dataObject) {
101 storageEngine.remove(dataObject);
102 } // remove
103
104 /**
105 * Stores a data object.
106 *
107 * @param object
108 * the data object to be stored.
109 */
110 @Override
111 public void store(DataObject dataObject) {
112 storageEngine.store(dataObject);
113 } // store
114
115 /**
116 * Stores a list of data objects.
117 *
118 * @param dataObjects
119 * the data objects to be stored.
120 */
121 @Override
122 public void store(List<DataObject> dataObjects) {
123 for (final DataObject dataObject : dataObjects) {
124 store(dataObject);
125 }
126
127 } // store
128
129 /**
130 * Updates a data object.
131 *
132 * @param dataObject
133 * the data object to be updated.
134 */
135 @Override
136 public void update(DataObject dataObject) {
137 storageEngine.update(dataObject);
138 } // update
139
140 /**
141 * Updates a list of data objects.
142 *
143 * @param dataObjects
144 * the list of data objects to be updated.
145 */
146 @Override
147 public void update(List<DataObject> dataObjects) {
148 for (final DataObject obj : dataObjects) {
149 update(obj);
150 }
151
152 } // update
153
154 /**
155 * Generates a UUID. This id is used to identify data objects.
156 *
157 * @return an new UUID
158 */
159 public static String generateUUID() {
160 return org.safehaus.uuid.UUIDGenerator.getInstance().generateRandomBasedUUID().toString();
161 } // generateUUID
162
163 /**
164 * Creates a new storage engine. The type of the storage engine is determined
165 * based on the file extension. Dynamic class loading should be used to avoid
166 * any license conflicts.
167 *
168 * @param file
169 * the file from which the storage engine is created.
170 * @return the new storage engine.
171 */
172 public static StorageEngine createStorageEngine(File file) {
173 LOGGER.info(LOADING_DB + file + Constant.SINGLE_QUOTE);
174 StorageEngine engine = null;
175
176 switch (getEngineType(file)) {
177 case igd:
178 case igdc:
179 engine = new Db4oStorageEngine(file.getPath());
180 break;
181 case igx:
182 engine = new XMLStorageEngine(file.getPath());
183 break;
184 case igz:
185 engine = new ZipStorageEngine(file.getPath());
186 break;
187 default:
188 }
189
190 return engine;
191 } // createStorageEngine
192
193 public static StorageEngineType getEngineType(File file) {
194 String extension = file.getName().substring(file.getName().lastIndexOf(Constant.DOT) + 1);
195 return StorageEngineType.valueOf(extension);
196 } // getFileType
197
198 /**
199 * Disposes the storage engine.
200 */
201 @Override
202 public void dispose() {
203 if (storageEngine != null) {
204 storageEngine.dispose();
205 }
206 } // dispose
207
208 @Override
209 public <T extends DataObject> List<T> load(Class<T> clazz, String fieldName, Object value) {
210 return storageEngine.load(clazz, fieldName, value);
211 } // load
212
213 @Override
214 public void commit() {
215 if (storageEngine != null) {
216 storageEngine.commit();
217 }
218 } // commit
219
220 /*
221 * (non-Javadoc)
222 * @see org.ximtec.igesture.storage.StorageEngine#copyTo(java.io.File)
223 */
224 @Override
225 public void copyTo(File file) {
226 storageEngine.copyTo(file);
227
228 }
229
230 }