Source code: com/clra/rowing/RowingUtils.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: RowingUtils.java,v $
5 * $Date: 2003/02/26 03:38:45 $
6 * $Revision: 1.3 $
7 */
8
9 package com.clra.rowing;
10
11 import java.io.IOException;
12 import java.lang.reflect.InvocationTargetException;
13 import java.rmi.RemoteException;
14 import java.util.Date;
15 import java.util.Locale;
16 import java.util.Vector;
17 import javax.ejb.CreateException;
18 import javax.ejb.EJBException;
19 import javax.ejb.EJBHome;
20 import javax.ejb.EJBObject;
21 import javax.ejb.FinderException;
22 import javax.ejb.Handle;
23 import javax.ejb.RemoveException;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import javax.rmi.PortableRemoteObject;
27 import org.apache.log4j.Category;
28
29 /**
30 * Utilities for finding and creating "base" entities: Member, RowingSession,
31 * Boat, and Oarset. Other entities are created by operations on these base
32 * entities.</p><p>
33 *
34 * These utilities are implemented by calls to EJB's. They are appropriate
35 * where entities should be cached in memory, perhaps because the entities
36 * will be modified shortly.</p><p>
37 *
38 * The class RowingDBRead defines utilities with similar signatures that
39 * are implemented by directly reading from the database. These operations
40 * are faster if objects are not already in memory, and if the objects
41 * are not anticipated to require modification.</p>
42 *
43 * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
44 * @version $Revision: 1.3 $ $Date: 2003/02/26 03:38:45 $
45 */
46 public final class RowingUtils {
47
48 private final static String base = RowingUtils.class.getName();
49 private final static Category theLog = Category.getInstance( base );
50
51 private static IRowingSessionHome _homeRowingSession = null;
52 private static IParticipantHome _homeParticipant = null;
53
54 private static IRowingSessionHome lookupRowingSessionHome()
55 throws NamingException {
56
57 IRowingSessionHome retVal = null;
58
59 InitialContext jndiContext = new InitialContext();
60 Object ref = jndiContext.lookup( Configuration.ROWINGSESSION_HOME() );
61 retVal = (IRowingSessionHome)
62 PortableRemoteObject.narrow (ref, IRowingSessionHome.class);
63
64 return retVal;
65 } // lookupRowingSessionHome()
66
67 private static IParticipantHome lookupParticipantHome()
68 throws NamingException {
69
70 IParticipantHome retVal = null;
71
72 InitialContext jndiContext = new InitialContext();
73 Object ref = jndiContext.lookup( Configuration.PARTICIPANT_HOME() );
74 retVal = (IParticipantHome)
75 PortableRemoteObject.narrow (ref, IParticipantHome.class);
76
77 return retVal;
78 } // lookupParticipantHome()
79
80 /**
81 * Returns the factory for RowingSession instances.<p>
82 *
83 * <strong>
84 * Note: this operation should be used only by this class, unit tests,
85 * and the implementation class for IRowingSession
86 * </strong>
87 */
88 public static IRowingSessionHome getRowingSessionHome()
89 throws NamingException {
90
91 if ( _homeRowingSession == null ) {
92 _homeRowingSession = lookupRowingSessionHome();
93 }
94 return _homeRowingSession;
95 }
96
97 /**
98 * Returns the factory for Participant instances.<p>
99 *
100 * <strong>
101 * Note: this operation should be used only by this class, unit tests,
102 * and the implementation class for IParticipant
103 * </strong>
104 */
105 public static IParticipantHome getParticipantHome()
106 throws NamingException {
107
108 if ( _homeParticipant == null ) {
109 _homeParticipant = lookupParticipantHome();
110 }
111 return _homeParticipant;
112 }
113
114 /** Returns the rowing session specified by the rowing id */
115 public static IRowingSession findRowingSession( Integer rowingId )
116 throws RemoteException, FinderException, NamingException {
117
118 IRowingSession retVal = null;
119 try {
120 retVal = getRowingSessionHome().findByPrimaryKey( rowingId );
121 }
122 catch( RemoteException x ) {
123 theLog.error( "RowingUtils.findRowingSession: " + x.getMessage(), x );
124 // Null out the session factory; it will be recreated on next call
125 _homeRowingSession = null;
126 throw x;
127 }
128 catch( FinderException x ) {
129 theLog.error( "RowingUtils.findRowingSession: " + x.getMessage(), x );
130 throw x;
131 }
132 catch( EJBException x ) {
133 theLog.error( "RowingUtils.findRowingSession: " + x.getMessage(), x );
134 throw x;
135 }
136
137 return retVal;
138 } // findRowingSession(Integer)
139
140 /** Creates a rowing session */
141 public static IRowingSession createRowingSession( Date date,
142 RowingSessionLevel level, RowingSessionType type )
143 throws RemoteException, CreateException, NamingException {
144
145 IRowingSession retVal = null;
146 try {
147 retVal = getRowingSessionHome().create( date, level, type );
148 // createRowingSessionList().addRowingSession( retVal );
149 }
150 catch( CreateException x ) {
151 theLog.error( "RowingUtils.findRowingSession: " + x.getMessage(), x );
152 throw x;
153 }
154 catch( EJBException x ) {
155 theLog.error( "RowingUtils.findRowingSession: " + x.getMessage(), x );
156 throw x;
157 }
158
159 return retVal;
160 } // createRowingSession(..)
161
162 /** Returns the participant specified by the participant id */
163 public static IParticipant findParticipant( Integer participantId )
164 throws RemoteException, FinderException, NamingException {
165
166 IParticipant retVal = null;
167 try {
168 retVal = getParticipantHome().findByPrimaryKey( participantId );
169 }
170 catch( RemoteException x ) {
171 theLog.error( "RowingUtils.findParticipant: " + x.getMessage(), x );
172 // Null out the session factory; it will be recreated on next call
173 _homeParticipant = null;
174 throw x;
175 }
176 catch( FinderException x ) {
177 theLog.error( "RowingUtils.findParticipant: " + x.getMessage(), x );
178 throw x;
179 }
180 catch( EJBException x ) {
181 theLog.error( "RowingUtils.findParticipant: " + x.getMessage(), x );
182 throw x;
183 }
184
185 return retVal;
186 } // findParticipant(Integer)
187
188 /** Creates a participant */
189 public static IParticipant createParticipant( Integer memberId,
190 Integer rowingId, SeatPreference seatPreference )
191 throws RemoteException, CreateException, NamingException {
192
193 IParticipant retVal = null;
194 try {
195 retVal = getParticipantHome().create(memberId, rowingId, seatPreference);
196 }
197 catch( CreateException x ) {
198 theLog.error( "RowingUtils.findParticipant: " + x.getMessage(), x );
199 throw x;
200 }
201 catch( EJBException x ) {
202 theLog.error( "RowingUtils.findParticipant: " + x.getMessage(), x );
203 throw x;
204 }
205
206 return retVal;
207 } // createParticipant(..)
208
209 /**
210 * Returns a "fake" rowing session, one which is neither persistent nor
211 * managed by the EJB container. The primary key of the session (the session
212 * id) is null. Other session accessors return valid default values.
213 */
214 public static RowingSessionSnapshot createRowingSessionDefaults() {
215
216 // Used only for construction
217 final Integer FAKE_ID = new Integer( Integer.MIN_VALUE );
218
219 // An anonymous extension of RowingSessionSnapshot that returns a null id.
220 RowingSessionSnapshot retVal = new RowingSessionSnapshot(
221 // Used only for construction
222 FAKE_ID,
223 // A non-persistent state (should be TENATIVE?)
224 RowingSessionState.NEW,
225 // Today's date
226 new Date(),
227 RowingSessionLevel.REGULAR,
228 RowingSessionType.PRACTICE ) {
229
230 // Overrides parent to return a null id
231 public Integer getId() { return null; }
232 };
233
234 return retVal;
235 } // createRowingSessionDefaults()
236
237 } // RowingUtils
238
239 /*
240 * Log:$
241 */
242