001: /**
002: *
003: */package org.rapla.storage.impl;
004:
005: import java.util.ArrayList;
006: import java.util.Collection;
007: import java.util.HashMap;
008: import java.util.HashSet;
009: import java.util.Iterator;
010: import java.util.List;
011:
012: import org.rapla.components.util.Assert;
013: import org.rapla.entities.EntityNotFoundException;
014: import org.rapla.entities.RaplaObject;
015: import org.rapla.entities.RaplaType;
016: import org.rapla.entities.dynamictype.DynamicType;
017: import org.rapla.entities.internal.CategoryImpl;
018: import org.rapla.entities.storage.EntityResolver;
019: import org.rapla.entities.storage.RefEntity;
020: import org.rapla.storage.LocalCache;
021:
022: public class EntityStore implements EntityResolver {
023: HashMap entities = new HashMap();
024: HashSet idsToRemove = new HashSet();
025: LocalCache parent;
026: HashMap dynamicTypes = new HashMap();
027: CategoryImpl super Category;
028: HashMap passwordList = new HashMap();
029: long repositoryVersion;
030:
031: public EntityStore(LocalCache parent, CategoryImpl super Category) {
032: this .parent = parent;
033: this .super Category = super Category;
034: // put( superCategory);
035: }
036:
037: public void addAll(Collection collection) {
038: Iterator it = collection.iterator();
039: while (it.hasNext()) {
040: put((RefEntity) it.next());
041: }
042: }
043:
044: public void put(RefEntity entity) {
045: Object id = entity.getId();
046: Assert.notNull(id);
047: if (entity.getRaplaType().equals(DynamicType.TYPE)) {
048: DynamicType dynamicType = (DynamicType) entity;
049: dynamicTypes.put(dynamicType.getElementKey(), entity);
050: }
051: entities.put(id, entity);
052: }
053:
054: public void addRemoveId(Object id) {
055: idsToRemove.add(id);
056: }
057:
058: public DynamicType getDynamicType(String key) {
059: // todo super
060: DynamicType type = (DynamicType) dynamicTypes.get(key);
061: if (type == null && parent != null) {
062: type = parent.getDynamicType(key);
063: }
064: return type;
065: }
066:
067: public Collection getList() {
068: return entities.values();
069: }
070:
071: public Collection getRemoveIds() {
072: return idsToRemove;
073: }
074:
075: // Implementation of EntityResolver
076: public RefEntity resolve(Object id) throws EntityNotFoundException {
077: RefEntity result = get(id);
078: if (result == null) {
079: throw new EntityNotFoundException("Object for id "
080: + id.toString() + " not found");
081: }
082: return result;
083: }
084:
085: public CategoryImpl getSuperCategory() {
086: return super Category;
087: }
088:
089: public void putPassword(Object userid, String password) {
090: passwordList.put(userid, password);
091: }
092:
093: public String getPassword(Object userid) {
094: return (String) passwordList.get(userid);
095: }
096:
097: public RefEntity get(Object id) {
098: if (id.equals(super Category.getId())) {
099: return super Category;
100: }
101: RefEntity entity = (RefEntity) entities.get(id);
102: if (entity != null)
103: return entity;
104:
105: if (parent != null) {
106: return parent.get(id);
107:
108: }
109: return null;
110: }
111:
112: public Collection getCollection(RaplaType raplaType) {
113: List collection = new ArrayList();
114: Iterator it = entities.values().iterator();
115: while (it.hasNext()) {
116: RaplaObject obj = (RaplaObject) it.next();
117: if (obj.getRaplaType().equals(raplaType)) {
118: collection.add(obj);
119: }
120: }
121: return collection;
122: }
123:
124: public long getRepositoryVersion() {
125: return repositoryVersion;
126: }
127:
128: public void setRepositoryVersion(long repositoryVersion) {
129: this.repositoryVersion = repositoryVersion;
130: }
131:
132: }
|