KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > UserPropertiesController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.io.StringReader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.apache.xerces.parsers.DOMParser;
34 import org.exolab.castor.jdo.Database;
35 import org.exolab.castor.jdo.OQLQuery;
36 import org.exolab.castor.jdo.QueryResults;
37 import org.infoglue.cms.applications.common.VisualFormatter;
38 import org.infoglue.cms.entities.content.DigitalAsset;
39 import org.infoglue.cms.entities.kernel.BaseEntityVO;
40 import org.infoglue.cms.entities.management.ContentTypeDefinition;
41 import org.infoglue.cms.entities.management.Language;
42 import org.infoglue.cms.entities.management.UserContentTypeDefinition;
43 import org.infoglue.cms.entities.management.UserProperties;
44 import org.infoglue.cms.entities.management.UserPropertiesVO;
45 import org.infoglue.cms.entities.management.impl.simple.LanguageImpl;
46 import org.infoglue.cms.entities.management.impl.simple.UserContentTypeDefinitionImpl;
47 import org.infoglue.cms.entities.management.impl.simple.UserPropertiesImpl;
48 import org.infoglue.cms.exception.Bug;
49 import org.infoglue.cms.exception.ConstraintException;
50 import org.infoglue.cms.exception.SystemException;
51 import org.infoglue.cms.security.InfoGluePrincipal;
52 import org.infoglue.cms.util.ConstraintExceptionBuffer;
53 import org.w3c.dom.CDATASection JavaDoc;
54 import org.w3c.dom.Document JavaDoc;
55 import org.w3c.dom.Node JavaDoc;
56 import org.w3c.dom.NodeList JavaDoc;
57 import org.xml.sax.InputSource JavaDoc;
58
59 /**
60  * This class is the controller for all handling of extranet roles properties.
61  */

62
63 public class UserPropertiesController extends BaseController
64 {
65     private final static Logger logger = Logger.getLogger(UserPropertiesController.class.getName());
66
67     /**
68      * Factory method
69      */

70
71     public static UserPropertiesController getController()
72     {
73         return new UserPropertiesController();
74     }
75     
76     
77     public UserProperties getUserPropertiesWithId(Integer JavaDoc userPropertiesId, Database db) throws SystemException, Bug
78     {
79         return (UserProperties) getObjectWithId(UserPropertiesImpl.class, userPropertiesId, db);
80     }
81     
82     public UserPropertiesVO getUserPropertiesVOWithId(Integer JavaDoc userPropertiesId) throws SystemException, Bug
83     {
84         return (UserPropertiesVO) getVOWithId(UserPropertiesImpl.class, userPropertiesId);
85     }
86   
87     public List JavaDoc getUserPropertiesVOList() throws SystemException, Bug
88     {
89         return getAllVOObjects(UserPropertiesImpl.class, "userPropertiesId");
90     }
91
92     /**
93      * This method created a new UserPropertiesVO in the database.
94      */

95
96     public UserPropertiesVO create(Integer JavaDoc languageId, Integer JavaDoc contentTypeDefinitionId, UserPropertiesVO userPropertiesVO) throws ConstraintException, SystemException
97     {
98         Database db = CastorDatabaseService.getDatabase();
99         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
100
101         UserProperties userProperties = null;
102
103         beginTransaction(db);
104         try
105         {
106             userProperties = create(languageId, contentTypeDefinitionId, userPropertiesVO, db);
107             commitTransaction(db);
108         }
109         catch(Exception JavaDoc e)
110         {
111             logger.error("An error occurred so we should not completes the transaction:" + e, e);
112             rollbackTransaction(db);
113             throw new SystemException(e.getMessage());
114         }
115     
116         return userProperties.getValueObject();
117     }
118
119     /**
120      * This method created a new UserPropertiesVO in the database. It also updates the extranetgroup
121      * so it recognises the change.
122      */

123
124     public UserProperties create(Integer JavaDoc languageId, Integer JavaDoc contentTypeDefinitionId, UserPropertiesVO userPropertiesVO, Database db) throws ConstraintException, SystemException, Exception JavaDoc
125     {
126         Language language = LanguageController.getController().getLanguageWithId(languageId, db);
127         ContentTypeDefinition contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db);
128
129         UserProperties userProperties = new UserPropertiesImpl();
130         userProperties.setLanguage((LanguageImpl)language);
131         userProperties.setContentTypeDefinition((ContentTypeDefinition)contentTypeDefinition);
132     
133         userProperties.setValueObject(userPropertiesVO);
134
135         db.create(userProperties);
136         
137         return userProperties;
138     }
139     
140     /**
141      * This method updates an extranet role properties.
142      */

143
144     public UserPropertiesVO update(Integer JavaDoc languageId, Integer JavaDoc contentTypeDefinitionId, UserPropertiesVO userPropertiesVO) throws ConstraintException, SystemException, Exception JavaDoc
145     {
146         UserPropertiesVO realUserPropertiesVO = userPropertiesVO;
147         
148         if(userPropertiesVO.getId() == null)
149         {
150             InfoGluePrincipal infoGluePrincipal = UserControllerProxy.getController().getUser(userPropertiesVO.getUserName());
151             List JavaDoc userPropertiesVOList = UserPropertiesController.getController().getUserPropertiesVOList(infoGluePrincipal.getName(), languageId);
152             if(userPropertiesVOList != null && userPropertiesVOList.size() > 0)
153             {
154                 realUserPropertiesVO = (UserPropertiesVO)userPropertiesVOList.get(0);
155                 realUserPropertiesVO.setValue(userPropertiesVO.getValue());
156             }
157             else
158             {
159                 logger.info("Creating the entity because there was no version at all for: " + contentTypeDefinitionId + " " + languageId);
160                 realUserPropertiesVO = create(languageId, contentTypeDefinitionId, userPropertiesVO);
161             }
162         }
163
164         return (UserPropertiesVO) updateEntity(UserPropertiesImpl.class, (BaseEntityVO) realUserPropertiesVO);
165     }
166
167     public UserPropertiesVO update(UserPropertiesVO userPropertiesVO, String JavaDoc[] users) throws ConstraintException, SystemException
168     {
169         Database db = CastorDatabaseService.getDatabase();
170         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
171
172         UserProperties userProperties = null;
173
174         beginTransaction(db);
175
176         try
177         {
178             //add validation here if needed
179
userProperties = getUserPropertiesWithId(userPropertiesVO.getUserPropertiesId(), db);
180             userProperties.setValueObject(userPropertiesVO);
181
182             //If any of the validations or setMethods reported an error, we throw them up now before create.
183
ceb.throwIfNotEmpty();
184             
185             commitTransaction(db);
186         }
187         catch(ConstraintException ce)
188         {
189             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
190             rollbackTransaction(db);
191             throw ce;
192         }
193         catch(Exception JavaDoc e)
194         {
195             logger.error("An error occurred so we should not complete the transaction:" + e, e);
196             rollbackTransaction(db);
197             throw new SystemException(e.getMessage());
198         }
199
200         return userProperties.getValueObject();
201     }
202     
203
204
205     public void delete(UserPropertiesVO userPropertiesVO) throws ConstraintException, SystemException
206     {
207         deleteEntity(UserPropertiesImpl.class, userPropertiesVO.getUserPropertiesId());
208     }
209
210     /**
211      * This method deletes the relation to a digital asset - not the asset itself.
212      */

213     public void deleteDigitalAssetRelation(Integer JavaDoc userPropertiesId, DigitalAsset digitalAsset, Database db) throws SystemException, Bug
214     {
215         UserProperties userProperties = getUserPropertiesWithId(userPropertiesId, db);
216         
217         userProperties.getDigitalAssets().remove(digitalAsset);
218         digitalAsset.getUserProperties().remove(userProperties);
219     }
220
221     /**
222      * This method gets a list of roleProperties for a role
223      * The result is a list of propertiesblobs - each propertyblob is a list of actual properties.
224      */

225
226     public List JavaDoc getUserPropertiesVOList(String JavaDoc userName, Integer JavaDoc languageId) throws ConstraintException, SystemException
227     {
228         Database db = CastorDatabaseService.getDatabase();
229         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
230
231         List JavaDoc userPropertiesVOList = new ArrayList JavaDoc();
232
233         beginTransaction(db);
234
235         try
236         {
237             List JavaDoc userProperties = getUserPropertiesList(userName, languageId, db, true);
238             userPropertiesVOList = toVOList(userProperties);
239             
240             //If any of the validations or setMethods reported an error, we throw them up now before create.
241
ceb.throwIfNotEmpty();
242             
243             commitTransaction(db);
244         }
245         catch(ConstraintException ce)
246         {
247             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
248             rollbackTransaction(db);
249             throw ce;
250         }
251         catch(Exception JavaDoc e)
252         {
253             logger.error("An error occurred so we should not complete the transaction:" + e, e);
254             rollbackTransaction(db);
255             throw new SystemException(e.getMessage());
256         }
257
258         return userPropertiesVOList;
259     }
260
261     /**
262      * This method gets a list of userProperties for a user
263      * The result is a list of propertiesblobs - each propertyblob is a list of actual properties.
264      */

265
266     public List JavaDoc getUserPropertiesList(String JavaDoc userName, Integer JavaDoc languageId, Database db, boolean readOnly) throws ConstraintException, SystemException, Exception JavaDoc
267     {
268         List JavaDoc userPropertiesList = new ArrayList JavaDoc();
269
270         OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.UserPropertiesImpl f WHERE f.userName = $1 AND f.language = $2");
271         oql.bind(userName);
272         oql.bind(languageId);
273
274         QueryResults results = null;
275         if(readOnly)
276         {
277             results = oql.execute(Database.ReadOnly);
278         }
279         else
280         {
281             logger.info("Fetching entity in read/write mode:" + userName);
282             results = oql.execute();
283         }
284         
285         while (results.hasMore())
286         {
287             UserProperties userProperties = (UserProperties)results.next();
288             userPropertiesList.add(userProperties);
289         }
290
291         results.close();
292         oql.close();
293
294         return userPropertiesList;
295     }
296     
297
298     /**
299      * This method fetches all content types available for this user.
300      */

301     
302     public List JavaDoc getContentTypeDefinitionVOList(String JavaDoc userName) throws ConstraintException, SystemException
303     {
304         Database db = CastorDatabaseService.getDatabase();
305         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
306
307         List JavaDoc contentTypeDefinitionVOList = new ArrayList JavaDoc();
308
309         beginTransaction(db);
310
311         try
312         {
313             List JavaDoc userContentTypeDefinitionList = getUserContentTypeDefinitionList(userName, db);
314             Iterator JavaDoc contentTypeDefinitionsIterator = userContentTypeDefinitionList.iterator();
315             while(contentTypeDefinitionsIterator.hasNext())
316             {
317                 UserContentTypeDefinition userContentTypeDefinition = (UserContentTypeDefinition)contentTypeDefinitionsIterator.next();
318                 contentTypeDefinitionVOList.add(userContentTypeDefinition.getContentTypeDefinition().getValueObject());
319             }
320     
321             ceb.throwIfNotEmpty();
322     
323             commitTransaction(db);
324         }
325         catch(ConstraintException ce)
326         {
327             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
328             rollbackTransaction(db);
329             throw ce;
330         }
331         catch(Exception JavaDoc e)
332         {
333             logger.error("An error occurred so we should not complete the transaction:" + e, e);
334             rollbackTransaction(db);
335             throw new SystemException(e.getMessage());
336         }
337
338         return contentTypeDefinitionVOList;
339     }
340
341     /**
342      * This method fetches all user content types available for this user within a transaction.
343      */

344     
345     public List JavaDoc getUserContentTypeDefinitionList(String JavaDoc userName, Database db) throws ConstraintException, SystemException, Exception JavaDoc
346     {
347         List JavaDoc userContentTypeDefinitionList = new ArrayList JavaDoc();
348         
349         OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.UserContentTypeDefinitionImpl f WHERE f.userName = $1");
350         oql.bind(userName);
351
352         QueryResults results = oql.execute();
353         logger.info("Fetching entity in read/write mode:" + userName);
354
355         while (results.hasMore())
356         {
357             UserContentTypeDefinition userContentTypeDefinition = (UserContentTypeDefinition)results.next();
358             userContentTypeDefinitionList.add(userContentTypeDefinition);
359         }
360
361         results.close();
362         oql.close();
363
364         return userContentTypeDefinitionList;
365     }
366     
367     /**
368      * This method fetches all content types available for this role.
369      */

370
371     public void updateContentTypeDefinitions(String JavaDoc userName, String JavaDoc[] contentTypeDefinitionIds) throws ConstraintException, SystemException
372     {
373         Database db = CastorDatabaseService.getDatabase();
374         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
375
376         List JavaDoc contentTypeDefinitionVOList = new ArrayList JavaDoc();
377
378         beginTransaction(db);
379
380         try
381         {
382             List JavaDoc userContentTypeDefinitionList = this.getUserContentTypeDefinitionList(userName, db);
383             Iterator JavaDoc contentTypeDefinitionsIterator = userContentTypeDefinitionList.iterator();
384             while(contentTypeDefinitionsIterator.hasNext())
385             {
386                 UserContentTypeDefinition userContentTypeDefinition = (UserContentTypeDefinition)contentTypeDefinitionsIterator.next();
387                 db.remove(userContentTypeDefinition);
388             }
389             
390             for(int i=0; i<contentTypeDefinitionIds.length; i++)
391             {
392                 Integer JavaDoc contentTypeDefinitionId = new Integer JavaDoc(contentTypeDefinitionIds[i]);
393                 ContentTypeDefinition contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db);
394                 UserContentTypeDefinitionImpl userContentTypeDefinitionImpl = new UserContentTypeDefinitionImpl();
395                 userContentTypeDefinitionImpl.setUserName(userName);
396                 userContentTypeDefinitionImpl.setContentTypeDefinition(contentTypeDefinition);
397                 db.create(userContentTypeDefinitionImpl);
398             }
399             
400             ceb.throwIfNotEmpty();
401
402             commitTransaction(db);
403         }
404         catch(ConstraintException ce)
405         {
406             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
407             rollbackTransaction(db);
408             throw ce;
409         }
410         catch(Exception JavaDoc e)
411         {
412             logger.error("An error occurred so we should not complete the transaction:" + e, e);
413             rollbackTransaction(db);
414             throw new SystemException(e.getMessage());
415         }
416     }
417     
418     /**
419      * This method fetches a value from the xml that is the userProperties Value. It then updates that
420      * single value and saves it back to the db.
421      */

422      
423     public void updateAttributeValue(Integer JavaDoc userPropertiesId, String JavaDoc attributeName, String JavaDoc attributeValue) throws SystemException, Bug
424     {
425         UserPropertiesVO userPropertiesVO = getUserPropertiesVOWithId(userPropertiesId);
426         
427         if(userPropertiesVO != null)
428         {
429             try
430             {
431                 logger.info("attributeName:" + attributeName);
432                 logger.info("versionValue:" + userPropertiesVO.getValue());
433                 logger.info("attributeValue:" + attributeValue);
434                 InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(userPropertiesVO.getValue()));
435                 
436                 DOMParser parser = new DOMParser();
437                 parser.parse(inputSource);
438                 Document JavaDoc document = parser.getDocument();
439                 
440                 NodeList JavaDoc nl = document.getDocumentElement().getChildNodes();
441                 Node JavaDoc attributesNode = nl.item(0);
442                 
443                 boolean existed = false;
444                 nl = attributesNode.getChildNodes();
445                 for(int i=0; i<nl.getLength(); i++)
446                 {
447                     Node JavaDoc n = nl.item(i);
448                     if(n.getNodeName().equalsIgnoreCase(attributeName))
449                     {
450                         if(n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null)
451                         {
452                             n.getFirstChild().setNodeValue(attributeValue);
453                             existed = true;
454                             break;
455                         }
456                         else
457                         {
458                             CDATASection JavaDoc cdata = document.createCDATASection(attributeValue);
459                             n.appendChild(cdata);
460                             existed = true;
461                             break;
462                         }
463                     }
464                 }
465                 
466                 if(existed == false)
467                 {
468                     org.w3c.dom.Element JavaDoc attributeElement = document.createElement(attributeName);
469                     attributesNode.appendChild(attributeElement);
470                     CDATASection JavaDoc cdata = document.createCDATASection(attributeValue);
471                     attributeElement.appendChild(cdata);
472                 }
473                 
474                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
475                 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
476                 logger.info("sb:" + sb);
477                 userPropertiesVO.setValue(sb.toString());
478                 update(userPropertiesVO.getLanguageId(), userPropertiesVO.getContentTypeDefinitionId(), userPropertiesVO);
479             }
480             catch(Exception JavaDoc e)
481             {
482                 e.printStackTrace();
483             }
484         }
485     }
486
487     
488     /**
489      * This method fetches a value from the xml that is the userProperties Value.
490      */

491      
492     public String JavaDoc getAttributeValue(Integer JavaDoc userPropertiesId, String JavaDoc attributeName, boolean escapeHTML) throws SystemException, Bug
493     {
494         String JavaDoc value = "";
495         
496         UserPropertiesVO userPropertiesVO = getUserPropertiesVOWithId(userPropertiesId);
497         
498         if(userPropertiesVO != null)
499         {
500             try
501             {
502                 logger.info("attributeName:" + attributeName);
503                 logger.info("VersionValue:" + userPropertiesVO.getValue());
504                 InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(userPropertiesVO.getValue()));
505                 
506                 DOMParser parser = new DOMParser();
507                 parser.parse(inputSource);
508                 Document JavaDoc document = parser.getDocument();
509                 
510                 NodeList JavaDoc nl = document.getDocumentElement().getChildNodes();
511                 Node JavaDoc n = nl.item(0);
512                 
513                 nl = n.getChildNodes();
514                 for(int i=0; i<nl.getLength(); i++)
515                 {
516                     n = nl.item(i);
517                     if(n.getNodeName().equalsIgnoreCase(attributeName))
518                     {
519                         if(n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null)
520                         {
521                             value = n.getFirstChild().getNodeValue();
522                             logger.info("Getting value: " + value);
523                             if(value != null && escapeHTML)
524                                 value = new VisualFormatter().escapeHTML(value);
525                             break;
526                         }
527                     }
528                 }
529             }
530             catch(Exception JavaDoc e)
531             {
532                 e.printStackTrace();
533             }
534         }
535         //logger.info("value:" + value);
536
return value;
537     }
538     
539     /**
540      * This method should return a list of those digital assets the contentVersion has.
541      */

542         
543     public List JavaDoc getDigitalAssetVOList(Integer JavaDoc userPropertiesId) throws SystemException, Bug
544     {
545         Database db = CastorDatabaseService.getDatabase();
546         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
547
548         List JavaDoc digitalAssetVOList = new ArrayList JavaDoc();
549
550         beginTransaction(db);
551
552         try
553         {
554             UserProperties userProperties = UserPropertiesController.getController().getUserPropertiesWithId(userPropertiesId, db);
555             if(userProperties != null)
556             {
557                 Collection JavaDoc digitalAssets = userProperties.getDigitalAssets();
558                 digitalAssetVOList = toVOList(digitalAssets);
559             }
560                         
561             commitTransaction(db);
562         }
563         catch(Exception JavaDoc e)
564         {
565             logger.info("An error occurred when we tried to fetch the list of digitalAssets belonging to this userProperties:" + e);
566             e.printStackTrace();
567             rollbackTransaction(db);
568             throw new SystemException(e.getMessage());
569         }
570         
571         return digitalAssetVOList;
572     }
573
574     
575     /**
576      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
577      * is handling.
578      */

579
580     public BaseEntityVO getNewVO()
581     {
582         return new UserPropertiesVO();
583     }
584
585
586
587 }
588  
Popular Tags