KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > language > LanguageManagerBean


1 /*
2  * Created on Mar 15, 2004
3  *
4  */

5 package com.nightlabs.ipanema.language;
6 import java.rmi.RemoteException JavaDoc;
7 import java.util.Collection JavaDoc;
8
9 import javax.ejb.CreateException JavaDoc;
10 import javax.ejb.EJBException JavaDoc;
11 import javax.ejb.SessionBean JavaDoc;
12 import javax.ejb.SessionContext JavaDoc;
13 import javax.jdo.FetchPlan;
14 import javax.jdo.JDOObjectNotFoundException;
15 import javax.jdo.PersistenceManager;
16
17 import org.apache.log4j.Logger;
18
19 import com.nightlabs.ipanema.base.BaseSessionBeanImpl;
20 import com.nightlabs.ipanema.language.id.LanguageID;
21
22 /**
23  * @ejb.bean name="ipanema/ejb/IpanemaBaseBean/LanguageManager"
24  * jndi-name="ipanema/ejb/IpanemaBaseBean/LanguageManager"
25  * type="Stateless"
26  *
27  * @ejb.util generate = "physical"
28  */

29 public abstract class LanguageManagerBean extends BaseSessionBeanImpl implements SessionBean JavaDoc
30 {
31     public static final Logger LOGGER = Logger.getLogger(LanguageManagerBean.class);
32     /**
33      * @see com.nightlabs.ipanema.base.BaseSessionBeanImpl#setSessionContext(javax.ejb.SessionContext)
34      */

35     public void setSessionContext(SessionContext JavaDoc sessionContext)
36             throws EJBException JavaDoc, RemoteException JavaDoc
37     {
38         super.setSessionContext(sessionContext);
39     }
40     /**
41      * @ejb.create-method
42      * @ejb.permission role-name="LanguageManager-read"
43      */

44     public void ejbCreate() throws CreateException JavaDoc
45     {
46 // try
47
// {
48
// LOGGER.debug("LanguageManagerBean created by " + this.getPrincipalString());
49
// }
50
// catch (ModuleException e)
51
// {
52
// throw new CreateException(e.getMessage());
53
// }
54
}
55     /**
56      * @see javax.ejb.SessionBean#ejbRemove()
57      *
58      * @ejb.permission unchecked="true"
59      */

60     public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc { }
61
62     /**
63      * Creates a language if it does not exist. If it exists already, nothing is done.
64      *
65      * @param languageID ISO639-2 language code
66      * @param nativeName should be the language name in itself (e.g. French = Francais)
67      *
68      * @ejb.interface-method
69      *
70      * @ejb.permission role-name="LanguageManager-write"
71      **/

72     public void createLanguage(String JavaDoc languageID, String JavaDoc nativeName)
73     throws LanguageException
74     {
75         LOGGER.debug("LanguageManagerBean.createLanguage");
76         try {
77             PersistenceManager pm = this.getPersistenceManager();
78             try {
79                 pm.getExtent(Language.class, false);
80
81                 try {
82                     Language existingLang = (Language)pm.getObjectById(LanguageID.create(languageID));
83                     return;
84                 } catch (JDOObjectNotFoundException e) { }
85
86                 Language newLang = new Language(languageID, nativeName);
87                 pm.makePersistent(newLang);
88                 LOGGER.debug("new language created..");
89             } finally {
90                 pm.close();
91             }
92         } catch(Exception JavaDoc e) {
93             LOGGER.debug("exception: " + e.getMessage());
94             throw new LanguageException(e);
95         }
96     }
97
98
99     /**
100      * @return A Collection containing all languages that the current organisationID knows.
101      * @throws LanguageException if sth. unexpected happens - e.g. if the PersistenceManager
102      * is not accessible.
103      *
104      * @ejb.interface-method
105      *
106      * @ejb.permission role-name="LanguageManager-read"
107      */

108     public Collection JavaDoc getLanguages()
109     throws LanguageException
110     {
111         try {
112           PersistenceManager pm = getPersistenceManager();
113           try {
114               pm.getFetchPlan().addGroup(FetchPlan.ALL);
115               return pm.detachCopyAll((Collection JavaDoc)pm.newQuery(Language.class).execute());
116           } finally {
117             pm.close();
118           }
119         } catch (Exception JavaDoc x) {
120             throw new LanguageException(x);
121         }
122     }
123     
124     /**
125      * @param languageID ISO639-2 language code
126      * @param throwExceptionIfNotExistent whether to return null or to throw a
127      * LanguageNotFoundException if desired language does not exist.
128      * @return An instance of the desired Language.
129      * @throws LanguageNotFoundException If the desired Language does not exist and
130      * throwExceptionIfNotExistent is true.
131      * This exception is an inheritor of LanguageException
132      * @throws LanguageException If it's not a LanguageNotFoundException, sth. unexpected
133      * happened - maybe the PersistenceManager is not accessible.
134      *
135      * @see getLanguage(String languageID)
136      */

137     public Language getLanguage(String JavaDoc languageID, boolean throwExceptionIfNotExistent)
138     throws LanguageException
139     {
140         try {
141             PersistenceManager pm = getPersistenceManager();
142             try {
143                 Language lang = (Language)pm.getObjectById(LanguageID.create(languageID), true);
144                 if (lang==null && throwExceptionIfNotExistent)
145                     throw new LanguageNotFoundException("No language registered with languageID=\""+languageID+"\"!");
146                 return lang;
147             } finally {
148                 pm.close();
149             }
150         } catch (Exception JavaDoc x) {
151             throw new LanguageException(x);
152         }
153     }
154
155     /**
156      * @param languageID ISO639-2 language code
157      * @return An instance of the desired Language. Never null!
158      * @throws LanguageNotFoundException If the desired Language does not exist.
159      * This exception is an inheritor of LanguageException
160      * @throws LanguageException If it's not a LanguageNotFoundException, sth. unexpected
161      * happened - maybe the PersistenceManager is not accessible.
162      *
163      * @see getLanguage(String languageID, boolean throwExceptionIfNotExistent)
164      */

165     public Language getLanguage(String JavaDoc languageID)
166     throws LanguageException
167     {
168         return getLanguage(languageID, true);
169     }
170
171 }
Popular Tags