fix parameter assignments
[jSite.git] / src / de / todesbaum / jsite / application / Project.java
1 /*
2  * jSite - a tool for uploading websites into Freenet Copyright (C) 2006 David
3  * Roden
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your option) any later
8  * version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17  * 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                 String shortUri = uri;
269                 if (shortUri.startsWith("freenet:")) {
270                         shortUri = shortUri.substring("freenet:".length());
271                 }
272                 if (shortUri.startsWith("SSK@")) {
273                         shortUri = shortUri.substring("SSK@".length());
274                 }
275                 if (shortUri.startsWith("USK@")) {
276                         shortUri = shortUri.substring("USK@".length());
277                 }
278                 if (shortUri.endsWith("/")) {
279                         shortUri = shortUri.substring(0, shortUri.length() - 1);
280                 }
281                 return shortUri;
282         }
283
284         /**
285          * Shortens the name of the given file by removing the local path of the
286          * project and leading file separators.
287          *
288          * @param file
289          *            The file whose name should be shortened
290          * @return The shortened name of the file
291          */
292         public String shortenFilename(File file) {
293                 String filename = file.getPath();
294                 if (filename.startsWith(localPath)) {
295                         filename = filename.substring(localPath.length());
296                         if (filename.startsWith(File.separator)) {
297                                 filename = filename.substring(1);
298                         }
299                 }
300                 return filename;
301         }
302
303         /**
304          * Returns the options for the file with the given name. If the file does
305          * not yet have any options, a new set of default options is created and
306          * returned.
307          *
308          * @param filename
309          *            The name of the file, relative to the project root
310          * @return The options for the file
311          */
312         public FileOption getFileOption(String filename) {
313                 FileOption fileOption = fileOptions.get(filename);
314                 if (fileOption == null) {
315                         fileOption = new FileOption(DefaultMIMETypes.guessMIMEType(filename));
316                         fileOptions.put(filename, fileOption);
317                 }
318                 return fileOption;
319         }
320
321         /**
322          * Sets options for a file.
323          *
324          * @param filename
325          *            The filename to set the options for, relative to the project
326          *            root
327          * @param fileOption
328          *            The options to set for the file, or <code>null</code> to
329          *            remove the options for the file
330          */
331         public void setFileOption(String filename, FileOption fileOption) {
332                 if (fileOption != null) {
333                         fileOptions.put(filename, fileOption);
334                 } else {
335                         fileOptions.remove(filename);
336                 }
337         }
338
339         /**
340          * Returns all file options.
341          *
342          * @return All file options
343          */
344         public Map<String, FileOption> getFileOptions() {
345                 return Collections.unmodifiableMap(fileOptions);
346         }
347
348         /**
349          * Sets all file options.
350          *
351          * @param fileOptions
352          *            The file options
353          */
354         public void setFileOptions(Map<String, FileOption> fileOptions) {
355                 this.fileOptions.clear();
356                 this.fileOptions.putAll(fileOptions);
357         }
358
359         /**
360          * {@inheritDoc}
361          * <p>
362          * Projects are compared by their name only.
363          */
364         public int compareTo(Project project) {
365                 return name.compareToIgnoreCase(project.name);
366         }
367
368         /**
369          * Returns the edition of the project.
370          *
371          * @return The edition of the project
372          */
373         public int getEdition() {
374                 return edition;
375         }
376
377         /**
378          * Sets the edition of the project.
379          *
380          * @param edition
381          *            The edition to set
382          */
383         public void setEdition(int edition) {
384                 this.edition = edition;
385         }
386
387         /**
388          * Constructs the final request URI including the edition number.
389          *
390          * @param offset
391          *            The offset for the edition number
392          * @return The final request URI
393          */
394         public String getFinalRequestURI(int offset) {
395                 return "USK@" + requestURI + "/" + path + "/" + (edition + offset) + "/";
396         }
397
398 }