Source code: com/flexstor/common/gui/addressbook/AddressBookModel.java
1 /*
2 * AddressBookModel.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:32 $ 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.gui.addressbook;
12
13 import java.util.ArrayList;
14 import java.util.Enumeration;
15 import java.util.Vector;
16
17 import com.flexstor.common.data.ejb.address.AddressData;
18 import com.flexstor.common.data.ejb.address.EmailAddressData;
19 import com.flexstor.common.data.ejb.address.FtpAddressData;
20 import com.flexstor.common.exceptions.ejb.DuplicateRecordException;
21 import com.flexstor.common.gateway.exceptions.TransactionFailedException;
22 import com.flexstor.common.resources.FileDataSource;
23 import com.flexstor.common.resources.Resources;
24 import com.flexstor.common.settings.UserManager;
25 import com.flexstor.common.util.Diagnostic;
26
27 /**
28 This is the model for data base. The data source is database here.
29 * @author Mahesh Gidwani
30 */
31
32 public class AddressBookModel extends AbstractAddressBookModel
33 {
34 protected AddressData currentAddress = new AddressData();
35 protected boolean bRefreshRolData = false;
36 protected EmailAddressData defaultEMailAddress = null ;
37
38 /**
39 * @param String ugName name of the user/group for which this model is to be created.
40 * @param boolean isUser idicates if the ugName is user.
41 */
42 public AddressBookModel(String ugName,boolean isUser)
43 {
44 super.init(ugName,isUser);
45 }
46
47 /**
48 * In order to use this constructor, init should be called on created instace.
49 */
50 public AddressBookModel()
51 {
52 }
53
54
55 /**
56 * Implementation for AddressBookModelI.
57 * Saves one address to the database and links it to the user's role.
58 * @param an AddressData to be added
59 * @exception com.flexstor.common.gateway.exceptions.TransactionFailedException if bean call fails
60 * @exception com.flexstor.common.exceptions.ejb.DuplicateRecordException if the name of the address already exists
61 */
62
63 public void saveAddress(AddressData address)
64 throws TransactionFailedException,DuplicateRecordException
65 {
66 if(roleData == null) // no role, no life
67 return ;
68 try
69 {
70 gw.connect();
71 if(address.getKey() == null)
72 {
73 address.setEditable(true);
74 gw.add( address, roleData.getKey() );
75 addToList(address);
76 setCurrentAddress( address );
77 bRefreshRolData = false;
78 }
79 else
80 {
81 updateAddress(address);
82 }
83 }
84 catch(TransactionFailedException e1)
85 {
86 throw(e1);
87 }
88 catch(DuplicateRecordException e2)
89 {
90 throw(e2);
91 }
92 finally
93 {
94 gw.dispose();
95 }
96
97 }
98
99 /**
100 * Implementation for AddressBookModelI.
101 * Saves one address to the database and links it to the user's role.
102 * @param an AddressData to be added
103 * @exception com.flexstor.common.gateway.exceptions.TransactionFailedException if bean call fails
104 */
105 public void updateAddress(AddressData address)
106 throws TransactionFailedException,DuplicateRecordException
107 {
108 if(roleData == null) // no role, no life
109 return ;
110 gw.connect();
111 try
112 {
113 if (address.getKey() != null)
114 {
115 gw.update(address);
116 removeFromList(currentAddress);
117 addToList(address);
118 setCurrentAddress( address );
119 bRefreshRolData = false;
120 }
121 }
122 catch(TransactionFailedException e1)
123 {
124 throw(e1);
125 }
126 catch(DuplicateRecordException e2)
127 {
128 throw(e2);
129 }
130 finally
131 {
132 gw.dispose();
133 }
134 }
135
136 /**
137 * Implementation for AddressBookModelI.
138 * Deletes one address from the database. The bean removes it from all roles.
139 * In addition, the user role is refreshed to reflect the current state.
140 * @param an AddressData address to be deleted
141 * @exception com.flexstor.common.gateway.exceptions.TransactionFailedException if bean call fails
142 */
143 public void deleteAddress ( AddressData address )
144 throws TransactionFailedException
145 {
146
147 //System.out.println("DBG1:AddBookModel:deleteAddress:list of add before deleting ");
148 //printAddresses();
149 super.deleteAddress( address );
150 removeFromList(address);
151 setCurrentAddress(null);
152 //System.out.println("DBG2:AddBookModel:deleteAddress:list of add after deleting ");
153 //printAddresses();
154 }
155
156
157 /**
158 * Implementation for AddressBookModelI.
159 * Refreshes the contents of the address list.
160 * @exception com.flexstor.common.gateway.exceptions.TransactionFailedException if bean call fails
161 */
162 public void refresh ( int addressType )
163 throws TransactionFailedException
164 {
165 if ( roleData == null ) // if you do not have any role, why live ?
166 return;
167
168 try
169 {
170 if ( ! bRefreshRolData )
171 {
172 bRefreshRolData = refreshRoleData();
173 }
174 if( addressType == AddressData.EMAIL_ADDRESS )
175 {
176 refresh( lstEmailAddresses,roleData.getEMailAddresses() );
177 }
178 else if( addressType == AddressData.FTP_ADDRESS )
179 {
180 refresh( lstFTPAddresses,roleData.getFtpAddresses() );
181 }
182 }
183 catch( TransactionFailedException e )
184 {
185 throw(e);
186 }
187 }
188
189 /**
190 * Refreshes the list of address. Helper method for refres(int).
191 */
192 private void refresh(ArrayList lstAddressList,Vector vAddresses) throws TransactionFailedException
193 {
194 try
195 {
196 Vector v;
197 gw.connect();
198 lstAddressList.clear();
199 v = gw.get( vAddresses );
200 for ( Enumeration e = v.elements(); e.hasMoreElements(); )
201 lstAddressList.add( e.nextElement() );
202 }
203 catch( TransactionFailedException e )
204 {
205 throw(e);
206 }
207 finally
208 {
209 gw.dispose();
210 }
211 }
212
213 /**
214 * Implementation for AddressBookModelI.
215 * sets the current address.
216 * @param an object of current EmailAddressData or FtpAddressData object
217 */
218 public void setCurrentAddress(AddressData address)
219 {
220 currentAddress = address;
221 }
222
223 /**
224 * Creates a duplicate copy of current object and return.
225 * Implementation for AddressBookModelI.
226 * gets the current address.
227 * @return an object of current EmailAddressData or FtpAddressData object
228 */
229 public AddressData getCurrentAddress()
230 {
231 if( currentAddress instanceof EmailAddressData )
232 {
233 EmailAddressData mailData = new EmailAddressData((EmailAddressData)currentAddress);
234 mailData.setKey(currentAddress.getKey());
235 return mailData;
236 }
237 else if( currentAddress instanceof FtpAddressData )
238 {
239 FtpAddressData ftpData = new FtpAddressData((FtpAddressData)currentAddress);
240 ftpData.setKey(currentAddress.getKey());
241 return currentAddress;
242 }
243 return null;
244 }
245
246 /**
247 * Add the address to the corresponding address list.
248 */
249 private void addToList(AddressData address)
250 {
251 if(address instanceof EmailAddressData)
252 lstEmailAddresses.add(address);
253 else if(address instanceof FtpAddressData)
254 lstFTPAddresses.add(address);
255 }
256
257 /**
258 * Gets the current user's email address, this object is cached
259 */
260 public EmailAddressData getDefaultEMailAddress() throws TransactionFailedException
261 {
262 if (defaultEMailAddress == null)
263 {
264 try
265 {
266 gw.connect();
267 defaultEMailAddress = (EmailAddressData)gw.get(UserManager.getEMailAddressKey());
268 }
269 catch(TransactionFailedException e)
270 {
271 throw(e);
272 }
273 finally
274 {
275 gw.dispose();
276 }
277 }
278 //Diagnostic.trace(Diagnostic.SETTINGS, "user's e-mail=" + defaultAddress);
279 return defaultEMailAddress;
280 }
281
282 public static void main(String[] args)
283 {
284 try
285 {
286
287 com.flexstor.common.settings.Settings.registerDataSource ( com.flexstor.common.settings.Settings.RUNTIME, new com.flexstor.common.settings.datasource.RuntimeDataSource() );
288 com.flexstor.common.settings.Settings.registerDataSource ( com.flexstor.common.settings.Settings.USER, new com.flexstor.common.settings.datasource.UserDataSource(new java.util.Hashtable()));
289 try
290 {
291 //java.util.Hashtable hash = new com.flexstor.common.settings.datasource.ConfigSettingsLoader().loadSettings ( args[0] + "conf/flexstordb.properties" );
292 //com.flexstor.common.settings.Settings.registerDataSource ( com.flexstor.common.settings.Settings.CONFIG, new com.flexstor.common.settings.datasource.ConfigDataSource(hash) );
293 //com.flexstor.common.settings.Settings.addString ( com.flexstor.common.settings.Settings.CONFIG_DIRECTORY, args[0] + "conf/" );
294 Resources.registerDataSource( new FileDataSource( "E:\\MRroke\\FromRorke\\Aug16Build48\\build48a.tar\\resources\\Resources.en" ) );
295
296
297 //com.flexstor.common.resources.Resources.registerDataSource( new com.flexstor.common.resources.FileDataSource( args[0] + "resources/Resources.en" ) );
298 com.flexstor.common.resources.Resources.cacheAll();
299 // Setup diagnostics
300 Diagnostic.enableOutput ( true );
301 Diagnostic.setTraceLevel ( 3 );
302 Diagnostic.addTraceCategory ( Diagnostic.CAT_GATEWAY );
303 Diagnostic.addTraceCategory ( Diagnostic.SETTINGS );
304 Diagnostic.addOutputDevice ( System.out );
305 //save //load
306 //Gateway.enableDebugging ( "D:/flexstordb/30/cso", false, true);
307
308 new com.flexstor.common.gateway.InitGateway ( "JONAS", "192.168.0.56", "6300",true );
309 // initializing address book model
310 AddressBookModel model = new AddressBookModel("demo1",true);
311
312 //creating address to be added
313 EmailAddressData data = new EmailAddressData();
314 data.setName("mahesh_gidwani");
315 data.setAddress("mgidwani@impetus.co.in");
316 //data.setDescription("testing bss ");
317 model.saveAddress(data);
318 System.out.println();
319
320 //com.flexstor.common.settings.UserManager um = com.flexstor.common.settings.UserManager.getInstance();
321 //um.init ( "demo1" );
322 ///////////////
323
324 //com.flexstor.common.gateway.InitGateway igw = new com.flexstor.common.gateway.InitGateway ( "ORACLE", "206.147.164.210", "2225" );
325 // initializing user
326 /*com.flexstor.common.settings.UserManager um = com.flexstor.common.settings.UserManager.getInstance();
327 um.init ( "demo1" );
328
329 com.flexstor.common.settings.AddressManager am = com.flexstor.common.settings.AddressManager.create(com.flexstor.common.settings.AddressManager.USER);
330 (new AddressBook(new Frame(), 1, true, com.flexstor.common.settings.AddressManager.getInstance())).setVisible(true);*/
331 //(new AddressBookViewer(new Frame(), 1, true, new AddressBookModel("demo1", true))).setVisible(true);
332 }
333 catch ( Exception e ){ e.printStackTrace();}
334 try{System.in.read();}catch(Exception e){}
335 }catch(Exception e ){ e.printStackTrace();}
336 System.exit(0);
337 }
338
339 }