View Javadoc

1   package com.atlassian.core.ofbiz.test.mock;
2   
3   import com.atlassian.core.ofbiz.CoreFactory;
4   import org.ofbiz.core.entity.GenericDelegator;
5   import org.ofbiz.core.entity.GenericEntityException;
6   import org.ofbiz.core.entity.GenericPK;
7   import org.ofbiz.core.entity.GenericValue;
8   import org.ofbiz.core.entity.model.ModelEntity;
9   import org.ofbiz.core.entity.model.ModelField;
10  import org.ofbiz.core.util.UtilMisc;
11  import org.ofbiz.core.util.UtilValidate;
12  
13  import java.util.*;
14  
15  public class MockGenericValue extends GenericValue {
16      Map fields;
17      boolean created = false;
18      boolean stored = false;
19      boolean refreshed = false;
20      boolean removed = false;
21      Map related = new HashMap();
22  
23      GenericDelegator gd;
24  
25      public MockGenericValue(GenericValue value) {
26          this(value.getEntityName());
27          this.fields = value.getFields(value.getAllKeys());
28      }
29  
30      public MockGenericValue(String entityName) {
31          super(new ModelEntity(), null);
32          this.entityName = entityName;
33          this.fields = new HashMap();
34      }
35  
36      public MockGenericValue(String entityName, Map fields) {
37          this(entityName);
38  
39          if (fields != null)
40              this.fields = fields;
41      }
42  
43      public Object get(String name) {
44          return fields.get(name);
45      }
46  
47      public void set(String name, Object value) {
48          fields.put(name, value);
49      }
50  
51      public Collection getAllKeys() {
52          return fields.keySet();
53      }
54  
55      public Map getFields(Collection collection)
56      {
57          Map selectedFields = new HashMap();
58          for (Iterator iterator = collection.iterator(); iterator.hasNext();)
59          {
60              String key = (String) iterator.next();
61              selectedFields.put(key, fields.get(key));
62          }
63          return selectedFields;
64      }
65  
66      public Map getAllFields()
67      {
68          return fields;
69      }
70  
71      public List getRelated(String s) throws GenericEntityException {
72          final Object related = this.related.get(s);
73          return related != null ? (List) related : Collections.EMPTY_LIST;
74      }
75  
76      public List getRelated(String s, Map map, List order) throws GenericEntityException {
77          return CoreFactory.getGenericDelegator().getRelated(s, map, order, this);
78      }
79  
80      public void setRelated(String s, List relatedGVs) {
81          related.put(s, relatedGVs);
82      }
83  
84      public GenericValue create() throws GenericEntityException {
85          created = true;
86          return CoreFactory.getGenericDelegator().create(this);
87      }
88  
89      public boolean isCreated() {
90          return created;
91      }
92  
93      public boolean isStored() {
94          return stored;
95      }
96  
97      public boolean isRemoved() {
98          return removed;
99      }
100 
101     public boolean isRefreshed() {
102         return refreshed;
103     }
104 
105     public ModelEntity getModelEntity() {
106         return new MockModelEntity(this);
107     }
108 
109     public boolean matchesFields(Map keyValuePairs) {
110         if (fields == null) return true;
111         if (keyValuePairs == null || keyValuePairs.size() == 0) return true;
112         Iterator entries = keyValuePairs.entrySet().iterator();
113         while (entries.hasNext()) {
114             Map.Entry anEntry = (Map.Entry) entries.next();
115             if (!UtilValidate.areEqual(anEntry.getValue(), this.fields.get(anEntry.getKey()))) {
116                 return false;
117             }
118         }
119         return true;
120     }
121 
122     public GenericPK getPrimaryKey() {
123         return new GenericPK(getModelEntity(), UtilMisc.toMap("id", fields.get("id")));
124     }
125 
126 
127     public void setDelegator(GenericDelegator internalDelegator) {
128         this.gd = internalDelegator;
129     }
130 
131     public GenericDelegator getDelegator() {
132         return gd;
133     }
134 
135     public void store() throws GenericEntityException {
136         stored = true;
137         CoreFactory.getGenericDelegator().store(this);
138     }
139 
140     public void remove() throws GenericEntityException {
141         removed = true;
142         CoreFactory.getGenericDelegator().removeValue(this);
143     }
144 
145     public void removeRelated(String relationName) throws GenericEntityException {
146         related.remove(relationName);
147     }
148 
149     public void refresh() throws GenericEntityException {
150         refreshed = true;
151         CoreFactory.getGenericDelegator().refresh(this);
152     }
153 
154     public String toString() {
155         StringBuffer theString = new StringBuffer();
156         theString.append("[GenericEntity:");
157         theString.append(getEntityName());
158         theString.append(']');
159 
160         Iterator entries = fields.entrySet().iterator();
161         Map.Entry anEntry = null;
162         while (entries.hasNext()) {
163             anEntry = (Map.Entry) entries.next();
164             theString.append('[');
165             theString.append(anEntry.getKey());
166             theString.append(',');
167             theString.append(anEntry.getValue());
168             theString.append(']');
169         }
170         return theString.toString();
171     }
172 
173     public Object dangerousGetNoCheckButFast(ModelField modelField) {
174         if (modelField == null) throw new IllegalArgumentException("Cannot get field with a null modelField");
175         return fields.get(modelField.getName());
176     }
177 
178     public boolean equals(Object o) {
179         if (this == o) return true;
180         if (!(o instanceof MockGenericValue)) return false;
181         if (!super.equals(o)) return false;
182 
183         final MockGenericValue mockGenericValue = (MockGenericValue) o;
184 
185         if (fields != null ? !fields.equals(mockGenericValue.fields) : mockGenericValue.fields != null) return false;
186 
187         return true;
188     }
189 
190     public int hashCode() {
191         int result = super.hashCode();
192         result = 29 * result + (fields != null ? fields.hashCode() : 0);
193         result = 29 * result + (created ? 1 : 0);
194         return result;
195     }
196 
197     public void setString(String s, String s1) {
198         this.set(s, s1);
199     }
200 
201     public List getRelatedOrderBy(String relationName, List orderBy) throws GenericEntityException {
202         return CoreFactory.getGenericDelegator().getRelatedOrderBy(relationName, orderBy, this);
203     }
204 
205     public List getRelatedByAnd(String relationName, Map fields) throws GenericEntityException {
206         return CoreFactory.getGenericDelegator().getRelatedByAnd(relationName, fields, this);
207     }
208 
209     public class MockModelEntity extends ModelEntity {
210         GenericValue value;
211 
212         public MockModelEntity() {
213 
214         }
215 
216         public MockModelEntity(GenericValue value) {
217             this.value = value;
218             this.setEntityName(value.getEntityName());
219         }
220 
221         public List getAllFieldNames() {
222             List fieldnames = new ArrayList();
223 
224             for (Iterator iterator = value.getAllKeys().iterator(); iterator.hasNext();) {
225                 String key = (String) iterator.next();
226                 fieldnames.add(key);
227             }
228 
229             return fieldnames;
230         }
231 
232         public ModelField getField(String fieldName) {
233             ModelField field = null;
234 
235             if (value.getAllKeys().contains(fieldName)) {
236                 field = new ModelField();
237                 field.setName(fieldName);
238             }
239 
240             return field;
241         }
242     }
243 
244     public Set entrySet()
245     {
246         return fields.entrySet();
247     }
248 
249     public Set keySet()
250     {
251         return fields.keySet();
252     }
253 
254     public int size()
255     {
256         return fields.size();
257     }
258 
259     public boolean isEmpty()
260     {
261         return fields.isEmpty();
262     }
263 
264     public Collection values()
265     {
266         return fields.values();
267     }
268 
269     public Object clone()
270     {
271         return new MockGenericValue(entityName, new HashMap(fields));
272     }
273 }