KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > structure > ExtentUsage


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.structure;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Comparator JavaDoc;
35 import java.util.HashMap JavaDoc;
36
37 import com.genimen.djeneric.language.Messages;
38 import com.genimen.djeneric.repository.DjExtent;
39 import com.genimen.djeneric.repository.DjObject;
40 import com.genimen.djeneric.repository.DjOql;
41 import com.genimen.djeneric.repository.DjRelation;
42 import com.genimen.djeneric.repository.DjSession;
43 import com.genimen.djeneric.repository.exceptions.DjenericException;
44 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
45 import com.genimen.djeneric.repository.exceptions.PropertyRequiredException;
46 import com.genimen.djeneric.repository.exceptions.QbeFilterException;
47 import com.genimen.djeneric.repository.oql.core.ParseException;
48 import com.genimen.djeneric.util.DjLogger;
49
50 public class ExtentUsage implements Cloneable JavaDoc
51 {
52   DjExtent _extent;
53   int _seq = 0;
54   String JavaDoc _title = null;
55   String JavaDoc _oqlExpression = null;
56   String JavaDoc _id = null;
57   ResourceDefinition _imageIcon = null;
58   boolean _editAllowed = true;
59   String JavaDoc _editorTarget = null;
60   boolean _insertAllowed = true;
61   boolean _deleteAllowed = true;
62   boolean _multiRow = true;
63   int _rowsDisplayed = 5;
64   String JavaDoc _descriptorExpression = null;
65   String JavaDoc _customNodeClass = null;
66   EditorDefinition _editor = null;
67   ExtentUsage _parent = null;
68   RelationDescriptor _isDetailVia = null;
69
70   ArrayList JavaDoc _detailRelations = new ArrayList JavaDoc();
71   ArrayList JavaDoc _propertyUsages = new ArrayList JavaDoc();
72
73   // If recursive is true; all is inherited from the parent extent usage
74
boolean _recursive = false;
75
76   public ExtentUsage()
77   {
78   }
79
80   public ExtentUsage(DjExtent extent)
81   {
82     _extent = extent;
83   }
84
85   public Object JavaDoc clone()
86   {
87     ExtentUsage result = null;
88     try
89     {
90       result = (ExtentUsage) super.clone();
91       if (_editor != null) result._editor = (EditorDefinition) _editor.clone();
92
93       result._propertyUsages = new ArrayList JavaDoc();
94       for (int i = 0; i < _propertyUsages.size(); i++)
95         result._propertyUsages.add(((PropertyUsage) _propertyUsages.get(i)).clone());
96
97       result._detailRelations = new ArrayList JavaDoc();
98
99       for (int i = 0; i < getDetailRelationCount(); i++)
100       {
101         RelationUsage usg = getDetailRelation(i);
102         result.addDetail((ExtentUsage) usg.getDetail().clone(), usg.getRelation());
103       }
104     }
105     catch (CloneNotSupportedException JavaDoc e)
106     {
107       DjLogger.log(e);
108     }
109     return result;
110   }
111
112   public boolean equals(Object JavaDoc obj)
113   {
114     if (obj == this) return true;
115
116     if (!(obj instanceof ExtentUsage)) return false;
117     ExtentUsage other = (ExtentUsage) obj;
118
119     return _extent == other._extent && _seq == other._seq && safeCompare(_title, other._title)
120            && safeCompare(_oqlExpression, other._oqlExpression) && safeCompare(_id, other._id)
121            && safeCompare(_editorTarget, other._editorTarget) && _imageIcon == other._imageIcon
122            && _editAllowed == other._editAllowed && _insertAllowed == other._insertAllowed
123            && _deleteAllowed == other._deleteAllowed && _multiRow == other._multiRow && _multiRow == other._multiRow
124            && _rowsDisplayed == other._rowsDisplayed && safeCompare(_descriptorExpression, other._descriptorExpression)
125            && safeCompare(_editor, other._editor) && _parent == other._parent
126            && safeCompare(_isDetailVia, other._isDetailVia) && safeCompare(_detailRelations, other._detailRelations)
127            && safeCompare(_propertyUsages, other._propertyUsages);
128   }
129
130   public int hashCode()
131   {
132     int result = 0;
133     if (_extent != null) result += _extent.hashCode();
134     if (_title != null) result += _title.hashCode();
135     if (_oqlExpression != null) result += _oqlExpression.hashCode();
136     if (_editorTarget != null) result += _editorTarget.hashCode();
137
138     return result;
139   }
140
141   private boolean safeCompare(Object JavaDoc one, Object JavaDoc two)
142   {
143     if (one == null && two == null) return true;
144     if (one == null || two == null) return false;
145
146     return one.equals(two);
147   }
148
149   public String JavaDoc toString()
150   {
151     return getName() + "(" + getExtent() + ")";
152   }
153
154   public void moveDetailDown(int detailNum)
155   {
156     if (detailNum + 1 >= _detailRelations.size()) return;
157
158     Object JavaDoc o = _detailRelations.get(detailNum);
159     _detailRelations.remove(detailNum);
160     _detailRelations.add(detailNum + 1, o);
161   }
162
163   public void moveDetailUp(int detailNum)
164   {
165     if (detailNum == 0 || detailNum >= _detailRelations.size()) return;
166
167     Object JavaDoc o = _detailRelations.get(detailNum);
168     _detailRelations.remove(detailNum);
169     _detailRelations.add(detailNum - 1, o);
170   }
171
172   public void setEditor(EditorDefinition editor)
173   {
174     _editor = editor;
175   }
176
177   public EditorDefinition getEditor()
178   {
179     if (isRecursive())
180     {
181       return getParent().getEditor();
182     }
183     else
184     {
185       return _editor;
186
187     }
188   }
189
190   public RelationUsage addDetail(ExtentUsage detail, DjRelation via)
191   {
192     if (isRecursive())
193     {
194       return getParent().addDetail(detail, via);
195     }
196     else
197     {
198       RelationUsage ru = new RelationUsage(this, detail, via);
199       _detailRelations.add(ru);
200       detail._parent = this;
201       if (via != null) detail.setIsDetailVia(new RelationDescriptor(via, detail.getExtent()));
202       return ru;
203     }
204   }
205
206   public ExtentUsage getParent()
207   {
208     return _parent;
209   }
210
211   public void setRecursive(boolean r)
212   {
213     _recursive = r;
214   }
215
216   public boolean isRecursive()
217   {
218     return _recursive;
219   }
220
221   public void removeDetailRelation(RelationUsage d)
222   {
223     if (isRecursive())
224     {
225       getParent().removeDetailRelation(d);
226     }
227     else
228     {
229       _detailRelations.remove(d);
230       d.getDetail()._parent = null;
231     }
232   }
233
234   public RelationUsage[] getDetailRelations()
235   {
236     if (isRecursive())
237     {
238       return getParent().getDetailRelations();
239     }
240     else
241     {
242       return (RelationUsage[]) _detailRelations.toArray(new RelationUsage[0]);
243     }
244   }
245
246   public ExtentUsage[] getChildren()
247   {
248     RelationUsage[] rels = getDetailRelations();
249     ExtentUsage[] result = new ExtentUsage[rels.length];
250     for (int i = 0; i < rels.length; i++)
251     {
252       result[i] = rels[i].getDetail();
253     }
254     return result;
255   }
256
257   public RelationUsage getDetailRelation(int idx)
258   {
259     if (isRecursive())
260     {
261       return getParent().getDetailRelation(idx);
262     }
263     else
264     {
265       return (RelationUsage) _detailRelations.get(idx);
266     }
267   }
268
269   public int getDetailRelationCount()
270   {
271     if (isRecursive())
272     {
273       return getParent().getDetailRelationCount();
274     }
275     else
276     {
277       return _detailRelations.size();
278     }
279   }
280
281   // Used primarily by the modeler's navigator editor
282
// See RelationDescriptor class for more info
283
public void setIsDetailVia(RelationDescriptor via)
284   {
285     _isDetailVia = via;
286   }
287
288   public void setExtent(DjExtent extent)
289   {
290     _extent = extent;
291   }
292
293   public boolean isMultiRow()
294   {
295     return _multiRow;
296   }
297
298   public int getRowsDisplayed()
299   {
300     return _rowsDisplayed;
301   }
302
303   public void setRowsDisplayed(int rows)
304   {
305     _rowsDisplayed = rows;
306   }
307
308   public void setMultiRow(boolean multirow)
309   {
310     _multiRow = multirow;
311   }
312
313   public int getPropertyUsageCount()
314   {
315     return _propertyUsages.size();
316   }
317
318   public int getPropertyUsageIndex(String JavaDoc propertyName) throws ObjectNotDefinedException
319   {
320     for (int i = 0; i < getPropertyUsageCount(); i++)
321     {
322       if (getPropertyUsage(i).getPropertyName().equals(propertyName))
323       {
324         return i;
325       }
326     }
327     throw new ObjectNotDefinedException(Messages.getString("ExtentUsage.PropertyNotDefined", propertyName));
328   }
329
330   public PropertyUsage getPropertyUsage(int idx)
331   {
332     return (PropertyUsage) _propertyUsages.get(idx);
333   }
334
335   public void addPropertyUsage(PropertyUsage usg)
336   {
337     _propertyUsages.add(usg);
338   }
339
340   public void addPropertyUsage(int atIdx, PropertyUsage usg)
341   {
342     _propertyUsages.add(atIdx, usg);
343   }
344
345   public void removePropertyUsage(int idx)
346   {
347     _propertyUsages.remove(idx);
348   }
349
350   public void removePropertyUsage(PropertyUsage cu)
351   {
352     _propertyUsages.remove(cu);
353   }
354
355   public String JavaDoc getName()
356   {
357     return getExtent().getName();
358   }
359
360   public void setSeq(int seq)
361   {
362     _seq = seq;
363   }
364
365   public int getSeq()
366   {
367     return _seq;
368   }
369
370   public void sortPropertyUsages()
371   {
372     Collections.sort(_propertyUsages, new PropertyUsageComparator());
373   }
374
375   public String JavaDoc getTitle()
376   {
377     if (_title == null && _extent != null)
378     {
379       return _extent.getNamePlural();
380     }
381     return _title;
382   }
383
384   public void setTitle(String JavaDoc title)
385   {
386     if (title != null)
387     {
388       if (title.trim().equals(""))
389       {
390         title = null;
391       }
392     }
393     _title = title;
394   }
395
396   public String JavaDoc getId()
397   {
398     if (_id == null && _extent != null)
399     {
400       return _extent.getName().toLowerCase();
401     }
402     return _id;
403   }
404
405   public void setId(String JavaDoc id)
406   {
407     if (id != null)
408     {
409       if (id.trim().equals(""))
410       {
411         id = null;
412       }
413     }
414     _id = id;
415   }
416
417   public boolean isEditAllowed()
418   {
419     return _editAllowed;
420   }
421
422   public void setEditAllowed(boolean allowed)
423   {
424     _editAllowed = allowed;
425   }
426
427   public boolean isInsertAllowed()
428   {
429     return _insertAllowed;
430   }
431
432   public void setInsertAllowed(boolean allowed)
433   {
434     _insertAllowed = allowed;
435   }
436
437   public boolean isDeleteAllowed()
438   {
439     return _deleteAllowed;
440   }
441
442   public void setDeleteAllowed(boolean allowed)
443   {
444     _deleteAllowed = allowed;
445   }
446
447   public DjExtent getExtent()
448   {
449     return _extent;
450   }
451
452   public RelationDescriptor getVia()
453   {
454     return _isDetailVia;
455   }
456
457   public void setDescriptorExpression(String JavaDoc descriptorExpression)
458   {
459     _descriptorExpression = descriptorExpression;
460   }
461
462   public String JavaDoc getDescriptorExpression()
463   {
464     return _descriptorExpression;
465   }
466
467   public void checkRequiredProperties(DjObject obj) throws ObjectNotDefinedException, DjenericException
468   {
469     ArrayList JavaDoc missingProperties = null;
470
471     for (int i = 0; i < getPropertyUsageCount(); i++)
472     {
473       if ((getPropertyUsage(i).isRequired() || getPropertyUsage(i).getProperty().isRequired())
474           && obj.isNull(getPropertyUsage(i).getPropertyName()))
475       {
476         if (missingProperties == null)
477         {
478           missingProperties = new ArrayList JavaDoc();
479         }
480         missingProperties.add(getPropertyUsage(i).getPropertyName());
481       }
482     }
483     if (missingProperties != null)
484     {
485       throw new PropertyRequiredException(Messages.getString("global.RequiredPropNull"), obj, missingProperties);
486     }
487   }
488
489   public int[] getPropertySortIndices()
490   {
491     ArrayList JavaDoc lst = new ArrayList JavaDoc();
492     for (int i = 0; i < getPropertyUsageCount(); i++)
493     {
494       if (getPropertyUsage(i).getSortOrder() != 0) lst.add(getPropertyUsage(i));
495     }
496     Collections.sort(lst, new PropertyUsageSortOrderComparator());
497     int[] result = new int[lst.size()];
498
499     try
500     {
501       for (int i = 0; i < lst.size(); i++)
502       {
503         result[i] = getExtent().getPropertyIndex(((PropertyUsage) lst.get(i)).getBaseProperty().getName());
504         if (((PropertyUsage) lst.get(i)).getSortOrder() < 0) result[i] = -result[i];
505       }
506     }
507     catch (ObjectNotDefinedException onde)
508     {
509       // This is impossible since the property was just retrieved from the
510
// extent
511
DjLogger.log(onde);
512     }
513     return result;
514   }
515
516   public void removeFromParent()
517   {
518     if (_parent != null)
519     {
520       _parent.removeDetailRelation(this, _isDetailVia);
521     }
522   }
523
524   protected void removeDetailRelation(ExtentUsage detail, RelationDescriptor isDetailVia)
525   {
526     if (isRecursive())
527     {
528       getParent().removeDetailRelation(detail, isDetailVia);
529     }
530     else
531     {
532       RelationUsage ru;
533       try
534       {
535         ru = new RelationUsage(this, detail, isDetailVia.getRelation());
536         _detailRelations.remove(ru);
537         detail._parent = null;
538         detail.setIsDetailVia(null);
539       }
540       catch (ObjectNotDefinedException e)
541       {
542         // The relation is already gone. Ignore this attempt then
543
}
544     }
545   }
546
547   public String JavaDoc getOqlExpression()
548   {
549     return _oqlExpression;
550   }
551
552   public void setOqlExpression(String JavaDoc oqlExpression)
553   {
554     if (oqlExpression != null && oqlExpression.trim().length() == 0) oqlExpression = null;
555     _oqlExpression = oqlExpression;
556   }
557
558   public DjOql createDefaultOql(DjSession session)
559   {
560     DjOql result = session.createOql(getExtent());
561     String JavaDoc expr = getOqlExpression();
562     if (expr != null) result.setWhereExpression(expr);
563
564     return result;
565   }
566
567   public void validateOqlExpression() throws QbeFilterException
568   {
569     try
570     {
571       String JavaDoc expr = getOqlExpression();
572
573       if (expr == null || expr.trim().length() == 0) return;
574
575       DjOql test = new DjOql(getExtent());
576       test.setWhereExpression(getOqlExpression());
577       test.getFromWhereClause();
578     }
579     catch (ParseException e)
580     {
581       throw new QbeFilterException(e);
582     }
583
584   }
585
586   public void validate() throws QbeFilterException, DjenericException
587   {
588     validateOqlExpression();
589     for (int i = 0; i < getPropertyUsageCount(); i++)
590     {
591       try
592       {
593         getPropertyUsage(i).validate();
594       }
595       catch (DjenericException dje)
596       {
597         throw new DjenericException(getId() + ": " + dje.getMessage());
598       }
599     }
600   }
601
602   public ResourceDefinition getImageIconResource()
603   {
604     return _imageIcon;
605   }
606
607   public void setImageIconResource(ResourceDefinition definition)
608   {
609     _imageIcon = definition;
610   }
611
612   public void deleteInvalidNodes()
613   {
614     RelationUsage[] rels = getDetailRelations();
615     for (int i = 0; i < rels.length; i++)
616     {
617       if (!rels[i].isValid()) removeDetailRelation(rels[i]);
618       else rels[i].getDetail().deleteInvalidNodes();
619     }
620   }
621
622   /**
623    * Returns the name of the property that designates the object to be edited by the editor (if any)
624    * When null is returned the editor should edit 'this'
625    */

626   public String JavaDoc getEditorTarget()
627   {
628     return _editorTarget;
629   }
630
631   /**
632    * Sets the name of the property that designates the object to be edited by the editor (if any)
633    * When set to null the editor should edit 'this'
634    */

635   public void setEditorTarget(String JavaDoc editorTarget)
636   {
637     if (editorTarget != null && editorTarget.trim().length() == 0) editorTarget = null;
638     _editorTarget = editorTarget;
639   }
640
641   public String JavaDoc getRelationsAsXml(int indent)
642   {
643     if (isRecursive()) return "";
644
645     StringBuffer JavaDoc result = new StringBuffer JavaDoc(500);
646     StringBuffer JavaDoc indentStr = new StringBuffer JavaDoc(20);
647     for (int i = 0; i < indent; i++)
648       indentStr.append(" ");
649
650     for (int i = 0; i < getDetailRelationCount(); i++)
651     {
652       RelationUsage rel = getDetailRelation(i);
653       result.append(indentStr.toString());
654       result.append("<detail relation=\"" + rel.getRelation().getName() + "\" extent=\"" + rel.getDetail().getExtent()
655                     + "\">\n");
656       result.append(rel.getDetail().getRelationsAsXml(indent + 2));
657       result.append(indentStr.toString());
658       result.append("</detail>\n");
659     }
660     return result.toString();
661   }
662
663   public String JavaDoc getCustomNodeClass()
664   {
665     return _customNodeClass;
666   }
667
668   public void setCustomNodeClass(String JavaDoc customNodeClass)
669   {
670     if (customNodeClass != null)
671     {
672       customNodeClass = customNodeClass.trim();
673       if (customNodeClass.length() == 0) customNodeClass = null;
674     }
675     _customNodeClass = customNodeClass;
676   }
677
678   protected void collectUsageIds(HashMap JavaDoc ids)
679   {
680     ids.put(getId(), this);
681     if (getParent() != null) getParent().collectUsageIds(ids);
682   }
683
684   public HashMap JavaDoc getParameters()
685   {
686     HashMap JavaDoc result = new HashMap JavaDoc();
687     collectUsageIds(result);
688     return result;
689   }
690 }
691
692 class PropertyUsageSortOrderComparator implements Comparator JavaDoc
693 {
694
695   public int compare(Object JavaDoc o1, Object JavaDoc o2)
696   {
697     PropertyUsage f1 = (PropertyUsage) o1;
698     PropertyUsage f2 = (PropertyUsage) o2;
699
700     return f1.getSortOrder() - f2.getSortOrder();
701   }
702 }
Popular Tags