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
/**
19
 * @author naveen
20
 *
21
 */
22
public class EntityContainer implements Serializable {
23
 
24
	/**
25
	 * 
26
	 */
27
	private static final long serialVersionUID = 1L;
28
 
29
	private Map<Long, Entity> entities;
22 naveen 30
	private Map<Long, List<Entity>> entitiesbycategory;
10 shop2020 31
 
32
	/**
33
	 * 
34
	 */
35
	public EntityContainer() {
21 naveen 36
		// Lazy initialization
10 shop2020 37
	}
38
 
39
	/**
40
	 * 
41
	 * @return Map
21 naveen 42
	 * @throws Exception 
10 shop2020 43
	 */
21 naveen 44
	@SuppressWarnings("unchecked")
45
	public Map<Long, Entity> getEntities() throws Exception {
46
		// De-serialize
22 naveen 47
		if(this.entities == null) {
48
			String entitiesDBFile = CN.CONTENT_DB_PATH + "entities" + ".ser";
49
			String entitiesbycategoryDBFile = CN.CONTENT_DB_PATH + 
50
				"entitiesbycategory" + ".ser";
51
 
52
			this.entities = (Map<Long, Entity>) DBUtils.read(entitiesDBFile);
53
			this.entitiesbycategory = (Map<Long, List<Entity>>) 
54
				DBUtils.read(entitiesbycategoryDBFile);
55
		}
21 naveen 56
 
57
		if(this.entities == null) {
22 naveen 58
			// A sorted map to ensure sequence
59
			this.entities = new TreeMap<Long, Entity>();
60
			this.entitiesbycategory = new TreeMap<Long, List<Entity>>();
21 naveen 61
		}
62
 
10 shop2020 63
		return this.entities;
64
	}
65
 
41 naveen 66
	/** 
22 naveen 67
	 * @return the entitiesbycategory
68
	 * @throws Exception 
69
	 */
34 naveen 70
	public Map<Long, List<Entity>> getEntitiesbyCategory() throws Exception {
22 naveen 71
		if(this.entities == null) {
72
			this.getEntities();
73
		}
74
 
75
		return this.entitiesbycategory;
76
	}
77
 
78
	/**
10 shop2020 79
	 * 
80
	 * @param entityID
81
	 * @return Entity
21 naveen 82
	 * @throws Exception 
10 shop2020 83
	 */
21 naveen 84
	public Entity getEntity(long entityID) throws Exception {
85
		if(this.entities == null) {
86
			this.getEntities();
87
		}
88
 
10 shop2020 89
		return this.entities.get(new Long(entityID));
90
	}
91
 
92
	/**
93
	 * 
22 naveen 94
	 * @param categoryID
95
	 * @return List<Entity>
21 naveen 96
	 * @throws Exception 
10 shop2020 97
	 */
22 naveen 98
	public List<Entity> getEntities(long categoryID) throws Exception {
21 naveen 99
		if(this.entities == null) {
100
			this.getEntities();
101
		}
34 naveen 102
		Utils.logger.info("this.entitiesbycategory=" + this.entitiesbycategory);
21 naveen 103
 
22 naveen 104
		return this.entitiesbycategory.get(new Long(categoryID));
10 shop2020 105
	}
106
 
107
	/**
108
	 * 
22 naveen 109
	 * @param newEntity
21 naveen 110
	 * @throws Exception 
10 shop2020 111
	 */
22 naveen 112
	public void addEntity(Entity newEntity) throws Exception {
21 naveen 113
		if(this.entities == null) {
114
			this.getEntities();
115
		}
116
 
22 naveen 117
		this.entities.put(new Long(newEntity.getID()), newEntity);
118
 
119
		// Keep index by category ID
120
		List<Entity> catentities = 
121
			this.entitiesbycategory.get(newEntity.getCategoryID());
122
 
123
		if(catentities == null) {
124
			catentities = new ArrayList<Entity>();
125
		}
126
 
127
		catentities.add(newEntity);
34 naveen 128
		this.entitiesbycategory.put(new Long(newEntity.getCategoryID()), 
129
				catentities);
10 shop2020 130
	}
24 naveen 131
 
132
	/**
133
	 * 
134
	 * @param entityID
135
	 * @return ExpandedEntity 
136
	 * @throws Exception 
137
	 */
138
	public ExpandedEntity getExpandedEntity(long entityID) throws Exception {
139
		Entity entity = this.getEntity(entityID);
140
 
141
		ExpandedEntity expEntity = new ExpandedEntity(entity);
142
 
143
		return expEntity;
144
	}
10 shop2020 145
}