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.io.File;
22  
23  import net.sf.ginp.config.ModelUtil;
24  
25  import org.dom4j.Element;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   *  This class represents a collection of pictures as configured in the
31   *  ginp.xml config file. A PicCollection has a root and a files system of
32   *  folders and pictures. Renamed from Collection because that was too easy to
33   *  confuse with the java collections class
34   *
35   *@author     Doug Culnane
36   *@version    $Revision: 303 $
37   */
38  public class PicCollection {
39  
40               String         name                = "";
41               String         path                = "/";
42               String         root                = "";
43               boolean        sortDESC            = false;
44               boolean        userHasWriteAccess  = false;
45      private Log log = LogFactory.getLog(PicCollection.class);
46                 
47      /**
48       *  Constructor for the PicCollection object. The xml parameter contains the
49       *  configuration for this PicCollection.
50       *
51       *@param  elem    Dom4j document element.
52       */
53      public PicCollection(Element elem) {
54          root = elem.selectSingleNode("root").getText();
55          name = elem.selectSingleNode("name").getText();
56      }
57  
58  
59  
60      /**
61       *  Sets the path attribute of the PicCollection, which idenfifies the
62       *  current postion in the file system from the PicCollection's root.
63       *
64       *@param  s  The new path value.
65       */
66      public void setPath(String s) {
67      	
68      	//Sanitize Input
69      	s=s.replaceAll("\\.\\./","");
70      	s=s.replaceAll("\\.\\.\\\\","");
71      	
72          if (!(s.startsWith("/"))) {
73              s = "/" + s;
74          }
75          if (!(s.endsWith("/"))) {
76              s = s + "/";
77          }
78  
79          File  dir  = new File(root + s.substring(0, s.length() - 1));
80          if (dir.isDirectory()) {
81              path = s;
82              if (log.isDebugEnabled()) {
83                  log.debug("setPath to:" + path);
84              }
85          } else {
86              log.info("Attempt to setPath to non directory:" + dir.getAbsolutePath());
87          }
88       }
89  
90  
91      /**
92       *  Sets the userHasWriteAccess attribute of the PicCollection object
93       *
94       *@param  b  The new userHasWriteAccess value
95       */
96      public void setUserHasWriteAccess(boolean b) {
97          userHasWriteAccess = b;
98      }
99  
100 
101   
102 
103   
104     /**
105      *  Gets the name attribute of the PicCollection.
106      *
107      *@return    The PicCollection name.
108      */
109     public String getName() {
110         return name;
111     }
112 
113 
114 
115     /**
116      *  Gets the path of the collection from its root to the current location.
117      *
118      *@return    The path.
119      */
120     public String getPath() {
121         return path;
122     }
123 
124 
125 
126     /**
127      *  Gets the root attribute of the PicCollection object
128      *
129      *@return    The root value
130      */
131     public String getRoot() {
132         return root;
133     }
134 
135 
136     /**
137      *  Description of the Method
138      *
139      *@return    Description of the Return Value
140      */
141     public boolean userHasWriteAccess() {
142         return userHasWriteAccess;
143     }
144 
145 
146 
147 
148 
149     /**
150      *  Do a dump of info for debugging
151      *
152      *@return    The debugDump value
153      */
154     public String getDebugDump() {
155         return "name: " + name + "\n"
156                  + "root: " + root + "\n"
157                  + "path: " + path + "\n";
158     }
159 
160 
161 
162 	/**
163 	 * @return
164 	 */
165 	public int getPictureLength() {
166 		return ModelUtil.getFolderManager().getPicturesLength(this);
167 	}
168 
169 
170 
171 
172 
173 
174 	/**
175 	 * @param count
176 	 * @return
177 	 */
178 	public String getFolder(int count) {
179 		return ModelUtil.getFolderManager().getFolder(this,count);
180 	}
181 
182 
183 
184 	/**
185 	 * @param count
186 	 * @return
187 	 */
188 	public String getPicture(int count) {
189 		String pic[]=ModelUtil.getFolderManager().getPictures(this);
190 		if (count>=pic.length) { 
191 			return null;
192 		}
193 		return pic[count];
194 	}
195 
196 
197 
198 	/**
199 	 * @return
200 	 */
201 	public int getFoldersLength() {
202 		return ModelUtil.getFolderManager().getFoldersLength(this);
203 	}
204 
205 
206 
207 	/**
208 	 * @param picName
209 	 * @return
210 	 */
211 	public String getNextPictureName(String picName) {
212 		return ModelUtil.getFolderManager().getNextPictureName(picName,this);
213 	}
214 
215 
216 	/**
217 	 * @param picName
218 	 * @return
219 	 */
220 	public String getPrevPictureName(String picName) {
221 		return ModelUtil.getFolderManager().getPrevPictureName(picName,this);
222 	}
223 
224 	/**
225 	 * @param string
226 	 * @return
227 	 */
228 	public int getPicturesInDirectoryLength(String path) {
229 		return ModelUtil.getFolderManager().getPicturesLength(this,path);
230 	}
231 
232 
233 
234 	/**
235 	 * @return
236 	 */
237 	public String[] getPictures() {
238 		return ModelUtil.getFolderManager().getPictures(this);
239 	}
240 
241 
242 
243 }
244 
245