KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.dotmarketing.factories;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import org.apache.commons.beanutils.PropertyUtils;
11
12 import com.dotmarketing.beans.Host;
13 import com.dotmarketing.beans.Identifier;
14 import com.dotmarketing.beans.Inode;
15 import com.dotmarketing.beans.PermissionAsset;
16 import com.dotmarketing.beans.WebAsset;
17 import com.dotmarketing.cache.LiveCache;
18 import com.dotmarketing.cache.PermissionCache;
19 import com.dotmarketing.cms.factories.PublicRoleFactory;
20 import com.dotmarketing.db.DotConnect;
21 import com.dotmarketing.db.DotHibernate;
22 import com.dotmarketing.menubuilders.RefreshMenus;
23 import com.dotmarketing.portlets.containers.model.Container;
24 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory;
25 import com.dotmarketing.portlets.contentlet.model.Contentlet;
26 import com.dotmarketing.portlets.folders.model.Folder;
27 import com.dotmarketing.portlets.htmlpages.model.HTMLPage;
28 import com.dotmarketing.portlets.templates.model.Template;
29 import com.dotmarketing.services.ContainerServices;
30 import com.dotmarketing.services.ContentletMapServices;
31 import com.dotmarketing.services.ContentletServices;
32 import com.dotmarketing.services.PageServices;
33 import com.dotmarketing.services.TemplateServices;
34 import com.dotmarketing.util.Config;
35 import com.dotmarketing.util.Logger;
36 import com.dotmarketing.util.WebKeys;
37 import com.liferay.portal.model.Role;
38 import com.liferay.portal.model.User;
39 import com.liferay.portal.struts.ActionException;
40
41 /**
42  *
43  * @author maria, david(2005)
44  */

45 public class WebAssetFactory {
46
47     public static void createAsset(WebAsset webasset, String JavaDoc userId, Inode parent) {
48
49         webasset.setWorking(true);
50         webasset.setLive(false);
51         webasset.setDeleted(false);
52         webasset.setLocked(false);
53         webasset.setModDate(new java.util.Date JavaDoc());
54         webasset.setModUser(userId);
55         // persists the webasset
56
InodeFactory.saveInode(webasset);
57
58         // adds the webasset as child of the folder or parent inode
59
parent.addChild(webasset);
60
61         // create new identifier, with the URI
62
Identifier id = IdentifierFactory.createNewIdentifier(webasset, (Folder) parent);
63         
64         // set the identifier on the inode for future reference.
65
// and for when we get rid of identifiers all together
66
InodeFactory.saveInode(id);
67
68     }
69
70     public static void createAsset(WebAsset webasset, String JavaDoc userId) {
71
72         webasset.setWorking(true);
73         webasset.setLive(false);
74         webasset.setDeleted(false);
75         webasset.setLocked(false);
76         webasset.setModDate(new java.util.Date JavaDoc());
77         webasset.setModUser(userId);
78         // persists the webasset
79
InodeFactory.saveInode(webasset);
80
81         // create new identifier, without URI
82
Identifier id = IdentifierFactory.createNewIdentifier(webasset);
83
84         // set the identifier on the inode for future reference.
85
// and for when we get rid of identifiers all together
86
webasset.setIdentifier(id.getInode());
87         
88         InodeFactory.saveInode(id);
89         InodeFactory.saveInode(webasset);
90     }
91
92     public static void createAsset(WebAsset webasset, String JavaDoc userId, Inode parent, Identifier identifier) {
93
94         webasset.setWorking(true);
95         webasset.setLive(false);
96         webasset.setDeleted(false);
97         webasset.setLocked(false);
98         webasset.setModDate(new java.util.Date JavaDoc());
99         webasset.setModUser(userId);
100         
101         // set the identifier on the inode for future reference.
102
// and for when we get rid of identifiers all together
103
webasset.setIdentifier(identifier.getInode());
104         
105         
106         // persists the webasset
107
InodeFactory.saveInode(webasset);
108
109         // adds the webasset as child of the folder or parent inode
110
parent.addChild(webasset);
111
112         // adds asset to the existing identifier
113
identifier.addChild(webasset);
114         
115
116     }
117
118     public static void createAsset(WebAsset webasset, String JavaDoc userId, Identifier identifier) {
119
120         webasset.setWorking(true);
121         webasset.setLive(false);
122         webasset.setDeleted(false);
123         webasset.setLocked(false);
124         webasset.setModDate(new java.util.Date JavaDoc());
125         webasset.setModUser(userId);
126         
127         // set the identifier on the inode for future reference.
128
// and for when we get rid of identifiers all together
129
webasset.setIdentifier(identifier.getInode());
130         
131         
132         // persists the webasset
133
InodeFactory.saveInode(webasset);
134
135         // adds asset to the existing identifier
136
identifier.addChild(webasset);
137
138
139     }
140
141     public static void createAsset(WebAsset webasset, String JavaDoc userId, Inode parent, Identifier identifier,
142             boolean working) {
143         webasset.setWorking(working);
144         webasset.setLive(false);
145         webasset.setDeleted(false);
146         webasset.setLocked(false);
147         webasset.setModDate(new java.util.Date JavaDoc());
148         webasset.setModUser(userId);
149         // persists the webasset
150
InodeFactory.saveInode(webasset);
151
152         // adds the webasset as child of the folder or parent inode
153
parent.addChild(webasset);
154
155         // adds asset to the existing identifier
156
identifier.addChild(webasset);
157         webasset.addParent(identifier);
158         webasset.setIdentifier(identifier.getInode());
159
160         InodeFactory.saveInode(webasset);
161     }
162
163     public static void createAsset(WebAsset webasset, String JavaDoc userId, Identifier identifier, boolean working) {
164         webasset.setWorking(working);
165         webasset.setLive(false);
166         webasset.setDeleted(false);
167         webasset.setLocked(false);
168         webasset.setModDate(new java.util.Date JavaDoc());
169         webasset.setModUser(userId);
170         // persists the webasset
171
InodeFactory.saveInode(webasset);
172
173         // adds asset to the existing identifier
174
identifier.addChild(webasset);
175         webasset.addParent(identifier);
176         webasset.setIdentifier(identifier.getInode());
177
178         InodeFactory.saveInode(webasset);
179     }
180
181     public static WebAsset getParentWebAsset(Inode i) {
182         DotHibernate dh = new DotHibernate(WebAsset.class);
183         dh.setQuery("from inode in class " + WebAsset.class.getName() + " where ? in inode.children.elements");
184         dh.setParam(i.getInode());
185         return (WebAsset) dh.load();
186     }
187
188
189
190     public static void renameAsset(WebAsset webasset) {
191         java.util.List JavaDoc versions = getAssetVersionsandLive(webasset);
192         java.util.Iterator JavaDoc versIter = versions.iterator();
193         while (versIter.hasNext()) {
194             WebAsset currWebAsset = (WebAsset) versIter.next();
195             currWebAsset.setFriendlyName(webasset.getFriendlyName());
196         }
197     }
198
199     public static boolean editAsset(WebAsset currWebAsset, String JavaDoc userId) {
200
201         // gets the identifier for this asset
202
Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset);
203         WebAsset workingwebasset = null;
204
205         if (currWebAsset instanceof Contentlet) {
206             // gets the current working asset
207
String JavaDoc condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId();
208             workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset
209                     .getClass(), condition);
210         } else {
211             // gets the current working asset
212
workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass());
213         }
214
215         if (!workingwebasset.isLocked()) {
216             // sets lock true
217
workingwebasset.setLocked(true);
218             workingwebasset.setModUser(userId);
219             // persists the webasset
220
InodeFactory.saveInode(workingwebasset);
221             return true;
222         }
223         if ((workingwebasset.isLocked()) && (workingwebasset.getModUser().equals(userId))) {
224             return true;
225         }
226         return false;
227     }
228
229     public static WebAsset getBackAssetVersion(WebAsset versionWebAsset) throws Exception JavaDoc {
230         Identifier id = (Identifier) IdentifierFactory.getParentIdentifier(versionWebAsset);
231         if (id.getInode() == 0) {
232             throw new Exception JavaDoc("Web asset Identifier not found!");
233         }
234         WebAsset working = (WebAsset) IdentifierFactory.getWorkingChildOfClass(id, versionWebAsset.getClass());
235         if (working.getInode() == 0) {
236             throw new Exception JavaDoc("Working copy not found!");
237         }
238         return swapAssets(working, versionWebAsset);
239     }
240
241     /**
242      * This method is odd. You send it an asset, but that may not be the one
243      * that get published. The method will get the identifer of the asset you
244      * send it and find the working version of the asset and make that the live
245      * version.
246      *
247      * @param currWebAsset
248      * This asset's identifier will be used to find the "working"
249      * asset.
250      * @return This method returns the OLD live asset or null. Wierd.
251      */

252     @SuppressWarnings JavaDoc("unchecked")
253     public static WebAsset publishAsset(WebAsset currWebAsset) {
254
255         Logger.debug(WebAssetFactory.class, "Publishing asset!!!!");
256         // gets the identifier for this asset
257
Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset);
258         // gets the current working asset
259

260         WebAsset workingwebasset = null;
261
262         if (currWebAsset instanceof Contentlet) {
263             // gets the current working asset
264
String JavaDoc condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId();
265             workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset
266                     .getClass(), condition);
267         } else {
268             // gets the current working asset
269
workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass());
270         }
271
272         if (workingwebasset.getInode() == 0) {
273             workingwebasset = currWebAsset;
274         }
275
276         Logger.debug(WebAssetFactory.class, "workingwebasset=" + workingwebasset.getInode());
277
278         List JavaDoc<WebAsset> livewebassets = new ArrayList JavaDoc<WebAsset>();
279
280         try {
281             if (currWebAsset instanceof Contentlet) {
282                 // gets the current working asset
283
String JavaDoc condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId();
284                 livewebassets = (List JavaDoc<WebAsset>) IdentifierFactory.getLiveChildrenOfClass(identifier, currWebAsset.getClass(), condition);
285             } else {
286                 // gets the current working asset
287
livewebassets = (List JavaDoc<WebAsset>) IdentifierFactory.getLiveChildrenOfClass(identifier, currWebAsset.getClass());
288             }
289         } catch (Exception JavaDoc e) {
290         }
291
292         // sets new working to live
293
workingwebasset.setLive(true);
294         workingwebasset.setModDate(new java.util.Date JavaDoc());
295         // persists the webasset
296
InodeFactory.saveInode(workingwebasset);
297         Logger.debug(WebAssetFactory.class, "InodeFactory.saveInode(workingwebasset)");
298         
299         for (WebAsset livewebasset : livewebassets)
300             if ((livewebasset != null) && (livewebasset.getInode() > 0)
301                     && (livewebasset.getInode() != workingwebasset.getInode())) {
302     
303                 Logger.debug(WebAssetFactory.class, "livewebasset.getInode()=" + livewebasset.getInode());
304                 // sets previous live to false
305
livewebasset.setLive(false);
306                 livewebasset.setModDate(new java.util.Date JavaDoc());
307                 // removes from the folder
308
java.util.List JavaDoc parents = InodeFactory.getParentsOfClass(livewebasset, Folder.class);
309                 java.util.Iterator JavaDoc parentsIter = parents.iterator();
310                 while (parentsIter.hasNext()) {
311                     Inode inode = (Inode) parentsIter.next();
312                     inode.deleteChild(livewebasset);
313                 }
314                 identifier.addChild(livewebasset);
315     
316                 // persists it
317
InodeFactory.saveInode(livewebasset);
318             }
319         return livewebassets.size()>0?livewebassets.get(0):null;
320     }
321
322     public static WebAsset getLiveAsset(WebAsset currWebAsset) throws Exception JavaDoc {
323
324         Logger.debug(WebAssetFactory.class, "Publishing asset!!!!");
325         // gets the identifier for this asset
326
Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset);
327
328         WebAsset livewebasset = null;
329
330         if (currWebAsset instanceof Contentlet) {
331             // gets the current working asset
332
String JavaDoc condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId();
333             livewebasset = (WebAsset) IdentifierFactory.getLiveChildOfClassByCondition(identifier, currWebAsset
334                     .getClass(), condition);
335         } else {
336             // gets the current working asset
337
livewebasset = (WebAsset) IdentifierFactory.getLiveChildOfClass(identifier, currWebAsset.getClass());
338         }
339
340         return livewebasset;
341     }
342
343     public static boolean deleteAsset(WebAsset currWebAsset, String JavaDoc userId) {
344
345         // gets the identifier for this asset
346
Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset);
347
348         WebAsset workingwebasset = null;
349
350         if (currWebAsset instanceof Contentlet) {
351             // gets the current working asset
352
String JavaDoc condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId();
353             workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset
354                     .getClass(), condition);
355         } else {
356             // gets the current working asset
357
workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass());
358         }
359
360         WebAsset live = (WebAsset) IdentifierFactory.getLiveChildOfClass(identifier, currWebAsset.getClass());
361
362         if ((!workingwebasset.isLocked() || workingwebasset.getModUser().equals(userId))) {
363
364             if (live.getInode() > 0) {
365                 live.setLive(false);
366                 live.setModDate(new Date JavaDoc ());
367                 InodeFactory.saveInode(live);
368             }
369             
370             //Reset the mod date
371
workingwebasset.setModDate(new Date JavaDoc ());
372             // sets deleted to true
373
workingwebasset.setDeleted(true);
374             // persists the webasset
375
InodeFactory.saveInode(workingwebasset);
376             
377             if (workingwebasset instanceof Contentlet) {
378                 // Upodating lucene index
379
ContentletFactory.reIndexContentlet((Contentlet) workingwebasset);
380             }
381             
382             return true;
383         }
384         return false;
385     }
386
387     public static boolean deleteAssetVersion(WebAsset currWebAsset) {
388
389         if (!currWebAsset.isLive() && !currWebAsset.isWorking()) {
390             // it's a version so delete from database
391
InodeFactory.deleteInode(currWebAsset);
392             return true;
393         }
394         return false;
395
396     }
397
398     public static void unLockAsset(WebAsset currWebAsset) {
399
400         // gets the identifier for this asset
401
Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset);
402         WebAsset workingwebasset = null;
403
404         if (currWebAsset instanceof Contentlet) {
405             // gets the current working asset
406
String JavaDoc condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId();
407             workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset
408                     .getClass(), condition);
409         } else {
410             // gets the current working asset
411
workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass());
412         }
413         // unlocks current working asset
414
workingwebasset.setLocked(false);
415
416         // persists the webasset
417
InodeFactory.saveInode(workingwebasset);
418         
419         if (currWebAsset instanceof Contentlet) {
420             // Upodating lucene index
421
ContentletFactory.reIndexContentlet((Contentlet) currWebAsset);
422         }
423         
424     }
425
426     public static void unDeleteAsset(WebAsset currWebAsset) {
427
428         // gets the identifier for this asset
429
Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset);
430         // gets the current working asset
431
WebAsset workingwebasset = null;
432
433         if (currWebAsset instanceof Contentlet) {
434             // gets the current working asset
435
String JavaDoc condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId();
436             workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset
437                     .getClass(), condition);
438         } else {
439             // gets the current working asset
440
workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass());
441         }
442
443         WebAsset livewebasset = null;
444         try {
445             if (currWebAsset instanceof Contentlet) {
446                 // gets the current working asset
447
String JavaDoc condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId();
448                 livewebasset = (WebAsset) IdentifierFactory.getLiveChildOfClassByCondition(identifier, currWebAsset
449                         .getClass(), condition);
450             } else {
451                 // gets the current working asset
452
livewebasset = (WebAsset) IdentifierFactory.getLiveChildOfClass(identifier, currWebAsset.getClass());
453             }
454         } catch (Exception JavaDoc e) {
455         }
456
457         // sets deleted to true
458
workingwebasset.setDeleted(false);
459         // persists the webasset
460
InodeFactory.saveInode(workingwebasset);
461
462         if ((livewebasset != null) && (livewebasset.getInode() > 0)
463                 && (livewebasset.getInode() != workingwebasset.getInode())) {
464
465             // sets previous live to false
466
livewebasset.setDeleted(false);
467             // persists it
468
InodeFactory.saveInode(livewebasset);
469         }
470         
471         if (workingwebasset instanceof Contentlet) {
472             // Upodating lucene index
473
ContentletFactory.reIndexContentlet((Contentlet) workingwebasset);
474         }
475         
476         
477     }
478
479     public static boolean unPublishAsset(WebAsset currWebAsset, String JavaDoc userId, Inode parent) {
480
481         // gets the identifier for this asset
482
Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset);
483
484         WebAsset workingwebasset = null;
485
486         if (currWebAsset instanceof Contentlet) {
487             // gets the current working asset
488
String JavaDoc condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId();
489             workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset
490                     .getClass(), condition);
491         } else {
492             // gets the current working asset
493
workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass());
494         }
495
496         WebAsset livewebasset = null;
497
498         if (!workingwebasset.isLocked() || workingwebasset.getModUser().equals(userId)) {
499             try {
500                 // gets the current working asset
501
if (currWebAsset instanceof Contentlet) {
502                     // gets the current working asset
503
String JavaDoc condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId();
504                     livewebasset = (WebAsset) IdentifierFactory.getLiveChildOfClassByCondition(identifier, currWebAsset
505                             .getClass(), condition);
506                 } else {
507                     // gets the current working asset
508
livewebasset = (WebAsset) IdentifierFactory
509                             .getLiveChildOfClass(identifier, currWebAsset.getClass());
510                 }
511
512                 livewebasset.setLive(false);
513                 livewebasset.setWorking(true);
514                 livewebasset.setModDate(new java.util.Date JavaDoc());
515                 livewebasset.setModUser(userId);
516                 InodeFactory.saveInode(livewebasset);
517
518                 if ((livewebasset.getInode() != workingwebasset.getInode())) {
519
520                     // sets previous working to false
521
workingwebasset.setWorking(false);
522                     workingwebasset.setLocked(false);
523                     // persists it
524
InodeFactory.saveInode(workingwebasset);
525                     // removes from folder or parent inode
526
parent.deleteChild(workingwebasset);
527                 }
528                 
529                 if (currWebAsset instanceof HTMLPage) {
530                     //remove page from the live directory
531
PageServices.unpublishPageFile((HTMLPage)currWebAsset);
532                     
533                     //Refreshing the menues
534
RefreshMenus.deleteMenus();
535
536                 } else if (currWebAsset instanceof Container) {
537                     //remove container from the live directory
538
ContainerServices.unpublishContainerFile((Container)currWebAsset);
539                 } else if (currWebAsset instanceof Contentlet) {
540                     //remove contentlet from the live directory
541
ContentletServices.unpublishContentletFile((Contentlet)currWebAsset);
542                     //writes the contentlet object to a file
543
ContentletMapServices.unpublishContentletMapFile((Contentlet)currWebAsset);
544                 } else if (currWebAsset instanceof Template) {
545                     //remove template from the live directory
546
TemplateServices.unpublishTemplateFile((Template)currWebAsset);
547                 }
548
549                 LiveCache.removeAssetFromCache(currWebAsset);
550
551                 if (currWebAsset instanceof Contentlet) {
552                     // Upodating lucene index
553
ContentletFactory.reIndexContentlet((Contentlet) currWebAsset);
554                 }
555                 
556                 return true;
557             } catch (Exception JavaDoc e) {
558                 return false;
559             }
560         }
561         return false;
562     }
563
564     // TO-DO
565
// Do this one with a language condition...
566
public static java.util.List JavaDoc getAssetVersions(WebAsset currWebAsset) {
567         // gets the identifier for this asset
568
if (currWebAsset.isWorking()) {
569             Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset);
570             return IdentifierFactory.getVersionsChildrenOfClass(identifier, currWebAsset.getClass());
571         }
572         return new java.util.ArrayList JavaDoc();
573     }
574
575     /*
576      * public static java.util.List getWorkingAssetsOfClass(Class c) { return
577      * IdentifierFactory.getLiveOfClass(c); }
578      */

579
580     // TO-DO
581
// Do this one with a language condition.
582
public static java.util.List JavaDoc getAssetVersionsandLive(WebAsset currWebAsset) {
583         // gets the identifier for this asset
584
if (currWebAsset.isWorking()) {
585             Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset);
586             return IdentifierFactory.getVersionsandLiveChildrenOfClass(identifier, currWebAsset.getClass());
587         }
588         return new java.util.ArrayList JavaDoc();
589     }
590
591     // Do this one with a language condition.
592
public static java.util.List JavaDoc getAssetVersionsandLiveandWorking(WebAsset currWebAsset) {
593         // gets the identifier for this asset
594
if (currWebAsset.isWorking()) {
595             Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset);
596             return IdentifierFactory.getVersionsandLiveandWorkingChildrenOfClass(identifier, currWebAsset.getClass());
597         }
598         return new java.util.ArrayList JavaDoc();
599     }
600
601     // Swap assets properties and tree relationships to convert the newAsset
602
// into the workingAsset
603
// This method don´t swap the multitree relationships and correctly set the
604
// working/live and parent folder
605
// relationships and properties
606
private static WebAsset swapAssets(WebAsset workingAsset, WebAsset newAsset) throws Exception JavaDoc {
607         Folder parentFolder = null;
608         if (!isAbstractAsset(workingAsset)){
609             parentFolder = (Folder) InodeFactory.getParentOfClass(workingAsset, Folder.class);
610         }
611         Identifier identifier = (Identifier) IdentifierFactory.getIdentifierByInode(workingAsset);
612
613         // Retrieving assets properties excluding (inode, children, parents and
614
// parent)
615
Map JavaDoc workingAssetProps = PropertyUtils.describe(workingAsset);
616         workingAssetProps.remove("class");
617         workingAssetProps.remove("inode");
618         workingAssetProps.remove("children");
619         workingAssetProps.remove("parents");
620         workingAssetProps.remove("parent");
621         
622         Map JavaDoc newAssetProps = PropertyUtils.describe(newAsset);
623         newAssetProps.remove("class");
624         newAssetProps.remove("inode");
625         newAssetProps.remove("children");
626         newAssetProps.remove("parents");
627         newAssetProps.remove("parent");
628
629
630         
631         
632         
633         // Swaping props
634
Iterator JavaDoc keys = workingAssetProps.keySet().iterator();
635         while (keys.hasNext()) {
636             try {
637                 String JavaDoc key = (String JavaDoc) keys.next();
638                 Object JavaDoc x = workingAssetProps.get(key);
639                 if(x != null){
640                     PropertyUtils.setProperty(newAsset, key, x);
641                 }
642             } catch (NoSuchMethodException JavaDoc e) {
643             } catch (InvocationTargetException JavaDoc e) {
644             }
645
646         }
647         keys = newAssetProps.keySet().iterator();
648         while (keys.hasNext()) {
649             try {
650                 String JavaDoc key = (String JavaDoc) keys.next();
651                 Object JavaDoc x = newAssetProps.get(key);
652                 if(x!=null){
653                     PropertyUtils.setProperty(workingAsset, key, x);
654                 }
655             } catch (NoSuchMethodException JavaDoc e) {
656             } catch (InvocationTargetException JavaDoc e) {
657             }
658         }
659
660         // Setting working/live/locked/date/user properties
661
workingAsset.setWorking(true);
662         workingAsset.setLive(false);
663         workingAsset.setDeleted(false);
664         workingAsset.setLocked(false);
665         workingAsset.setModDate(new java.util.Date JavaDoc());
666
667         newAsset.setWorking(false);
668         newAsset.setLocked(false);
669
670         if (!isAbstractAsset(workingAsset)) {
671             // Removing the folder for the new version
672
parentFolder.deleteChild(newAsset);
673         }
674
675         // Saving changes
676
InodeFactory.saveInode(workingAsset);
677         InodeFactory.saveInode(newAsset);
678
679         // Swaping tree relationships
680
TreeFactory.swapTrees(workingAsset, newAsset);
681
682         if (!isAbstractAsset(workingAsset)) {
683             // Setting folders and identifiers
684
parentFolder.addChild(workingAsset);
685             parentFolder.deleteChild(newAsset);
686         }
687         identifier.addChild(workingAsset);
688         identifier.addChild(newAsset);
689
690         if (!isAbstractAsset(workingAsset)) {
691             if (newAsset.isLive()) {
692                 parentFolder.addChild(newAsset);
693             }
694         }
695
696         DotHibernate.flush();
697         DotHibernate.getSession().refresh(workingAsset);
698         DotHibernate.getSession().refresh(newAsset);
699
700         return workingAsset;
701
702     }
703
704     /**
705      * This method save the new asset as the new working version and change the
706      * current working as an old version.
707      *
708      * @param newWebAsset
709      * New webasset version to be converted as the working asset.
710      * @return The current working webasset (The new version), after the method
711      * execution is must use this class as the working asset instead the
712      * class you give as parameter.
713      * @throws Exception
714      * The method throw an exception when the new asset identifier
715      * or the working folder cannot be found.
716      */

717     public static WebAsset saveAsset(WebAsset newWebAsset, Identifier id) throws Exception JavaDoc {
718         if (id.getInode() == 0) {
719             throw new Exception JavaDoc("Web asset Identifier not found!");
720         }
721         WebAsset currWebAsset = null;
722         if (newWebAsset instanceof Contentlet) {
723             // gets the current working asset
724
String JavaDoc condition = "language_id = " + ((Contentlet) newWebAsset).getLanguageId();
725             currWebAsset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(id, newWebAsset.getClass(),
726                     condition);
727         } else {
728             // gets the current working asset
729
currWebAsset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(id, newWebAsset.getClass());
730         }
731         if (currWebAsset.getInode() == 0) {
732             throw new Exception JavaDoc("Working copy not found!");
733         }
734
735         WebAsset workingAsset = swapAssets(currWebAsset, newWebAsset);
736
737         // Check
738
List JavaDoc workingAssets = null;
739         if (workingAsset instanceof Contentlet) {
740             // gets the current working asset
741
String JavaDoc condition = "language_id = " + ((Contentlet) workingAsset).getLanguageId();
742             workingAssets = IdentifierFactory.getWorkingChildrenOfClassByCondition(id, workingAsset.getClass(),
743                     condition);
744         } else {
745             // gets the current working asset
746
workingAssets = IdentifierFactory.getWorkingChildrenOfClass(id, workingAsset.getClass());
747         }
748
749         // if there is more than one working asset
750
if (workingAssets.size() > 1) {
751             Iterator JavaDoc iter = workingAssets.iterator();
752             while (iter.hasNext()) {
753                 WebAsset webAsset = (WebAsset) iter.next();
754                 if (webAsset.getInode() != workingAsset.getInode()) {
755                     webAsset.setWorking(false);
756                     InodeFactory.saveInode(webAsset);
757                 }
758             }
759         }
760
761         return workingAsset;
762     }
763
764     public static List JavaDoc getAssetsPerConditionWithPermission(Host host, String JavaDoc condition, Class JavaDoc c, Role[] roles,
765             int limit, int offset, String JavaDoc orderby, long parent) {
766         return getAssetsPerConditionWithPermission(host.getInode(), condition, c, roles, limit, offset, orderby, parent);
767     }
768
769     public static List JavaDoc getAssetsPerConditionWithPermission(long hostId, String JavaDoc condition, Class JavaDoc c, Role[] roles,
770             int limit, int offset, String JavaDoc orderby, long parent) {
771         DotHibernate dh = new DotHibernate(c);
772
773         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
774         try {
775
776             boolean isCMSAdmin = PublicRoleFactory.containsRole(roles, PublicRoleFactory.getCMSAdminRole());
777             
778             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
779
780             String JavaDoc rolesStr = "";
781
782             for (int i = 0; i < roles.length; i++) {
783                 rolesStr += roles[i].getRoleId();
784                 if (i != (roles.length - 1)) {
785                     rolesStr += ",";
786                 }
787             }
788             Logger.debug(WebAssetFactory.class, "rolesStr=" + rolesStr);
789             sb.append("select {" + tableName + ".*} from " + tableName + ", inode " + tableName + "_1_ where "
790                     + tableName + ".inode = " + tableName + "_1_.inode and " + tableName + ".inode in (");
791             sb.append("select distinct " + tableName + "_condition.inode ");
792             sb.append(" from " + tableName + " " + tableName + "_condition");
793             if (!isCMSAdmin) {
794                 sb.append(", tree tree, identifier identifier, permission permission ");
795             }
796             if (parent != 0) {
797                 sb.append(", tree tree2");
798             }
799             sb.append(" where " + condition);
800
801             if (parent != 0) {
802                 sb.append(" and " + tableName + "_condition.inode = tree2.child");
803                 sb.append(" and tree2.parent = " + parent);
804             }
805             if (!isCMSAdmin) {
806                 sb.append(" and " + tableName + "_condition.inode = tree.child ");
807                 sb.append(" and tree.parent = identifier.inode ");
808                 sb.append(" and identifier.inode = permission.inode_id ");
809                 sb.append(" and permission.roleid in (" + rolesStr + ") ");
810                 sb.append(" and permission.permission = " + Config.getStringProperty("PERMISSION_READ"));
811             }
812             sb.append(") and " + tableName + ".inode in (select tree.child from identifier, tree where host_inode = '"
813                     + hostId + "' and tree.parent = identifier.inode)");
814             sb.append(" order by " + orderby);
815             if (limit != 0) {
816                 dh.setFirstResult(offset);
817                 dh.setMaxResults(limit);
818             }
819
820             Logger.debug(WebAssetFactory.class, sb.toString());
821
822             dh.setSQLQuery(sb.toString());
823             return dh.list();
824
825         } catch (Exception JavaDoc e) {
826             Logger.warn(WebAssetFactory.class, "getAssetsPerConditionWithPermission failed:" + e, e);
827         }
828
829         return new ArrayList JavaDoc();
830     }
831
832     public static List JavaDoc getAssetsPerConditionWithPermission(String JavaDoc condition, Class JavaDoc c, Role[] roles, int limit,
833             int offset, String JavaDoc orderby, long parent) {
834         DotHibernate dh = new DotHibernate(c);
835
836         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
837         try {
838
839             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
840
841             String JavaDoc rolesStr = "";
842
843             for (int i = 0; i < roles.length; i++) {
844                 rolesStr += roles[i].getRoleId();
845                 if (i != (roles.length - 1)) {
846                     rolesStr += ",";
847                 }
848             }
849             Logger.debug(WebAssetFactory.class, "rolesStr=" + rolesStr);
850             sb.append("select {" + tableName + ".*} from " + tableName + ", inode " + tableName + "_1_ where "
851                     + tableName + ".inode = " + tableName + "_1_.inode and " + tableName + ".inode in (");
852             sb.append("select distinct " + tableName + "_condition.inode ");
853             sb.append(" from " + tableName + " " + tableName + "_condition, tree tree, identifier identifier ");
854             if (parent != 0) {
855                 sb.append(", tree tree2");
856             }
857
858             sb.append(", permission permission ");
859
860             sb.append(" where " + condition);
861
862             if (parent != 0) {
863                 sb.append(" and " + tableName + "_condition.inode = tree2.child");
864                 sb.append(" and tree2.parent = " + parent);
865             }
866             sb.append(" and " + tableName + "_condition.inode = tree.child ");
867             sb.append(" and tree.parent = identifier.inode ");
868
869             sb.append(" and identifier.inode = permission.inode_id ");
870             sb.append(" and permission.roleid in (" + rolesStr + ") ");
871             sb.append(" and permission.permission = " + Config.getStringProperty("PERMISSION_READ") + ")");
872
873             sb.append(" order by " + orderby);
874
875             if (limit != 0) {
876                 dh.setFirstResult(offset);
877                 dh.setMaxResults(limit);
878             }
879
880             Logger.debug(WebAssetFactory.class, sb.toString());
881
882             dh.setSQLQuery(sb.toString());
883             return dh.list();
884
885         } catch (Exception JavaDoc e) {
886             Logger.warn(WebAssetFactory.class, "getAssetsPerConditionWithPermission failed:" + e, e);
887         }
888
889         return new ArrayList JavaDoc();
890
891     }
892
893     public static int getCountAssetsPerConditionWithPermission(Host host, String JavaDoc condition, Class JavaDoc c, Role[] roles) {
894         return getCountAssetsPerConditionWithPermission(host.getInode(), condition, c, roles);
895     }
896
897     public static int getCountAssetsPerConditionWithPermission(long hostId, String JavaDoc condition, Class JavaDoc c, Role[] roles) {
898         return getCountAssetsPerConditionWithPermission(hostId, condition, c, roles, 0);
899     }
900
901     public static int getCountAssetsPerConditionWithPermission(String JavaDoc condition, Class JavaDoc c, Role[] roles) {
902         return getCountAssetsPerConditionWithPermission(condition, c, roles, 0);
903
904     }
905
906     public static int getCountAssetsPerConditionWithPermission(Host host, String JavaDoc condition, Class JavaDoc c, Role[] roles,
907             long parent) {
908         return getCountAssetsPerConditionWithPermission(host.getInode(), condition, c, roles, parent);
909     }
910
911     public static int getCountAssetsPerConditionWithPermission(long hostId, String JavaDoc condition, Class JavaDoc c, Role[] roles,
912             long parent) {
913         DotConnect db = new DotConnect();
914
915         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
916         try {
917
918             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
919
920             String JavaDoc rolesStr = "";
921             for (int i = 0; i < roles.length; i++) {
922                 rolesStr += roles[i].getRoleId();
923                 if (i != (roles.length - 1)) {
924                     rolesStr += ",";
925                 }
926             }
927             Logger.debug(WebAssetFactory.class, "rolesStr=" + rolesStr);
928
929             sb.append("select count(distinct " + tableName + "_condition.inode) as mycount ");
930             sb.append(" from " + tableName + " " + tableName + "_condition, inode " + tableName
931                     + "_1_, tree tree, identifier identifier, permission permission ");
932             if (parent != 0) {
933                 sb.append(", tree tree2");
934             }
935             sb.append(" where " + condition);
936             sb.append(" and " + tableName + "_condition.inode = " + tableName + "_1_.inode ");
937             if (parent != 0) {
938                 sb.append(" and " + tableName + "_1_.inode = tree2.child");
939                 sb.append(" and tree2.parent = " + parent);
940             }
941             sb.append(" and " + tableName + "_1_.inode = tree.child ");
942             sb.append(" and tree.parent = identifier.inode ");
943             sb.append(" and identifier.inode = permission.inode_id ");
944             sb.append(" and permission.roleid in (" + rolesStr + ") ");
945             sb.append(" and permission.permission = " + Config.getStringProperty("PERMISSION_READ"));
946             sb.append(" and " + tableName
947                     + "_condition.inode in (select tree.child from identifier, tree where host_inode = '" + hostId
948                     + "' and tree.parent = identifier.inode)");
949
950             Logger.debug(WebAssetFactory.class, sb.toString());
951
952             db.setSQL(sb.toString());
953             return db.getInt("mycount");
954
955         } catch (Exception JavaDoc e) {
956             Logger.error(WebAssetFactory.class, "getCountAssetsPerConditionWithPermission failed:" + e, e);
957         }
958
959         return 0;
960
961     }
962
963     public static int getCountAssetsPerConditionWithPermission(String JavaDoc condition, Class JavaDoc c, Role[] roles, long parent) {
964         DotConnect db = new DotConnect();
965
966         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
967         try {
968
969             String JavaDoc tableName = ((Inode) c.newInstance()).getType();
970
971             String JavaDoc rolesStr = "";
972             for (int i = 0; i < roles.length; i++) {
973                 rolesStr += roles[i].getRoleId();
974                 if (i != (roles.length - 1)) {
975                     rolesStr += ",";
976                 }
977             }
978             Logger.debug(WebAssetFactory.class, "rolesStr=" + rolesStr);
979
980             sb.append("select count(distinct " + tableName + "_condition.inode) as count ");
981             sb.append(" from " + tableName + " " + tableName + "_condition, inode " + tableName
982                     + "_1_, tree tree, identifier identifier, permission permission ");
983             if (parent != 0) {
984                 sb.append(", tree tree2");
985             }
986             sb.append(" where " + condition);
987             sb.append(" and " + tableName + "_condition.inode = " + tableName + "_1_.inode ");
988             if (parent != 0) {
989                 sb.append(" and " + tableName + "_1_.inode = tree2.child");
990                 sb.append(" and tree2.parent = " + parent);
991             }
992             sb.append(" and " + tableName + "_1_.inode = tree.child ");
993             sb.append(" and tree.parent = identifier.inode ");
994             sb.append(" and identifier.inode = permission.inode_id ");
995             sb.append(" and permission.roleid in (" + rolesStr + ") ");
996             sb.append(" and permission.permission = " + Config.getStringProperty("PERMISSION_READ"));
997
998             Logger.debug(WebAssetFactory.class, sb.toString());
999
1000            db.setSQL(sb.toString());
1001            return db.getInt("count");
1002
1003        } catch (Exception JavaDoc e) {
1004            Logger.error(WebAssetFactory.class, "getCountAssetsPerConditionWithPermission failed:" + e, e);
1005        }
1006
1007        return 0;
1008    }
1009
1010    // Generic method for all Assets and host.
1011
public static java.util.List JavaDoc getAssetsAndPermissionsPerRoleAndCondition(Host host, Role[] roles, String JavaDoc condition,
1012            int limit, int offset, String JavaDoc orderby, Class JavaDoc assetsClass, String JavaDoc tableName) {
1013        return getAssetsAndPermissionsPerRoleAndCondition(host.getInode(), roles, condition, limit, offset, orderby,
1014                assetsClass, tableName);
1015    }
1016
1017    public static java.util.List JavaDoc<PermissionAsset> getAssetsAndPermissionsPerRoleAndCondition(long hostId, Role[] roles,
1018            String JavaDoc condition, int limit, int offset, String JavaDoc orderby, Class JavaDoc assetsClass, String JavaDoc tableName) {
1019        java.util.List JavaDoc<PermissionAsset> entries = new java.util.ArrayList JavaDoc<PermissionAsset>();
1020        orderby = tableName + "." + orderby;
1021        java.util.List JavaDoc elements = WebAssetFactory.getAssetsPerConditionWithPermission(hostId, condition, assetsClass,
1022                roles, limit, offset, orderby, 0);
1023        java.util.Iterator JavaDoc elementsIter = elements.iterator();
1024
1025        while (elementsIter.hasNext()) {
1026
1027            WebAsset asset = (WebAsset) elementsIter.next();
1028            Folder folderParent = null;
1029            if (!WebAssetFactory.isAbstractAsset(asset))
1030                folderParent = (Folder) InodeFactory.getParentOfClass(asset, Folder.class);
1031
1032            java.util.List JavaDoc permissions = PermissionCache.getPermissionIdsByRolesFromCache(asset, roles);
1033
1034            PermissionAsset permAsset = new PermissionAsset();
1035            if (!WebAssetFactory.isAbstractAsset(asset))
1036                permAsset.setPathToMe(folderParent.getPath());
1037            else
1038                permAsset.setPathToMe("");
1039            permAsset.setPermissions(permissions);
1040            permAsset.setAsset(asset);
1041            entries.add(permAsset);
1042        }
1043        return entries;
1044    }
1045
1046    // Generic method for all Assets.
1047
public static java.util.List JavaDoc<PermissionAsset> getAssetsAndPermissionsPerRoleAndCondition(Role[] roles,
1048            String JavaDoc condition, int limit, int offset, String JavaDoc orderby, Class JavaDoc assetsClass, String JavaDoc tableName) {
1049
1050        java.util.List JavaDoc<PermissionAsset> entries = new java.util.ArrayList JavaDoc<PermissionAsset>();
1051        orderby = tableName + "." + orderby;
1052        java.util.List JavaDoc elements = WebAssetFactory.getAssetsPerConditionWithPermission(condition, assetsClass, roles,
1053                limit, offset, orderby, 0);
1054        java.util.Iterator JavaDoc elementsIter = elements.iterator();
1055
1056        while (elementsIter.hasNext()) {
1057
1058            WebAsset asset = (WebAsset) elementsIter.next();
1059            Folder folderParent = null;
1060            if (!WebAssetFactory.isAbstractAsset(asset))
1061                folderParent = (Folder) InodeFactory.getParentOfClass(asset, Folder.class);
1062
1063            java.util.List JavaDoc permissions = PermissionCache.getPermissionIdsByRolesFromCache(asset, roles);
1064
1065            PermissionAsset permAsset = new PermissionAsset();
1066            if (!WebAssetFactory.isAbstractAsset(asset))
1067                permAsset.setPathToMe(folderParent.getPath());
1068            else
1069                permAsset.setPathToMe("");
1070            permAsset.setPermissions(permissions);
1071            permAsset.setAsset(asset);
1072            entries.add(permAsset);
1073        }
1074        return entries;
1075    }
1076
1077    public static boolean isAbstractAsset(WebAsset asset) {
1078        if (asset instanceof Contentlet || asset instanceof Container || asset instanceof Template)
1079            return true;
1080        return false;
1081    }
1082
1083    public static void changeAssetMenuOrder(WebAsset asset, int newValue, User user) throws ActionException {
1084
1085        // Checking permissions
1086
if (!PermissionFactory.doesUserHavePermission(asset, Config.getIntProperty("PERMISSION_WRITE"), user))
1087            throw new ActionException(WebKeys.USER_PERMISSIONS_EXCEPTION);
1088
1089        if (newValue == -1) {
1090            asset.setShowOnMenu(false);
1091        } else {
1092            asset.setShowOnMenu(true);
1093        }
1094        asset.setSortOrder(newValue);
1095            
1096        InodeFactory.saveInode(asset);
1097        RefreshMenus.deleteMenus();
1098    }
1099
1100}
1101
Popular Tags