Subversion Repositories SmartDukaan

Rev

Rev 3485 | Details | Compare with Previous | Last modification | View Log | RSS feed

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