Subversion Repositories SmartDukaan

Rev

Rev 2838 | Rev 3485 | 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
    /**
114
     * update helpdpc for an given entity
115
     * @param helpdoc
116
     */
117
    @SuppressWarnings("unchecked")
118
	public void updateHelpdoc(Helpdoc helpdoc){
119
    	Map states = views.getHelpdocMap();
120
    	states.put(new Long(helpdoc.getID()), helpdoc);
121
    }
122
 
123
 
124
    /**
1153 rajveer 125
     * get metadata for all entities
126
     * @return
127
     */
1050 rajveer 128
    @SuppressWarnings("unchecked")
129
	public Map<Long, EntityState> getEntitiesMetadata(){
130
    	return views.getEntityMetadataMap();
131
    }
132
 
1153 rajveer 133
    /**
134
     * Get metadata for an given entity.
135
     * @param entityId
136
     * @return
137
     */
1050 rajveer 138
    public EntityState getEntityMetadata(long entityId){
139
    	return (EntityState) views.getEntityMetadataMap().get(entityId);
140
    }
141
 
1153 rajveer 142
    /**
143
     * update metadata for an given entity
144
     * @param entityMetadata
145
     */
1050 rajveer 146
    @SuppressWarnings("unchecked")
147
	public void updateEntityMetadata(EntityState entityMetadata){
148
    	Map states = views.getEntityMetadataMap();
149
    	states.put(new Long(entityMetadata.getID()), entityMetadata);
150
    }
151
 
152
 
1153 rajveer 153
    /**
154
     * Create an entity in database. This is done in transaction.
155
     * @param entity
156
     * @param entityMetadata
157
     * @throws Exception
158
     */
1050 rajveer 159
    public void createEntity(Entity entity, EntityState entityMetadata) throws Exception{
160
    	txnRunner.run(new StoreEntity(entity, entityMetadata));
161
    }
162
 
1153 rajveer 163
    /**
164
     * Delete an entity from database. This is done in transaction.
165
     * @param entityId
166
     * @throws Exception
167
     */
1050 rajveer 168
    public void deleteEntity(long entityId) throws Exception{
169
    	txnRunner.run(new DeleteEntity(entityId));
170
    }
171
 
1153 rajveer 172
    /**
173
     * Get entity for given id.
174
     * @param entityId
175
     * @return
176
     * @throws Exception
177
     */
1050 rajveer 178
    public Entity getEntity(long entityId) throws Exception{
179
    	return (Entity)views.getEntityMap().get(new Long(entityId));
180
    }
1153 rajveer 181
 
182
    /**
183
     * Update an existing entity
184
     * @param entity
185
     * @throws Exception
186
     */
1050 rajveer 187
    public void updateEntity(Entity entity) throws Exception{
188
    	Map entities = views.getEntityMap();
189
		entities.put(new Long(entity.getID()), entity);
190
    }
191
 
1153 rajveer 192
    /**
193
     * Get all entities.
194
     * @return
195
     * @throws Exception
196
     */
1050 rajveer 197
    @SuppressWarnings("unchecked")
198
	public Map<Long, Entity> getEntities() throws Exception{
199
    	return views.getEntityMap();
200
    }
201
 
1153 rajveer 202
    /**
203
     * Get dataobject. Generic method which will return the object with the name.
204
     * @param dataObjectName
205
     * @return
206
     * @throws Exception
207
     */
1050 rajveer 208
    public Object getDataObject(String dataObjectName) throws Exception{
209
    	return views.getDataObjectMap().get(dataObjectName);
210
    }
211
 
1153 rajveer 212
    /**
213
     * Store data object
214
     * @param dataObjectName
215
     * @param dataObject
216
     * @throws Exception
217
     */
1050 rajveer 218
    public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{
219
    	Map dataObjects = views.getDataObjectMap();
220
    	dataObjects.put(dataObjectName, dataObject);
221
    }
222
 
1153 rajveer 223
    /**
224
     * Delete dataobject
225
     * @param dataObjectName
226
     * @throws Exception
227
     */
1050 rajveer 228
    public void deleteDataObject(String dataObjectName) throws Exception{
229
    	views.getDataObjectMap().remove(dataObjectName);
230
    }
231
 
1153 rajveer 232
    /**
233
     * Class extending TransactionWorker used for deleting entity.
234
     * @author rajveer
235
     *
236
     */
1050 rajveer 237
    private class DeleteEntity implements TransactionWorker{
238
    	long entityId;
239
    	public DeleteEntity(long entityId) {
240
			this.entityId = entityId;
241
		}
242
 
243
		@Override
244
		public void doWork() throws Exception {
245
			views.getEntityMap().remove(new Long(entityId));
246
        	views.getEntityMetadataMap().remove(entityId);
247
		}
248
    }
249
 
1153 rajveer 250
    /**
251
     * Class extending TransactionWorker used for creating entity.
252
     * @author rajveer
253
     *
254
     */
1050 rajveer 255
    private class StoreEntity implements TransactionWorker{
256
    	Entity entity;
257
    	EntityState entityMetadata; 
258
    	public StoreEntity(Entity entity, EntityState entityMetadata) {
259
			this.entity = entity;
260
			this.entityMetadata = entityMetadata;
261
			//System.out.println(entity);
262
		}
263
 
264
		@Override
265
		public void doWork() throws Exception {
266
	    	Map entities = views.getEntityMap();
267
	    	entities.put(new Long(entity.getID()), entity);
268
 
269
	    	Map states = views.getEntityMetadataMap();
270
        	states.put(new Long(entityMetadata.getID()), entityMetadata);
271
		}
272
    }
273
 
2768 mandeep.dh 274
    /**
2838 mandeep.dh 275
     * Returns all special pages from the database.
2768 mandeep.dh 276
     */
2838 mandeep.dh 277
    public Map<Long, SpecialPage> getSpecialPages() {
278
        return views.getSpecialPagesMap();
2768 mandeep.dh 279
    }
280
 
281
    /**
2838 mandeep.dh 282
     * Updates a given special page in the database.
2768 mandeep.dh 283
     */
2838 mandeep.dh 284
    public void updateSpecialPage(SpecialPage specialPage) {
285
        Map<Long, SpecialPage> states = views.getSpecialPagesMap();
286
        states.put(new Long(specialPage.getID()), specialPage);
2768 mandeep.dh 287
    }
288
 
289
    /**
2838 mandeep.dh 290
     * Returns a special page object given a id after looking it up from map
2768 mandeep.dh 291
     * of values loaded from database.
292
     */
2838 mandeep.dh 293
    public SpecialPage getSpecialPage(long specialPageId) {
294
        return views.getSpecialPagesMap().get(specialPageId);
2768 mandeep.dh 295
    }
296
 
3356 rajveer 297
    /**
298
     * Removes a special page from database, if exists in database.
299
     */
300
    public SpecialPage deleteSpecialPage(long specialPageId) {
301
        return views.getSpecialPagesMap().remove(specialPageId);
302
    }
303
 
1050 rajveer 304
}