Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/gui/addressbook/AbstractAddressBookModel.java


1   /*
2    * AbstractAddressBookModel.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  
15  import com.flexstor.common.data.ejb.address.AddressData;
16  import com.flexstor.common.data.ejb.address.EmailAddressData;
17  import com.flexstor.common.data.ejb.address.FtpAddressData;
18  import com.flexstor.common.data.ejb.role.RoleData;
19  import com.flexstor.common.exceptions.ejb.DuplicateRecordException;
20  import com.flexstor.common.gateway.GroupGateway;
21  import com.flexstor.common.gateway.SettingsGateway;
22  import com.flexstor.common.gateway.UserGateway;
23  import com.flexstor.common.gateway.exceptions.TransactionFailedException;
24  
25  
26  /**
27      Abstracte model for address book, which interacts with the gatway to communicate 
28      with  bean, which in turn talks to database.
29  */
30  public abstract class AbstractAddressBookModel implements AddressBookModelI
31  {
32      protected SettingsGateway gw          = new SettingsGateway();
33      protected RoleData roleData           = null;
34      protected ArrayList lstEmailAddresses   = new ArrayList();     // all Email addresses
35      protected ArrayList lstFTPAddresses     = new ArrayList();     // all addresses
36      protected boolean   bEmailInitialized   = false;
37      protected boolean   bFTPInitialized     = false;
38      protected String ugName ;
39      protected boolean  bIsUser = false;   
40  
41     /**
42      * Initializes the manager.
43      * @param ugName Name of the user or group.
44      * @param Whether its a user or group.
45      */
46     public boolean init( String ugName, boolean bIsUser )
47     {
48  
49       if ( ( ugName == null ) ||
50            ( ugName.trim().length() == 0 ) )
51       { 
52         return false;
53       }
54       this.ugName = ugName;
55       this.bIsUser = bIsUser;
56       
57       UserGateway  userGateway  = null;
58       GroupGateway groupGateway = null;
59  
60       if ( bIsUser )
61       {
62        try
63        {
64          userGateway = new UserGateway();
65          userGateway.connect ( ugName );
66          roleData = userGateway.getRoleData();
67          return true;
68        }
69        catch ( TransactionFailedException exception )
70        {
71          return false;
72        }
73        finally
74        {
75          if ( userGateway != null )
76            userGateway.dispose();
77        }
78       }
79  
80       try
81       {
82         groupGateway = new GroupGateway();
83         groupGateway.connect ( ugName );
84         roleData = groupGateway.getRoleData();
85         return true;
86       }
87       catch ( TransactionFailedException exception )
88       {
89          return false;
90       }
91       finally
92       {
93          if ( groupGateway != null )
94            groupGateway.dispose();
95       }
96     }
97     
98     /**
99      * Implementation for AddressBookModelI.
100     * Deletes one address from the database. The bean removes it from all roles.
101     * @param an AddressData or object to be deleted
102     * @exception com.flexstor.common.gateway.exceptions.TransactionFailedException if bean call fails
103     */
104    public void deleteAddress ( AddressData address )
105       throws TransactionFailedException
106    {
107       try
108       {
109          gw.connect();
110          gw.delete(address);
111          removeFromList(address);
112          //setCurrentAddress( null );
113       }
114       catch(TransactionFailedException e)
115       {
116          throw(e);
117       }
118       finally
119       {
120          gw.dispose();
121       }
122    }
123 
124    /**
125     * Implementation for AddressBookModelI.
126     * Refreshes the contents of specified type of address list.
127     * @exception com.flexstor.common.gateway.exceptions.TransactionFailedException if bean call fails
128     */
129    public abstract void refresh (int addressType )
130       throws TransactionFailedException;
131 
132    /**
133     * Implementation for AddressBookModelI.
134     * Saves one address to the database.
135     * If the data object contains a key, an update is performed for the
136     * existing object, otherwise an insert.
137     * @param an AddressData to be added/updated
138     * @exception com.flexstor.common.gateway.exceptions.TransactionFailedException if bean call fails
139     * @exception com.flexstor.common.exceptions.ejb.DuplicateRecordException if the name of the address already exists
140     */
141    public abstract void saveAddress( AddressData address )
142       throws TransactionFailedException, DuplicateRecordException;
143 
144    /** 
145     * Implementation for AddressBookModelI.
146     * Saves one address to the database .
147     * If the data object contains a key, an update is performed for the
148     * existing object.
149     * @param an EmailAddressData or FtpAddressData object to be updated
150     * @exception com.flexstor.common.gateway.exceptions.TransactionFailedException if bean call fails
151     * @exception com.flexstor.common.exceptions.ejb.DuplicateRecordException if the name of the address already exists
152     */
153    public abstract void updateAddress ( AddressData data )
154       throws TransactionFailedException, DuplicateRecordException; 
155       
156    /**
157     * Implementation for AddressBookModelI.
158     * Gets all send addresses for all roles the user is a member of. This call is cached.
159     * To refresh the data, refresh() must be called.
160     * @param int AddressType for which the list of address needed.
161     *            for ftp Address AddressData.FTP_ADDRESS and for email address 
162                  AddressData.EMAIL_ADDRESS must be passed.    
163     * @return an arrayList of AddressData objects.
164     * @exception com.flexstor.common.gateway.exceptions.TransactionFailedException if bean call fails
165     */
166    public ArrayList getAddresses(int addressType)
167       throws TransactionFailedException
168    {
169       if (addressType == AddressData.EMAIL_ADDRESS )
170       {
171         if(!bEmailInitialized)
172         {
173          refresh(AddressData.EMAIL_ADDRESS);
174          bEmailInitialized = true;
175         }
176         return lstEmailAddresses;
177       }
178       else if (addressType == AddressData.FTP_ADDRESS)
179       {
180         if(!bFTPInitialized) 
181         {
182          refresh(AddressData.FTP_ADDRESS);
183          bFTPInitialized = true;
184         }
185         return lstFTPAddresses;
186       }
187       return null;
188    }
189   /**
190    * This method removes the address from list.
191    */
192    protected void removeFromList(AddressData address)
193    {
194       if(address instanceof EmailAddressData)
195       {
196         for(int i = 0 ; i<lstEmailAddresses.size();i++)
197         {
198             EmailAddressData data = (EmailAddressData)lstEmailAddresses.get(i);
199             if(data.getName().equals(address.getName()))
200                 lstEmailAddresses.remove(i);        
201         }
202         
203       }
204       else if(address instanceof FtpAddressData)
205       {
206         for(int i = 0 ; i<lstFTPAddresses.size();i++)
207         {
208             FtpAddressData data = ( FtpAddressData )lstFTPAddresses.get(i);
209             if(data.getName().equals(address.getName()))
210                 lstFTPAddresses.remove(i);        
211         }
212       }  
213         
214    }
215  
216    protected boolean refreshRoleData()
217    {
218      UserGateway  userGateway  = null;
219      GroupGateway groupGateway = null;
220 
221      if ( bIsUser )
222      {
223       try
224       {
225         userGateway = new UserGateway();
226         userGateway.connect ( ugName );
227         roleData = userGateway.getRoleData();
228         return true;
229       }
230       catch ( TransactionFailedException exception )
231       {
232         return false;
233       }
234       finally
235       {
236         if ( userGateway != null )
237           userGateway.dispose();
238       }
239      }
240 
241      try
242      {
243        groupGateway = new GroupGateway();
244        groupGateway.connect ( ugName );
245        roleData = groupGateway.getRoleData();
246        return true;
247      }
248      catch ( TransactionFailedException exception )
249      {
250         return false;
251      }
252      finally
253      {
254         if ( groupGateway != null )
255           groupGateway.dispose();
256      }       
257    } 
258    
259   /** 
260    * Implementation for AddressBookModelI.
261    * gets the current address.
262    * @return an object of current EmailAddressData or FtpAddressData object 
263    */  
264    public abstract AddressData getCurrentAddress();
265   
266   /** 
267    * Implementation for AddressBookModelI.
268    * sets the current address.
269    * @param an object of current EmailAddressData or FtpAddressData object 
270    */  
271    public abstract void setCurrentAddress(AddressData data) throws TransactionFailedException;
272 
273    public abstract EmailAddressData getDefaultEMailAddress() throws TransactionFailedException;
274 }