KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.dotmarketing.factories;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import com.dotmarketing.beans.Host;
7 import com.dotmarketing.beans.Identifier;
8 import com.dotmarketing.beans.Inode;
9 import com.dotmarketing.beans.WebAsset;
10 import com.dotmarketing.cache.IdentifierCache;
11 import com.dotmarketing.db.DotConnect;
12 import com.dotmarketing.db.DotHibernate;
13 import com.dotmarketing.portlets.files.model.File;
14 import com.dotmarketing.portlets.folders.model.Folder;
15 import com.dotmarketing.portlets.htmlpages.model.HTMLPage;
16 import com.dotmarketing.portlets.links.model.Link;
17 import com.dotmarketing.util.Config;
18 import com.dotmarketing.util.Logger;
19 import com.liferay.portal.model.Role;
20
21 /**
22  *
23  * @author will
24  */

25 public class IdentifierFactory {
26
27     public static Object JavaDoc getLiveChildOfClass(Inode o, Class JavaDoc c) {
28         Inode ret = (Inode) InodeFactory.getInodeOfClassByCondition(c, "live = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue() +
29                     " and identifier = " + o.getInode());
30         if(ret.getInode() > 0) {
31             return ret;
32         }
33         return InodeFactory.getChildOfClassbyCondition(o, c, "live = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
34     }
35
36     public static Object JavaDoc getChildOfClassByRelationType(Inode o, Class JavaDoc c, String JavaDoc relationType, boolean previewMode) {
37         return getChildOfClassByRelationType(o.getInode(), c, relationType, previewMode);
38     }
39
40     public static Object JavaDoc getChildOfClassByRelationType(long inode, Class JavaDoc c, String JavaDoc relationType, boolean previewMode) {
41         String JavaDoc condition = (previewMode) ? " working = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " "
42                 : " live = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " ";
43         return InodeFactory.getChildOfClassByRelationTypeAndCondition(inode, c, relationType, condition);
44
45     }
46
47     public static Object JavaDoc getLiveChildOfClassByCondition(Inode o, Class JavaDoc c, String JavaDoc condition) {
48         condition += " and live = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue();
49         return InodeFactory.getChildOfClassbyCondition(o, c, condition);
50     }
51
52     public static Object JavaDoc getLiveChildOfClassByRelationType(Inode o, Class JavaDoc c, String JavaDoc relationType) {
53         return getChildOfClassByRelationType(o, c, relationType, false);
54     }
55
56     public static Object JavaDoc getWorkingChildOfClassByRelationType(Inode o, Class JavaDoc c, String JavaDoc relationType) {
57         return getChildOfClassByRelationType(o, c, relationType, true);
58     }
59
60     public static java.util.List JavaDoc getLiveChildrenOfClass(Inode o, Class JavaDoc c) {
61         return InodeFactory.getChildrenClassByCondition(o, c, "live = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
62     }
63
64     public static java.util.List JavaDoc getLiveChildrenOfClass(Inode o, Class JavaDoc c, String JavaDoc condition) {
65         condition += " and live = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue();
66         return InodeFactory.getChildrenClassByCondition(o, c, condition);
67     }
68
69     public static java.util.List JavaDoc getWorkingChildrenOfClass(Inode o, Class JavaDoc c) {
70         String JavaDoc tableName = "";
71         try {
72             tableName = ((Inode) c.newInstance()).getType();
73         } catch (Exception JavaDoc e) {
74         }
75         return InodeFactory.getChildrenClassByConditionAndOrderBy(o, c, "working="
76                 + com.dotmarketing.db.DbConnectionFactory.getDBTrue(), tableName + "_1_.inode desc");
77     }
78
79     public static java.util.List JavaDoc getWorkingChildrenOfClass(Inode o, Class JavaDoc c, String JavaDoc orderBy) {
80
81         return InodeFactory.getChildrenClassByConditionAndOrderBy(o, c, "working="
82                 + com.dotmarketing.db.DbConnectionFactory.getDBTrue(), orderBy);
83     }
84
85     public static java.util.List JavaDoc getWorkingChildrenOfClassByCondition(Inode o, Class JavaDoc c, String JavaDoc condition) {
86         String JavaDoc tableName = "";
87         try {
88             tableName = ((Inode) c.newInstance()).getType();
89         } catch (Exception JavaDoc e) {
90         }
91         condition += " and working = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue();
92         return InodeFactory.getChildrenClassByConditionAndOrderBy(o, c, condition, tableName + "_1_.inode desc");
93     }
94
95     public static Object JavaDoc getWorkingChildOfClass(Inode inode, Class JavaDoc c) {
96         Inode ret = (Inode) InodeFactory.getInodeOfClassByCondition(c, "working = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue() +
97                 " and identifier = " + inode.getInode());
98         if(ret.getInode() > 0) {
99             return ret;
100         }
101         return InodeFactory.getChildOfClassbyCondition(inode, c, "working = "
102                 + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
103     }
104
105     public static Object JavaDoc getWorkingChildOfClassByCondition(Inode inode, Class JavaDoc c, String JavaDoc condition) {
106         condition += " and working = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue();
107         return InodeFactory.getChildOfClassbyCondition(inode, c, condition);
108     }
109
110     public static java.util.List JavaDoc getWorkingOfClass(Class JavaDoc c) {
111         try {
112             DotHibernate dh = new DotHibernate(c);
113             dh.setQuery("from inode in class " + c.getName() + " where working = "
114                     + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
115
116             return dh.list();
117         } catch (Exception JavaDoc e) {
118             Logger.warn(IdentifierFactory.class, "getWorkingOfClass failed:" + e, e);
119         }
120         return new java.util.ArrayList JavaDoc();
121     }
122
123     public static java.util.List JavaDoc getLiveOfClass(Class JavaDoc c) {
124         try {
125             DotHibernate dh = new DotHibernate(c);
126             dh.setQuery("from inode in class " + c.getName() + " where live = "
127                     + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
128
129             return dh.list();
130         } catch (Exception JavaDoc e) {
131             Logger.warn(IdentifierFactory.class, "getWorkingOfClass failed:" + e, e);
132         }
133         return new java.util.ArrayList JavaDoc();
134     }
135
136     public static void updateIdentifierURI(WebAsset webasset, Folder folder) {
137
138         Identifier identifier = getIdentifierByInode(webasset);
139
140         if (webasset instanceof HTMLPage) {
141             identifier.setURI(folder.getPath() + ((HTMLPage) webasset).getPageUrl());
142         } else if (webasset instanceof File) {
143             identifier.setURI(folder.getPath() + ((File) webasset).getFileName());
144         } else if (webasset instanceof Link) {
145             identifier.setURI(folder.getPath() + ((Link) webasset).getProtocal() + ((Link) webasset).getUrl());
146         }
147
148         else {
149             identifier.setURI(folder.getPath() + identifier.getInode());
150         }
151
152         InodeFactory.saveInode(identifier);
153
154     }
155
156     public static java.util.List JavaDoc getVersionsandLiveandWorkingChildrenOfClass(Inode o, Class JavaDoc c) {
157         return InodeFactory.getChildrenClassByConditionAndOrderBy(o, c, " 1 = 1 ", "mod_date desc");
158     }
159
160     public static java.util.List JavaDoc getVersionsandLiveChildrenOfClass(Inode o, Class JavaDoc c) {
161         return InodeFactory.getChildrenClassByConditionAndOrderBy(o, c, "working <> "
162                 + com.dotmarketing.db.DbConnectionFactory.getDBTrue(), "mod_date desc ");
163     }
164
165     public static java.util.List JavaDoc getVersionsandLiveChildrenOfClass(Inode o, Class JavaDoc c, String JavaDoc orderBy) {
166
167         return InodeFactory.getChildrenClassByConditionAndOrderBy(o, c, "working <> "
168                 + com.dotmarketing.db.DbConnectionFactory.getDBTrue(), orderBy);
169
170     }
171
172     public static java.util.List JavaDoc getVersionsChildrenOfClass(Inode o, Class JavaDoc c) {
173
174         return InodeFactory.getChildrenClassByCondition(o, c, "live <> " + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
175
176     }
177
178     public static java.util.List JavaDoc getVersionsChildrenOfClass(Inode o, Class JavaDoc c, String JavaDoc orderBy) {
179
180         return InodeFactory.getChildrenClassByConditionAndOrderBy(o, c, "live <> "
181                 + com.dotmarketing.db.DbConnectionFactory.getDBTrue(), orderBy);
182
183     }
184
185     public static Identifier getIdentifierByURI(String JavaDoc uri, Host host) {
186         return getIdentifierByURI(uri, host.getInode());
187     }
188
189     public static Identifier getIdentifierByURI(String JavaDoc uri, long hostId) {
190         DotHibernate dh = new DotHibernate(Identifier.class);
191         dh.setQuery("from inode in class com.dotmarketing.beans.Identifier where uri = ? and host_inode = ?");
192         dh.setParam(uri);
193         dh.setParam(hostId);
194         return (Identifier) dh.load();
195
196     }
197
198     
199     public static Identifier getParentIdentifier(WebAsset webasset) {
200
201         return (Identifier) getIdentifierByInode(webasset);
202
203     }
204
205     /**
206      * Gets the parent Identifer by the inode.
207      * @deprecated All web assets have their identifiers set Webasset.getIdentifier(). Use only if you need to get the identifier uri,
208      * @param inode This takes an inode and finds the identifier for it.
209      * @return the Identifier inode
210      */

211     public static Identifier getIdentifierByInode(Inode inode) {
212         if (inode.getInode() == 0) {
213             return new Identifier();
214         }
215
216         Identifier id = new Identifier ();
217
218         if (inode.getIdentifier() > 0) {
219             id = (Identifier) InodeFactory.getInode(inode.getIdentifier(), Identifier.class);
220         }
221         else {
222             id = (Identifier) InodeFactory.getParentOfClass(inode, Identifier.class);
223         }
224
225         /* save it for future reference */
226         if (id.getInode() > 0 && inode.getIdentifier() == 0) {
227             inode.setIdentifier(id.getInode());
228         }
229         return id;
230     }
231
232     public static Identifier getIdentifierByWebAsset(WebAsset webAsset) {
233         return getIdentifierByInode((Inode) webAsset);
234     }
235
236     public static Identifier createNewIdentifier(WebAsset webasset, Folder folder) {
237
238         Identifier identifier = new Identifier();
239
240         // get an inode #
241

242         if (webasset.getInode() > 0) {
243             Logger.debug(IdentifierFactory.class, "createNewIdentifier Identifier=" + identifier.getInode());
244
245             identifier.setURI(webasset.getURI(folder));
246
247             Host host = HostFactory.getParentHost(folder);
248
249             identifier.setHostInode(host.getInode());
250
251             Logger.debug(IdentifierFactory.class, "createNewIdentifier for asset=" + identifier.getURI());
252
253             InodeFactory.saveInode(identifier);
254             identifier.addChild(webasset);
255
256             // set the identifier on the inode for future reference.
257
// and for when we get rid of identifiers all together
258
InodeFactory.saveInode(identifier);
259
260             Logger.debug(IdentifierFactory.class, "createNewIdentifier Web Asset=" + webasset.getInode());
261
262             InodeFactory.saveInode(identifier);
263             InodeFactory.saveInode(webasset);
264             DotHibernate.flush();
265             IdentifierCache.addAssetToIdentifierCache(webasset);
266         }
267
268         return identifier;
269     }
270
271     public static Identifier createNewIdentifier(WebAsset webasset) {
272
273         Identifier identifier = new Identifier();
274         identifier.setURI(webasset.getType() + "." + webasset.getInode());
275
276         // get an inode #
277
Logger.debug(IdentifierFactory.class, "createNewIdentifier for asset=" + webasset.getInode());
278
279         // set the identifier on the inode for future reference.
280
// and for when we get rid of identifiers all together
281
InodeFactory.saveInode(identifier);
282         identifier.addChild(webasset);
283         webasset.setIdentifier(identifier.getInode());
284
285         Logger.debug(IdentifierFactory.class, "createNewIdentifier Web Asset=" + webasset.getInode());
286
287         InodeFactory.saveInode(identifier);
288         InodeFactory.saveInode(webasset);
289         DotHibernate.flush();
290         IdentifierCache.addAssetToIdentifierCache(webasset);
291
292         return identifier;
293     }
294
295     public static List JavaDoc getIdentifiersPerConditionWithPermission(Host host, String JavaDoc condition, Class JavaDoc c, Role[] roles,
296             int limit, int offset, String JavaDoc orderby) {
297         return getIdentifiersPerConditionWithPermission(host.getInode(), condition, c, roles, limit, offset, orderby);
298     }
299
300     public static List JavaDoc getIdentifiersPerConditionWithPermission(long hostId, String JavaDoc condition, Class JavaDoc c, Role[] roles,
301             int limit, int offset, String JavaDoc orderby) {
302
303         DotHibernate dh = new DotHibernate(Identifier.class);
304
305         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
306         try {
307
308             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
309
310             String JavaDoc rolesStr = "";
311
312             for (int i = 0; i < roles.length; i++) {
313                 rolesStr += roles[i].getRoleId();
314                 if (i != (roles.length - 1)) {
315                     rolesStr += ",";
316                 }
317             }
318             sb
319                     .append("select {identifier.*} from identifier, inode identifier_1_ where identifier.inode = identifier_1_.inode and identifier.inode in ( ");
320             sb.append("select distinct identifier.inode from identifier identifier, ");
321             sb.append("tree tree, " + tableName + " " + tableName + "_condition, permission permission ");
322             sb.append("where " + condition);
323             sb.append("and identifier.inode = tree.parent ");
324             sb.append("and tree.child = " + tableName + "_condition.inode ");
325             sb.append("and identifier.inode = permission.inode_id ");
326             sb.append("and permission.roleid in (" + rolesStr + ") ");
327             sb.append("and permission.permission = " + Config.getStringProperty("PERMISSION_READ") + ") ");
328             sb.append("and identifier.host_inode = " + hostId + " ");
329
330             if (limit != 0) {
331                 dh.setFirstResult(offset);
332                 dh.setMaxResults(limit);
333             }
334             Logger.debug(IdentifierFactory.class, sb.toString());
335
336             dh.setSQLQuery(sb.toString());
337             return dh.list();
338
339         } catch (Exception JavaDoc e) {
340             Logger.warn(IdentifierFactory.class, "getIdentifiersPerConditionWithPermission failed:" + e, e);
341         }
342
343         return new ArrayList JavaDoc();
344     }
345
346     public static List JavaDoc getIdentifiersPerConditionWithPermission(String JavaDoc condition, Class JavaDoc c, Role[] roles, int limit,
347             int offset, String JavaDoc orderby) {
348
349         DotHibernate dh = new DotHibernate(Identifier.class);
350
351         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
352         try {
353
354             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
355
356             String JavaDoc rolesStr = "";
357
358             for (int i = 0; i < roles.length; i++) {
359                 rolesStr += roles[i].getRoleId();
360                 if (i != (roles.length - 1)) {
361                     rolesStr += ",";
362                 }
363             }
364             sb
365                     .append("select {identifier.*} from identifier, inode identifier_1_ where identifier.inode = identifier_1_.inode and identifier.inode in ( ");
366             sb.append("select distinct identifier.inode from identifier identifier, ");
367             sb.append("tree tree, " + tableName + " " + tableName + "_condition, permission permission ");
368             sb.append("where " + condition);
369             sb.append("and identifier.inode = tree.parent ");
370             sb.append("and tree.child = " + tableName + "_condition.inode ");
371             sb.append("and identifier.inode = permission.inode_id ");
372             sb.append("and permission.roleid in (" + rolesStr + ") ");
373             sb.append("and permission.permission = " + Config.getStringProperty("PERMISSION_READ") + ") ");
374
375             if (limit != 0) {
376                 dh.setFirstResult(offset);
377                 dh.setMaxResults(limit);
378             }
379             Logger.debug(IdentifierFactory.class, sb.toString());
380
381             dh.setSQLQuery(sb.toString());
382             return dh.list();
383
384         } catch (Exception JavaDoc e) {
385             Logger.warn(IdentifierFactory.class, "getIdentifiersPerConditionWithPermission failed:" + e, e);
386         }
387
388         return new ArrayList JavaDoc();
389     }
390
391     public static int getCountIdentifiersPerConditionWithPermission(Host host, String JavaDoc condition, Class JavaDoc c, Role[] roles) {
392         return getCountIdentifiersPerConditionWithPermission(host.getInode(), condition, c, roles);
393     }
394
395     public static int getCountIdentifiersPerConditionWithPermission(long hostId, String JavaDoc condition, Class JavaDoc c, Role[] roles) {
396         DotConnect db = new DotConnect();
397
398         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
399         try {
400
401             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
402
403             String JavaDoc rolesStr = "";
404
405             for (int i = 0; i < roles.length; i++) {
406                 rolesStr += roles[i].getRoleId();
407                 if (i != (roles.length - 1)) {
408                     rolesStr += ",";
409                 }
410             }
411             sb.append("select count(distinct identifier.inode) mycount from identifier identifier, ");
412             sb.append("tree tree, " + tableName + " " + tableName + "_condition, permission permission ");
413             sb.append("where " + condition);
414             sb.append("and identifier.inode = tree.parent ");
415             sb.append("and tree.child = " + tableName + "_condition.inode ");
416             sb.append("and identifier.inode = permission.inode_id ");
417             sb.append("and permission.roleid in (" + rolesStr + ") ");
418             sb.append("and permission.permission = " + Config.getStringProperty("PERMISSION_READ") + " ");
419             sb.append("and identifier.host_inode = " + hostId + " ");
420
421             Logger.debug(IdentifierFactory.class, sb.toString());
422
423             db.setSQL(sb.toString());
424             return db.getInt("mycount");
425
426         } catch (Exception JavaDoc e) {
427             Logger.warn(IdentifierFactory.class, "getIdentifiersPerConditionWithPermission failed:" + e, e);
428         }
429
430         return 0;
431     }
432
433     public static int getCountIdentifiersPerConditionWithPermission(String JavaDoc condition, Class JavaDoc c, Role[] roles) {
434
435         DotConnect db = new DotConnect();
436
437         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
438         try {
439
440             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
441
442             String JavaDoc rolesStr = "";
443
444             for (int i = 0; i < roles.length; i++) {
445                 rolesStr += roles[i].getRoleId();
446                 if (i != (roles.length - 1)) {
447                     rolesStr += ",";
448                 }
449             }
450             sb.append("select count(distinct identifier.inode) as mycount from identifier identifier, ");
451             sb.append("tree tree, " + tableName + " " + tableName + "_condition, permission permission ");
452             sb.append("where " + condition);
453             sb.append("and identifier.inode = tree.parent ");
454             sb.append("and tree.child = " + tableName + "_condition.inode ");
455             sb.append("and identifier.inode = permission.inode_id ");
456             sb.append("and permission.roleid in (" + rolesStr + ") ");
457             sb.append("and permission.permission = " + Config.getStringProperty("PERMISSION_READ"));
458
459             Logger.debug(IdentifierFactory.class, sb.toString());
460
461             db.setSQL(sb.toString());
462             return db.getInt("mycount");
463
464         } catch (Exception JavaDoc e) {
465             Logger.warn(IdentifierFactory.class, "getIdentifiersPerConditionWithPermission failed:" + e, e);
466         }
467
468         return 0;
469     }
470     
471     public static Identifier getIdentifierByInodeNoLock(Inode inode) {
472         return getIdentifierByInodeNoLock (inode, true);
473     }
474
475     public static Identifier getIdentifierByInodeNoLock(Inode inode, boolean create) {
476         DotHibernate dh = new DotHibernate(Identifier.class);
477         if (inode.getInode() == 0) {
478             return new Identifier();
479         }
480
481         Identifier id = null;
482         
483         StringBuffer JavaDoc querie = new StringBuffer JavaDoc();
484         if (inode.getIdentifier() > 0) {
485             querie.append("Select {identifier.*} from identifier with (nolock), inode identifier_1_ with (nolock)");
486             querie.append(" where identifier.inode = identifier_1_.inode and identifier.inode="+inode.getIdentifier());
487             
488             dh.setSQLQuery(querie.toString());
489             id = (Identifier) dh.load();
490             
491         } else {
492             Logger.debug(IdentifierFactory.class, "getIdentifierByInode: " + inode.getInode());
493             querie.append("Select {identifier.*} from identifier with (nolock), inode identifier_1_ with (nolock), tree with (nolock)");
494             querie.append(" where tree.child = "+inode.getInode()+" and tree.parent = identifier.inode and identifier.inode = identifier_1_.inode");
495             
496             dh.setSQLQuery(querie.toString());
497             id = (Identifier) dh.list().get(0);
498         }
499
500         /* save it for future reference */
501         if (id.getInode() > 0 && inode.getIdentifier() == 0) {
502             inode.setIdentifier(id.getInode());
503         }
504         return id;
505     }
506
507 }
508
Popular Tags