View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package base;
21  
22  import base.backup.Backup;
23  import base.network.NetworkManager;
24  import base.network.Workstation;
25  import base.service.Service;
26  import base.session.Session;
27  import base.user.User;
28  
29  public class IdManager {
30      private static IdManager instance;
31      private int currentUserId = 0;
32      private int currentWorkstationId = 0;
33      private int currentSessionId = 0;
34      private int currentServiceId = 0;
35      private int currentBackupId = 0;
36  
37      protected IdManager() {
38          // We must access to the InternetCafeManager in order to retrieve the
39          // correct current values...
40          this.currentUserId = retrieveMaxUserId();
41          this.currentWorkstationId = retrieveMaxWorkstationId();
42          this.currentSessionId = retrieveMaxSessionId();
43          this.currentServiceId = retrieveMaxServiceId();
44          this.currentBackupId = retrieveMaxBackupId();
45      }
46  
47      public static IdManager getInstance() {
48          return (instance == null) ? (instance = new IdManager()) : instance;
49      }
50  
51      private int retrieveMaxUserId() {
52          int max = -1; // This is an invalid value...
53          User[] user = InternetCafeManager.getInstance().getUser();
54  
55          for (int i = 0; i < user.length; i++) {
56              if (user[i].getId() > max) {
57                  max = user[i].getId();
58              }
59          }
60  
61          return max;
62      }
63  
64      private int retrieveMaxWorkstationId() {
65          int max = -1; // This is an invalid value...
66          Workstation[] workstation = NetworkManager.getInstance().getNetwork()
67                                                    .getAllWorkstation();
68  
69          for (int i = 0; i < workstation.length; i++) {
70              if (workstation[i].getId() > max) {
71                  max = workstation[i].getId();
72              }
73          }
74  
75          return max;
76      }
77  
78      private int retrieveMaxSessionId() {
79          int max = -1; // This is an invalid value...
80          Session[] session = InternetCafeManager.getInstance().getSession();
81  
82          for (int i = 0; i < session.length; i++) {
83              if (session[i].getId() > max) {
84                  max = session[i].getId();
85              }
86          }
87  
88          return max;
89      }
90  
91      private int retrieveMaxServiceId() {
92          int max = -1; // This is an invalid value...
93          Service[] service = InternetCafeManager.getInstance().getService();
94  
95          for (int i = 0; i < service.length; i++) {
96              if (service[i].getId() > max) {
97                  max = service[i].getId();
98              }
99          }
100 
101         return max;
102     }
103 
104     private int retrieveMaxBackupId() {
105         int max = -1; // This is an invalid value...
106         Backup[] backup = InternetCafeManager.getInstance().getBackup();
107 
108         for (int i = 0; i < backup.length; i++) {
109             if (backup[i].getId() > max) {
110                 max = backup[i].getId();
111             }
112         }
113 
114         return max;
115     }
116 
117     /***
118      * @return Returns the currenSessionId.
119      */
120     public int getCurrentSessionId() {
121         return currentSessionId;
122     }
123 
124     /***
125      * @return Returns the currentUserId.
126      */
127     public int getCurrentUserId() {
128         return currentUserId;
129     }
130 
131     /***
132      * @return Returns the currentWorkstationId.
133      */
134     public int getCurrentWorkstationId() {
135         return currentWorkstationId;
136     }
137 
138     /***
139      * @return Returns the currentServiceId.
140      */
141     public int getCurrentServiceId() {
142         return currentServiceId;
143     }
144 
145     /***
146      * @return Returns the currentBackupId.
147      */
148     public int getCurrentBackupId() {
149         return currentBackupId;
150     }
151 
152     /***
153      * @return Returns the currentSessionId increased of 1 unit.
154      */
155     public int getNextSessionId() {
156         return ++currentSessionId;
157     }
158 
159     /***
160      * @return Returns the currentUserId increased of 1 unit.
161      */
162     public int getNextUserId() {
163         return ++currentUserId;
164     }
165 
166     /***
167      * @return Returns the currentWorkstationId increased of 1 unit.
168      */
169     public int getNextWorkstationId() {
170         return ++currentWorkstationId;
171     }
172 
173     /***
174      * @return Returns the currentServiceId increased of 1 unit.
175      */
176     public int getNextServiceId() {
177         return ++currentServiceId;
178     }
179 
180     /***
181      * @return Returns the currentBackupId increased of 1 unit.
182      */
183     public int getNextBackupId() {
184         return ++currentBackupId;
185     }
186 }