Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 shop2020 1
/**
2
 * 
3
 */
4
package in.shop2020.metamodel.definitions;
5
 
6
import in.shop2020.metamodel.core.Entity;
21 naveen 7
import in.shop2020.metamodel.util.CN;
8
import in.shop2020.metamodel.util.DBUtils;
24 naveen 9
import in.shop2020.metamodel.util.ExpandedEntity;
34 naveen 10
import in.shop2020.util.Utils;
24 naveen 11
 
10 shop2020 12
import java.io.Serializable;
22 naveen 13
import java.util.ArrayList;
14
import java.util.List;
10 shop2020 15
import java.util.Map;
22 naveen 16
import java.util.TreeMap;
10 shop2020 17
 
18
/**
49 naveen 19
 * Single point access to stored shop2020 core objects
20
 * 
10 shop2020 21
 * @author naveen
22
 *
23
 */
24
public class EntityContainer implements Serializable {
25
 
26
	/**
27
	 * 
28
	 */
29
	private static final long serialVersionUID = 1L;
30
 
49 naveen 31
	/**
32
	 * Hashtable of Entity ID to Entity object
33
	 */
10 shop2020 34
	private Map<Long, Entity> entities;
49 naveen 35
 
36
	/**
37
	 * Hashtable of Category ID to list of Entity objects
38
	 */
22 naveen 39
	private Map<Long, List<Entity>> entitiesbycategory;
10 shop2020 40
 
41
	/**
49 naveen 42
	 * Instantiates required data structures only when needed
10 shop2020 43
	 */
44
	public EntityContainer() {
21 naveen 45
		// Lazy initialization
10 shop2020 46
	}
47
 
48
	/**
49 naveen 49
	 * Returns all stored Entity objects 
10 shop2020 50
	 * 
51
	 * @return Map
21 naveen 52
	 * @throws Exception 
10 shop2020 53
	 */
21 naveen 54
	@SuppressWarnings("unchecked")
55
	public Map<Long, Entity> getEntities() throws Exception {
56
		// De-serialize
22 naveen 57
		if(this.entities == null) {
58
			String entitiesDBFile = CN.CONTENT_DB_PATH + "entities" + ".ser";
59
			String entitiesbycategoryDBFile = CN.CONTENT_DB_PATH + 
60
				"entitiesbycategory" + ".ser";
61
 
62
			this.entities = (Map<Long, Entity>) DBUtils.read(entitiesDBFile);
63
			this.entitiesbycategory = (Map<Long, List<Entity>>) 
64
				DBUtils.read(entitiesbycategoryDBFile);
65
		}
21 naveen 66
 
67
		if(this.entities == null) {
22 naveen 68
			// A sorted map to ensure sequence
69
			this.entities = new TreeMap<Long, Entity>();
70
			this.entitiesbycategory = new TreeMap<Long, List<Entity>>();
21 naveen 71
		}
72
 
10 shop2020 73
		return this.entities;
74
	}
75
 
41 naveen 76
	/** 
49 naveen 77
	 * Returns hashtable of list entities to category ID
78
	 * 
79
	 * @return the entitiesbycategory Entities by category
22 naveen 80
	 * @throws Exception 
81
	 */
34 naveen 82
	public Map<Long, List<Entity>> getEntitiesbyCategory() throws Exception {
22 naveen 83
		if(this.entities == null) {
84
			this.getEntities();
85
		}
86
 
87
		return this.entitiesbycategory;
88
	}
89
 
90
	/**
49 naveen 91
	 * Resolves Entity ID into Entity object
10 shop2020 92
	 * 
93
	 * @param entityID
94
	 * @return Entity
21 naveen 95
	 * @throws Exception 
10 shop2020 96
	 */
21 naveen 97
	public Entity getEntity(long entityID) throws Exception {
98
		if(this.entities == null) {
99
			this.getEntities();
100
		}
101
 
10 shop2020 102
		return this.entities.get(new Long(entityID));
103
	}
104
 
105
	/**
49 naveen 106
	 * Returns list of entities for a category ID
10 shop2020 107
	 * 
22 naveen 108
	 * @param categoryID
109
	 * @return List<Entity>
21 naveen 110
	 * @throws Exception 
10 shop2020 111
	 */
22 naveen 112
	public List<Entity> getEntities(long categoryID) throws Exception {
21 naveen 113
		if(this.entities == null) {
114
			this.getEntities();
115
		}
34 naveen 116
		Utils.logger.info("this.entitiesbycategory=" + this.entitiesbycategory);
21 naveen 117
 
22 naveen 118
		return this.entitiesbycategory.get(new Long(categoryID));
10 shop2020 119
	}
120
 
121
	/**
49 naveen 122
	 * Convienience method to add new Entity 
10 shop2020 123
	 * 
22 naveen 124
	 * @param newEntity
21 naveen 125
	 * @throws Exception 
10 shop2020 126
	 */
22 naveen 127
	public void addEntity(Entity newEntity) throws Exception {
21 naveen 128
		if(this.entities == null) {
129
			this.getEntities();
130
		}
131
 
22 naveen 132
		this.entities.put(new Long(newEntity.getID()), newEntity);
133
 
134
		// Keep index by category ID
135
		List<Entity> catentities = 
136
			this.entitiesbycategory.get(newEntity.getCategoryID());
137
 
138
		if(catentities == null) {
139
			catentities = new ArrayList<Entity>();
140
		}
141
 
142
		catentities.add(newEntity);
34 naveen 143
		this.entitiesbycategory.put(new Long(newEntity.getCategoryID()), 
144
				catentities);
10 shop2020 145
	}
24 naveen 146
 
147
	/**
49 naveen 148
	 * Returns expand form of entity object. All references are resolved into 
149
	 * corresponding detail object
24 naveen 150
	 * 
151
	 * @param entityID
152
	 * @return ExpandedEntity 
153
	 * @throws Exception 
154
	 */
155
	public ExpandedEntity getExpandedEntity(long entityID) throws Exception {
156
		Entity entity = this.getEntity(entityID);
157
 
158
		ExpandedEntity expEntity = new ExpandedEntity(entity);
159
 
160
		return expEntity;
161
	}
10 shop2020 162
}