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 ; 8 9 30 public class SVODemoUserBean extends SessionAdapterBean { 31 32 private Mediator userMediator; 33 34 35 38 public void ejbCreate() { 39 46 47 String mediatorClass = "org.bsf.smartValueObject.mediator.HibernateMediator"; 48 logInfo("using " + mediatorClass + " as mediator impl"); 49 try { 50 Class clazz = 51 SVODemoUserBean.class.getClassLoader().loadClass(mediatorClass); 52 53 Constructor ctor; 54 try { 55 ctor = clazz.getConstructor(new Class [] { MediatorConfig.class }); 56 userMediator = (Mediator) 57 ctor.newInstance( new Object [] { new DemoConfig() } ); 58 } catch (NoSuchMethodException e) { 59 userMediator = (Mediator) clazz.newInstance(); 60 } 61 62 } catch(Exception 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 78 public Object getCompanyVo( Object propotype ) { 79 Object result = null; 80 try { 81 result = userMediator.getGraph( propotype ); 82 } catch( MediatorException e ) { 83 handleExceptionAsSystemException( e ); 84 } 85 return result; 86 } 87 88 91 public String updateCompanyVo( Object 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 111 public void modifyConcurrently() { 112 try { 113 CompanyVO vo = new CompanyVO(); 114 vo.setId( new Long ( 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 buildUserGraph() { 130 CompanyVO comp = new CompanyVO(); 131 comp.setName( "The big big company" ); 132 133 Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "Europe/Paris" ) ); 135 cal.set( Calendar.HOUR_OF_DAY, 12 ); 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 174 } 175 | Popular Tags |