View Javadoc

1   /*
2    *  ginp - Java Web Application for Viewing Photo Collections
3    *  Copyright (C) 2004  Douglas John Culnane <doug@culnane.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package net.sf.ginp;
20  
21  import java.util.List;
22  import java.util.Locale;
23  import java.util.Hashtable;
24  import java.util.Vector;
25  import java.util.ResourceBundle;
26  import java.util.MissingResourceException;
27  
28  import net.sf.ginp.commands.*;
29  import net.sf.ginp.config.Configuration;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   *  Model class to represent 1 user session instance of the application. All
35   *  state information is stored in this class.
36   *
37   *@author     Doug Culnane
38   *@version    $Revision: 303 $
39   */
40  public class GinpModel {
41  
42                        List          collections;
43                        int           currCollectionId  = 0;
44                        String        currentPage       = "ginpservlet?cmd=selectpath&path=/&colid=0";
45                        Locale        locale            = new Locale("en");
46                        int           pageHeight        = 500;
47                        int           pageWidth         = 600;
48                        int           pageOffset        = 0;
49                        int           pagePosition      = 0;
50                        String        userName          = "";
51      private static    Hashtable     commands          = new Hashtable();
52      private Log log = LogFactory.getLog(GinpModel.class);
53      
54      
55      
56      static {
57          // Add Commands
58          commands  = new Hashtable();
59          commands.put("adminmode", new AdminMode());
60          commands.put("logon", new Logon());
61          commands.put("selectcollection", new SelectCollection());
62          commands.put("selectpath", new SelectPath());
63          commands.put("showpicture", new ShowPicture());
64          commands.put("setlanguagecode", new SetLanguageCode());
65          commands.put("viewpage", new ViewPage());
66          commands.put("uploadconfig", new UploadConfig());
67          commands.put("setconfiguration", new SetConfiguration());
68      }
69      
70      
71      /**
72       *  Constructor for the GinpModel object requiring the absolute path of the
73       *  configuration file.
74       *
75       *@param  config
76       */
77      public GinpModel() {
78          this.setUserName("anon");
79      }
80  
81  
82      
83      /**
84       *  Gets the Command object from the stored Model Commands.
85       *
86       *@param  commandName  Identifing name of command.
87       *@return              The command object
88       */
89      static Command getCommand(String commandName) {
90          return (Command) commands.get(commandName);
91      }
92         
93      
94      
95      /**
96       *  Gets the pageOffset attribute of the GinpModel object
97       *
98       *@return    The pageOffset value
99       */
100     public int getPageOffset() {
101         return pageOffset;
102     }
103 
104 
105     /**
106      *  Sets the pageOffset attribute of the GinpModel object
107      *
108      *@param  i  The new pageOffset value
109      */
110     public void setPageOffset(int i) {
111         pageOffset = i;
112     }
113 
114 
115     /**
116      *  Gets the pagePosition attribute of the GinpModel object
117      *
118      *@return    The pagePosition value
119      */
120     public int getPagePosition() {
121         return pagePosition;
122     }
123 
124 
125     /**
126      *  Sets the pagePosition attribute of the GinpModel object
127      *
128      *@param  i  The new pagePosition value
129      */
130     public void setPagePosition(int i) {
131         pagePosition = i;
132     }
133 
134 
135     /**
136      *  Sets the currect PicCollection number of the GinpModel
137      *
138      *@param  id  Integer id of the collection selected as current.
139      *@return     True if comand done without error.
140      */
141     public boolean setCurrectCollection(int id) {
142         boolean  done  = false;
143         if (collections != null) {
144             if (collections.size() > id && id >= 0) {
145                 currCollectionId = id;
146                 done = true;
147             }
148         }
149         return done;
150     }
151 
152 
153     /**
154      *  Sets the current page attribute of the GinpModel object
155      *
156      *@param  url  The new current page url
157      */
158     public void setCurrentPage(String url) {
159         currentPage = url;
160     }
161 
162 
163     /**
164      *  Sets the locale attribute of the GinpModel object
165      *
166      *@param  loc  The new locale value
167      */
168     public void setLocale(Locale loc) {
169         log.debug("setLocale: " + loc.toString());
170         if (Configuration.getForcelocale() != null) {
171             if (Configuration.getForcelocale().equals("")) {
172                 locale = loc;
173             } else {
174                 locale = new Locale(Configuration.getForcelocale());
175             }
176         } else {
177             locale = loc;
178         }
179     }
180 
181 
182     /**
183      *  Sets the page height attribute of the Model. This is used when
184      *  presentation elements require the available size of the page in pixels.
185      *
186      *@param  i  The users current page height in pixels.
187      */
188     public void setPageHeight(int i) {
189         pageHeight = i;
190     }
191 
192 
193     /**
194      *  Sets the page width attribute of the Model. This is used when
195      *  presentation elements require the available size of the page in pixels.
196      *
197      *@param  i  The users current page width in pixels.
198      */
199     public void setPageWidth(int i) {
200         pageWidth = i;
201     }
202 
203 
204 
205     /**
206      *  Sets the user's log on name for the model. This is used to parse the
207      *  configuration file for the user's collections
208      *
209      *@param  user  The new userName value
210      */
211     public void setUserName(String user) {
212 
213         userName = user;
214         try {
215 
216             collections = Configuration.getCollectionForUser(user);
217             
218             // If there is only one collection avaiable to the user select it
219             if (collections.size() == 1) {
220                 setCurrectCollection(0);
221                 setCurrentPage(Configuration.getCollectionPageName());
222                 getCollection().setPath("");
223             } else {
224                 setCurrectCollection(-1);
225                 setCurrentPage("collections.jsp");
226             }
227 
228         } catch (Exception ex) {
229             //Make sure we log the error or config file writers will have nothing
230             //to go on unless they have a debugger handy
231             log.error("Error Loading Config File", ex);
232         }
233 
234     }
235 
236 
237 
238     /**
239      *  Gets the collection attribute of the GinpModel object
240      *
241      *@param  i  Description of the Parameter
242      *@return    The collection value
243      */
244     public PicCollection getCollection(int i) {
245         return (PicCollection) collections.get(i);
246     }
247 
248 
249     /**
250      *  Gets the collection attribute of the GinpModel object
251      *
252      *@return    The collection value
253      */
254     public PicCollection getCollection() {
255         return (PicCollection) collections.get(currCollectionId);
256     }
257 
258 
259     /**
260      *  Gets the collections attribute of the GinpModel object
261      *
262      *@return    The collections value
263      */
264     public PicCollection[] getCollections() {
265         return (PicCollection[]) collections.toArray(new PicCollection[0]);
266     }
267 
268 
269 
270     /**
271      *  Gets the collection attribute of the GinpModel object
272      *
273      *@return    The collection value
274      */
275     public int getCurrCollectionId() {
276         return currCollectionId;
277     }
278 
279 
280     /**
281      *  Gets the pageLoctaion attribute of the GinpModel object
282      *
283      *@return    The pageLoctaion value
284      */
285     public String getCurrentPage() {
286         return currentPage;
287     }
288 
289 
290     /**
291      *  Gets the prefered Locale for this GinpModel
292      *
293      *@return    The prefered Locale
294      */
295     public Locale getLocale() {
296         return locale;
297     }
298 
299     /**
300      *  Gets the users last set page height in pixels.
301      *
302      *@return    The page height value in pixels
303      */
304     public int getPageHeight() {
305         return pageHeight;
306     }
307 
308 
309     /**
310      *  Gets the users last set page width in pixels.
311      *
312      *@return    The page width value in pixels
313      */
314     public int getPageWidth() {
315         return pageWidth;
316     }
317 
318 
319     
320     /**
321      *  Gets this model's user log on name.
322      *
323      *@return    The model's user name
324      */
325     public String getUserName() {
326         return userName;
327     }
328 
329 
330     /**
331      *  Perform a command on the model.
332      *
333      *@param  command  The commands identifiying name.
334      *@param  params   The CommandParameters to be actioned
335      */
336     public void doCommand(String command, Vector params) {
337         Command  cmd  = getCommand(command);
338         if (cmd != null) {
339             log.debug("Model doing command: " + command);
340             cmd.action(this, params);
341         }
342     }
343 
344 
345     /**
346      *  Translate the supplied English text in to this Models prefured language.
347      *
348      *@param  lookupcode                    English Lookup Code Key.
349      *@return                               A translator of the text or null if
350      *      not knowen.
351      *@exception  MissingResourceException  Description of the Exception
352      */
353     public String translate(String lookupcode) throws MissingResourceException {
354         return ResourceBundle.getBundle("net.sf.ginp.Ginp", locale).getString(lookupcode);
355     }
356 
357 
358     /**
359      *  Gets a String that is a Dump of Debug info about this models state.
360      *
361      *@return    The debugDump value
362      */
363     public String getDebugDump() {
364         return this.getCollection().getDebugDump()
365                  + "pageHeight    :" + pageHeight + "\n"
366                  + "pageWidth     :" + pageWidth + "\n"
367                  //+ "thumbMaxSize  :" + thumbMaxSize + "\n"
368                  + "pageOffset    :" + pageOffset + "\n"
369                  + "pagePosition  :" + pagePosition + "\n"
370                  + "userName      :" + userName + "\n";
371     }
372     
373     /**
374      *  Gets a String that is a Dump of Debug info about this models state.
375      *
376      *@return    The debugDump value
377      */
378     public String getDebugInfo() {
379         return  " UserName=" + userName
380                 + " currCollectionId=" + currCollectionId
381                  + " locale=" + locale.toString();
382     }
383 
384     /**
385      *  check valid username and password
386      *
387      *@param  validUserName  the username
388      *@param  userPass       the password
389      *@return                whether they have access or not
390      */
391     public Boolean accessCheck(String validUserName, String userPass) {
392         return Configuration.accessCheck(validUserName, userPass);
393     }
394 }
395