KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > appmappersistence > HibernateApplicationMappingMgr


1 /*
2  * Shark Hibernate AppMapPersistence - Open Wide
3  */

4
5 package org.enhydra.shark.appmappersistence;
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
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 /**
22  * Implementation of ApplicationMappings interface
23  *
24  * @author Vladislav Pernin
25  */

26 public class HibernateApplicationMappingMgr implements ApplicationMappingManager {
27     private final String JavaDoc COUNTER_NAME = "APPLICATIONMAPPINGS";
28
29     public static boolean _debug_ = false;
30
31     private static final String JavaDoc 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     /**
40      * Method saveApplicationMapping save new application mapping in database.
41      *
42      * @param trans
43      * a MappingTransaction
44      * @param am
45      * an ApplicationMap
46      * @return a boolean
47      * @exception RootException
48      *
49      */

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 JavaDoc(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 JavaDoc e) {
73             e.printStackTrace();
74             throw new RootException(e);
75         }
76         return retVal;
77     }
78
79     /**
80      * Method deleteApplicationMapping delete specified application mapping
81      * from database.
82      *
83      * @param trans
84      * a MappingTransaction
85      * @param am
86      * an ApplicationMap
87      * @return a boolean
88      * @exception RootException
89      *
90      */

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 JavaDoc 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             //delete object
111
if (hibAppMap != null) {
112                 session.delete(hibAppMap);
113             } else
114                 throw new RootException("Object does not exist in database !");
115         } catch (Exception JavaDoc e) {
116             e.printStackTrace();
117             throw new RootException(e);
118         }
119         return retVal;
120     }
121
122     public List JavaDoc getAllApplicationMappings(ApplicationMappingTransaction trans) throws RootException {
123         List JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc packageId, String JavaDoc processDefinitionId, String JavaDoc 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     /**
152      * Method getApplicationMap finds application map in database.
153      *
154      * @param trans
155      * a MappingTransaction
156      * @param pkgId
157      * an Integer
158      * @param pDefId
159      * an Integer
160      * @param appDefId
161      * an Integer
162      * @return an ApplicationMap, or null if map do not exist for specified
163      * params
164      * @exception RootException
165      *
166      */

167     public ApplicationMap getApplicationMap(ApplicationMappingTransaction trans, String JavaDoc pkgId, String JavaDoc pDefId, String JavaDoc 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 JavaDoc e) {
185             e.printStackTrace();
186             throw new RootException(e);
187         }
188     }
189
190     /**
191      * Method getDBTransaction create Hibernate DBTransaction.
192      *
193      * @param t
194      * a MappingTransaction
195      * @return a DBTransaction
196      *
197      */

198     private Transaction getDBTransaction(ApplicationMappingTransaction t) {
199         try {
200             if (t instanceof HibernateApplicationMappingTransaction)
201                 return ((HibernateApplicationMappingTransaction) t).getHibernateTransaction();
202         } catch (Exception JavaDoc e) {
203             e.printStackTrace();
204         }
205         //error
206
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 JavaDoc ex) {
229             throw new TransactionException(ex);
230         }
231     }
232     
233 }
234
Popular Tags