KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > web > renderers > tree > WebFolder


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 modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes 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,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.web.renderers.tree;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35 import org.w3c.dom.DOMException JavaDoc;
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38
39 import com.genimen.djeneric.language.Messages;
40 import com.genimen.djeneric.repository.DjAssociation;
41 import com.genimen.djeneric.repository.DjCursor;
42 import com.genimen.djeneric.repository.DjList;
43 import com.genimen.djeneric.repository.DjObject;
44 import com.genimen.djeneric.repository.DjOql;
45 import com.genimen.djeneric.repository.DjProperty;
46 import com.genimen.djeneric.repository.DjSession;
47 import com.genimen.djeneric.repository.exceptions.DjenericException;
48 import com.genimen.djeneric.repository.oql.core.ParseException;
49 import com.genimen.djeneric.structure.ExtentUsage;
50 import com.genimen.djeneric.structure.RelationUsage;
51 import com.genimen.djeneric.tools.specifier.Specifier;
52 import com.genimen.djeneric.tools.specifier.interfaces.DjenericObjectContainer;
53
54 public class WebFolder extends AbstractWebNode implements DjenericObjectContainer
55 {
56   public final static int MAX_OBJECTS_IN_FOLDER = 300;
57   protected boolean _allDisplayed = true;
58   protected boolean _filtered = false;
59   protected int[] _sortIndices = null;
60   protected DjList _objects = null;
61   protected DjOql _oql = null;
62   protected RelationUsage _via = null;
63   protected Boolean JavaDoc _canFilter = null;
64
65   public WebFolder(DjSession session)
66   {
67     super(session);
68   }
69
70   public void reload() throws DjenericException
71   {
72     clearLoadedObjects();
73     load();
74     setExpanded(true);
75   }
76
77   public void delete() throws Exception JavaDoc
78   {
79     // delete for all contents considered too dangerous.. do nothing
80
}
81
82   /**
83    * Returns a list of objects that is contained by this folder
84    * @return the list
85    * @throws DjenericException
86    */

87   public DjList getObjects() throws DjenericException
88   {
89     if (_objects != null) return _objects;
90
91     try
92     {
93       setStatusMessage(Messages.getString("global.Loading"), true);
94
95       initOql();
96
97       _allDisplayed = true;
98       _objects = new DjList();
99
100       ExtentUsage usage = getExtentUsage();
101       _objects.setStoredTypeName(usage.getExtent());
102
103       DjCursor cursor;
104       if (isRootFolder() || !isContentsLoadedViaRelation())
105       {
106         if (_oql != null) cursor = getSession().getObjectsCursor(_oql, true);
107         else cursor = getSession().getObjectsCursor(usage.getExtent(), true);
108       }
109       else
110       {
111         DjAssociation assoc = getObject().getDetailAssociationByName(getVia().getRelation().getName(),
112                                                                      usage.getExtent());
113         if (_oql != null) cursor = assoc.getObjectsCursor(_oql, true);
114         else cursor = assoc.getObjectsCursor(true);
115       }
116
117       try
118       {
119         int maxObjects = MAX_OBJECTS_IN_FOLDER;
120         DjObject obj;
121         while (((obj = cursor.getNext()) != null) && maxObjects-- > 0)
122         {
123           _objects.add(obj);
124         }
125         if (maxObjects < 0)
126         {
127           _allDisplayed = false;
128         }
129       }
130       finally
131       {
132         cursor.close();
133       }
134
135       if (_sortIndices == null) _sortIndices = getExtentUsage().getExtent().getPropertySortIndices();
136       _objects.sort(_sortIndices);
137       return _objects;
138     }
139     finally
140     {
141       setStatusMessage(Specifier.OK_MSG, true);
142     }
143   }
144
145   public boolean isContentsLoadedViaRelation()
146   {
147     return getVia() != null && getVia().getRelation() != null;
148   }
149
150   protected boolean isRootFolder()
151   {
152     return getVia() == null;
153   }
154
155   public String JavaDoc toString()
156   {
157     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(50);
158
159     sb.append(super.toString());
160     if (_filtered) sb.append(Messages.getString("DjenericFolder.Filtered"));
161     if (!_allDisplayed) sb.append(Messages.getString("DjenericFolder.Partially"));
162
163     return sb.toString();
164   }
165
166   /**
167    * Displays the filter dialog, performs the query and displays the result in the tree
168    */

169   public void filter(DjOql oql) throws DjenericException
170   {
171     // Overridden for folder nodes
172

173     clearLoadedObjects();
174
175     _oql = oql;
176
177     // Add any additional where clause to the existing one:
178
if (hasOqlFilter())
179     {
180       String JavaDoc wcl = "(" + getExtentUsage().getOqlExpression() + ")";
181       String JavaDoc flr = oql.getWhereExpression();
182       if (flr != null && flr.trim().length() > 0) wcl += " and (" + flr + ")";
183       oql.setWhereExpression(wcl);
184     }
185
186     setOqlParameters(_oql, true);
187
188     _filtered = true;
189     DjList lst = getObjects();
190     showObjects();
191     expandPath();
192
193     String JavaDoc msg;
194     if (!_allDisplayed) msg = Messages.getString("DjenericFolder.FoundTruncated", String.valueOf(lst.size()),
195                                                  getExtentUsage().getExtent().getNamePlural());
196     else
197     {
198       if (lst.size() == 1) msg = Messages.getString("DjenericFolder.Found1", getExtentUsage().getExtent()
199           .getNameSingular());
200       else msg = Messages.getString("DjenericFolder.Found", String.valueOf(lst.size()), getExtentUsage().getExtent()
201           .getNamePlural());
202     }
203     setStatusMessage(msg, true);
204   }
205
206   /**
207    * Returns the object that this folder belongs to
208    */

209   public DjObject getObject()
210   {
211     AbstractWebNode parent = getParent();
212     if (parent != null && parent instanceof WebNode)
213     {
214       return ((WebNode) parent).getObject();
215     }
216     return null;
217   }
218
219   /**
220    * Returns a descriptor for the relation that defines the contents of this folder.
221    * If there is no relation (because this folder is customized for example) then
222    * null is returned
223    * @return
224    */

225   public RelationUsage getVia()
226   {
227     return _via;
228   }
229
230   /**
231    * Sets the descriptor for the relation that defines the contents of this folder.
232    * The extentusage of this folder is also set to the usage of the detail side of
233    * the relation.
234    * @return
235    */

236   public void setVia(RelationUsage via)
237   {
238     _via = via;
239     setExtentUsage(via.getDetail());
240   }
241
242   public void load() throws DjenericException
243   {
244     // This does NOT expand the node; it just forces the children (folders) to be created
245
showObjects();
246   }
247
248   public WebNode[] getChildNodes() throws Exception JavaDoc
249   {
250     load();
251     ArrayList JavaDoc result = new ArrayList JavaDoc();
252     for (int i = 0; i < getChildCount(); i++)
253       result.add(getChildAt(i));
254     return (WebNode[]) result.toArray(new WebNode[0]);
255   }
256
257   public boolean canDelete()
258   {
259     return false;
260   }
261
262   public boolean canFilter() throws DjenericException
263   {
264     if (getExtentUsage() == null || !getSession().canQuery()) return false;
265
266     if (_canFilter != null) return _canFilter.booleanValue();
267
268     boolean qry = false;
269
270     DjProperty[] props = getExtentUsage().getExtent().getProperties();
271     for (int i = 0; i < props.length && !qry; i++)
272     {
273       qry = props[i].isQueryable();
274     }
275
276     _canFilter = new Boolean JavaDoc(qry);
277     return qry;
278   }
279
280   public boolean canExport() throws DjenericException
281   {
282     return getExtentUsage() != null && getExtentUsage().getEditor() != null && getSession().canQuery();
283   }
284
285   public boolean hasOqlFilter()
286   {
287     if (getExtentUsage() == null) return false;
288
289     String JavaDoc filter = getExtentUsage().getOqlExpression();
290     return filter != null && filter.trim().length() != 0;
291   }
292
293   /**
294    * Initializes the OQL object if there is a OQL expression defined for the extentusage
295    * @throws DjenericException
296    *
297    */

298   protected void initOql() throws DjenericException
299   {
300     if (hasOqlFilter() && _oql == null)
301     {
302       _oql = getExtentUsage().createDefaultOql(getSession());
303
304       setOqlParameters(_oql, false);
305     }
306   }
307
308   private void setOqlParameters(DjOql oql, boolean ignoreNonExistingParameters) throws DjenericException
309   {
310     String JavaDoc[] parameters;
311     try
312     {
313       parameters = oql.getParameterNames();
314     }
315     catch (ParseException e)
316     {
317       throw new DjenericException(e);
318     }
319     HashMap JavaDoc parentIds = new HashMap JavaDoc();
320     collectParameters(parentIds);
321
322     for (int i = 0; i < parameters.length; i++)
323     {
324       DjObject obj = (DjObject) parentIds.get(parameters[i]);
325       if (obj == null && !ignoreNonExistingParameters) throw new DjenericException("Parameter with ID " + parameters[i]
326                                                                                    + " not defined in tree");
327       if (obj != null) oql.setParameter(parameters[i], obj.getObjectId());
328     }
329   }
330
331   protected void clearLoadedObjects()
332   {
333     _alreadyLoaded = false;
334     _objects = null;
335     _oql = null;
336     _filtered = false;
337   }
338
339   /**
340    * Displays (refreshes) the objects displayed in the tree
341    * @throws DjenericException
342    */

343   protected void showObjects() throws DjenericException
344   {
345     boolean isCollapsed = !isExpanded();
346     removeAllChildren();
347
348     try
349     {
350       ExtentUsage usage = getExtentUsage();
351       boolean detailHasKids = (usage.getDetailRelationCount() != 0);
352       getObjects();
353
354       for (int j = 0; j < _objects.size(); j++)
355       {
356         DjObject obj = _objects.getDjenericObjectAt(j);
357         WebNode node = new WebNode(getSession());
358         node.setExtentUsage(usage);
359         node.setObject(obj);
360         insertAsChild(node);
361       }
362
363       _alreadyLoaded = true;
364     }
365     finally
366     {
367       if (!isCollapsed)
368       {
369         expandPath();
370       }
371     }
372   }
373
374   public void setExpanded(boolean expand) throws DjenericException
375   {
376     if (!_expanded && expand && !_alreadyLoaded)
377     {
378       showObjects();
379     }
380     _expanded = expand;
381   }
382
383   public Element JavaDoc asXml(Document JavaDoc doc) throws DOMException JavaDoc, DjenericException
384   {
385     Element JavaDoc node = doc.createElement("folder");
386
387     node.setAttribute("title", toString());
388     node.setAttribute("nodeid", String.valueOf(getPath()));
389     node.setAttribute("expandable", String.valueOf(isExpandable()));
390     node.setAttribute("expanded", String.valueOf(isExpanded()));
391
392     node.setAttribute("canedit", "false");
393     node.setAttribute("candelete", String.valueOf(canDelete()));
394     node.setAttribute("cancreate", String.valueOf(canCreate()));
395     node.setAttribute("canfilter", String.valueOf(canFilter()));
396     node.setAttribute("cancopy", String.valueOf(canCopy()));
397     node.setAttribute("canexport", String.valueOf(canExport()));
398     node.setAttribute("canimport", String.valueOf(getSession().canModify() && getSession().canCreate()));
399
400     if (isExpanded())
401     {
402       for (int i = 0; i < getChildCount(); i++)
403       {
404         node.appendChild(getChildAt(i).asXml(doc));
405       }
406     }
407
408     return node;
409   }
410 }
Popular Tags