KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > session > helper > TypeHelperBean


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Aizaz Ahmed
22  * Vivek Lakshmanan
23  * Andrew Overholt
24  * Matthew Wringe
25  *
26  */

27 /*
28  * TypeHelperBean.java
29  *
30  * Created on Aug 26, 2004
31  *
32  */

33 package olstore.session.helper;
34
35 import java.util.Collection;
36 import java.util.Iterator;
37 import java.util.Vector;
38
39 import javax.ejb.CreateException;
40 import javax.ejb.SessionBean;
41 import javax.ejb.SessionContext;
42
43 import olstore.dto.PropertyValue;
44 import olstore.dto.TypeValue;
45 import olstore.framework.EJBHomeFactory;
46
47 import org.apache.commons.beanutils.BeanUtils;
48
49 import olstore.entity.PropertyLocal;
50 import olstore.entity.PropertyLocalHome;
51 import olstore.entity.TypeLocalHome;
52
53 /**
54  * @ejb.bean name="TypeHelper"
55  * type="Stateless"
56  * view-type="local"
57  * local-jndi-name="TypeHelperLocal_L"
58  *
59  * @ejb.ejb-ref
60  * ejb-name="Type"
61  * view-type="local"
62  *
63  * @ejb.ejb-ref
64  * ejb-name="Property"
65  * view-type="local"
66  *
67  * @ejb.transaction
68  * type="Required"
69  *
70  *--
71  * This is needed for JOnAS.
72  * If you are not using JOnAS you can safely remove the tags below.
73  * @jonas.bean ejb-name="TypeHelper"
74  * jndi-name="TypeHelperLocal"
75  *
76  *--
77  **/

78
79 public abstract class TypeHelperBean implements SessionBean {
80     
81     SessionContext ejbContext;
82     
83     // ------------------------------------------------------------------
84
// SessionBean implementation
85
// ------------------------------------------------------------------
86

87     
88     public void setSessionContext(SessionContext ctx) {
89         ejbContext = ctx;
90     }
91     
92     
93     public void ejbRemove() {
94     }
95     
96     
97     public void ejbCreate() throws CreateException {
98     }
99     
100     public void ejbPassivate() {
101     }
102     
103     public void ejbActivate() {
104     }
105     
106     // ------------------------------------------------------------------
107
// TypeHelper implementation
108
// ------------------------------------------------------------------
109

110     /**
111      *
112      * @ejb.interface-method
113      *
114      */

115     
116     public TypeValue getTypeValueForName ( String name ) throws Exception {
117         
118         EJBHomeFactory factory = EJBHomeFactory.getInstance();
119         TypeLocalHome typeHome = (TypeLocalHome) factory.getLocalHome ( EJBHomeFactory.TYPE );
120         olstore.entity.TypeLocal type = typeHome.findByName ( name );
121         TypeValue typeVal = TypeToDTO ( type );
122         return typeVal;
123     }
124     
125     /**
126      *
127      * @ejb.interface-method
128      *
129      */

130     public void saveType ( TypeValue typeVal ) throws Exception {
131         
132         //determine if it already exists
133
EJBHomeFactory factory = EJBHomeFactory.getInstance();
134         TypeLocalHome typeHome = (TypeLocalHome) factory.getLocalHome ( EJBHomeFactory.TYPE );
135         olstore.entity.TypeLocal type = null;
136         
137         String name = typeVal.getName();
138         try {
139             type = typeHome.findByName ( name );
140         } catch ( Exception e ) {
141             //could not find the Type, try and create a new
142
//one by this name
143
type = typeHome.create ( name );
144         }
145         
146         updateType ( type, typeVal );
147     }
148     
149     private void updateType ( olstore.entity.TypeLocal type , TypeValue typeVal ) throws Exception {
150         
151         //Get instance vars we will need
152
EJBHomeFactory factory = EJBHomeFactory.getInstance();
153         PropertyLocalHome propHome = (PropertyLocalHome) factory.getLocalHome ( EJBHomeFactory.PROP );
154         TypeLocalHome typeHome = (TypeLocalHome) factory.getLocalHome ( EJBHomeFactory.TYPE );
155         
156         
157         //Clear any existing Properties
158
Collection props = type.getProperties();
159         Iterator propIt = props.iterator();
160         while ( propIt.hasNext() ) {
161             PropertyLocal prop = (PropertyLocal) propIt.next();
162             propIt.remove();
163             prop.remove();
164         }
165         
166         //Load new Properties
167
Collection newProps = typeVal.getProperties();
168         Iterator newPropIt = newProps.iterator();
169         while ( newPropIt.hasNext() ) {
170             PropertyValue newProp = (PropertyValue) newPropIt.next();
171             PropertyLocal newPropLocal = propHome.create (
172                     newProp.getName(), newProp.getValue() );
173             props.add ( newPropLocal );
174         }
175         
176     }
177     
178     
179     private TypeValue TypeToDTO ( olstore.entity.TypeLocal type ) throws Exception {
180         TypeValue typeVal = new TypeValue();
181         type.setName ( typeVal.getName() );
182         
183         //properties
184
Vector newProps = new Vector();
185         Collection oldProps = type.getProperties();
186         Iterator it = oldProps.iterator();
187         while ( it.hasNext() ) {
188             PropertyLocal prop = (PropertyLocal)it.next();
189             PropertyValue propVal = new PropertyValue();
190             BeanUtils.copyProperties ( propVal, prop );
191             newProps.add ( propVal );
192         }
193         typeVal.setProperties ( newProps );
194         
195         return typeVal;
196     }
197     
198 }
199
Popular Tags