add javadoc
[jSite.git] / src / de / todesbaum / jsite / application / Project.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.application;
21
22 import java.io.File;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import de.todesbaum.util.mime.DefaultMIMETypes;
28
29 /**
30  * Container for project information.
31  * 
32  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
33  */
34 public class Project implements Comparable<Project> {
35
36         /** The name of the project. */
37         protected String name;
38
39         /** The description of the project. */
40         protected String description;
41
42         /** The insert URI of the project. */
43         protected String insertURI;
44
45         /** The request URI of the project. */
46         protected String requestURI;
47
48         /** The index file of the project. */
49         protected String indexFile;
50
51         /** The local path of the project. */
52         protected String localPath;
53
54         /** The remote path of the URI. */
55         protected String path;
56
57         /** The time of the last insertion. */
58         protected long lastInsertionTime;
59
60         /** The edition to insert to. */
61         protected int edition;
62
63         /** Options for files. */
64         protected Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
65
66         /**
67          * Empty constructor.
68          */
69         public Project() {
70                 /* do nothing. */
71         }
72
73         /**
74          * Creates a new project from an existing one.
75          * 
76          * @param project
77          *            The project to clone
78          */
79         public Project(Project project) {
80                 name = project.name;
81                 description = project.description;
82                 insertURI = project.insertURI;
83                 requestURI = project.requestURI;
84                 path = project.path;
85                 edition = project.edition;
86                 localPath = project.localPath;
87                 indexFile = project.indexFile;
88                 lastInsertionTime = project.lastInsertionTime;
89                 fileOptions = new HashMap<String, FileOption>(project.fileOptions);
90         }
91
92         /**
93          * Returns the name of the project.
94          * 
95          * @return The name of the project
96          */
97         public String getName() {
98                 return name;
99         }
100
101         /**
102          * Sets the name of the project.
103          * 
104          * @param name
105          *            The name of the project
106          */
107         public void setName(String name) {
108                 this.name = name;
109         }
110
111         /**
112          * Returns the description of the project.
113          * 
114          * @return The description of the project
115          */
116         public String getDescription() {
117                 return description;
118         }
119
120         /**
121          * Sets the description of the project.
122          * 
123          * @param description
124          *            The description of the project
125          */
126         public void setDescription(String description) {
127                 this.description = description;
128         }
129
130         /**
131          * Returns the local path of the project.
132          * 
133          * @return The local path of the project
134          */
135         public String getLocalPath() {
136                 return localPath;
137         }
138
139         /**
140          * Sets the local path of the project.
141          * 
142          * @param localPath
143          *            The local path of the project
144          */
145         public void setLocalPath(String localPath) {
146                 this.localPath = localPath;
147         }
148
149         /**
150          * Returns the name of the index file of the project, relative to the
151          * project’s local path.
152          * 
153          * @return The name of the index file of the project
154          */
155         public String getIndexFile() {
156                 return indexFile;
157         }
158
159         /**
160          * Sets the name of the index file of the project, relative to the project’s
161          * local path.
162          * 
163          * @param indexFile
164          *            The name of the index file of the project
165          */
166         public void setIndexFile(String indexFile) {
167                 this.indexFile = indexFile;
168         }
169
170         /**
171          * Returns the time the project was last inserted, in milliseconds since the
172          * epoch.
173          * 
174          * @return The time of the last insertion
175          */
176         public long getLastInsertionTime() {
177                 return lastInsertionTime;
178         }
179
180         /**
181          * Sets the time the project was last inserted, in milliseconds since the
182          * last epoch.
183          * 
184          * @param lastInserted
185          *            The time of the last insertion
186          */
187         public void setLastInsertionTime(long lastInserted) {
188                 lastInsertionTime = lastInserted;
189         }
190
191         /**
192          * Returns the remote path of the project. The remote path is the path that
193          * directly follows the request URI of the project.
194          * 
195          * @return The remote path of the project
196          */
197         public String getPath() {
198                 return path;
199         }
200
201         /**
202          * Sets the remote path of the project. The remote path is the path that
203          * directly follows the request URI of the project.
204          * 
205          * @param path
206          *            The remote path of the project
207          */
208         public void setPath(String path) {
209                 this.path = path;
210         }
211
212         /**
213          * Returns the insert URI of the project.
214          * 
215          * @return The insert URI of the project
216          */
217         public String getInsertURI() {
218                 return insertURI;
219         }
220
221         /**
222          * Sets the insert URI of the project.
223          * 
224          * @param insertURI
225          *            The insert URI of the project
226          */
227         public void setInsertURI(String insertURI) {
228                 this.insertURI = shortenURI(insertURI);
229         }
230
231         /**
232          * Returns the request URI of the project.
233          * 
234          * @return The request URI of the project
235          */
236         public String getRequestURI() {
237                 return requestURI;
238         }
239
240         /**
241          * Sets the request URI of the project.
242          * 
243          * @param requestURI
244          *            The request URI of the project
245          */
246         public void setRequestURI(String requestURI) {
247                 this.requestURI = shortenURI(requestURI);
248         }
249
250         /**
251          * {@inheritDoc}
252          * <p>
253          * This method returns the name of the project.
254          */
255         @Override
256         public String toString() {
257                 return name;
258         }
259
260         /**
261          * Shortens the given URI by removing scheme and key-type prefixes.
262          * 
263          * @param uri
264          *            The URI to shorten
265          * @return The shortened URI
266          */
267         private String shortenURI(String uri) {
268                 if (uri.startsWith("freenet:")) {
269                         uri = uri.substring("freenet:".length());
270                 }
271                 if (uri.startsWith("SSK@")) {
272                         uri = uri.substring("SSK@".length());
273                 }
274                 if (uri.startsWith("USK@")) {
275                         uri = uri.substring("USK@".length());
276                 }
277                 if (uri.endsWith("/")) {
278                         uri = uri.substring(0, uri.length() - 1);
279                 }
280                 return uri;
281         }
282
283         /**
284          * Shortens the name of the given file by removing the local path of the
285          * project and leading file separators.
286          * 
287          * @param file
288          *            The file whose name should be shortened
289          * @return The shortened name of the file
290          */
291         public String shortenFilename(File file) {
292                 String filename = file.getPath();
293                 if (filename.startsWith(localPath)) {
294                         filename = filename.substring(localPath.length());
295                         if (filename.startsWith(File.separator)) {
296                                 filename = filename.substring(1);
297                         }
298                 }
299                 return filename;
300         }
301
302         /**
303          * Returns the options for the file with the given name. If the file does
304          * not yet have any options, a new set of default options is created and
305          * returned.
306          * 
307          * @param filename
308          *            The name of the file, relative to the project root
309          * @return The options for the file
310          */
311         public FileOption getFileOption(String filename) {
312                 FileOption fileOption = fileOptions.get(filename);
313                 if (fileOption == null) {
314                         fileOption = new FileOption(DefaultMIMETypes.guessMIMEType(filename));
315                         fileOptions.put(filename, fileOption);
316                 }
317                 return fileOption;
318         }
319
320         /**
321          * Sets options for a file.
322          * 
323          * @param filename
324          *            The filename to set the options for, relative to the project
325          *            root
326          * @param fileOption
327          *            The options to set for the file, or <code>null</code> to
328          *            remove the options for the file
329          */
330         public void setFileOption(String filename, FileOption fileOption) {
331                 if (fileOption != null) {
332                         fileOptions.put(filename, fileOption);
333                 } else {
334                         fileOptions.remove(filename);
335                 }
336         }
337
338         /**
339          * Returns all file options.
340          * 
341          * @return All file options
342          */
343         public Map<String, FileOption> getFileOptions() {
344                 return Collections.unmodifiableMap(fileOptions);
345         }
346
347         /**
348          * Sets all file options.
349          * 
350          * @param fileOptions
351          *            The file options
352          */
353         public void setFileOptions(Map<String, FileOption> fileOptions) {
354                 this.fileOptions.clear();
355                 this.fileOptions.putAll(fileOptions);
356         }
357
358         /**
359          * {@inheritDoc}
360          * <p>
361          * Projects are compared by their name only.
362          */
363         public int compareTo(Project project) {
364                 return name.compareToIgnoreCase(project.name);
365         }
366
367         /**
368          * Returns the edition of the project.
369          * 
370          * @return The edition of the project
371          */
372         public int getEdition() {
373                 return edition;
374         }
375
376         /**
377          * Sets the edition of the project.
378          * 
379          * @param edition
380          *            The edition to set
381          */
382         public void setEdition(int edition) {
383                 this.edition = edition;
384         }
385
386         /**
387          * Constructs the final request URI including the edition number.
388          * 
389          * @param offset
390          *            The offset for the edition number
391          * @return The final request URI
392          */
393         public String getFinalRequestURI(int offset) {
394                 return "USK@" + requestURI + "/" + path + "/" + (edition + offset) + "/";
395         }
396
397 }