1 27 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 78 79 public abstract class TypeHelperBean implements SessionBean { 80 81 SessionContext ejbContext; 82 83 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 110 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 130 public void saveType ( TypeValue typeVal ) throws Exception { 131 132 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 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 EJBHomeFactory factory = EJBHomeFactory.getInstance(); 153 PropertyLocalHome propHome = (PropertyLocalHome) factory.getLocalHome ( EJBHomeFactory.PROP ); 154 TypeLocalHome typeHome = (TypeLocalHome) factory.getLocalHome ( EJBHomeFactory.TYPE ); 155 156 157 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 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 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 |