1 4 5 package org.enhydra.shark.appmappersistence; 6 import java.util.ArrayList ; 7 import java.util.List ; 8 9 import net.sf.hibernate.Query; 10 import net.sf.hibernate.Session; 11 import net.sf.hibernate.Transaction; 12 13 import org.enhydra.shark.api.ApplicationMappingTransaction; 14 import org.enhydra.shark.api.RootException; 15 import org.enhydra.shark.api.TransactionException; 16 import org.enhydra.shark.api.internal.appmappersistence.ApplicationMap; 17 import org.enhydra.shark.api.internal.appmappersistence.ApplicationMappingManager; 18 import org.enhydra.shark.api.internal.working.CallbackUtilities; 19 import org.enhydra.shark.utilities.hibernate.HibernateUtilities; 20 21 26 public class HibernateApplicationMappingMgr implements ApplicationMappingManager { 27 private final String COUNTER_NAME = "APPLICATIONMAPPINGS"; 28 29 public static boolean _debug_ = false; 30 31 private static final String DBG_PARAM_NAME = "HibernateApplicationMappingMgr.debug"; 32 33 public void configure(CallbackUtilities cus) throws RootException { 34 if (null == cus) 35 throw new RootException("Cannot configure without call back impl."); 36 _debug_ = Boolean.valueOf(cus.getProperty(DBG_PARAM_NAME, "false")).booleanValue(); 37 } 38 39 50 public boolean saveApplicationMapping(ApplicationMappingTransaction trans, ApplicationMap am) throws RootException { 51 boolean retVal = true; 52 if (!checkValidity(am)) { 53 throw new RootException("Application mapping is not valid"); 54 } 55 try { 56 Session session = ((HibernateApplicationMappingTransaction) trans).getSession(); 57 HibernateApplicationMap hibAM = (HibernateApplicationMap) createApplicationMap(); 58 59 if (am.getApplicationMode() != null) { 60 hibAM.setApplicationMode(new Integer (am.getApplicationMode().intValue())); 61 } 62 hibAM.setApplicationName(am.getApplicationName()); 63 hibAM.setUsername(am.getUsername()); 64 hibAM.setPassword(am.getPassword()); 65 hibAM.setPackageId(am.getPackageId()); 66 hibAM.setProcessDefinitionId(am.getProcessDefinitionId()); 67 hibAM.setToolAgentClassName(am.getToolAgentClassName()); 68 hibAM.setApplicationDefinitionId(am.getApplicationDefinitionId()); 69 hibAM.setId(HibernateUtilities.getNext(COUNTER_NAME).intValue()); 70 71 session.save(hibAM); 72 } catch (Exception e) { 73 e.printStackTrace(); 74 throw new RootException(e); 75 } 76 return retVal; 77 } 78 79 91 public boolean deleteApplicationMapping(ApplicationMappingTransaction trans, ApplicationMap am) throws RootException { 92 boolean retVal = true; 93 try { 94 Session session = ((HibernateApplicationMappingTransaction) trans).getSession(); 95 96 String pDefId = am.getProcessDefinitionId(); 97 if (pDefId == null || pDefId.trim().equals("")) { 98 pDefId = HibernateUtilities.NULL_VALUE_FOR_PROCID; 99 } 100 Query qApplicationMapQuery = 101 session.createQuery( 102 "from HibernateApplicationMap appMap where appMap.packageId = :keyValueParam_1 and " 103 + "appMap.processDefId = :keyValueParam_2 and " 104 + "appMap.applicationDefinitionId = :keyValueParam_3"); 105 qApplicationMapQuery.setString("keyValueParam_1", am.getPackageId()); 106 qApplicationMapQuery.setString("keyValueParam_2", pDefId); 107 qApplicationMapQuery.setString("keyValueParam_3", am.getApplicationDefinitionId()); 108 HibernateApplicationMap hibAppMap = (HibernateApplicationMap) qApplicationMapQuery.uniqueResult(); 109 110 if (hibAppMap != null) { 112 session.delete(hibAppMap); 113 } else 114 throw new RootException("Object does not exist in database !"); 115 } catch (Exception e) { 116 e.printStackTrace(); 117 throw new RootException(e); 118 } 119 return retVal; 120 } 121 122 public List getAllApplicationMappings(ApplicationMappingTransaction trans) throws RootException { 123 List list = new ArrayList (); 124 try { 125 Session session = ((HibernateApplicationMappingTransaction) trans).getSession(); 126 session.clear(); 127 Query qApplicationMapQuery = session.createQuery("from HibernateApplicationMap appMap"); 128 list = qApplicationMapQuery.list(); 129 return list; 130 131 } catch (Exception e) { 132 e.printStackTrace(); 133 throw new RootException(e); 134 } 135 } 136 137 public ApplicationMap createApplicationMap() { 138 return new HibernateApplicationMap(); 139 } 140 141 public boolean deleteApplicationMapping(ApplicationMappingTransaction trans, String packageId, String processDefinitionId, String applicationId) 142 throws RootException { 143 boolean retVal = true; 144 ApplicationMap am = this.createApplicationMap(); 145 am.setPackageId(packageId); 146 am.setProcessDefinitionId(processDefinitionId); 147 am.setApplicationDefinitionId(applicationId); 148 return this.deleteApplicationMapping(trans, am); 149 } 150 151 167 public ApplicationMap getApplicationMap(ApplicationMappingTransaction trans, String pkgId, String pDefId, String appDefId) throws RootException { 168 ApplicationMap am = null; 169 try { 170 Session session = ((HibernateApplicationMappingTransaction) trans).getSession(); 171 session.clear(); 172 Query qApplicationMapQuery = 173 session.createQuery( 174 "from HibernateApplicationMap appMap where appMap.packageId = :keyValueParam_1 and " 175 + "appMap.processDefId = :keyValueParam_2 and " 176 + "appMap.applicationDefinitionId = :keyValueParam_3"); 177 qApplicationMapQuery.setString("keyValueParam_1", pkgId); 178 qApplicationMapQuery.setString("keyValueParam_2", pDefId); 179 qApplicationMapQuery.setString("keyValueParam_3", appDefId); 180 181 ApplicationMap appMap = (HibernateApplicationMap) qApplicationMapQuery.uniqueResult(); 182 return appMap; 183 184 } catch (Exception e) { 185 e.printStackTrace(); 186 throw new RootException(e); 187 } 188 } 189 190 198 private Transaction getDBTransaction(ApplicationMappingTransaction t) { 199 try { 200 if (t instanceof HibernateApplicationMappingTransaction) 201 return ((HibernateApplicationMappingTransaction) t).getHibernateTransaction(); 202 } catch (Exception e) { 203 e.printStackTrace(); 204 } 205 return null; 207 } 208 209 public boolean checkValidity(ApplicationMap am) { 210 if (am == null 211 || am.getPackageId() == null 212 || am.getPackageId().trim().equals("") 213 || am.getApplicationDefinitionId() == null 214 || am.getApplicationDefinitionId().trim().equals("") 215 || am.getToolAgentClassName() == null 216 || am.getToolAgentClassName().trim().equals("") 217 || am.getApplicationName() == null 218 || am.getApplicationName().trim().equals("")) { 219 return false; 220 } else { 221 return true; 222 } 223 } 224 225 public ApplicationMappingTransaction getApplicationMappingTransaction() throws TransactionException { 226 try { 227 return new HibernateApplicationMappingTransaction(ThreadLocalSession.currentSession().beginTransaction()); 228 } catch (Exception ex) { 229 throw new TransactionException(ex); 230 } 231 } 232 233 } 234 | Popular Tags |