Java tutorial
/* * ginp - Java Web Application for Viewing Photo Collections * Copyright (C) 2004 Douglas John Culnane <doug@culnane.net> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA */ package net.sf.ginp; import net.sf.ginp.commands.*; // good style - we support them all! import net.sf.ginp.config.Configuration; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.Hashtable; import java.util.List; import java.util.Locale; import java.util.ResourceBundle; import java.util.Vector; /** * Model class to represent 1 user session instance of the application. All * state information is stored in this class. * *@author Doug Culnane *@version $Revision$ */ public class GinpModel { private static Hashtable commands = new Hashtable(); static { // Add Commands commands = new Hashtable(); commands.put("adminmode", new AdminMode()); commands.put("logon", new Logon()); commands.put("selectcollection", new SelectCollection()); commands.put("selectpath", new SelectPath()); commands.put("showpicture", new ShowPicture()); commands.put("setlanguagecode", new SetLanguageCode()); commands.put("viewpage", new ViewPage()); commands.put("uploadconfig", new UploadConfig()); commands.put("setconfiguration", new SetConfiguration()); } List collections; int currCollectionId = 0; String currentPage = "ginpservlet?cmd=selectpath&path=/&colid=0"; Locale locale = new Locale("en"); int pageHeight = 500; int pageWidth = 600; int pageOffset = 0; int pagePosition = 0; String userName = ""; /** * apache Commons Logger specific to this class. */ private Log log = LogFactory.getLog(GinpModel.class); /** * Constructor for the GinpModel object requiring the absolute path * of the configuration file. * *@param config */ public GinpModel() { this.setUserName("anon"); } /** * Gets the Command object from the stored Model Commands. * *@param commandName Identifing name of command. *@return The command object */ static Command getCommand(final String commandName) { return (Command) commands.get(commandName); } /** * Gets the pageOffset attribute of the GinpModel object. * *@return The pageOffset value */ public final int getPageOffset() { return pageOffset; } /** * Sets the pageOffset attribute of the GinpModel object. * *@param i The new pageOffset value */ public final void setPageOffset(final int i) { pageOffset = i; } /** * Gets the pagePosition attribute of the GinpModel object. * *@return The pagePosition value */ public final int getPagePosition() { return pagePosition; } /** * Sets the pagePosition attribute of the GinpModel object. * *@param i The new pagePosition value */ public final void setPagePosition(final int i) { pagePosition = i; } /** * Sets the currect PicCollection number of the GinpModel. * *@param id Integer id of the collection selected as current. *@return True if comand done without error. */ public final boolean setCurrectCollection(final int id) { boolean done = false; if (collections != null) { if ((collections.size() > id) && (id >= 0)) { currCollectionId = id; done = true; } } return done; } /** * Sets the current page attribute of the GinpModel object. * *@param url The new current page url */ public final void setCurrentPage(final String url) { currentPage = url; } /** * Sets the locale attribute of the GinpModel object. * *@param loc The new locale value */ public final void setLocale(final Locale loc) { log.debug("setLocale: " + loc.toString()); if (Configuration.getForcelocale() != null) { if (Configuration.getForcelocale().equals("")) { locale = loc; } else { locale = new Locale(Configuration.getForcelocale()); } } else { locale = loc; } } /** * Sets the page height attribute of the Model. This is used when * presentation elements require the available size of the page * in pixels. * *@param i The users current page height in pixels. */ public final void setPageHeight(final int i) { pageHeight = i; } /** * Sets the page width attribute of the Model. This is used when * presentation elements require the available size of the page * in pixels. * *@param i The users current page width in pixels. */ public final void setPageWidth(final int i) { pageWidth = i; } /** * Sets the user's log on name for the model. This is used to * parse the configuration file for the user's collections. * *@param user The new userName value */ public final void setUserName(final String user) { userName = user; try { collections = Configuration.getCollectionForUser(user); // If there is only one collection avaiable to the user select it if (collections.size() == 1) { setCurrectCollection(0); setCurrentPage(Configuration.getCollectionPageName()); getCollection().setPath(""); } else { setCurrectCollection(-1); setCurrentPage("collections.jsp"); } } catch (Exception ex) { // Make sure we log the error or config file writers will have // nothing to go on unless they have a debugger handy log.error("Error Loading Config File", ex); } } /** * Gets the collection attribute of the GinpModel object. * *@param i Description of the Parameter *@return The collection value */ public final PicCollection getCollection(final int i) { return (PicCollection) collections.get(i); } /** * Gets the collection attribute of the GinpModel object. * *@return The collection value */ public final PicCollection getCollection() { return (PicCollection) collections.get(currCollectionId); } /** * Gets the collections attribute of the GinpModel object. * *@return The collections value */ public final PicCollection[] getCollections() { return (PicCollection[]) collections.toArray(new PicCollection[0]); } /** * Gets the collection attribute of the GinpModel object. * *@return The collection value */ public final int getCurrCollectionId() { return currCollectionId; } /** * Gets the pageLoctaion attribute of the GinpModel object. * *@return The pageLoctaion value */ public final String getCurrentPage() { return currentPage; } /** * Gets the prefered Locale for this GinpModel. * *@return The prefered Locale */ public final Locale getLocale() { return locale; } /** * Gets the users last set page height in pixels. * *@return The page height value in pixels */ public final int getPageHeight() { return pageHeight; } /** * Gets the users last set page width in pixels. * *@return The page width value in pixels */ public final int getPageWidth() { return pageWidth; } /** * Gets this model's user log on name. * *@return The model's user name */ public final String getUserName() { return userName; } /** * Perform a command on the model. * *@param command The commands identifiying name. *@param params The CommandParameters to be actioned */ public final void doCommand(final String command, final Vector params) { Command cmd = getCommand(command); if (cmd != null) { log.debug("Model doing command: " + command); cmd.action(this, params); } } /** * Translate the supplied English text into this Model's * preferred language. * *@param lookupcode English Lookup Code Key. *@return A translator of the text or null if not known. */ public final String translate(final String lookupcode) { return ResourceBundle.getBundle("net.sf.ginp.Ginp", locale).getString(lookupcode); } /** * Gets a String that is a Dump of Debug info about this models state. * *@return The debugDump value */ public final String getDebugDump() { return this.getCollection().getDebugDump() + "pageHeight :" + pageHeight + "\n" + "pageWidth :" + pageWidth + "\n" //+ "thumbMaxSize :" + thumbMaxSize + "\n" + "pageOffset :" + pageOffset + "\n" + "pagePosition :" + pagePosition + "\n" + "userName :" + userName + "\n"; } /** * Gets a String that is a Dump of Debug info about this models state. * *@return The debugDump value */ public final String getDebugInfo() { return " UserName=" + userName + " currCollectionId=" + currCollectionId + " locale=" + locale.toString(); } /** * check valid username and password. * *@param validUserName the username *@param userPass the password *@return whether they have access or not */ public final Boolean accessCheck(final String validUserName, final String userPass) { return Configuration.accessCheck(validUserName, userPass); } }