Subversion Repositories SmartDukaan

Rev

Rev 1050 | Rev 1153 | 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;
6
import in.shop2020.util.Utils;
7
 
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
 
16
public class StorageManager {
17
 
18
    private final ContentDatabase db;
19
    private final ContentViews views;
20
    private final TransactionRunner txnRunner;
21
    private static StorageManager storageUtils;
22
    private static final String homeDir =  Utils.BERKELEY_DB_PATH;
23
 
24
    static{
25
		synchronized(StorageManager.class){
26
			storageUtils = new StorageManager(homeDir);
27
		}
28
	}
29
 
30
    /**
31
     * Run the sample program.
32
     */
33
    public static void main(String[] args) {
34
 
35
        System.out.println("\nRunning sample: " + StorageManager.class);
36
 
37
 
38
        try {
39
        	Collection<Entity> entities=  StorageManager.getStorageManager().getEntitisByCategory(10002);
40
        	for(Entity entity: entities){
41
        		System.out.println(entity.getID());
42
        	}
43
        	//StorageManager.getStorageManager().printEntitis(10002);
44
        } catch (Exception e) {
45
            // If an exception reaches this point, the last transaction did not
46
            // complete.  If the exception is RunRecoveryException, follow
47
            // the Berkeley DB recovery procedures before running again.
48
            e.printStackTrace();
49
        } finally {
50
        	StorageManager.getStorageManager().close();
51
        }
52
    }
53
 
54
    public static StorageManager getStorageManager(){
55
		return storageUtils;
56
    }
57
    /**
58
     * Open the database and views.
59
     */
60
    private StorageManager(String homeDir)
61
        throws DatabaseException {
62
 
63
    	db = new ContentDatabase(homeDir);
64
        views = new ContentViews(db);
65
        txnRunner = new TransactionRunner(db.getEnvironment());
66
    }
67
 
68
 
69
    /**
70
     * Close the database cleanly.
71
     */
72
    public void close() throws DatabaseException {
73
        db.close();
74
    }
75
 
76
 
77
    @SuppressWarnings("unchecked")
78
	public Collection<Entity> getEntitisByCategory(long categoryId){
79
    	return views.getEntityByCategoryMap().duplicates(categoryId);
80
 
81
    }
82
 
83
    @SuppressWarnings("unchecked")
84
	public Map<Long, EntityState> getEntitiesMetadata(){
85
    	return views.getEntityMetadataMap();
86
    }
87
 
88
    public EntityState getEntityMetadata(long entityId){
89
    	return (EntityState) views.getEntityMetadataMap().get(entityId);
90
    }
91
 
92
    @SuppressWarnings("unchecked")
93
	public void updateEntityMetadata(EntityState entityMetadata){
94
    	Map states = views.getEntityMetadataMap();
95
    	states.put(new Long(entityMetadata.getID()), entityMetadata);
96
    }
97
 
98
 
99
    public void createEntity(Entity entity, EntityState entityMetadata) throws Exception{
100
    	txnRunner.run(new StoreEntity(entity, entityMetadata));
101
    }
102
 
103
    public void deleteEntity(long entityId) throws Exception{
104
    	txnRunner.run(new DeleteEntity(entityId));
105
    }
106
 
107
    public Entity getEntity(long entityId) throws Exception{
108
    	return (Entity)views.getEntityMap().get(new Long(entityId));
109
    }
110
    public void updateEntity(Entity entity) throws Exception{
111
    	Map entities = views.getEntityMap();
112
		entities.put(new Long(entity.getID()), entity);
113
    }
114
 
115
    @SuppressWarnings("unchecked")
116
	public Map<Long, Entity> getEntities() throws Exception{
117
    	return views.getEntityMap();
118
    }
119
 
120
    public Object getDataObject(String dataObjectName) throws Exception{
121
    	return views.getDataObjectMap().get(dataObjectName);
122
    }
123
 
124
    public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{
125
    	Map dataObjects = views.getDataObjectMap();
126
    	dataObjects.put(dataObjectName, dataObject);
127
    }
128
 
129
    public void deleteDataObject(String dataObjectName) throws Exception{
130
    	views.getDataObjectMap().remove(dataObjectName);
131
    }
132
 
133
    private class DeleteEntity implements TransactionWorker{
134
    	long entityId;
135
    	public DeleteEntity(long entityId) {
136
			this.entityId = entityId;
137
		}
138
 
139
		@Override
140
		public void doWork() throws Exception {
141
			views.getEntityMap().remove(new Long(entityId));
142
        	views.getEntityMetadataMap().remove(entityId);
143
		}
144
    }
145
 
146
 
147
    private class StoreEntity implements TransactionWorker{
148
    	Entity entity;
149
    	EntityState entityMetadata; 
150
    	public StoreEntity(Entity entity, EntityState entityMetadata) {
151
			this.entity = entity;
152
			this.entityMetadata = entityMetadata;
153
			//System.out.println(entity);
154
		}
155
 
156
		@Override
157
		public void doWork() throws Exception {
158
	    	Map entities = views.getEntityMap();
159
	    	entities.put(new Long(entity.getID()), entity);
160
 
161
	    	Map states = views.getEntityMetadataMap();
162
        	states.put(new Long(entityMetadata.getID()), entityMetadata);
163
		}
164
    }
165
 
166
}