-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatron.java
213 lines (175 loc) · 5.77 KB
/
Patron.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
207
208
209
210
211
212
// 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 Patron for the ATM application */
//==============================================================
public class Patron extends EntityBase implements IView
{
private static final String myTableName = "Patron";
protected Properties dependencies;
// GUI Components
private String updateStatusMessage = "";
// constructor for this class
//----------------------------------------------------------
public Patron(String patronID)
throws InvalidPrimaryKeyException
{
super(myTableName);
//setDependencies();
String query = "SELECT * FROM " + myTableName + " WHERE (patronID = " + patronID + ")";
Vector allDataRetrieved = getSelectQueryResult(query);
// You must get one patron at least
if (allDataRetrieved != null)
{
int size = allDataRetrieved.size();
// There should be EXACTLY one patron. More than that is an error
if (size != 1)
{
throw new InvalidPrimaryKeyException("Multiple patrons matching id : "
+ patronID + " found.");
}
else
{
// copy all the retrieved data into persistent state
Properties retrievedPatronData = (Properties)allDataRetrieved.elementAt(0);
persistentState = new Properties();
Enumeration allKeys = retrievedPatronData.propertyNames();
while (allKeys.hasMoreElements() == true)
{
String nextKey = (String)allKeys.nextElement();
String nextValue = retrievedPatronData.getProperty(nextKey);
if (nextValue != null)
{
persistentState.setProperty(nextKey, nextValue);
}
}
}
}
// If no Patron found for this user name, throw an exception
else
{
throw new InvalidPrimaryKeyException("No patron matching id : "
+ patronID + " found.");
}
}
//----------------------------------------------------------
public Patron(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(Patron a, Patron b)
{
String aNum = (String)a.getState("patronID");
String bNum = (String)b.getState("patronID");
return aNum.compareTo(bNum);
}
//-----------------------------------------------------------------------------------
public void update()
{
updateStateInDatabase();
}
//-----------------------------------------------------------------------------------
private void updateStateInDatabase()
{
try
{
if (persistentState.getProperty("patronID") != null)
{
Properties whereClause = new Properties();
whereClause.setProperty("patronID",
persistentState.getProperty("patronID"));
updatePersistentState(mySchema, persistentState, whereClause);
updateStatusMessage = "Patron data for patronID : " + persistentState.getProperty("patronID") + " updated successfully in database!";
}
else
{
Integer patronID =
insertAutoIncrementalPersistentState(mySchema, persistentState);
persistentState.setProperty("patronID", "" + patronID.intValue());
updateStatusMessage = "Patron data for new patron : " + persistentState.getProperty("patronID")
+ "installed successfully in database!";
System.out.println(updateStatusMessage);
}
}
catch (SQLException ex)
{
updateStatusMessage = "Error in installing patron data in database!";
}
//DEBUG System.out.println("updateStateInDatabase " + updateStatusMessage);
}
/**
* This method is needed solely to enable the patron information to be displayable in a table
*
*/
//--------------------------------------------------------------------------
public Vector getEntryListView()
{
Vector v = new Vector();
v.addElement(persistentState.getProperty("patronID"));
v.addElement(persistentState.getProperty("name"));
v.addElement(persistentState.getProperty("address"));
v.addElement(persistentState.getProperty("city"));
v.addElement(persistentState.getProperty("stateCode"));
v.addElement(persistentState.getProperty("zip"));
v.addElement(persistentState.getProperty("email"));
v.addElement(persistentState.getProperty("dateOfBirth"));
v.addElement(persistentState.getProperty("status"));
return v;
}
//-----------------------------------------------------------------------------------
protected void initializeSchema(String tableName)
{
if (mySchema == null)
{
mySchema = getSchemaInfo(tableName);
}
}
}