KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.exolab.castor.jdo.Database;
34 import org.exolab.castor.jdo.OQLQuery;
35 import org.exolab.castor.jdo.QueryResults;
36 import org.infoglue.cms.entities.kernel.BaseEntityVO;
37 import org.infoglue.cms.entities.management.Language;
38 import org.infoglue.cms.entities.management.LanguageVO;
39 import org.infoglue.cms.entities.management.Repository;
40 import org.infoglue.cms.entities.management.RepositoryLanguage;
41 import org.infoglue.cms.entities.management.impl.simple.LanguageImpl;
42 import org.infoglue.cms.entities.management.impl.simple.RepositoryLanguageImpl;
43 import org.infoglue.cms.exception.Bug;
44 import org.infoglue.cms.exception.ConstraintException;
45 import org.infoglue.cms.exception.SystemException;
46 import org.infoglue.cms.util.ConstraintExceptionBuffer;
47 import org.infoglue.deliver.util.CacheController;
48
49 /**
50  * This class handles all interaction with Languages and persistence of them.
51  *
52  * @author mattias
53  */

54
55 public class LanguageController extends BaseController
56 {
57     private final static Logger logger = Logger.getLogger(LanguageController.class.getName());
58
59     /**
60      * Factory method
61      */

62
63     public static LanguageController getController()
64     {
65         return new LanguageController();
66     }
67
68     /**
69      * This method returns a specific LanguageVO object
70      */

71     /*
72     public LanguageVO getLanguageVOWithId(Integer languageId) throws SystemException, Bug
73     {
74         return (LanguageVO)getVOWithId(LanguageImpl.class, languageId);
75     }
76     */

77     
78     /**
79      * This method return a LanguageVO
80      */

81     
82     public LanguageVO getLanguageVOWithId(Integer JavaDoc languageId) throws SystemException, Exception JavaDoc
83     {
84         String JavaDoc key = "" + languageId;
85         logger.info("key:" + key);
86         LanguageVO languageVO = (LanguageVO)CacheController.getCachedObject("languageCache", key);
87         if(languageVO != null)
88         {
89             logger.info("There was an cached languageVO:" + languageVO);
90         }
91         else
92         {
93             languageVO = (LanguageVO)getVOWithId(LanguageImpl.class, languageId);
94             
95             CacheController.cacheObject("languageCache", key, languageVO);
96         }
97                 
98         return languageVO;
99     }
100
101     
102     /**
103      * This method returns a specific LanguageVO object
104      */

105     
106     public LanguageVO getLanguageVOWithId(Integer JavaDoc languageId, Database db) throws SystemException, Bug
107     {
108         return (LanguageVO)getVOWithId(LanguageImpl.class, languageId, db);
109     }
110
111     /**
112      * This method returns language with the languageCode sent in.
113      */

114     
115     public Locale JavaDoc getLocaleWithId(Integer JavaDoc languageId)
116     {
117         Locale JavaDoc locale = Locale.getDefault();
118         
119         if (languageId != null)
120         {
121             try
122             {
123                 LanguageVO languageVO = getLanguageVOWithId(languageId);
124                 locale = new Locale JavaDoc(languageVO.getLanguageCode());
125             }
126             catch (Exception JavaDoc e)
127             {
128                 logger.error("An error occurred in getLocaleWithId: getting locale with languageid:" + languageId + "," + e, e);
129             }
130         }
131         
132         return locale;
133     }
134
135     /**
136      * Returns the LanguageVO with the given name.
137      *
138      * @param name
139      * @return
140      * @throws SystemException
141      * @throws Bug
142      */

143     
144     public LanguageVO getLanguageVOWithName(String JavaDoc name) throws SystemException, Bug
145     {
146         LanguageVO languageVO = null;
147         
148         Database db = CastorDatabaseService.getDatabase();
149
150         try
151         {
152             beginTransaction(db);
153
154             Language language = getLanguageWithName(name, db);
155             if(language != null)
156                 languageVO = language.getValueObject();
157             
158             commitTransaction(db);
159         }
160         catch (Exception JavaDoc e)
161         {
162             logger.info("An error occurred so we should not complete the transaction:" + e);
163             rollbackTransaction(db);
164             throw new SystemException(e.getMessage());
165         }
166         
167         return languageVO;
168     }
169     
170     /**
171      * Returns the Language with the given name fetched within a given transaction.
172      *
173      * @param name
174      * @param db
175      * @return
176      * @throws SystemException
177      * @throws Bug
178      */

179
180     public Language getLanguageWithName(String JavaDoc name, Database db) throws SystemException, Bug
181     {
182         Language language = null;
183         
184         try
185         {
186             OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.LanguageImpl f WHERE f.name = $1");
187             oql.bind(name);
188             
189             QueryResults results = oql.execute();
190             this.logger.info("Fetching entity in read/write mode" + name);
191
192             if (results.hasMore())
193             {
194                 language = (Language)results.next();
195             }
196             
197             results.close();
198             oql.close();
199         }
200         catch(Exception JavaDoc e)
201         {
202             throw new SystemException("An error occurred when we tried to fetch a named language. Reason:" + e.getMessage(), e);
203         }
204         
205         return language;
206     }
207
208     /**
209      * Returns the LanguageVO with the given languageCode.
210      *
211      * @param code
212      * @return
213      * @throws SystemException
214      * @throws Bug
215      */

216     
217     public LanguageVO getLanguageVOWithCode(String JavaDoc code, Database db) throws SystemException, Bug
218     {
219         LanguageVO languageVO = null;
220         
221         Language language = getLanguageWithCode(code, db);
222         if(language != null)
223             languageVO = language.getValueObject();
224         
225         return languageVO;
226     }
227
228     /**
229      * Returns the LanguageVO with the given languageCode.
230      *
231      * @param code
232      * @return
233      * @throws SystemException
234      * @throws Bug
235      */

236     
237     public LanguageVO getLanguageVOWithCode(String JavaDoc code) throws SystemException, Bug
238     {
239         LanguageVO languageVO = null;
240         
241         Database db = CastorDatabaseService.getDatabase();
242
243         try
244         {
245             beginTransaction(db);
246
247             Language language = getLanguageWithCode(code, db);
248             if(language != null)
249                 languageVO = language.getValueObject();
250             
251             commitTransaction(db);
252         }
253         catch (Exception JavaDoc e)
254         {
255             logger.info("An error occurred so we should not complete the transaction:" + e);
256             rollbackTransaction(db);
257             throw new SystemException(e.getMessage());
258         }
259         
260         return languageVO;
261     }
262     
263     /**
264      * Returns the Language with the given languageCode fetched within a given transaction.
265      *
266      * @param code
267      * @param db
268      * @return
269      * @throws SystemException
270      * @throws Bug
271      */

272
273     public Language getLanguageWithCode(String JavaDoc code, Database db) throws SystemException, Bug
274     {
275         Language language = null;
276         
277         try
278         {
279             OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.LanguageImpl f WHERE f.languageCode = $1");
280             oql.bind(code);
281             
282             QueryResults results = oql.execute();
283             this.logger.info("Fetching entity in read/write mode" + code);
284
285             if (results.hasMore())
286             {
287                 language = (Language)results.next();
288             }
289             
290             results.close();
291             oql.close();
292         }
293         catch(Exception JavaDoc e)
294         {
295             throw new SystemException("An error occurred when we tried to fetch a named language. Reason:" + e.getMessage(), e);
296         }
297         
298         return language;
299     }
300     
301     public LanguageVO create(LanguageVO languageVO) throws ConstraintException, SystemException
302     {
303         Language ent = new LanguageImpl();
304         ent.setValueObject(languageVO);
305         ent = (Language) createEntity(ent);
306         return ent.getValueObject();
307     }
308
309     public LanguageVO create(Database db, LanguageVO languageVO) throws ConstraintException, SystemException, Exception JavaDoc
310     {
311         Language ent = new LanguageImpl();
312         ent.setValueObject(languageVO);
313         db.create(ent);
314         return ent.getValueObject();
315     }
316
317     /**
318      * This method removes a Language from the system and also cleans out all depending repositoryLanguages.
319      */

320     
321     public void delete(LanguageVO languageVO) throws ConstraintException, SystemException
322     {
323         Database db = CastorDatabaseService.getDatabase();
324         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
325
326         Language language = null;
327
328         try
329         {
330             beginTransaction(db);
331
332             delete(db, languageVO);
333             
334             commitTransaction(db);
335         }
336         catch(Exception JavaDoc e)
337         {
338             logger.error("An error occurred so we should not complete the transaction:" + e, e);
339             rollbackTransaction(db);
340             throw new SystemException(e.getMessage());
341         }
342     }
343
344     /**
345      * This method removes a Language from the system and also cleans out all depending repositoryLanguages.
346      */

347     
348     public void delete(Database db, LanguageVO languageVO) throws ConstraintException, SystemException
349     {
350         Language language = getLanguageWithId(languageVO.getId(), db);
351         RepositoryLanguageController.getController().deleteAllRepositoryLanguageWithLanguage(language, db);
352             
353         deleteEntity(LanguageImpl.class, languageVO.getLanguageId(), db);
354     }
355
356
357     public Language getLanguageWithId(Integer JavaDoc languageId, Database db) throws SystemException, Bug
358     {
359         return (Language) getObjectWithId(LanguageImpl.class, languageId, db);
360     }
361
362
363     public Language getLanguageWithId(Integer JavaDoc languageId) throws ConstraintException, SystemException, Bug
364     {
365         Database db = CastorDatabaseService.getDatabase();
366         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
367
368         Language language = null;
369
370         try
371         {
372             beginTransaction(db);
373
374             language = getLanguageWithId(languageId, db);
375         
376             //If any of the validations or setMethods reported an error, we throw them up now before create.
377
ceb.throwIfNotEmpty();
378             
379             commitTransaction(db);
380         }
381         catch(Exception JavaDoc e)
382         {
383             logger.error("An error occurred so we should not complete the transaction:" + e, e);
384             rollbackTransaction(db);
385             throw new SystemException(e.getMessage());
386         }
387         
388         return language;
389     }
390
391
392     public LanguageVO getLanguageVOWithRepositoryLanguageId(Integer JavaDoc repositoryLanguageId) throws ConstraintException, SystemException, Bug
393     {
394         Database db = CastorDatabaseService.getDatabase();
395         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
396
397         LanguageVO languageVO = null;
398
399         try
400         {
401             beginTransaction(db);
402
403             languageVO = getLanguageWithRepositoryLanguageId(repositoryLanguageId, db).getValueObject();
404             
405             //If any of the validations or setMethods reported an error, we throw them up now before create.
406
ceb.throwIfNotEmpty();
407             
408             commitTransaction(db);
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         return languageVO;
418     }
419
420     public Language getLanguageWithRepositoryLanguageId(Integer JavaDoc repositoryLanguageId, Database db) throws ConstraintException, SystemException, Bug
421     {
422         RepositoryLanguage repositoryLanguage = (RepositoryLanguage) getObjectWithId(RepositoryLanguageImpl.class, repositoryLanguageId, db);
423         Language language = repositoryLanguage.getLanguage();
424         
425         return language;
426     }
427
428     
429     public List JavaDoc getLanguageVOList(Integer JavaDoc repositoryId) throws ConstraintException, SystemException
430     {
431         Database db = CastorDatabaseService.getDatabase();
432         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
433
434         List JavaDoc languageVOList = new ArrayList JavaDoc();
435
436         beginTransaction(db);
437
438         try
439         {
440             Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db);
441             Collection JavaDoc repositoryLanguageList = repository.getRepositoryLanguages();
442             Iterator JavaDoc repositoryLanguageIterator = repositoryLanguageList.iterator();
443             while(repositoryLanguageIterator.hasNext())
444             {
445                 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)repositoryLanguageIterator.next();
446                 languageVOList.add(repositoryLanguage.getLanguage().getValueObject());
447             }
448             
449             //If any of the validations or setMethods reported an error, we throw them up now before create.
450
ceb.throwIfNotEmpty();
451             
452             commitTransaction(db);
453         }
454         catch(ConstraintException ce)
455         {
456             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
457             rollbackTransaction(db);
458             throw ce;
459         }
460         catch(Exception JavaDoc e)
461         {
462             logger.error("An error occurred so we should not complete the transaction:" + e, e);
463             rollbackTransaction(db);
464             throw new SystemException(e.getMessage());
465         }
466
467         return languageVOList;
468     }
469     
470     public List JavaDoc getLanguageVOList(Integer JavaDoc repositoryId, Database db) throws ConstraintException, SystemException
471     {
472         List JavaDoc languageVOList = new ArrayList JavaDoc();
473
474         Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db);
475         Collection JavaDoc repositoryLanguageList = repository.getRepositoryLanguages();
476         Iterator JavaDoc repositoryLanguageIterator = repositoryLanguageList.iterator();
477         while(repositoryLanguageIterator.hasNext())
478         {
479             RepositoryLanguage repositoryLanguage = (RepositoryLanguage)repositoryLanguageIterator.next();
480             languageVOList.add(repositoryLanguage.getLanguage().getValueObject());
481         }
482             
483         return languageVOList;
484     }
485
486
487
488     public List JavaDoc getLanguageList(Integer JavaDoc repositoryId, Database db) throws ConstraintException, SystemException
489     {
490         List JavaDoc languageList = new ArrayList JavaDoc();
491
492         Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db);
493         Collection JavaDoc repositoryLanguageList = repository.getRepositoryLanguages();
494         Iterator JavaDoc repositoryLanguageIterator = repositoryLanguageList.iterator();
495         while(repositoryLanguageIterator.hasNext())
496         {
497             RepositoryLanguage repositoryLanguage = (RepositoryLanguage)repositoryLanguageIterator.next();
498             languageList.add(repositoryLanguage.getLanguage());
499         }
500
501         return languageList;
502     }
503
504     public List JavaDoc getLanguageVOList(Database db) throws SystemException, Bug
505     {
506         return getAllVOObjects(LanguageImpl.class, "languageId", db);
507     }
508
509     public List JavaDoc getLanguageVOList() throws SystemException, Bug
510     {
511         return getAllVOObjects(LanguageImpl.class, "languageId");
512     }
513
514     /**
515      * This method returns the master language.
516      * todo - add attribute on repositoryLanguage to be able to sort them... and then fetch the first
517      */

518     
519     public LanguageVO getMasterLanguage(Integer JavaDoc repositoryId) throws SystemException, Exception JavaDoc
520     {
521         LanguageVO languageVO = null;
522
523         String JavaDoc languageKey = "" + repositoryId;
524         logger.info("languageKey:" + languageKey);
525         languageVO = (LanguageVO)CacheController.getCachedObject("masterLanguageCache", languageKey);
526         if(languageVO != null)
527         {
528             logger.info("There was an cached master language:" + languageVO.getName());
529         }
530         else
531         {
532             Database db = CastorDatabaseService.getDatabase();
533             ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
534     
535             Language language = null;
536     
537             beginTransaction(db);
538     
539             try
540             {
541                 language = getMasterLanguage(db, repositoryId);
542                 
543                 //If any of the validations or setMethods reported an error, we throw them up now before create.
544
ceb.throwIfNotEmpty();
545
546                 if(language != null)
547                 {
548                     languageVO = language.getValueObject();
549                     CacheController.cacheObject("masterLanguageCache", languageKey, languageVO);
550                 }
551                 
552                 commitTransaction(db);
553             }
554             catch(Exception JavaDoc e)
555             {
556                 logger.error("An error occurred so we should not complete the transaction:" + e, e);
557                 rollbackTransaction(db);
558                 throw new SystemException(e.getMessage());
559             }
560         }
561         
562         return languageVO;
563     }
564
565     
566     /**
567      * This method returns the master language.
568      * todo - add attribute on repositoryLanguage to be able to sort them... and then fetch the first
569      */

570     
571     public LanguageVO getMasterLanguage(Integer JavaDoc repositoryId, Database db) throws SystemException, Exception JavaDoc
572     {
573         LanguageVO languageVO = null;
574
575         String JavaDoc languageKey = "" + repositoryId;
576         logger.info("languageKey:" + languageKey);
577         languageVO = (LanguageVO)CacheController.getCachedObject("masterLanguageCache", languageKey);
578         if(languageVO != null)
579         {
580             logger.info("There was an cached master language:" + languageVO.getName());
581         }
582         else
583         {
584             Language language = getMasterLanguage(db, repositoryId);
585                 
586             if(language != null)
587             {
588                 languageVO = language.getValueObject();
589                 CacheController.cacheObject("masterLanguageCache", languageKey, languageVO);
590             }
591         }
592         
593         return languageVO;
594     }
595
596     
597     /**
598      * This method returns the master language within an transaction.
599      */

600     
601     public Language getMasterLanguage(Database db, Integer JavaDoc repositoryId) throws SystemException, Exception JavaDoc
602     {
603         Language language = null;
604
605         OQLQuery oql = db.getOQLQuery( "SELECT l FROM org.infoglue.cms.entities.management.impl.simple.LanguageImpl l WHERE l.repositoryLanguages.repository.repositoryId = $1 ORDER BY l.repositoryLanguages.sortOrder, l.languageId");
606         oql.bind(repositoryId);
607         
608         QueryResults results = oql.execute(Database.ReadOnly);
609         
610         if (results.hasMore())
611         {
612             language = (Language)results.next();
613         }
614         
615         results.close();
616         oql.close();
617
618         return language;
619     }
620
621     /**
622      * This method deletes the Repository sent in from the system.
623      */

624     
625     public void deleteLanguage(Integer JavaDoc languageId, Database db) throws SystemException, Bug
626     {
627         try
628         {
629             db.remove(getLanguageWithId(languageId, db));
630         }
631         catch(Exception JavaDoc e)
632         {
633             throw new SystemException("An error occurred when we tried to delete Language in the database. Reason: " + e.getMessage(), e);
634         }
635     }
636
637     public LanguageVO update(LanguageVO languageVO) throws ConstraintException, SystemException
638     {
639         Database db = CastorDatabaseService.getDatabase();
640         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
641
642         Language language = null;
643
644         beginTransaction(db);
645
646         try
647         {
648             //add validation here if needed
649
language = getLanguageWithId(languageVO.getLanguageId(), db);
650             language.setValueObject(languageVO);
651
652             //If any of the validations or setMethods reported an error, we throw them up now before create.
653
ceb.throwIfNotEmpty();
654             
655             commitTransaction(db);
656         }
657         catch(ConstraintException ce)
658         {
659             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
660             rollbackTransaction(db);
661             throw ce;
662         }
663         catch(Exception JavaDoc e)
664         {
665             logger.error("An error occurred so we should not complete the transaction:" + e, e);
666             rollbackTransaction(db);
667             throw new SystemException(e.getMessage());
668         }
669
670
671         return language.getValueObject();
672     }
673     
674     /**
675      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
676      * is handling.
677      */

678
679     public BaseEntityVO getNewVO()
680     {
681         return new LanguageVO();
682     }
683     
684 }
685  
686
Popular Tags