Source code: com/flexstor/common/gateway/SequenceGateway.java
1 /*
2 * SequenceGateway.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:29 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.gateway;
12
13 import com.flexstor.common.constants.SequenceConstantsI;
14 import com.flexstor.common.data.ejb.sequence.SequenceData;
15 import com.flexstor.common.exceptions.ejb.NotFoundException;
16 import com.flexstor.common.gateway.debug.GatewayDebugOutput;
17 import com.flexstor.common.gateway.exceptions.TransactionFailedException;
18 import com.flexstor.ejb.EjbObject;
19 import com.flexstor.ejb.sequence.FlexSequence;
20 import com.flexstor.ejb.sequence.FlexSequenceHome;
21
22 /**
23 * Get the next sequence number for the defined sequence type.
24 * @author David Cardozo
25 * @version 3.0
26 */
27 public class SequenceGateway
28 extends Gateway
29 implements SequenceConstantsI
30 {
31 // MKS macro expander
32 public final static String IDENTIFIER = "$Id: SequenceGateway.java,v 1.3 2003/08/11 02:22:29 aleric Exp $";
33
34 private FlexSequence sequence = null;
35
36 protected String getHomeName ( )
37 {
38 return SEQUENCE_HOME;
39 }
40
41 protected EjbObject getBeanObject ( )
42 {
43 return sequence;
44 }
45
46 /**
47 * Sends the request for the sequence data to the server.
48 * @throws TransactionFailedException Throws if this transaction fails.
49 */
50 public void connect ()
51 throws TransactionFailedException
52 {
53 try
54 {
55 if ( canLoadObject() )
56 return;
57
58 FlexSequenceHome home = (FlexSequenceHome) getHome ( );
59
60 GatewayDebugOutput.println ( 3, "Getting FlexSequence object" );
61 sequence = home.create();
62 }
63 catch ( NotFoundException e )
64 {
65 throw new TransactionFailedException ( e );
66 }
67 catch ( Throwable e )
68 {
69 throw buildException(e, "Connecting to FlexSequence", IDENTIFIER );
70 }
71 }
72
73 /**
74 * Get the sequence number for the specified sequence type
75 * @param sSequenceType The type for which the next sequence is requested.
76 * @throws TransactionFailedException Throws if this transaction fails.
77 */
78 public long getSequence ( String sSequenceType )
79 throws TransactionFailedException
80 {
81 try
82 {
83 GatewayDebugOutput.println ( 3, "Getting Sequence Number for " + sSequenceType );
84
85 if ( canLoadObject() )
86 return ((Long) retrieveObject( "Sequence_getSequence" )).longValue();
87
88 long nSequence = sequence.getSequence( sSequenceType );
89
90 if ( canSaveObject() )
91 storeObject( "Sequence_getSequence", new Long(nSequence) );
92
93 return nSequence;
94 }
95 catch ( Throwable e )
96 {
97 throw buildException(e, "Getting Sequence", IDENTIFIER );
98 }
99 }
100
101 /**
102 * Get the sequence number for the specified sequence type.
103 * Checks against the database to make sure this id is not already in the table; if
104 * it is get the next sequence.
105 * @param sSequenceType The type for which the next sequence is requested.
106 * @throws TransactionFailedException Throws if this transaction fails.
107 */
108 public long getSequence ( String sSequenceType, String sTable, String sColumn )
109 throws TransactionFailedException
110 {
111 try
112 {
113 GatewayDebugOutput.println ( 3, "Getting Sequence Number for " + sSequenceType );
114
115 if ( canLoadObject() )
116 return ((Long) retrieveObject( "Sequence_getSequence" )).longValue();
117
118 long nSequence = sequence.getSequence( sSequenceType, sTable, sColumn );
119
120 if ( canSaveObject() )
121 storeObject( "Sequence_getSequence", new Long(nSequence) );
122
123 return nSequence;
124 }
125 catch ( Throwable e )
126 {
127 throw buildException(e, "Getting Sequence", IDENTIFIER );
128 }
129 }
130
131 /**
132 * Get a list of sequence numbers for the specified sequence type.
133 *
134 * @param sSequenceType The type for which the next sequence is requested.
135 * @param nCount The number of sequence numbers to return.
136 * @throws TransactionFailedException Throws if this transaction fails.
137 */
138 public SequenceData getSequenceData ( String sSequenceType, int nCount )
139 throws TransactionFailedException
140 {
141 try
142 {
143 GatewayDebugOutput.println ( 3, "Getting Sequence List for " + sSequenceType );
144
145 if ( canLoadObject() )
146 return (SequenceData)retrieveObject("Sequence_getSequenceData_" + sSequenceType + "_" + nCount );
147
148 SequenceData seq = sequence.getSequence( sSequenceType, nCount );
149
150 if ( canSaveObject() )
151 storeObject( "Sequence_getSequenceData", seq );
152
153 return seq;
154 }
155 catch ( Throwable e )
156 {
157 throw buildException(e, "Getting Sequence Data", IDENTIFIER );
158 }
159 }
160 }