-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransaction.java
207 lines (170 loc) · 5.63 KB
/
Transaction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// specify the package
package model;
// system imports
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import javax.swing.JFrame;
// project imports
import exception.InvalidPrimaryKeyException;
import database.*;
import impresario.IView;
import userinterface.View;
import userinterface.ViewFactory;
/** The class containing the Transaction for the BookStore application */
//==============================================================
public class Transaction extends EntityBase implements IView
{
private static final String myTableName = "Transaction";
protected Properties dependencies;
// GUI Components
private String updateStatusMessage = "";
// constructor for this class
//----------------------------------------------------------
public Transaction(String transactionID)
throws InvalidPrimaryKeyException
{
super(myTableName);
//setDependencies();
String query = "SELECT * FROM " + myTableName + " WHERE (transID = " + transactionID + ")";
Vector allDataRetrieved = getSelectQueryResult(query);
// You must get one transaction at least
if (allDataRetrieved != null)
{
int size = allDataRetrieved.size();
// There should be EXACTLY one Transaction. More than that is an error
if (size != 1)
{
throw new InvalidPrimaryKeyException("Multiple Transactions matching id : "
+ transactionID + " found.");
}
else
{
// copy all the retrieved data into persistent state
Properties retrieveTransData = (Properties)allDataRetrieved.elementAt(0);
persistentState = new Properties();
Enumeration allKeys = retrieveTransData.propertyNames();
while (allKeys.hasMoreElements() == true)
{
String nextKey = (String)allKeys.nextElement();
String nextValue = retrieveTransData.getProperty(nextKey);
if (nextValue != null)
{
persistentState.setProperty(nextKey, nextValue);
}
}
}
}
// If no book found for this user name, throw an exception
else
{
throw new InvalidPrimaryKeyException("No transaction matching id : "
+ transactionID + " found.");
}
}
//----------------------------------------------------------
public Transaction(Properties props)
{
super(myTableName);
//setDependencies();
persistentState = new Properties();
Enumeration allKeys = props.propertyNames();
while (allKeys.hasMoreElements() == true)
{
String nextKey = (String)allKeys.nextElement();
String nextValue = props.getProperty(nextKey);
if (nextValue != null)
{
persistentState.setProperty(nextKey, nextValue);
}
}
}
//-----------------------------------------------------------------------------------
private void setDependencies()
{
dependencies = new Properties();
myRegistry.setDependencies(dependencies);
}
//----------------------------------------------------------
public Object getState(String key)
{
if (key.equals("UpdateStatusMessage") == true)
return updateStatusMessage;
return persistentState.getProperty(key);
}
//----------------------------------------------------------------
public void stateChangeRequest(String key, Object value)
{
myRegistry.updateSubscribers(key, this);
}
/** Called via the IView relationship */
//----------------------------------------------------------
public void updateState(String key, Object value)
{
stateChangeRequest(key, value);
}
//-----------------------------------------------------------------------------------
public static int compare(Transaction a, Transaction b)
{
String aNum = (String)a.getState("transID");
String bNum = (String)b.getState("transID");
return aNum.compareTo(bNum);
}
//-----------------------------------------------------------------------------------
public void update()
{
updateStateInDatabase();
}
//-----------------------------------------------------------------------------------
private void updateStateInDatabase()
{
try
{
if (persistentState.getProperty("transID") != null)
{
Properties whereClause = new Properties();
whereClause.setProperty("transID", persistentState.getProperty("transID"));
updatePersistentState(mySchema, persistentState, whereClause);
updateStatusMessage = "Transaction data for transID : " + persistentState.getProperty("transID") + " updated successfully in database!";
}
else
{
Integer transID =
insertAutoIncrementalPersistentState(mySchema, persistentState);
persistentState.setProperty("transID", "" + transID.intValue());
updateStatusMessage = "Transaction data for transaction book : " + persistentState.getProperty("transID")
+ "installed successfully in database!";
System.out.println(updateStatusMessage);
}
}
catch (SQLException ex)
{
updateStatusMessage = "Error in installing transaction data in database!";
}
//DEBUG System.out.println("updateStateInDatabase " + updateStatusMessage);
}
/**
* This method is needed solely to enable the book information to be displayable in a table
*
*/
//--------------------------------------------------------------------------
public Vector getEntryListView()
{
Vector v = new Vector();
v.addElement(persistentState.getProperty("transID"));
v.addElement(persistentState.getProperty("bookID"));
v.addElement(persistentState.getProperty("patronID"));
v.addElement(persistentState.getProperty("transType"));
v.addElement(persistentState.getProperty("dateOfTrans"));
return v;
}
//-----------------------------------------------------------------------------------
protected void initializeSchema(String tableName)
{
if (mySchema == null)
{
mySchema = getSchemaInfo(tableName);
}
}
}