KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > factories > InodeFactory


1 package com.dotmarketing.factories;
2
3 import java.util.HashSet JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Set JavaDoc;
6
7 import com.dotmarketing.beans.Inode;
8 import com.dotmarketing.beans.Tree;
9 import com.dotmarketing.db.DotConnect;
10 import com.dotmarketing.db.DotHibernate;
11 import com.dotmarketing.exception.DotRuntimeException;
12 import com.dotmarketing.portlets.events.model.Event;
13 import com.dotmarketing.util.Logger;
14 import com.dotmarketing.util.UtilMethods;
15
16 /**
17  *
18  * @author will
19  */

20 public class InodeFactory {
21
22
23     /*
24      * Returns a single child inode of the specified class. If no child is
25      * found, a new instance is returned
26      */

27     public static Object JavaDoc getChildOfClass(Inode inode, Class JavaDoc c) {
28         try {
29             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
30             DotHibernate dh = new DotHibernate(c);
31             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
32                     + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
33                     + "_1_.inode = " + tableName + ".inode";
34
35             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClass\n " + sql + "\n");
36
37             Logger.debug(InodeFactory.class, "inode: " + inode.getInode() + "\n");
38
39             dh.setSQLQuery(sql);
40
41             // dh.setQuery("from inode in class " + c.getName() + " where ? in
42
// inode.parents.elements");
43
dh.setParam(inode.getInode());
44
45             return dh.load();
46         } catch (Exception JavaDoc e) {
47             Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e);
48             throw new DotRuntimeException(e.toString());
49         }
50
51         /*
52          * try { return c.newInstance(); } catch (Exception e) { return new
53          * Object(); }
54          */

55     }
56
57     public static Object JavaDoc getChildOfClassbyCondition(Inode inode, Class JavaDoc c, String JavaDoc condition) {
58         try {
59             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
60             DotHibernate dh = new DotHibernate(c);
61             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
62                     + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
63                     + "_1_.inode = " + tableName + ".inode and " + condition;
64
65             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassbyCondition\n " + sql + "\n");
66
67             Logger.debug(InodeFactory.class, "inode: " + inode.getInode() + "\n");
68
69             dh.setSQLQuery(sql);
70
71             // dh.setQuery("from inode in class " + c.getName() + " where ? in
72
// inode.parents.elements");
73
dh.setParam(inode.getInode());
74
75             return dh.load();
76         } catch (Exception JavaDoc e) {
77             Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e);
78             throw new DotRuntimeException(e.toString());
79         }
80
81         /*
82          * try { return c.newInstance(); } catch (Exception e) { return new
83          * Object(); }
84          */

85     }
86
87     public static Object JavaDoc getChildOfClassByRelationTypeAndCondition(Inode inode, Class JavaDoc c, String JavaDoc relationType,
88             String JavaDoc condition) {
89         return getChildOfClassByRelationTypeAndCondition(inode.getInode(), c, relationType, condition);
90     }
91
92     public static Object JavaDoc getChildOfClassByRelationTypeAndCondition(long inode, Class JavaDoc c, String JavaDoc relationType,
93             String JavaDoc condition) {
94         try {
95             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
96             DotHibernate dh = new DotHibernate(c);
97             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
98                     + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
99                     + "_1_.inode = " + tableName + ".inode and " + condition;
100
101             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassbyCondition\n " + sql + "\n");
102
103             dh.setSQLQuery(sql);
104
105             // dh.setQuery("from inode in class " + c.getName() + " where ? in
106
// inode.parents.elements");
107
dh.setParam(inode);
108
109             Logger.debug(InodeFactory.class, "inode: " + inode + "\n");
110
111             return dh.load();
112         } catch (Exception JavaDoc e) {
113             Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e);
114             throw new DotRuntimeException(e.toString());
115         }
116
117         /*
118          * try { return c.newInstance(); } catch (Exception e) { return new
119          * Object(); }
120          */

121     }
122
123     /*
124      * Returns a single child inode of the specified class that also has the
125      * relation type. If no child is found, a new instance is returned
126      */

127     public static Object JavaDoc getChildOfClassByRelationType(Inode p, Class JavaDoc c, String JavaDoc relationType) {
128         try {
129             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
130             DotHibernate dh = new DotHibernate(c);
131
132             // dh.setQuery("from inode in class " + c.getName() + " where ? in
133
// (select tree.parent from com.dotmarketing.beans.Tree tree
134
// where inode.inode = tree.child and tree.relationType = ?)");
135
String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
136                     + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
137                     + "_1_.inode = " + tableName + ".inode and tree.relation_type = ?";
138
139             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassByRelationType\n " + sql + "\n");
140
141             dh.setSQLQuery(sql);
142
143             Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
144
145             dh.setParam(p.getInode());
146             dh.setParam(relationType);
147
148             return dh.load();
149         } catch (Exception JavaDoc e) {
150             Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e);
151             throw new DotRuntimeException(e.toString());
152         }
153
154         /*
155          * try { return c.newInstance(); } catch (Exception e) { return new
156          * Object(); }
157          */

158     }
159
160     public static java.util.List JavaDoc getChildrenClass(Inode p, Class JavaDoc c) {
161         return getChildrenClass(p, c, 0, 0);
162     }
163
164     public static java.util.List JavaDoc getChildrenClass(Inode p, Class JavaDoc c, int limit, int offset) {
165         try {
166             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
167             DotHibernate dh = new DotHibernate(c);
168
169             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
170                     + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
171                     + "_1_.inode = " + tableName + ".inode";
172
173             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClass\n " + sql + "\n");
174
175             dh.setSQLQuery(sql);
176
177             Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
178
179             dh.setParam(p.getInode());
180
181             if (limit != 0) {
182                 dh.setFirstResult(offset);
183                 dh.setMaxResults(limit);
184             }
185
186             return dh.list();
187         } catch (Exception JavaDoc e) {
188             Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e);
189             throw new DotRuntimeException(e.toString());
190         }
191
192         // return new java.util.ArrayList();
193
}
194
195     public static java.util.List JavaDoc getChildrenClass(Inode p, Class JavaDoc c, String JavaDoc orderBy) {
196         return getChildrenClass(p, c, orderBy, 0, 0);
197     }
198
199     public static java.util.List JavaDoc getChildrenClass(Inode p, Class JavaDoc c, String JavaDoc orderBy, int limit, int offset) {
200         try {
201
202             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
203             DotHibernate dh = new DotHibernate(c);
204
205             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
206                     + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
207                     + "_1_.inode = " + tableName + ".inode order by " + orderBy;
208
209             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClass\n " + sql + "\n");
210
211             dh.setSQLQuery(sql);
212
213             Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
214
215             dh.setParam(p.getInode());
216
217             if (limit != 0) {
218                 dh.setFirstResult(offset);
219                 dh.setMaxResults(limit);
220             }
221
222             return dh.list();
223         } catch (Exception JavaDoc e) {
224             Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e);
225             throw new DotRuntimeException(e.toString());
226         }
227
228         // return new java.util.ArrayList();
229
}
230
231     public static java.util.List JavaDoc getChildrenClassByCondition(Inode p, Class JavaDoc c, String JavaDoc condition) {
232         return getChildrenClassByCondition(p, c, condition, 0, 0);
233     }
234
235     public static java.util.List JavaDoc getChildrenClassByCondition(Inode p, Class JavaDoc c, String JavaDoc condition, int limit, int offset) {
236         try {
237             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
238             DotHibernate dh = new DotHibernate(c);
239
240             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
241                     + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
242                     + "_1_.inode = " + tableName + ".inode and " + condition;
243
244             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByCondition\n " + sql);
245
246             dh.setSQLQuery(sql);
247
248             Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
249
250             dh.setParam(p.getInode());
251
252             if (limit != 0) {
253                 dh.setFirstResult(offset);
254                 dh.setMaxResults(limit);
255             }
256
257             return dh.list();
258         } catch (Exception JavaDoc e) {
259             Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e);
260             throw new DotRuntimeException(e.toString());
261         }
262
263         // return new java.util.ArrayList();
264
}
265
266     public static java.util.List JavaDoc getChildrenClassByOrder(Inode p, Class JavaDoc c, String JavaDoc order) {
267         return getChildrenClassByOrder(p, c, order, 0, 0);
268     }
269
270     public static java.util.List JavaDoc getChildrenClassByOrder(Inode p, Class JavaDoc c, String JavaDoc order, int limit, int offset) {
271         try {
272             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
273             DotHibernate dh = new DotHibernate(c);
274
275             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
276                     + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
277                     + "_1_.inode = " + tableName + ".inode order by " + order;
278
279             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByOrder\n " + sql);
280
281             Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
282
283             Logger.debug(InodeFactory.class, "order: " + order + "\n");
284
285             dh.setSQLQuery(sql);
286
287             dh.setParam(p.getInode());
288
289             if (limit != 0) {
290                 dh.setFirstResult(offset);
291                 dh.setMaxResults(limit);
292             }
293
294             return dh.list();
295         } catch (Exception JavaDoc e) {
296             Logger.error(InodeFactory.class, "getChildrenClassByOrder failed:" + e, e);
297             throw new DotRuntimeException(e.toString());
298         }
299
300         // return new java.util.ArrayList();
301
}
302
303     public static java.util.List JavaDoc getChildrenClassOrderByRandom(Inode p, Class JavaDoc c) {
304         return getChildrenClassOrderByRandom(p, c, 0, 0);
305     }
306
307     public static java.util.List JavaDoc getChildrenClassOrderByRandom(Inode p, Class JavaDoc c, int limit, int offset) {
308         try {
309             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
310             DotHibernate dh = new DotHibernate(c);
311
312             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
313                     + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
314                     + "_1_.inode = " + tableName + ".inode order by rand()";
315
316             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByOrder\n " + sql);
317
318             Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
319
320             dh.setFirstResult(offset);
321             dh.setMaxResults(limit);
322
323             dh.setSQLQuery(sql);
324
325             dh.setParam(p.getInode());
326
327             if (limit != 0) {
328                 dh.setFirstResult(offset);
329                 dh.setMaxResults(limit);
330             }
331
332             return dh.list();
333         } catch (Exception JavaDoc e) {
334             Logger.error(InodeFactory.class, "getChildrenClassByOrder failed:" + e, e);
335             throw new DotRuntimeException(e.toString());
336         }
337
338         // return new java.util.ArrayList();
339
}
340
341     public static java.util.List JavaDoc getChildrenClassByCondition(Inode p1, Inode p2, Class JavaDoc c, String JavaDoc condition, int limit,
342             int offset) {
343         return getChildrenClassByCondition(p1.getInode(), p2.getInode(), c, condition, limit, offset);
344     }
345
346     public static java.util.List JavaDoc getChildrenClassByCondition(Inode p1, Inode p2, Class JavaDoc c, String JavaDoc condition) {
347         return getChildrenClassByCondition(p1.getInode(), p2.getInode(), c, condition, 0, 0);
348     }
349
350     public static java.util.List JavaDoc getChildrenClassByCondition(long p1, long p2, Class JavaDoc c, String JavaDoc condition) {
351         return getChildrenClassByCondition(p1, p2, c, condition, 0, 0);
352     }
353
354     public static java.util.List JavaDoc getChildrenClassByCondition(long p1, long p2, Class JavaDoc c, String JavaDoc condition, int limit,
355             int offset) {
356         try {
357             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
358             DotHibernate dh = new DotHibernate(c);
359
360             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName
361                     + ", tree tree, tree tree2, inode " + tableName
362                     + "_1_ where tree.parent = ? and tree2.parent = ? and tree.child = " + tableName
363                     + ".inode and tree2.child = " + tableName + ".inode and " + tableName + "_1_.inode = "
364                     + tableName + ".inode and " + condition;
365
366             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByCondition\n " + sql + "\n");
367
368             dh.setSQLQuery(sql);
369
370             Logger.debug(InodeFactory.class, "inode: " + p1 + "\n");
371             Logger.debug(InodeFactory.class, "inode: " + p2 + "\n");
372
373             dh.setParam(p1);
374             dh.setParam(p2);
375
376             if (limit != 0) {
377                 dh.setFirstResult(offset);
378                 dh.setMaxResults(limit);
379             }
380
381             return dh.list();
382         } catch (Exception JavaDoc e) {
383             Logger.warn(InodeFactory.class, "getChildrenClassByCondition failed:" + e, e);
384
385             // throw new DotRuntimeException(e.toString());
386
}
387
388         return new java.util.ArrayList JavaDoc();
389     }
390
391     public static java.util.List JavaDoc getChildrenClassByConditionAndOrderBy(long[] inodes, Class JavaDoc c, String JavaDoc condition,
392             String JavaDoc orderBy) {
393
394         if (inodes == null || inodes.length == 0) {
395             return InodeFactory.getInodesOfClassByConditionAndOrderBy(c, condition, orderBy);
396         }
397
398         try {
399
400             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
401             DotHibernate dh = new DotHibernate(c);
402
403             StringBuffer JavaDoc sql = new StringBuffer JavaDoc("SELECT {" + tableName + ".*} from " + tableName + " " + tableName);
404
405             for (int x = 1; x < inodes.length + 1; x++) {
406                 if (x == 1) {
407                     sql.append(", tree tree, ");
408                 } else {
409                     sql.append(", tree tree" + x + ", ");
410                 }
411             }
412             sql.append(" inode " + tableName + "_1_ ");
413             sql.append(" where ");
414
415             sql.append(tableName + "_1_.inode = " + tableName + ".inode and ");
416             for (int x = 1; x < inodes.length + 1; x++) {
417                 if (x == 1) {
418                     sql.append(" ( tree.parent = ? and ");
419                     sql.append(" tree.child = " + tableName + ".inode ) and ");
420                 } else {
421                     sql.append(" (tree" + x + ".parent = ? and ");
422                     sql.append(" tree" + x + ".child = " + tableName + ".inode ) and ");
423                 }
424             }
425
426             String JavaDoc query = sql.toString();
427             query = query.substring(0, query.lastIndexOf("and"));
428
429             // Validate condition
430
condition = (UtilMethods.isSet(condition) ? " and " + condition : "");
431             // Validate order
432
orderBy = (UtilMethods.isSet(orderBy) ? " order by " + orderBy : "");
433
434             // Create the final query
435
query += condition + orderBy;
436
437             // Set the query
438
dh.setSQLQuery(query);
439
440             for (int x = 0; x < inodes.length; x++) {
441                 dh.setParam(inodes[x]);
442             }
443
444             return dh.list();
445         } catch (Exception JavaDoc e) {
446             Logger.debug(InodeFactory.class, "getChildrenClassByConditionAndOrderBy failed:" + e, e);
447
448             // throw new DotRuntimeException(e.toString());
449
}
450
451         return new java.util.ArrayList JavaDoc();
452     }
453
454     public static java.util.List JavaDoc getChildrenClassByConditionAndOrderBy(long p1, long p2, Class JavaDoc c, String JavaDoc condition,
455             String JavaDoc orderBy) {
456
457         return getChildrenClassByConditionAndOrderBy(p1, p2, c, condition, orderBy, 0, 0);
458
459     }
460
461     public static java.util.List JavaDoc getChildrenClassByConditionAndOrderBy(long p1, long p2, Class JavaDoc c, String JavaDoc condition,
462             String JavaDoc orderBy, int limit, int offset) {
463         try {
464             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
465             DotHibernate dh = new DotHibernate(c);
466
467             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName
468                     + ", tree tree, tree tree2, inode " + tableName
469                     + "_1_ where tree.parent = ? and tree2.parent = ? and tree.child = " + tableName
470                     + ".inode and tree2.child = " + tableName + ".inode and " + tableName + "_1_.inode = "
471                     + tableName + ".inode and " + condition + " order by " + orderBy;
472
473             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByCondition\n " + sql + "\n");
474
475             dh.setSQLQuery(sql);
476
477             Logger.debug(InodeFactory.class, "inode 1: " + p1 + "\n");
478
479             Logger.debug(InodeFactory.class, "inode 2: " + p2 + "\n");
480
481             dh.setParam(p1);
482             dh.setParam(p2);
483
484             if (limit != 0) {
485                 dh.setFirstResult(offset);
486                 dh.setMaxResults(limit);
487             }
488
489             return dh.list();
490         } catch (Exception JavaDoc e) {
491             Logger.warn(InodeFactory.class, "getChildrenClass failed:" + e, e);
492
493             // throw new DotRuntimeException(e.toString());
494
}
495
496         return new java.util.ArrayList JavaDoc();
497     }
498
499     public static java.util.List JavaDoc getChildrenClass(List JavaDoc inodes, Class JavaDoc c, String JavaDoc orderBy) {
500         return getChildrenClass(inodes, c, orderBy, 0, 0);
501     }
502
503     public static java.util.List JavaDoc getChildrenClass(List JavaDoc inodes, Class JavaDoc c, int limit, int offset) {
504         return getChildrenClassByConditionAndOrderBy(inodes, c, "", "", limit, offset);
505     }
506
507     public static java.util.List JavaDoc getChildrenClass(List JavaDoc inodes, Class JavaDoc c, String JavaDoc orderBy, int limit, int offset) {
508         return getChildrenClassByConditionAndOrderBy(inodes, c, "", orderBy, limit, offset);
509     }
510
511     public static java.util.List JavaDoc getChildrenClassByConditionAndOrderBy(List JavaDoc inodes, Class JavaDoc c, String JavaDoc condition,
512             String JavaDoc orderBy) {
513
514         return getChildrenClassByConditionAndOrderBy(inodes, c, condition, orderBy, 0, 0);
515
516     }
517
518     public static java.util.List JavaDoc getChildrenClassByConditionAndOrderBy(List JavaDoc inodes, Class JavaDoc c, String JavaDoc condition,
519             String JavaDoc orderBy, int limit, int offset) {
520
521         if (inodes == null || inodes.size() == 0) {
522             return InodeFactory.getInodesOfClassByConditionAndOrderBy(c, condition, orderBy);
523
524         }
525
526         try {
527
528             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
529             DotHibernate dh = new DotHibernate(c);
530
531             StringBuffer JavaDoc sql = new StringBuffer JavaDoc("SELECT {" + tableName + ".*} from " + tableName + " " + tableName);
532
533             for (int x = 1; x < inodes.size() + 1; x++) {
534                 if (x == 1) {
535                     sql.append(", tree tree, ");
536                 } else {
537                     sql.append(", tree tree" + x + ", ");
538                 }
539             }
540             sql.append(" inode " + tableName + "_1_ ");
541             sql.append(" where ");
542
543             sql.append(tableName + "_1_.inode = " + tableName + ".inode and ");
544             for (int x = 1; x < inodes.size() + 1; x++) {
545                 if (x == 1) {
546                     sql.append(" ( tree.parent = ? and ");
547                     sql.append(" tree.child = " + tableName + ".inode ) ");
548                 } else {
549                     sql.append(" and (tree" + x + ".parent = ? and ");
550                     sql.append(" tree" + x + ".child = " + tableName + ".inode ) ");
551                 }
552             }
553
554             // only if we send condition
555
if (UtilMethods.isSet(condition)) {
556                 sql.append(" and " + condition);
557             }
558
559             // only if we send orderby
560
if (UtilMethods.isSet(orderBy)) {
561                 sql.append(" order by ");
562                 sql.append(orderBy);
563             }
564
565             dh.setSQLQuery(sql.toString());
566
567             for (int x = 0; x < inodes.size(); x++) {
568                 Inode i = (Inode) inodes.get(x);
569                 dh.setParam(i.getInode());
570             }
571             if (limit != 0) {
572                 dh.setFirstResult(offset);
573                 dh.setMaxResults(limit);
574             }
575
576             return dh.list();
577         } catch (Exception JavaDoc e) {
578             Logger.debug(InodeFactory.class, "getChildrenClassByConditionAndOrderBy failed:" + e, e);
579
580             // throw new DotRuntimeException(e.toString());
581
}
582
583         return new java.util.ArrayList JavaDoc();
584     }
585
586     public static java.util.List JavaDoc getChildrenClassByCondition(List JavaDoc inodes, Class JavaDoc c, String JavaDoc condition) {
587         return getChildrenClassByCondition(inodes, c, condition, 0, 0);
588
589     }
590
591     public static java.util.List JavaDoc getChildrenClassByCondition(List JavaDoc inodes, Class JavaDoc c, String JavaDoc condition, int limit,
592             int offset) {
593
594         if (inodes == null || inodes.size() == 0) {
595             return InodeFactory.getInodesOfClassByCondition(Event.class, condition);
596
597         }
598
599         try {
600
601             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
602             DotHibernate dh = new DotHibernate(c);
603
604             StringBuffer JavaDoc sql = new StringBuffer JavaDoc("SELECT {" + tableName + ".*} from " + tableName + " " + tableName);
605
606             for (int x = 1; x < inodes.size() + 1; x++) {
607                 if (x == 1) {
608                     sql.append(", tree tree, ");
609                 } else {
610                     sql.append(", tree tree" + x + ", ");
611                 }
612             }
613             sql.append(" inode " + tableName + "_1_ ");
614             sql.append(" where ");
615
616             sql.append(tableName + "_1_.inode = " + tableName + ".inode and ");
617             for (int x = 1; x < inodes.size() + 1; x++) {
618                 if (x == 1) {
619                     sql.append(" ( tree.parent = ? and ");
620                     sql.append(" tree.child = " + tableName + ".inode ) and ");
621                 } else {
622                     sql.append(" (tree" + x + ".parent = ? and ");
623                     sql.append(" tree" + x + ".child = " + tableName + ".inode ) and ");
624                 }
625             }
626
627             sql.append(condition);
628             dh.setSQLQuery(sql.toString());
629
630             for (int x = 0; x < inodes.size(); x++) {
631                 Inode i = (Inode) inodes.get(x);
632                 dh.setParam(i.getInode());
633             }
634             if (limit != 0) {
635                 dh.setFirstResult(offset);
636                 dh.setMaxResults(limit);
637             }
638
639             return dh.list();
640         } catch (Exception JavaDoc e) {
641             Logger.debug(InodeFactory.class, "getChildrenClassByConditionAndOrderBy failed:" + e, e);
642
643             // throw new DotRuntimeException(e.toString());
644
}
645
646         return new java.util.ArrayList JavaDoc();
647     }
648
649     public static java.util.List JavaDoc getChildrenClassByConditionAndOrderBy(Inode p, Class JavaDoc c, String JavaDoc condition,
650             String JavaDoc orderby) {
651
652         return getChildrenClassByConditionAndOrderBy(p.getInode(), c, condition, orderby, 0, 0);
653     }
654
655     public static java.util.List JavaDoc getChildrenClassByConditionAndOrderBy(Inode p, Class JavaDoc c, String JavaDoc condition,
656             String JavaDoc orderby, int limit, int offset) {
657
658         return getChildrenClassByConditionAndOrderBy(p.getInode(), c, condition, orderby, limit, offset);
659     }
660
661     public static java.util.List JavaDoc getChildrenClassByConditionAndOrderBy(long p, Class JavaDoc c, String JavaDoc condition, String JavaDoc orderby) {
662
663         return getChildrenClassByConditionAndOrderBy(p, c, condition, orderby, 0, 0);
664     }
665
666     public static java.util.List JavaDoc getChildrenClassByConditionAndOrderBy(long p, Class JavaDoc c, String JavaDoc condition,
667             String JavaDoc orderby, int limit, int offset) {
668         try {
669             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
670             DotHibernate dh = new DotHibernate(c);
671
672             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
673                     + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
674                     + "_1_.inode = " + tableName + ".inode and " + condition + " order by " + orderby;
675
676             if(!UtilMethods.isSet(orderby))
677                 sql += tableName + ".inode desc";
678             
679             //sql += (sql.toLowerCase().indexOf("limit") == -1 ? ", " + tableName + ".inode desc" : "");
680

681             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByConditionAndOrderBy\n " + sql + "\n");
682
683             dh.setSQLQuery(sql);
684
685             Logger.debug(InodeFactory.class, "inode: " + p + "\n");
686
687             dh.setParam(p);
688
689             if (limit != 0) {
690                 dh.setFirstResult(offset);
691                 dh.setMaxResults(limit);
692             }
693
694             return dh.list();
695         } catch (Exception JavaDoc e) {
696             Logger.warn(InodeFactory.class, "getChildrenClassByConditionAndOrderBy failed:" + e, e);
697
698             // throw new DotRuntimeException(e.toString());
699
}
700
701         return new java.util.ArrayList JavaDoc();
702     }
703
704     public static Inode getInode(String JavaDoc x, Class JavaDoc c) {
705         try {
706             return (Inode) new DotHibernate(c).load(Long.parseLong(x));
707         } catch (Exception JavaDoc e) {
708             return (Inode) new DotHibernate(c).load(x);
709         }
710     }
711
712     public static Inode getInode(long x, Class JavaDoc c) {
713         return (Inode) new DotHibernate(c).load(x);
714     }
715
716     public static java.util.List JavaDoc getInodesOfClass(Class JavaDoc c) {
717         return getInodesOfClass(c, 0, 0);
718     }
719
720     public static java.util.List JavaDoc getInodesOfClass(Class JavaDoc c, int limit, int offset) {
721         try {
722             DotHibernate dh = new DotHibernate(c);
723             dh.setQuery("from inode in class " + c.getName());
724
725             if (limit != 0) {
726                 dh.setFirstResult(offset);
727                 dh.setMaxResults(limit);
728             }
729
730             return dh.list();
731         } catch (Exception JavaDoc e) {
732             Logger.warn(InodeFactory.class, "getObjectsOfClass failed:" + e, e);
733             // throw new DotRuntimeException(e.toString());
734
}
735
736         return new java.util.ArrayList JavaDoc();
737     }
738
739     public static java.util.List JavaDoc getInodesOfClassByCondition(Class JavaDoc c, String JavaDoc condition) {
740         return getInodesOfClassByCondition(c, condition, 0, 0);
741     }
742
743     public static java.util.List JavaDoc getInodesOfClassByCondition(Class JavaDoc c, String JavaDoc condition, int limit, int offset) {
744         try {
745             DotHibernate dh = new DotHibernate(c);
746             dh.setQuery("from inode in class " + c.getName() + " where " + condition);
747
748             if (limit != 0) {
749                 dh.setFirstResult(offset);
750                 dh.setMaxResults(limit);
751             }
752
753             return dh.list();
754         } catch (Exception JavaDoc e) {
755             Logger.warn(InodeFactory.class, "getInodesOfClassByCondition(Class c, String condition) failed:" + e, e);
756
757             // throw new DotRuntimeException(e.toString());
758
}
759
760         return new java.util.ArrayList JavaDoc();
761     }
762
763     public static Object JavaDoc getInodeOfClassByCondition(Class JavaDoc c, String JavaDoc condition) {
764         try {
765             DotHibernate dh = new DotHibernate(c);
766             dh.setQuery("from inode in class " + c.getName() + " where " + condition);
767             Logger.debug(InodeFactory.class, "getInodeOfClassByCondition query: " + dh.getQuery());
768             return dh.load();
769         } catch (Exception JavaDoc e) {
770             Logger.warn(InodeFactory.class, "getInodeOfClassByCondition(Class c, String condition) failed:" + e, e);
771
772             // throw new DotRuntimeException(e.toString());
773
}
774
775         try {
776             return c.newInstance();
777         } catch (Exception JavaDoc e) {
778             throw new DotRuntimeException(e.toString());
779         }
780     }
781
782     public static java.util.List JavaDoc getInodesOfClassByCondition(Class JavaDoc c, String JavaDoc condition, String JavaDoc orderby) {
783         return getInodesOfClassByConditionAndOrderBy(c, condition, orderby, 0, 0);
784     }
785
786     public static java.util.List JavaDoc getInodesOfClassByConditionAndOrderBy(Class JavaDoc c, String JavaDoc condition, String JavaDoc orderby) {
787         return getInodesOfClassByConditionAndOrderBy(c, condition, orderby, 0, 0);
788     }
789
790     public static java.util.List JavaDoc getInodesOfClassByConditionAndOrderBy(Class JavaDoc c, String JavaDoc condition, String JavaDoc orderby,
791             int limit, int offset) {
792         try {
793             DotHibernate dh = new DotHibernate(c);
794             dh.setQuery("from inode in class " + c.getName() + " where " + condition + " order by " + orderby);
795
796             if (limit != 0) {
797                 dh.setFirstResult(offset);
798                 dh.setMaxResults(limit);
799             }
800
801             return dh.list();
802         } catch (Exception JavaDoc e) {
803             Logger.warn(InodeFactory.class,
804                     "getInodesOfClassByCondition(Class c, String condition, String orderby) failed:" + e, e);
805             // throw new DotRuntimeException(e.toString());
806
}
807
808         return new java.util.ArrayList JavaDoc();
809     }
810
811     public static java.util.List JavaDoc getInodesOfClass(Class JavaDoc c, int maxRows) {
812         try {
813             DotHibernate dh = new DotHibernate(c);
814             dh.setMaxResults(maxRows);
815             dh.setQuery("from inode in class " + c.getName());
816
817             return dh.list();
818         } catch (Exception JavaDoc e) {
819             Logger.warn(InodeFactory.class, "getObjectsOfClass failed:" + e, e);
820
821             // throw new DotRuntimeException(e.toString());
822
}
823
824         return new java.util.ArrayList JavaDoc();
825     }
826
827     public static java.util.List JavaDoc getInodesOfClass(Class JavaDoc c, String JavaDoc orderBy) {
828         return getInodesOfClass(c, orderBy, 0, 0);
829     }
830
831     public static java.util.List JavaDoc getInodesOfClass(Class JavaDoc c, String JavaDoc orderBy, int limit, int offset) {
832         try {
833             DotHibernate dh = new DotHibernate(c);
834             dh.setQuery("from inode in class " + c.getName() + " order by " + orderBy);
835
836             if (limit != 0) {
837                 dh.setFirstResult(offset);
838                 dh.setMaxResults(limit);
839             }
840
841             return dh.list();
842         } catch (Exception JavaDoc e) {
843             Logger.warn(InodeFactory.class, "getInodesOfClass failed:" + e, e);
844         }
845
846         return new java.util.ArrayList JavaDoc();
847     }
848
849     public static Object JavaDoc getObject(long x, Class JavaDoc c) {
850         return new DotHibernate(c).load(x);
851     }
852
853     public static Object JavaDoc getParentOfClass(Inode i, Class JavaDoc c) {
854         return getParentOfClass(String.valueOf(i.getInode()), c);
855     }
856
857     public static Object JavaDoc getParentOfClass(String JavaDoc i, Class JavaDoc c) {
858         try {
859             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
860             DotHibernate dh = new DotHibernate(c);
861             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
862                     + tableName + "_1_ where tree.child = ? and tree.parent = " + tableName + ".inode and " + tableName
863                     + "_1_.inode = " + tableName + ".inode ";
864
865             Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentOfClass:\n " + sql + "\n");
866
867             dh.setSQLQuery(sql);
868             dh.setParam(i);
869
870             Logger.debug(InodeFactory.class, "inode: " + i + "\n");
871
872             List JavaDoc list = dh.list();
873
874             if ((list != null) && (list.size() != 0)) {
875                 return list.get(0);
876             }
877         } catch (Exception JavaDoc e) {
878             try {
879                 Logger.warn(InodeFactory.class, "getParentOfClass failed:" + e, e);
880
881                 return c.newInstance();
882             } catch (Exception JavaDoc ex) {
883                 Logger.warn(InodeFactory.class, "getParentOfClass failed:" + e, e);
884
885                 // throw new DotRuntimeException(e.toString());
886
}
887         }
888
889         try {
890             return c.newInstance();
891         } catch (Exception JavaDoc e) {
892             return new Object JavaDoc();
893         }
894     }
895
896     public static java.util.List JavaDoc getParentsOfClassNoLock(Inode p, Class JavaDoc c) {
897         try {
898             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
899             DotHibernate dh = new DotHibernate(c);
900             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName
901                     + " with (nolock), tree tree with (nolock), inode " + tableName + "_1_ with (nolock) where tree.child = ? and tree.parent = "
902                     + tableName + ".inode and " + tableName + "_1_.inode = " + tableName + ".inode ";
903
904             Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentOfClass:\n " + sql + "\n");
905
906             dh.setSQLQuery(sql);
907
908             //dh.setQuery("from inode in class " + c.getName() + " where ? in
909
// inode.children.elements");
910
dh.setParam(p.getInode());
911
912             Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
913
914             return dh.list();
915         } catch (Exception JavaDoc e) {
916             Logger.warn(InodeFactory.class, "getParentsOfClass failed:" + e, e);
917
918             //throw new DotRuntimeException(e.toString());
919
}
920
921         return new java.util.ArrayList JavaDoc();
922     }
923     
924     public static Object JavaDoc getParentOfClassByRelationType(Inode p, Class JavaDoc c, String JavaDoc relationType) {
925         try {
926             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
927             DotHibernate dh = new DotHibernate(c);
928
929             // dh.setQuery("from inode in class " + c.getName() + " where ? in
930
// (select tree.parent from com.dotmarketing.beans.Tree tree
931
// where inode.inode = tree.child and tree.relationType = ?)");
932
String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
933                     + tableName + "_1_ where tree.child = ? and tree.parent = " + tableName + ".inode and " + tableName
934                     + "_1_.inode = " + tableName + ".inode and tree.relation_type = ?";
935
936             Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassByRelationType\n " + sql + "\n");
937
938             dh.setSQLQuery(sql);
939
940             Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
941
942             dh.setParam(p.getInode());
943             dh.setParam(relationType);
944
945             return dh.load();
946         } catch (Exception JavaDoc e) {
947             Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e);
948             throw new DotRuntimeException(e.toString());
949         }
950     }
951
952     public static java.util.List JavaDoc getParentsOfClassByCondition(Inode p, Class JavaDoc c, String JavaDoc condition) {
953         try {
954             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
955             DotHibernate dh = new DotHibernate(c);
956
957             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
958                     + tableName + "_1_ where tree.child = ? and tree.parent = " + tableName + ".inode and " + tableName
959                     + "_1_.inode = " + tableName + ".inode and " + condition;
960
961             Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentsOfClassByCondition\n " + sql);
962
963             Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
964
965             Logger.debug(InodeFactory.class, "condition: " + condition + "\n");
966
967             dh.setSQLQuery(sql);
968             dh.setParam(p.getInode());
969
970             return dh.list();
971         } catch (Exception JavaDoc e) {
972             Logger.warn(InodeFactory.class, "getParentsOfClassByCondition failed:" + e, e);
973
974             // throw new DotRuntimeException(e.toString());
975
}
976
977         return new java.util.ArrayList JavaDoc();
978     }
979
980     public static java.util.List JavaDoc getParentsOfClass(Inode p, Class JavaDoc c) {
981         return getParentsOfClass( p, c, "");
982     }
983     public static java.util.List JavaDoc getParentsOfClass(Inode p, Class JavaDoc c, String JavaDoc sortOrder) {
984         try {
985             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
986             DotHibernate dh = new DotHibernate(c);
987             String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
988                     + tableName + "_1_ where tree.child = ? and tree.parent = " + tableName + ".inode and " + tableName
989                     + "_1_.inode = " + tableName + ".inode ";
990
991             if(UtilMethods.isSet(sortOrder)){
992                 sql = sql + " order by "+ sortOrder;
993             }
994             Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentOfClass:\n " + sql + "\n");
995
996             dh.setSQLQuery(sql);
997
998             // dh.setQuery("from inode in class " + c.getName() + " where ? in
999
// inode.children.elements");
1000
dh.setParam(p.getInode());
1001
1002            Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
1003
1004            return dh.list();
1005        } catch (Exception JavaDoc e) {
1006            Logger.warn(InodeFactory.class, "getParentsOfClass failed:" + e, e);
1007
1008            // throw new DotRuntimeException(e.toString());
1009
}
1010
1011        return new java.util.ArrayList JavaDoc();
1012    }
1013
1014    public static Object JavaDoc getParentOfClassByCondition(Inode inode, Class JavaDoc c, String JavaDoc condition) {
1015        try {
1016            String JavaDoc tableName = ((Inode) c.newInstance()).getType();
1017            DotHibernate dh = new DotHibernate(c);
1018            String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
1019                    + tableName + "_1_ where tree.child = ? and tree.parent = " + tableName + ".inode and " + tableName
1020                    + "_1_.inode = " + tableName + ".inode and " + condition;
1021
1022            Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentOfClassbyCondition\n " + sql + "\n");
1023
1024            Logger.debug(InodeFactory.class, "inode: " + inode.getInode() + "\n");
1025
1026            dh.setSQLQuery(sql);
1027            dh.setParam(inode.getInode());
1028
1029            return dh.load();
1030        } catch (Exception JavaDoc e) {
1031            Logger.warn(InodeFactory.class, "getParentOfClassbyCondition failed:" + e, e);
1032            throw new DotRuntimeException(e.toString());
1033        }
1034
1035        /*
1036         * try { return c.newInstance(); } catch (Exception e) { return new
1037         * Object(); }
1038         */

1039    }
1040
1041    public static void deleteInode(Object JavaDoc o) {
1042        Inode inode = (Inode) o;
1043
1044        try {
1045
1046            DotHibernate.delete(o);
1047
1048            // workaround for dbs where we can't have more than one constraint
1049
// or triggers
1050
DotConnect db = new DotConnect();
1051            db.setSQL("delete from tree where child = ? or parent =?");
1052            db.addParam(inode.getInode());
1053            db.addParam(inode.getInode());
1054            db.getResult();
1055
1056            // workaround for dbs where we can't have more than one constraint
1057
// or triggers
1058
db.setSQL("delete from multi_tree where child = ? or parent1 =? or parent2 = ?");
1059            db.addParam(inode.getInode());
1060            db.addParam(inode.getInode());
1061            db.addParam(inode.getInode());
1062            db.getResult();
1063
1064        } catch (Exception JavaDoc e) {
1065            throw new DotRuntimeException(e.getMessage());
1066        }
1067    }
1068
1069    public static void deleteChildrenOfClass(Inode parent, Class JavaDoc c) {
1070        java.util.List JavaDoc children = getChildrenClass(parent, c);
1071        java.util.Iterator JavaDoc childrenIter = children.iterator();
1072
1073        while (childrenIter.hasNext()) {
1074            parent.deleteChild((Inode) childrenIter.next());
1075        }
1076    }
1077
1078    public static void deleteChildrenOfClassByRelationType(Inode parent, Class JavaDoc c,String JavaDoc relationType) {
1079        java.util.List JavaDoc children = getChildrenClass(parent, c);
1080        java.util.Iterator JavaDoc childrenIter = children.iterator();
1081
1082        while (childrenIter.hasNext()) {
1083            parent.deleteChild((Inode) childrenIter.next());
1084        }
1085    }
1086    
1087    public static void saveInode(Inode o) {
1088
1089        DotHibernate.saveOrUpdate(o);
1090    }
1091
1092    public static int countChildrenOfClass(Inode i, Class JavaDoc c) {
1093        return countChildrenOfClass(i, c, 0, 5);
1094    }
1095
1096    public static int countChildrenOfClass(Inode i, Class JavaDoc c, int limit, int offset) {
1097        try {
1098
1099            String JavaDoc tableName = ((Inode) c.newInstance()).getType();
1100            DotConnect db = new DotConnect();
1101            db
1102                    .setSQL("select count(*) as test from inode, tree where inode.inode = tree.child and tree.parent = ? and inode.type = '"
1103                            + tableName + "' ");
1104            db.addParam(i.getInode());
1105            // db.addParam(tableName);
1106
return db.getInt("test");
1107        } catch (Exception JavaDoc e) {
1108            Logger.warn(InodeFactory.class, "countChildrenOfClass failed:" + e, e);
1109            throw new DotRuntimeException(e.toString());
1110            // return 0;
1111
}
1112    }
1113
1114    public static List JavaDoc getChildrenOfClassByRelationType(Inode p, Class JavaDoc c, String JavaDoc relationType) {
1115        try {
1116            String JavaDoc tableName = ((Inode) c.newInstance()).getType();
1117            DotHibernate dh = new DotHibernate(c);
1118
1119            // dh.setQuery("from inode in class " + c.getName() + " where ? in
1120
// (select tree.parent from com.dotmarketing.beans.Tree tree
1121
// where inode.inode = tree.child and tree.relationType = ?)");
1122
String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
1123                    + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
1124                    + "_1_.inode = " + tableName + ".inode and tree.relation_type = ?";
1125
1126            Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassByRelationType\n " + sql + "\n");
1127
1128            dh.setSQLQuery(sql);
1129
1130            Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
1131
1132            dh.setParam(p.getInode());
1133            dh.setParam(relationType);
1134
1135            return dh.list();
1136        } catch (Exception JavaDoc e) {
1137            Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e);
1138            throw new DotRuntimeException(e.toString());
1139        }
1140
1141        /*
1142         * try { return c.newInstance(); } catch (Exception e) { return new
1143         * Object(); }
1144         */

1145    }
1146
1147    public static List JavaDoc getChildrenOfClassByRelationTypeAndCondition(Inode inode, Class JavaDoc c, String JavaDoc relationType,
1148            String JavaDoc condition) {
1149        return getChildrenOfClassByRelationTypeAndCondition(inode.getInode(), c, relationType, condition);
1150    }
1151
1152    public static List JavaDoc getChildrenOfClassByRelationTypeAndCondition(long inode, Class JavaDoc c, String JavaDoc relationType,
1153            String JavaDoc condition) {
1154        try {
1155            String JavaDoc tableName = ((Inode) c.newInstance()).getType();
1156            DotHibernate dh = new DotHibernate(c);
1157            String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode "
1158                    + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName
1159                    + "_1_.inode = " + tableName + ".inode and " + condition;
1160
1161            Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassbyCondition\n " + sql + "\n");
1162
1163            dh.setSQLQuery(sql);
1164
1165            // dh.setQuery("from inode in class " + c.getName() + " where ? in
1166
// inode.parents.elements");
1167
dh.setParam(inode);
1168
1169            Logger.debug(InodeFactory.class, "inode: " + inode + "\n");
1170
1171            return dh.list();
1172        } catch (Exception JavaDoc e) {
1173            Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e);
1174            throw new DotRuntimeException(e.toString());
1175        }
1176
1177        /*
1178         * try { return c.newInstance(); } catch (Exception e) { return new
1179         * Object(); }
1180         */

1181    }
1182
1183    public static java.util.List JavaDoc getInodesOfClassByConditionAndOrderBy(Class JavaDoc c, String JavaDoc condition, String JavaDoc orderby,
1184            int limit) {
1185        try {
1186            DotHibernate dh = new DotHibernate(c);
1187            String JavaDoc query = "from inode in class " + c.getName();
1188            // condition
1189
query += (UtilMethods.isSet(condition) ? " where " + condition : "");
1190            // order
1191
query += (UtilMethods.isSet(orderby) ? " order by upper(" + orderby + ") " : "");
1192            // Limit to retrieve the "limit" number of entries in the DB
1193
if (limit != 0) {
1194                dh.setMaxResults(limit);
1195            }
1196
1197            dh.setQuery(query);
1198            return dh.list();
1199        } catch (Exception JavaDoc e) {
1200            Logger.warn(InodeFactory.class,
1201                    "getInodesOfClassByCondition(Class c, String condition, String orderby) failed:" + e, e);
1202            // throw new DotRuntimeException(e.toString());
1203
}
1204
1205        return new java.util.ArrayList JavaDoc();
1206    }
1207    
1208    public static java.util.List JavaDoc getChildrenClassByRelationType(Inode p, Class JavaDoc c,String JavaDoc relationType) {
1209        try {
1210            String JavaDoc tableName = ((Inode) c.newInstance()).getType();
1211            DotHibernate dh = new DotHibernate(c);
1212
1213            String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName
1214                    + ", tree tree, inode " + tableName + "_1_ where tree.parent = ? and tree.child = "
1215                    + tableName + ".inode and " + tableName + "_1_.inode = " + tableName + ".inode and tree.relation_type = '" + relationType + "'";
1216
1217            Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClass\n " + sql + "\n");
1218
1219            dh.setSQLQuery(sql);
1220
1221            Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
1222
1223            dh.setParam(p.getInode());
1224            return dh.list();
1225        } catch (Exception JavaDoc e) {
1226            Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e);
1227            throw new DotRuntimeException(e.toString());
1228        }
1229
1230        //return new java.util.ArrayList();
1231
}
1232
1233    public static java.util.List JavaDoc getParentsOfClassByRelationType(Inode p, Class JavaDoc c, String JavaDoc relationType) {
1234        try {
1235            String JavaDoc tableName = ((Inode) c.newInstance()).getType();
1236            DotHibernate dh = new DotHibernate(c);
1237
1238            String JavaDoc sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName
1239                    + ", tree tree, inode " + tableName + "_1_ where tree.child = ? and tree.parent = "
1240                    + tableName + ".inode and " + tableName + "_1_.inode = " + tableName + ".inode and tree.relation_type = ?";
1241
1242            Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentsOfClassByCondition\n " + sql);
1243
1244            Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n");
1245
1246            Logger.debug(InodeFactory.class, "relation: " + relationType + "\n");
1247
1248            dh.setSQLQuery(sql);
1249            dh.setParam(p.getInode());
1250            dh.setParam(relationType);
1251
1252            return dh.list();
1253        } catch (Exception JavaDoc e) {
1254            Logger.warn(InodeFactory.class, "getParentsOfClassByCondition failed:" + e, e);
1255
1256            //throw new DotRuntimeException(e.toString());
1257
}
1258
1259        return new java.util.ArrayList JavaDoc();
1260    }
1261
1262    public static java.util.List JavaDoc getChildrenClassByConditionAndOrderBy(long[] inodes, Class JavaDoc c, String JavaDoc condition,
1263            String JavaDoc orderBy,int quantity) {
1264
1265        if (inodes == null || inodes.length == 0)
1266        {
1267            return InodeFactory.getInodesOfClassByConditionAndOrderBy(c, condition, orderBy,quantity);
1268        }
1269
1270        try {
1271
1272            String JavaDoc tableName = ((Inode) c.newInstance()).getType();
1273            DotHibernate dh = new DotHibernate(c);
1274
1275            StringBuffer JavaDoc sql = new StringBuffer JavaDoc("SELECT {" + tableName + ".*} from " + tableName + " " + tableName);
1276
1277            for (int x = 1; x < inodes.length + 1; x++) {
1278                if (x == 1) {
1279                    sql.append(", tree tree ");
1280                } else {
1281                    sql.append(", tree tree" + x + " ");
1282                }
1283            }
1284            sql.append(", inode " + tableName + "_1_ ");
1285            sql.append(" where ");
1286
1287            sql.append(tableName + "_1_.inode = " + tableName + ".inode and ");
1288            for (int x = 1; x < inodes.length + 1; x++) {
1289                if (x == 1) {
1290                    sql.append(" ( tree.parent = ? and ");
1291                    sql.append(" tree.child = " + tableName + ".inode ) and ");
1292                } else {
1293                    sql.append(" (tree" + x + ".parent = ? and ");
1294                    sql.append(" tree" + x + ".child = " + tableName + ".inode ) and ");
1295                }
1296            }
1297            
1298            String JavaDoc query = sql.toString();
1299            query = query.substring(0,query.lastIndexOf("and"));
1300            
1301            //Validate condition
1302
condition = (UtilMethods.isSet(condition) ? " and " + condition : "");
1303            //Validate order
1304
orderBy = (UtilMethods.isSet(orderBy) ? " order by " + orderBy : "");
1305             
1306            //Create the final query
1307
query += condition + orderBy;
1308            
1309            //Set the query
1310
if(quantity >= 0)
1311            {
1312                dh.setFirstResult(0);
1313                dh.setMaxResults(quantity);
1314            }
1315            dh.setSQLQuery(query);
1316
1317            for (int x = 0; x < inodes.length; x++) {
1318                dh.setParam(inodes[x]);
1319            }
1320
1321            return dh.list();
1322        } catch (Exception JavaDoc e) {
1323            Logger.debug(InodeFactory.class, "getChildrenClassByConditionAndOrderBy failed:" + e, e);
1324
1325            //throw new DotRuntimeException(e.toString());
1326
}
1327
1328        return new java.util.ArrayList JavaDoc();
1329    }
1330}
1331
Popular Tags