Subversion Repositories SmartDukaan

Rev

Rev 3356 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1050 rajveer 1
package in.shop2020.storage.bdb;
2
 
2838 mandeep.dh 3
import in.shop2020.metamodel.core.SpecialPage;
1050 rajveer 4
import in.shop2020.metamodel.core.Entity;
5
import in.shop2020.metamodel.core.EntityState;
2275 rajveer 6
import in.shop2020.metamodel.core.Helpdoc;
1050 rajveer 7
import in.shop2020.util.Utils;
8
 
9
import java.util.Collection;
10
import java.util.Map;
11
 
12
import com.sleepycat.collections.TransactionRunner;
13
import com.sleepycat.collections.TransactionWorker;
14
import com.sleepycat.je.DatabaseException;
15
 
1153 rajveer 16
/**
17
 * Entry point for storing everything in berkley database. Singleton class which initialises the berkley database.
18
 * @author rajveer
19
 *
20
 */
1050 rajveer 21
public class StorageManager {
22
    private final ContentDatabase db;
23
    private final ContentViews views;
24
    private final TransactionRunner txnRunner;
25
    private static StorageManager storageUtils;
26
    private static final String homeDir =  Utils.BERKELEY_DB_PATH;
27
 
28
    static{
29
		synchronized(StorageManager.class){
30
			storageUtils = new StorageManager(homeDir);
31
		}
32
	}
33
 
34
    /**
35
     * Run the sample program.
36
     */
37
    public static void main(String[] args) {
38
        System.out.println("\nRunning sample: " + StorageManager.class);
39
 
40
        try {
41
        	Collection<Entity> entities=  StorageManager.getStorageManager().getEntitisByCategory(10002);
2768 mandeep.dh 42
        	for(Entity entity: entities) {
1050 rajveer 43
        		System.out.println(entity.getID());
44
        	}
45
        	//StorageManager.getStorageManager().printEntitis(10002);
46
        } catch (Exception e) {
47
            // If an exception reaches this point, the last transaction did not
48
            // complete.  If the exception is RunRecoveryException, follow
49
            // the Berkeley DB recovery procedures before running again.
50
            e.printStackTrace();
51
        } finally {
52
        	StorageManager.getStorageManager().close();
53
        }
54
    }
55
 
1153 rajveer 56
    /**
57
     * get storage manager
58
     * @return StorageManager
59
     */
1050 rajveer 60
    public static StorageManager getStorageManager(){
61
		return storageUtils;
62
    }
1153 rajveer 63
 
1050 rajveer 64
    /**
65
     * Open the database and views.
66
     */
67
    private StorageManager(String homeDir)
68
        throws DatabaseException {
69
 
70
    	db = new ContentDatabase(homeDir);
71
        views = new ContentViews(db);
72
        txnRunner = new TransactionRunner(db.getEnvironment());
73
    }
74
 
75
 
76
    /**
77
     * Close the database cleanly.
78
     */
79
    public void close() throws DatabaseException {
80
        db.close();
81
    }
82
 
1153 rajveer 83
    /**
84
     * Get the entities for a given category
85
     * @param categoryId
86
     * @return Collection<Entity>
87
     */
1050 rajveer 88
    @SuppressWarnings("unchecked")
89
	public Collection<Entity> getEntitisByCategory(long categoryId){
90
    	return views.getEntityByCategoryMap().duplicates(categoryId);
91
 
92
    }
93
 
2275 rajveer 94
 
1153 rajveer 95
    /**
2275 rajveer 96
     * get all helpdocs
97
     * @return
98
     */
99
    @SuppressWarnings("unchecked")
100
	public Map<Long, Helpdoc> getHelpdocs(){
101
    	return views.getHelpdocMap();
102
    }
103
 
104
    /**
105
     * Get helpdoc for an given id
106
     * @param helpdocId
107
     * @return
108
     */
109
    public Helpdoc getHelpdoc(long helpdocId){
110
    	return (Helpdoc) views.getHelpdocMap().get(helpdocId);
111
    }
112
 
113
    /**
3485 rajveer 114
     * Delete helpdoc for an given id
115
     * @param helpdocId
116
     * @return
117
     */
118
    public void deleteHelpdoc(long helpdocId){
119
    	views.getHelpdocMap().remove(helpdocId);
120
    }
121
 
122
    /**
2275 rajveer 123
     * update helpdpc for an given entity
124
     * @param helpdoc
125
     */
126
    @SuppressWarnings("unchecked")
127
	public void updateHelpdoc(Helpdoc helpdoc){
128
    	Map states = views.getHelpdocMap();
129
    	states.put(new Long(helpdoc.getID()), helpdoc);
130
    }
131
 
132
 
133
    /**
1153 rajveer 134
     * get metadata for all entities
135
     * @return
136
     */
1050 rajveer 137
    @SuppressWarnings("unchecked")
138
	public Map<Long, EntityState> getEntitiesMetadata(){
139
    	return views.getEntityMetadataMap();
140
    }
141
 
1153 rajveer 142
    /**
143
     * Get metadata for an given entity.
144
     * @param entityId
145
     * @return
146
     */
1050 rajveer 147
    public EntityState getEntityMetadata(long entityId){
148
    	return (EntityState) views.getEntityMetadataMap().get(entityId);
149
    }
150
 
1153 rajveer 151
    /**
152
     * update metadata for an given entity
153
     * @param entityMetadata
154
     */
1050 rajveer 155
    @SuppressWarnings("unchecked")
156
	public void updateEntityMetadata(EntityState entityMetadata){
157
    	Map states = views.getEntityMetadataMap();
158
    	states.put(new Long(entityMetadata.getID()), entityMetadata);
159
    }
160
 
161
 
1153 rajveer 162
    /**
163
     * Create an entity in database. This is done in transaction.
164
     * @param entity
165
     * @param entityMetadata
166
     * @throws Exception
167
     */
1050 rajveer 168
    public void createEntity(Entity entity, EntityState entityMetadata) throws Exception{
169
    	txnRunner.run(new StoreEntity(entity, entityMetadata));
170
    }
171
 
1153 rajveer 172
    /**
173
     * Delete an entity from database. This is done in transaction.
174
     * @param entityId
175
     * @throws Exception
176
     */
1050 rajveer 177
    public void deleteEntity(long entityId) throws Exception{
178
    	txnRunner.run(new DeleteEntity(entityId));
179
    }
180
 
1153 rajveer 181
    /**
182
     * Get entity for given id.
183
     * @param entityId
184
     * @return
185
     * @throws Exception
186
     */
1050 rajveer 187
    public Entity getEntity(long entityId) throws Exception{
188
    	return (Entity)views.getEntityMap().get(new Long(entityId));
189
    }
1153 rajveer 190
 
191
    /**
192
     * Update an existing entity
193
     * @param entity
194
     * @throws Exception
195
     */
1050 rajveer 196
    public void updateEntity(Entity entity) throws Exception{
197
    	Map entities = views.getEntityMap();
198
		entities.put(new Long(entity.getID()), entity);
199
    }
200
 
1153 rajveer 201
    /**
202
     * Get all entities.
203
     * @return
204
     * @throws Exception
205
     */
1050 rajveer 206
    @SuppressWarnings("unchecked")
207
	public Map<Long, Entity> getEntities() throws Exception{
208
    	return views.getEntityMap();
209
    }
210
 
1153 rajveer 211
    /**
212
     * Get dataobject. Generic method which will return the object with the name.
213
     * @param dataObjectName
214
     * @return
215
     * @throws Exception
216
     */
1050 rajveer 217
    public Object getDataObject(String dataObjectName) throws Exception{
218
    	return views.getDataObjectMap().get(dataObjectName);
219
    }
220
 
1153 rajveer 221
    /**
222
     * Store data object
223
     * @param dataObjectName
224
     * @param dataObject
225
     * @throws Exception
226
     */
1050 rajveer 227
    public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{
228
    	Map dataObjects = views.getDataObjectMap();
229
    	dataObjects.put(dataObjectName, dataObject);
230
    }
231
 
1153 rajveer 232
    /**
233
     * Delete dataobject
234
     * @param dataObjectName
235
     * @throws Exception
236
     */
1050 rajveer 237
    public void deleteDataObject(String dataObjectName) throws Exception{
238
    	views.getDataObjectMap().remove(dataObjectName);
239
    }
240
 
1153 rajveer 241
    /**
242
     * Class extending TransactionWorker used for deleting entity.
243
     * @author rajveer
244
     *
245
     */
1050 rajveer 246
    private class DeleteEntity implements TransactionWorker{
247
    	long entityId;
248
    	public DeleteEntity(long entityId) {
249
			this.entityId = entityId;
250
		}
251
 
252
		@Override
253
		public void doWork() throws Exception {
254
			views.getEntityMap().remove(new Long(entityId));
255
        	views.getEntityMetadataMap().remove(entityId);
256
		}
257
    }
258
 
1153 rajveer 259
    /**
260
     * Class extending TransactionWorker used for creating entity.
261
     * @author rajveer
262
     *
263
     */
1050 rajveer 264
    private class StoreEntity implements TransactionWorker{
265
    	Entity entity;
266
    	EntityState entityMetadata; 
267
    	public StoreEntity(Entity entity, EntityState entityMetadata) {
268
			this.entity = entity;
269
			this.entityMetadata = entityMetadata;
270
			//System.out.println(entity);
271
		}
272
 
273
		@Override
274
		public void doWork() throws Exception {
275
	    	Map entities = views.getEntityMap();
276
	    	entities.put(new Long(entity.getID()), entity);
277
 
278
	    	Map states = views.getEntityMetadataMap();
279
        	states.put(new Long(entityMetadata.getID()), entityMetadata);
280
		}
281
    }
282
 
2768 mandeep.dh 283
    /**
2838 mandeep.dh 284
     * Returns all special pages from the database.
2768 mandeep.dh 285
     */
2838 mandeep.dh 286
    public Map<Long, SpecialPage> getSpecialPages() {
287
        return views.getSpecialPagesMap();
2768 mandeep.dh 288
    }
289
 
290
    /**
2838 mandeep.dh 291
     * Updates a given special page in the database.
2768 mandeep.dh 292
     */
2838 mandeep.dh 293
    public void updateSpecialPage(SpecialPage specialPage) {
294
        Map<Long, SpecialPage> states = views.getSpecialPagesMap();
295
        states.put(new Long(specialPage.getID()), specialPage);
2768 mandeep.dh 296
    }
297
 
298
    /**
2838 mandeep.dh 299
     * Returns a special page object given a id after looking it up from map
2768 mandeep.dh 300
     * of values loaded from database.
301
     */
2838 mandeep.dh 302
    public SpecialPage getSpecialPage(long specialPageId) {
303
        return views.getSpecialPagesMap().get(specialPageId);
2768 mandeep.dh 304
    }
305
 
3356 rajveer 306
    /**
307
     * Removes a special page from database, if exists in database.
308
     */
309
    public SpecialPage deleteSpecialPage(long specialPageId) {
310
        return views.getSpecialPagesMap().remove(specialPageId);
311
    }
312
 
1050 rajveer 313
}