001: /**
002: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Aizaz Ahmed
022: * Vivek Lakshmanan
023: * Andrew Overholt
024: * Matthew Wringe
025: *
026: */package olstore.session.helper;
027:
028: import java.util.Collection;
029: import java.util.Iterator;
030: import java.util.Vector;
031:
032: import javax.ejb.CreateException;
033: import javax.ejb.SessionBean;
034: import javax.ejb.SessionContext;
035:
036: import olstore.dto.PropertyValue;
037: import olstore.dto.TypeValue;
038: import olstore.framework.EJBHomeFactory;
039:
040: import org.apache.commons.beanutils.BeanUtils;
041:
042: import olstore.entity.PropertyLocal;
043: import olstore.entity.PropertyLocalHome;
044: import olstore.entity.TypeLocalHome;
045:
046: /**
047: * @ejb.bean name="TypeHelper"
048: * type="Stateless"
049: * view-type="local"
050: * local-jndi-name="TypeHelperLocal_L"
051: *
052: * @ejb.ejb-ref
053: * ejb-name="Type"
054: * view-type="local"
055: *
056: * @ejb.ejb-ref
057: * ejb-name="Property"
058: * view-type="local"
059: *
060: * @ejb.transaction
061: * type="Required"
062: *
063: *--
064: * This is needed for JOnAS.
065: * If you are not using JOnAS you can safely remove the tags below.
066: * @jonas.bean ejb-name="TypeHelper"
067: * jndi-name="TypeHelperLocal"
068: *
069: *--
070: **/
071:
072: public abstract class TypeHelperBean implements SessionBean {
073:
074: SessionContext ejbContext;
075:
076: // ------------------------------------------------------------------
077: // SessionBean implementation
078: // ------------------------------------------------------------------
079:
080: public void setSessionContext(SessionContext ctx) {
081: ejbContext = ctx;
082: }
083:
084: public void ejbRemove() {
085: }
086:
087: public void ejbCreate() throws CreateException {
088: }
089:
090: public void ejbPassivate() {
091: }
092:
093: public void ejbActivate() {
094: }
095:
096: // ------------------------------------------------------------------
097: // TypeHelper implementation
098: // ------------------------------------------------------------------
099:
100: /**
101: *
102: * @ejb.interface-method
103: *
104: */
105:
106: public TypeValue getTypeValueForName(String name) throws Exception {
107:
108: EJBHomeFactory factory = EJBHomeFactory.getInstance();
109: TypeLocalHome typeHome = (TypeLocalHome) factory
110: .getLocalHome(EJBHomeFactory.TYPE);
111: olstore.entity.TypeLocal type = typeHome.findByName(name);
112: TypeValue typeVal = TypeToDTO(type);
113: return typeVal;
114: }
115:
116: /**
117: *
118: * @ejb.interface-method
119: *
120: */
121: public void saveType(TypeValue typeVal) throws Exception {
122:
123: //determine if it already exists
124: EJBHomeFactory factory = EJBHomeFactory.getInstance();
125: TypeLocalHome typeHome = (TypeLocalHome) factory
126: .getLocalHome(EJBHomeFactory.TYPE);
127: olstore.entity.TypeLocal type = null;
128:
129: String name = typeVal.getName();
130: try {
131: type = typeHome.findByName(name);
132: } catch (Exception e) {
133: //could not find the Type, try and create a new
134: //one by this name
135: type = typeHome.create(name);
136: }
137:
138: updateType(type, typeVal);
139: }
140:
141: private void updateType(olstore.entity.TypeLocal type,
142: TypeValue typeVal) throws Exception {
143:
144: //Get instance vars we will need
145: EJBHomeFactory factory = EJBHomeFactory.getInstance();
146: PropertyLocalHome propHome = (PropertyLocalHome) factory
147: .getLocalHome(EJBHomeFactory.PROP);
148: TypeLocalHome typeHome = (TypeLocalHome) factory
149: .getLocalHome(EJBHomeFactory.TYPE);
150:
151: //Clear any existing Properties
152: Collection props = type.getProperties();
153: Iterator propIt = props.iterator();
154: while (propIt.hasNext()) {
155: PropertyLocal prop = (PropertyLocal) propIt.next();
156: propIt.remove();
157: prop.remove();
158: }
159:
160: //Load new Properties
161: Collection newProps = typeVal.getProperties();
162: Iterator newPropIt = newProps.iterator();
163: while (newPropIt.hasNext()) {
164: PropertyValue newProp = (PropertyValue) newPropIt.next();
165: PropertyLocal newPropLocal = propHome.create(newProp
166: .getName(), newProp.getValue());
167: props.add(newPropLocal);
168: }
169:
170: }
171:
172: private TypeValue TypeToDTO(olstore.entity.TypeLocal type)
173: throws Exception {
174: TypeValue typeVal = new TypeValue();
175: type.setName(typeVal.getName());
176:
177: //properties
178: Vector newProps = new Vector();
179: Collection oldProps = type.getProperties();
180: Iterator it = oldProps.iterator();
181: while (it.hasNext()) {
182: PropertyLocal prop = (PropertyLocal) it.next();
183: PropertyValue propVal = new PropertyValue();
184: BeanUtils.copyProperties(propVal, prop);
185: newProps.add(propVal);
186: }
187: typeVal.setProperties(newProps);
188:
189: return typeVal;
190: }
191:
192: }
|