Subversion Repositories SmartDukaan

Rev

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

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