1 5 package com.nightlabs.ipanema.language; 6 import java.rmi.RemoteException ; 7 import java.util.Collection ; 8 9 import javax.ejb.CreateException ; 10 import javax.ejb.EJBException ; 11 import javax.ejb.SessionBean ; 12 import javax.ejb.SessionContext ; 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 29 public abstract class LanguageManagerBean extends BaseSessionBeanImpl implements SessionBean 30 { 31 public static final Logger LOGGER = Logger.getLogger(LanguageManagerBean.class); 32 35 public void setSessionContext(SessionContext sessionContext) 36 throws EJBException , RemoteException 37 { 38 super.setSessionContext(sessionContext); 39 } 40 44 public void ejbCreate() throws CreateException 45 { 46 } 55 60 public void ejbRemove() throws EJBException , RemoteException { } 61 62 72 public void createLanguage(String languageID, String 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 e) { 93 LOGGER.debug("exception: " + e.getMessage()); 94 throw new LanguageException(e); 95 } 96 } 97 98 99 108 public Collection getLanguages() 109 throws LanguageException 110 { 111 try { 112 PersistenceManager pm = getPersistenceManager(); 113 try { 114 pm.getFetchPlan().addGroup(FetchPlan.ALL); 115 return pm.detachCopyAll((Collection )pm.newQuery(Language.class).execute()); 116 } finally { 117 pm.close(); 118 } 119 } catch (Exception x) { 120 throw new LanguageException(x); 121 } 122 } 123 124 137 public Language getLanguage(String 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 x) { 151 throw new LanguageException(x); 152 } 153 } 154 155 165 public Language getLanguage(String languageID) 166 throws LanguageException 167 { 168 return getLanguage(languageID, true); 169 } 170 171 } | Popular Tags |