KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jahiatemplates > org > jahia > portlets_api > PortletsBean


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//--------------------------
13
// PortletsBean
14
//--------------------------
15
// Jerome Bedat 18.12.2000
16
//--------------------------
17
/**
18  * @todo: This class should probably be renamed to PortletsManager
19  */

20
21 package jahiatemplates.org.jahia.portlets_api;
22
23 import java.io.File JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import org.jahia.data.containers.JahiaContainer;
28 import org.jahia.exceptions.JahiaException;
29 import org.jahia.registries.ServicesRegistry;
30 import org.jahia.services.acl.ACLNotFoundException;
31 import org.jahia.services.acl.JahiaACLEntry;
32 import org.jahia.services.acl.JahiaBaseACL;
33 import org.jahia.services.usermanager.JahiaGroup;
34 import org.jahia.services.usermanager.JahiaGroupManagerService;
35 import org.jahia.services.usermanager.JahiaUser;
36 import org.jahia.utils.JahiaConsole;
37
38 public class PortletsBean
39 {
40
41     private static PortletsBean theObject = null;
42     private static String JavaDoc theDefaultUser = "guest";
43     private DesktopObj theDesktop;
44     private PortletObj thePortlet;
45     private SkinObj theSkin;
46     private Vector JavaDoc thePortletList;
47     private int theColumnCount;
48
49
50     /**
51      * PortletsBean
52      *
53      * @author Jerome Bedat
54      *
55      */

56     private PortletsBean()
57     {
58         JahiaConsole.println( "PortletsBean", "***** Starting the PortletBean *****" );
59     }
60
61
62     /**
63      * Retrieves an instance of the PortletsBean
64      *
65      * @author Jerome Bedat
66      *
67      */

68     public static synchronized PortletsBean getInstance()
69     {
70         if (theObject == null) {
71             theObject = new PortletsBean();
72         }
73         return theObject;
74     } // end getInstance
75

76
77
78     /**
79      * Retrieves a list of portlets for the specified page
80      *
81      * @author Jerome Bedat
82      *
83      */

84     public Enumeration JavaDoc getPortlets(int thePageID, String JavaDoc theUserName, String JavaDoc uRLtoTemplatesDir) {
85         try {
86             String JavaDoc theFile = theUserName + ".xml";
87             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
88             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, thePageID);
89             thePortletList = XMLFile.getPortletList();
90         } catch (JahiaException e) {
91             JahiaConsole.println( "PortletsBean -> loadPortlet", e.toString() );
92         }
93         return thePortletList.elements();
94     } // end getPortlets
95

96
97     /**
98      * Retrieves a list of portlets for the specified page and column
99      *
100      * @author Jerome Bedat
101      *
102      */

103     public Enumeration JavaDoc getPortletsFromColumn(int theColumn, int thePageID, String JavaDoc theUserName, String JavaDoc uRLtoTemplatesDir) {
104         try {
105             String JavaDoc theFile = theUserName + ".xml";
106             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
107             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, thePageID);
108             thePortletList = XMLFile.getPortletListFromColumn(theColumn);
109         } catch (JahiaException e) {
110             JahiaConsole.println( "PortletsBean -> loadPortlet", e.toString() );
111         }
112         return thePortletList.elements();
113     } // end getPortletsFromColumn
114

115
116     /**
117      * Retrieves a portlet for the specified page and portletid
118      *
119      * @author Jerome Bedat
120      *
121      */

122     public PortletObj getPortletByID(int thePortletID, int thePageID, String JavaDoc theUserName, String JavaDoc uRLtoTemplatesDir) {
123         try {
124             String JavaDoc theFile = theUserName + ".xml";
125             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
126             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, thePageID);
127             thePortlet = XMLFile.getPortlet(thePortletID);
128         } catch (JahiaException e) {
129             JahiaConsole.println( "PortletsBean -> loadPortlet", e.toString() );
130         }
131         return thePortlet;
132     } // end getPortletByID
133

134
135
136     /**
137      * Add new portlet in the specified page
138      *
139      * @author Jerome Bedat
140      *
141      */

142     public void addPortlet(int theJahiaEventObjectID, int theJahiaEventPageID, String JavaDoc theJahiaEventUserName, String JavaDoc uRLtoTemplatesDir) {
143         try {
144             String JavaDoc theFile = theJahiaEventUserName + ".xml";
145             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
146             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, theJahiaEventPageID);
147             XMLFile.addPortlet(theJahiaEventObjectID);
148         } catch (JahiaException e) {
149             JahiaConsole.println( "PortletsBean -> addPortlet", e.toString() );
150         }
151     } // end addPortlet
152

153
154     /**
155      * Update portlet settings for the specified portletid
156      *
157      * @author Jerome Bedat
158      *
159      */

160     public void setPortlet(PortletObj thePortlet, int thePageID, String JavaDoc theUserName, String JavaDoc uRLtoTemplatesDir) {
161         try {
162             String JavaDoc theFile = theUserName + ".xml";
163             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
164             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, thePageID);
165             XMLFile.updatePortlet(thePortlet);
166         } catch (JahiaException e) {
167             JahiaConsole.println( "PortletsBean -> setPortlet", e.toString() );
168         }
169     } // end setPortlet
170

171
172     /**
173      * Delete a Portlet
174      *
175      * @author Jerome Bedat
176      *
177      */

178     public void delPortlet(int theJahiaEventObjectID, int theJahiaEventPageID, String JavaDoc theJahiaEventUserName, String JavaDoc uRLtoTemplatesDir) {
179         try {
180             String JavaDoc theFile = theJahiaEventUserName + ".xml";
181             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
182             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, theJahiaEventPageID);
183             XMLFile.deletePortlet(theJahiaEventObjectID);
184         } catch (JahiaException e) {
185             JahiaConsole.println( "PortletsBean -> addPortlet", e.toString() );
186         }
187     } // end delPortlet
188

189
190     /**
191      * Add new portlet group (add new page)
192      *
193      * @author Jerome Bedat
194      *
195      */

196     public void addPortletGroup(int theJahiaEventObjectID, String JavaDoc theJahiaEventUserName, String JavaDoc uRLtoTemplatesDir) {
197         try {
198             String JavaDoc theFile = theJahiaEventUserName + ".xml";
199             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
200             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, theJahiaEventObjectID);
201             XMLFile.addPortletGroup();
202         } catch (JahiaException e) {
203             JahiaConsole.println( "PortletsBean -> addPortletGroup", e.toString() );
204         }
205     } // end addPortletGroup
206

207
208     /**
209      * Retrieves a column count for the specified page
210      *
211      * @author Jerome Bedat
212      *
213      */

214     public int getPortletsColumnCount(int thePageID, String JavaDoc theUserName, String JavaDoc uRLtoTemplatesDir) {
215         try {
216             String JavaDoc theFile = theUserName + ".xml";
217             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
218             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, thePageID);
219             theColumnCount = XMLFile.getColumnCount();
220         } catch (JahiaException e) {
221             JahiaConsole.println( "PortletsBean -> getPortletsColumnCount", e.toString() );
222         }
223         return theColumnCount;
224     } // end getPortletsColumnCount
225

226
227     /**
228      * Move the portlet to the right column
229      *
230      * @author Jerome Bedat
231      *
232      */

233     public void movePortletRight(int thePortletID, int thePageID, String JavaDoc theUserName, String JavaDoc uRLtoTemplatesDir) {
234         try {
235             String JavaDoc theFile = theUserName + ".xml";
236             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
237             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, thePageID);
238             XMLFile.moveRightPortlet(thePortletID);
239         } catch (JahiaException e) {
240             JahiaConsole.println( "PortletsBean -> moveXMLPortletRight", e.toString() );
241         }
242     } // end movePortletRight
243

244
245     /**
246      * Move the portlet to the left column
247      *
248      * @author Jerome Bedat
249      *
250      */

251     public void movePortletLeft(int thePortletID, int thePageID, String JavaDoc theUserName, String JavaDoc uRLtoTemplatesDir) {
252         try {
253             String JavaDoc theFile = theUserName + ".xml";
254             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
255             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, thePageID);
256             XMLFile.moveLeftPortlet(thePortletID);
257         } catch (JahiaException e) {
258             JahiaConsole.println( "PortletsBean -> moveXMLPortletLeft", e.toString() );
259         }
260     } // end movePortletLeft
261

262
263     /**
264      * Move the portlet to the bottom row
265      *
266      * @author Jerome Bedat
267      *
268      */

269     public void movePortletBottom(int thePortletID, int thePageID, String JavaDoc theUserName, String JavaDoc uRLtoTemplatesDir) {
270         try {
271             String JavaDoc theFile = theUserName + ".xml";
272             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
273             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, thePageID);
274             XMLFile.moveBottomPortlet(thePortletID);
275         } catch (JahiaException e) {
276             JahiaConsole.println( "PortletsBean -> moveXMLPortletBottom", e.toString() );
277         }
278     } // end movePortletBottom
279

280
281     /**
282      * Move the portlet to the top row
283      *
284      * @author Jerome Bedat
285      *
286      */

287     public void movePortletTop(int thePortletID, int thePageID, String JavaDoc theUserName, String JavaDoc uRLtoTemplatesDir) {
288         try {
289             String JavaDoc theFile = theUserName + ".xml";
290             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
291             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, thePageID);
292             XMLFile.moveTopPortlet(thePortletID);
293         } catch (JahiaException e) {
294             JahiaConsole.println( "PortletsBean -> moveXMLPortletTop", e.toString() );
295         }
296     } // end movePortletTop
297

298
299     /**
300      * Copy the Portlet Group from the specified user XML file to the other users files
301      *
302      * @author Jerome Bedat
303      *
304      */

305     public void copyPortletGroup(int thePageID, String JavaDoc theUserName, String JavaDoc uRLtoTemplatesDir) {
306         try {
307             String JavaDoc theFile = theUserName + ".xml";
308             String JavaDoc theDefaultFile = theDefaultUser + ".xml";
309             XMLPortlets XMLFile = new XMLPortlets(uRLtoTemplatesDir, theFile, theDefaultFile, thePageID);
310
311             File JavaDoc portletsAPIPathDir = new File JavaDoc( uRLtoTemplatesDir + File.separator + "portlets_api" + File.separator );
312             File JavaDoc[] portletsAPIFileList = portletsAPIPathDir.listFiles();
313
314             Vector JavaDoc sourceIDList = XMLFile.copyXMLPortletGroup(uRLtoTemplatesDir + File.separator + "portlets_api" + File.separator + theDefaultFile);
315             for(int i=0; i<portletsAPIFileList.length; i++) {
316                 if (!portletsAPIFileList[i].getAbsolutePath().equals(uRLtoTemplatesDir + File.separator + "portlets_api" + File.separator + theFile) && !portletsAPIFileList[i].getAbsolutePath().equals(uRLtoTemplatesDir + File.separator + "portlets_api" + File.separator + theDefaultFile)) {
317                     XMLFile.copyXMLPortletGroup(portletsAPIFileList[i].getAbsolutePath());
318                 }
319             }
320             unassignRightsToPortlets (sourceIDList);
321
322         } catch (JahiaException e) {
323             JahiaConsole.println( "PortletsBean -> copyPortletGroup", e.toString() );
324         }
325     } // end copyPortletGroup
326

327
328     /**
329      * Load a Skin from the DataBase
330      *
331      * @author Jerome Bedat
332      *
333      */

334     public SkinObj loadSkin(String JavaDoc theType, int theID) {
335         try {
336             PortletsDBManager DB = PortletsDBManager.getInstance();
337             if (theType.equals("portlet")) {
338                 theSkin = DB.load_portlet_skin(theID);
339             } else if (theType.equals("desktop")) {
340                 theSkin = DB.load_desktop_skin(theID);
341             }
342             if (theSkin == null) {
343                 theSkin = new SkinObj(1,"","");
344             }
345         } catch (JahiaException e) {
346             theSkin = new SkinObj(1,"","");
347             JahiaConsole.println( "PortletsBean -> loadSkin", e.toString() );
348         }
349         return theSkin;
350     } // end loadSkin
351

352
353     /**
354      * Print message in the JahiaConsole
355      *
356      * @author Jerome Bedat
357      *
358      */

359     public void consoleOutput(String JavaDoc theFirst, String JavaDoc theSecond) {
360         JahiaConsole.println( theFirst, theSecond );
361     } // end consoleOutput
362

363
364
365     //-------------------------------------------------------------------------
366
private void unassignRightsToPortlets (Vector JavaDoc ctnids) {
367
368         // get the administrator group name.
369
String JavaDoc adminGroupName =
370                 JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME;
371
372         // get the administrator group object.
373
JahiaGroup adminGroup = ServicesRegistry.getInstance().
374                 getJahiaGroupManagerService ().
375                 lookupGroup (JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME);
376
377         // get the administrator group object.
378
JahiaGroup usersGroup = ServicesRegistry.getInstance().
379                 getJahiaGroupManagerService ().
380                 lookupGroup (JahiaGroupManagerService.USERS_GROUPNAME);
381
382         // get the administrator group object.
383
JahiaGroup guestGroup = ServicesRegistry.getInstance().
384                 getJahiaGroupManagerService ().
385                 lookupGroup (JahiaGroupManagerService.GUEST_GROUPNAME);
386
387
388         // if the administrators group is not available .. just leave.
389
// ok ok .. it's a hack, but I'll fix this later. (Fulco) Fulco shame on you !
390
if (adminGroup == null) {
391             return;
392         }
393
394
395         // process all the fields to reset the rights.
396
for (int index = 0; index < ctnids.size(); index++) {
397             try {
398                 // get the field ID
399
int ctnid = ((Integer JavaDoc)ctnids.get (index)).intValue();
400
401                 // Get the container reference holding the field.
402
JahiaContainer container = ServicesRegistry.getInstance().
403                         getJahiaContainersService().loadContainerInfo (ctnid);
404
405                 if (container != null) {
406                     // Get the ACL of the container
407

408                     try {
409                         JahiaBaseACL acl = new JahiaBaseACL (container.getAclID());
410                         if (acl != null) {
411
412                             // remove all the user who are not an
413
// administrator for administrator users, keep
414
// the existing entry.
415

416                             // get all the user entries.
417
Vector JavaDoc usernames = acl.getUsernameList (null);
418                             if (usernames.size() > 0) {
419                                 for (int i=0; i<usernames.size(); i++) {
420                                     String JavaDoc username = (String JavaDoc)usernames.get(i);
421                                     JahiaUser user = ServicesRegistry.
422                                             getInstance().
423                                             getJahiaUserManagerService().
424                                             // FIXME_MULTISITE Hollis sietid is deducted from container ....
425
lookupUser (container.getSiteID(),username);
426                                     if ((user != null) &&
427                                         (!user.isMemberOfGroup (container.getSiteID(),adminGroupName)))
428                                     {
429                                         acl.removeUserEntry (user);
430                                     }
431                                 }
432                             }
433
434                             // disable the access to the users and guest group
435
JahiaACLEntry entry = new JahiaACLEntry (0, 0);
436                             entry.setPermission (JahiaBaseACL.READ_RIGHTS, JahiaACLEntry.ACL_YES);
437
438                             acl.setGroupEntry (usersGroup, entry);
439                             acl.setGroupEntry (guestGroup, entry);
440
441                             // set the new administration group rights
442
JahiaACLEntry adminEntry = new JahiaACLEntry ();
443                             for (int i=0; i<acl.size(); i++) {
444                                 adminEntry.setPermission (i, JahiaACLEntry.ACL_YES);
445                             }
446
447                             acl.setGroupEntry (adminGroup, adminEntry);
448                         }
449                     }
450                     catch (ACLNotFoundException ex) {
451                         // the ACL was not found .. go to the next field.
452
}
453                 }
454
455             }
456             catch (JahiaException ex) {
457                 // go to the next field.
458
}
459         }
460     }
461
462 }
463
Popular Tags