Source code: com/xpn/xwiki/objects/BaseCollection.java
1 /**
2 * ===================================================================
3 *
4 * Copyright (c) 2003 Ludovic Dubost, All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details, published at
15 * http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the
16 * root folder of this distribution.
17 *
18 * Created by
19 * User: Ludovic Dubost
20 * Date: 9 déc. 2003
21 * Time: 11:36:06
22 */
23 package com.xpn.xwiki.objects;
24
25
26 import com.xpn.xwiki.XWikiContext;
27 import com.xpn.xwiki.XWikiException;
28 import com.xpn.xwiki.doc.XWikiDocument;
29 import com.xpn.xwiki.objects.classes.BaseClass;
30 import com.xpn.xwiki.objects.classes.PropertyClass;
31 import org.apache.commons.collections.map.ListOrderedMap;
32 import org.dom4j.Document;
33 import org.dom4j.Element;
34 import org.dom4j.dom.DOMDocument;
35 import org.dom4j.io.OutputFormat;
36 import org.dom4j.io.XMLWriter;
37
38 import java.io.IOException;
39 import java.io.Serializable;
40 import java.io.StringWriter;
41 import java.util.*;
42
43 public abstract class BaseCollection extends BaseElement implements ObjectInterface, Serializable {
44 private String className;
45 private Map fields = ListOrderedMap.decorate(new HashMap());
46 private List fieldsToRemove = new ArrayList();
47 private int number;
48
49 public int getId() {
50 return hashCode();
51 }
52
53 public int hashCode() {
54 return (getName()+getClassName()).hashCode();
55 }
56
57 public void setId(int id) {
58 }
59
60 public int getNumber() {
61 return number;
62 }
63
64 public void setNumber(int number) {
65 this.number = number;
66 }
67
68 public void addPropertyForRemoval(PropertyInterface field) {
69 getFieldsToRemove().add(field);
70 }
71
72 public String getClassName() {
73 return (className == null) ? "" : className;
74 }
75
76 public void setClassName(String name) {
77 className = name;
78 }
79
80
81 public void checkField(String name) throws XWikiException {
82 /* // Let's stop checking.. This is a pain
83 if (getxWikiClass(context).safeget(name)==null) {
84 Object[] args = { name, getxWikiClass(context).getName() };
85 throw new XWikiException( XWikiException.MODULE_XWIKI_CLASSES, XWikiException.ERROR_XWIKI_CLASSES_FIELD_DOES_NOT_EXIST,
86 "Field {0} does not exist in class {1}", null, args);
87 } */
88 }
89
90 public PropertyInterface safeget(String name) {
91 return (PropertyInterface) getFields().get(name);
92 }
93
94 public PropertyInterface get(String name) throws XWikiException {
95 checkField(name);
96 return safeget(name);
97 }
98
99 public void safeput(String name, PropertyInterface property) {
100 addField(name, property);
101 if (property instanceof BaseProperty) {
102 ((BaseProperty)property).setObject(this);
103 ((BaseProperty)property).setName(name);
104 }
105 }
106
107 public void put(String name, PropertyInterface property) throws XWikiException {
108 checkField(name);
109 safeput(name, property);
110 }
111
112
113 public BaseClass getxWikiClass(XWikiContext context) {
114 String name = getClassName();
115 try {
116 if ((context==null)||(context.getWiki()==null))
117 return null;
118 XWikiDocument doc = context.getWiki().getDocument(name, context);
119 if (doc==null)
120 return null;
121 else
122 return doc.getxWikiClass();
123 } catch (Exception e) {
124 e.printStackTrace();
125 return null;
126 }
127 }
128
129 /*public void setxWikiClass(BaseClass xWikiClass) {
130 setClassName(xWikiClass.getName());
131 }*/
132
133 public String getStringValue(String name) {
134 BaseProperty prop = (BaseProperty) safeget(name);
135 if (prop==null)
136 return "";
137 else
138 return prop.getValue().toString();
139 }
140
141 public String getLargeStringValue(String name) {
142 return getStringValue(name);
143 }
144 public void setStringValue(String name, String value) {
145 StringProperty property = new StringProperty();
146 property.setName(name);
147 property.setValue(value);
148 safeput(name, property);
149 }
150
151 public void setLargeStringValue(String name, String value) {
152 LargeStringProperty property = new LargeStringProperty();
153 property.setName(name);
154 property.setValue(value);
155 safeput(name, property);
156 }
157
158
159 public int getIntValue(String name) {
160 try {
161 NumberProperty prop = (NumberProperty)safeget(name);
162 if (prop==null)
163 return 0;
164 else
165 return ((Number)prop.getValue()).intValue();
166 }
167 catch (Exception e) {
168 return 0;
169 }
170 }
171
172 public void setIntValue(String name, int value) {
173 NumberProperty property = new IntegerProperty();
174 property.setName(name);
175 property.setValue(new Integer(value));
176 safeput(name, property);
177 }
178
179 public long getLongValue(String name) {
180 try {
181 NumberProperty prop = (NumberProperty)safeget(name);
182 if (prop==null)
183 return 0;
184 else
185 return ((Number)prop.getValue()).longValue();
186 }
187 catch (Exception e) {
188 return 0;
189 }
190 }
191
192 public void setLongValue(String name, long value) {
193 NumberProperty property = new LongProperty();
194 property.setName(name);
195 property.setValue(new Long(value));
196 safeput(name, property);
197 }
198
199 public Date getDateValue(String name) {
200 try {
201 DateProperty prop = (DateProperty)safeget(name);
202 if (prop==null)
203 return null;
204 else
205 return (Date)prop.getValue();
206 }
207 catch (Exception e) {
208 return null;
209 }
210 }
211
212 public void setDateValue(String name, Date value) {
213 DateProperty property = new DateProperty();
214 property.setName(name);
215 property.setValue(value);
216 safeput(name, property);
217 }
218
219 public Set getSetValue(String name) {
220 ListProperty prop = (ListProperty)safeget(name);
221 if (prop==null)
222 return new HashSet();
223 else
224 return (Set)prop.getValue();
225 }
226
227 public void setSetValue(String name, Set value) {
228 ListProperty property = new ListProperty();
229 property.setValue(value);
230 safeput(name, property);
231 }
232
233 // These functions should not be used
234 // but instead our own implementation
235 private Map getFields() {
236 return fields;
237 }
238
239 public void setFields(Map fields) {
240 this.fields = fields;
241 }
242
243 public PropertyInterface getField(String name) {
244 return (PropertyInterface)fields.get(name);
245 }
246
247 public void addField(String name, PropertyInterface element) {
248 fields.put(name, element);
249 }
250
251 public void removeField(String name) {
252 Object field = safeget(name);
253 if (field!=null) {
254 fields.remove(name);
255 fieldsToRemove.add(field);
256 }
257 }
258
259 public Collection getFieldList() {
260 return fields.values();
261 }
262
263 public Set getPropertyList() {
264 return fields.keySet();
265 }
266
267 public Object[] getProperties() {
268 Object[] array = getFields().values().toArray();
269 return array;
270 }
271
272 public Object[] getPropertyNames() {
273 Object[] array = getFields().keySet().toArray();
274 return array;
275 }
276
277 public boolean equals(Object coll) {
278 if (!super.equals(coll))
279 return false;
280 BaseCollection collection = (BaseCollection) coll;
281 if (collection.getClassName()==null) {
282 if (getClassName()!=null)
283 return false;
284 } else if (!collection.getClassName().equals(getClassName()))
285 return false;
286
287 if (getFields().size()!=collection.getFields().size())
288 return false;
289
290 Iterator itfields = getFields().keySet().iterator();
291 while (itfields.hasNext()) {
292 String name = (String) itfields.next();
293 Object prop = getFields().get(name);
294 Object prop2 = collection.getFields().get(name);
295 if (!prop.equals(prop2))
296 return false;
297 }
298
299 return true;
300 }
301
302 public Object clone() {
303 BaseCollection collection = (BaseCollection) super.clone();
304 collection.setClassName(getClassName());
305 collection.setNumber(getNumber());
306 Map fields = getFields();
307 Map cfields = new HashMap();
308 Iterator itfields = fields.keySet().iterator();
309 while (itfields.hasNext()) {
310 String name = (String)itfields.next();
311 PropertyInterface prop = (PropertyInterface)((BaseElement)fields.get(name)).clone();
312 prop.setObject(collection);
313 cfields.put(name, prop);
314 }
315 collection.setFields(cfields);
316 return collection;
317 }
318
319 public void merge(BaseObject object) {
320 Iterator itfields = object.getPropertyList().iterator();
321 while (itfields.hasNext()) {
322 String name = (String) itfields.next();
323 if (safeget(name)==null)
324 safeput(name, (PropertyInterface) ((BaseElement)object.safeget(name)).clone());
325 }
326 }
327
328
329 public List getDiff(Object coll, XWikiContext context) {
330 ArrayList difflist = new ArrayList();
331 BaseCollection collection = (BaseCollection) coll;
332 Iterator itfields = getFields().keySet().iterator();
333 while (itfields.hasNext()) {
334 String name = (String) itfields.next();
335 BaseElement prop = (BaseElement)getFields().get(name);
336 BaseElement prop2 = (BaseElement)collection.getFields().get(name);
337
338 if (prop2==null) {
339 String dprop = ((PropertyClass)getxWikiClass(context).getField(name)).displayView(name, this,context);
340 difflist.add(new ObjectDiff(getClassName(), getNumber(), "added",
341 name, dprop , ""));
342 } else if (!prop.equals(prop2)) {
343 BaseClass bclass = getxWikiClass(context);
344 PropertyClass pclass = (PropertyClass) ((bclass==null) ? null : bclass.getField(name));
345 if (pclass==null) {
346 difflist.add(new ObjectDiff(getClassName(), getNumber(), "changed",
347 name, prop.toString() , prop2.toString()));
348 } else {
349 String dprop = pclass.displayView(name,this,context);
350 String dprop2 = pclass.displayView(name,collection,context);
351 difflist.add(new ObjectDiff(getClassName(), getNumber(), "changed",
352 name, dprop , dprop2));
353 }
354 }
355 }
356
357 itfields = collection.getFields().keySet().iterator();
358 while (itfields.hasNext()) {
359 String name = (String) itfields.next();
360 BaseElement prop = (BaseElement)getFields().get(name);
361 BaseElement prop2 = (BaseElement)collection.getFields().get(name);
362
363 if (prop==null) {
364 BaseClass bclass = getxWikiClass(context);
365 PropertyClass pclass = (PropertyClass) ((bclass==null) ? null : bclass.getField(name));
366 if (pclass==null) {
367 difflist.add(new ObjectDiff(getClassName(), getNumber(), "changed",
368 name, "" , prop2.toString()));
369 } else {
370 String dprop2 = ((PropertyClass)getxWikiClass(context).getField(name)).displayView(name,collection,context);
371 difflist.add(new ObjectDiff(getClassName(), getNumber(), "removed",
372 name, "" , dprop2));
373 }
374 }
375 }
376
377 return difflist;
378 }
379
380
381 public List getFieldsToRemove() {
382 return fieldsToRemove;
383 }
384
385 public void setFieldsToRemove(List fieldsToRemove) {
386 this.fieldsToRemove = fieldsToRemove;
387 }
388
389 public abstract Element toXML(BaseClass bclass);
390
391 public String toXMLString() {
392 Document doc = new DOMDocument();
393 doc.setRootElement(toXML(null));
394 OutputFormat outputFormat = new OutputFormat("", true);
395 StringWriter out = new StringWriter();
396 XMLWriter writer = new XMLWriter( out, outputFormat );
397 try {
398 writer.write(doc);
399 return out.toString();
400 } catch (IOException e) {
401 e.printStackTrace();
402 return "";
403 }
404 }
405
406 public String toString() {
407 return toXMLString();
408 }
409
410
411 }