KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > smartValueObject > demo > SVODemoUserBean


1 package org.bsf.smartValueObject.demo;
2
3 import org.bsf.commons.ejb.SessionAdapterBean;
4 import org.bsf.smartValueObject.mediator.*;
5
6 import java.util.*;
7 import java.lang.reflect.Constructor JavaDoc;
8
9 /**
10  * This stateful bean is the one used by each client to simulate a
11  * persistence service.
12  *
13  * @ejb:bean type="Stateful"
14  * name="SVODemoUser"
15  * jndi-name="ejb/SVODemoUser"
16  * view-type="both"
17  *
18  * @ejb:home local-extends="javax.ejb.EJBLocalHome"
19  * extends="javax.ejb.EJBHome"
20  *
21  * @ejb:interface local-extends="javax.ejb.EJBLocalObject"
22  * extends="javax.ejb.EJBObject"
23  *
24  * @ejb:transaction type="Required"
25  *
26  * @ejb.env-entry name="mediator.class_name"
27  * value="org.bsf.smartValueObject.mediator.HibernateMediator"
28  *
29  */

30 public class SVODemoUserBean extends SessionAdapterBean {
31
32     private Mediator userMediator;
33
34
35     /**
36      * @ejb:interface-method
37      */

38     public void ejbCreate() {
39         /*
40         TODO deprecated, throws exception
41
42         String mediatorClass = getEJBContext()
43                 .getEnvironment()
44                 .getProperty("mediator.class_name");
45         */

46
47         String JavaDoc mediatorClass = "org.bsf.smartValueObject.mediator.HibernateMediator";
48         logInfo("using " + mediatorClass + " as mediator impl");
49         try {
50             Class JavaDoc clazz =
51                 SVODemoUserBean.class.getClassLoader().loadClass(mediatorClass);
52
53             Constructor JavaDoc ctor;
54             try {
55                ctor = clazz.getConstructor(new Class JavaDoc[] { MediatorConfig.class });
56                userMediator = (Mediator)
57                         ctor.newInstance( new Object JavaDoc[] { new DemoConfig() } );
58             } catch (NoSuchMethodException JavaDoc e) {
59                userMediator = (Mediator) clazz.newInstance();
60             }
61
62         } catch(Exception JavaDoc e) {
63             logError("error", e);
64             handleExceptionAsSystemException(e);
65         }
66
67         try {
68             userMediator.updateGraph( buildUserGraph() );
69         } catch( MediatorException e ) {
70             handleExceptionAsSystemException( e );
71         }
72         logInfo( "SVODemoUser created..." );
73     }
74
75     /**
76      * @ejb:interface-method
77      */

78     public Object JavaDoc getCompanyVo( Object JavaDoc propotype ) {
79         Object JavaDoc result = null;
80         try {
81             result = userMediator.getGraph( propotype );
82         } catch( MediatorException e ) {
83             handleExceptionAsSystemException( e );
84         }
85         return result;
86     }
87
88     /**
89      * @ejb:interface-method
90      */

91     public String JavaDoc updateCompanyVo( Object JavaDoc propotype ) throws org.bsf.smartValueObject.mediator.ConcurrencyException {
92         try {
93             ChangeSummary cs = userMediator.updateGraph( propotype );
94             return cs.getReport();
95         } catch( MediatorException e ) {
96             if ( e instanceof ConcurrencyException ) {
97                 ConcurrencyException ce = (ConcurrencyException) e;
98                 throw ce;
99             } else {
100                 handleExceptionAsSystemException( e );
101             }
102         }
103         return null;
104     }
105
106     /**
107      * Simulates a concurrent modification on the graph
108      *
109      * @ejb:interface-method
110      */

111     public void modifyConcurrently() {
112         try {
113             CompanyVO vo = new CompanyVO();
114             vo.setId( new Long JavaDoc( 0 ) );
115             CompanyVO comp = (CompanyVO) userMediator.getGraph( vo );
116             Iterator subIt = comp.subsidiaries();
117             while ( subIt.hasNext() ) {
118                 SubsidiaryVO subsidiaryVO = (SubsidiaryVO) subIt.next();
119                 subsidiaryVO.setName( subsidiaryVO.getName() + " (Modified)" );
120                 break;
121             }
122             userMediator.updateGraph( comp );
123         } catch( MediatorException e ) {
124             handleExceptionAsSystemException( e );
125         }
126
127     }
128
129     private static final Object JavaDoc buildUserGraph() {
130         CompanyVO comp = new CompanyVO();
131         comp.setName( "The big big company" );
132
133         // set time to actual date, 0:00:00
134
Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "Europe/Paris" ) );
135         cal.set( Calendar.HOUR_OF_DAY, 12 ); // For the $@&#! offset, keep the same day
136
cal.set( Calendar.MINUTE, 0 );
137         cal.set( Calendar.SECOND, 0 );
138         cal.set( Calendar.MILLISECOND, 0 );
139         comp.setCreationDate( cal.getTime() );
140
141         SubsidiaryVO sub = new SubsidiaryVO();
142         sub.setName( "Paris" );
143         sub.setWorkforce( 2 );
144         comp.addSubsidiary( sub );
145
146         sub = new SubsidiaryVO();
147         sub.setName( "London" );
148         sub.setWorkforce( 1 );
149         comp.addSubsidiary( sub );
150
151         sub = new SubsidiaryVO();
152         sub.setName( "Berlin" );
153         sub.setWorkforce( 1 );
154         comp.addSubsidiary( sub );
155
156         return comp;
157     }
158
159     /*
160     private static class DemoConfig implements MediatorConfig {
161         private static final Collection classes = new ArrayList();
162
163         static {
164             classes.add(org.bsf.smartValueObject.demo.CompanyVO.class);
165             classes.add(org.bsf.smartValueObject.demo.SubsidiaryVO.class);
166         }
167
168
169         public Collection getClasses() {
170             return classes;
171         }
172     }
173     */

174 }
175
Popular Tags