85df0efd983eafbe825600274d856b1295af6589
[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  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31  */
32 public class Project implements Comparable<Project> {
33
34         protected String name;
35         protected String description;
36
37         protected String insertURI;
38         protected String requestURI;
39
40         protected String indexFile;
41         protected String localPath;
42         protected String path;
43         protected long lastInsertionTime;
44         /** The edition to insert to. */
45         protected int edition;
46
47         protected Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
48
49         public Project() {
50         }
51
52         /**
53          * Clone-constructor.
54          * 
55          * @param project
56          */
57         public Project(Project project) {
58                 name = project.name;
59                 description = project.description;
60                 insertURI = project.insertURI;
61                 requestURI = project.requestURI;
62                 path = project.path;
63                 edition = project.edition;
64                 localPath = project.localPath;
65                 indexFile = project.indexFile;
66                 lastInsertionTime = project.lastInsertionTime;
67                 fileOptions = new HashMap<String, FileOption>(project.fileOptions);
68         }
69
70         /**
71          * @return Returns the title.
72          */
73         public String getName() {
74                 return name;
75         }
76
77         /**
78          * @param title
79          *            The title to set.
80          */
81         public void setName(String title) {
82                 name = title;
83         }
84
85         /**
86          * @return Returns the description.
87          */
88         public String getDescription() {
89                 return description;
90         }
91
92         /**
93          * @param description
94          *            The description to set.
95          */
96         public void setDescription(String description) {
97                 this.description = description;
98         }
99
100         /**
101          * @return Returns the localPath.
102          */
103         public String getLocalPath() {
104                 return localPath;
105         }
106
107         /**
108          * @param localPath
109          *            The localPath to set.
110          */
111         public void setLocalPath(String localPath) {
112                 this.localPath = localPath;
113         }
114
115         /**
116          * @return Returns the indexFile.
117          */
118         public String getIndexFile() {
119                 return indexFile;
120         }
121
122         /**
123          * @param indexFile
124          *            The indexFile to set.
125          */
126         public void setIndexFile(String indexFile) {
127                 this.indexFile = indexFile;
128         }
129
130         /**
131          * @return Returns the lastInserted.
132          */
133         public long getLastInsertionTime() {
134                 return lastInsertionTime;
135         }
136
137         /**
138          * @param lastInserted
139          *            The lastInserted to set.
140          */
141         public void setLastInsertionTime(long lastInserted) {
142                 lastInsertionTime = lastInserted;
143         }
144
145         /**
146          * @return Returns the name.
147          */
148         public String getPath() {
149                 return path;
150         }
151
152         /**
153          * @param name
154          *            The name to set.
155          */
156         public void setPath(String name) {
157                 path = name;
158         }
159
160         /**
161          * @return Returns the insertURI.
162          */
163         public String getInsertURI() {
164                 return insertURI;
165         }
166
167         /**
168          * @param insertURI
169          *            The insertURI to set.
170          */
171         public void setInsertURI(String insertURI) {
172                 this.insertURI = shortenURI(insertURI);
173         }
174
175         /**
176          * @return Returns the requestURI.
177          */
178         public String getRequestURI() {
179                 return requestURI;
180         }
181
182         /**
183          * @param requestURI
184          *            The requestURI to set.
185          */
186         public void setRequestURI(String requestURI) {
187                 this.requestURI = shortenURI(requestURI);
188         }
189
190         @Override
191         public String toString() {
192                 return name;
193         }
194
195         private String shortenURI(String uri) {
196                 if (uri.startsWith("freenet:")) {
197                         uri = uri.substring("freenet:".length());
198                 }
199                 if (uri.startsWith("SSK@")) {
200                         uri = uri.substring("SSK@".length());
201                 }
202                 if (uri.startsWith("USK@")) {
203                         uri = uri.substring("USK@".length());
204                 }
205                 if (uri.endsWith("/")) {
206                         uri = uri.substring(0, uri.length() - 1);
207                 }
208                 return uri;
209         }
210
211         public String shortenFilename(File file) {
212                 String filename = file.getPath();
213                 if (filename.startsWith(localPath)) {
214                         filename = filename.substring(localPath.length());
215                         if (filename.startsWith(File.separator)) {
216                                 filename = filename.substring(1);
217                         }
218                 }
219                 return filename;
220         }
221
222         public FileOption getFileOption(String filename) {
223                 FileOption fileOption = fileOptions.get(filename);
224                 if (fileOption == null) {
225                         fileOption = new FileOption(DefaultMIMETypes.guessMIMEType(filename));
226                         fileOptions.put(filename, fileOption);
227                 }
228                 return fileOption;
229         }
230
231         public void setFileOption(String filename, FileOption fileOption) {
232                 if (fileOption != null) {
233                         fileOptions.put(filename, fileOption);
234                 } else {
235                         fileOptions.remove(filename);
236                 }
237         }
238
239         /**
240          * @return Returns the fileOptions.
241          */
242         public Map<String, FileOption> getFileOptions() {
243                 return Collections.unmodifiableMap(fileOptions);
244         }
245
246         /**
247          * @param fileOptions
248          *            The fileOptions to set.
249          */
250         public void setFileOptions(Map<String, FileOption> fileOptions) {
251                 this.fileOptions.clear();
252                 this.fileOptions.putAll(fileOptions);
253         }
254
255         /**
256          * {@inheritDoc}
257          */
258         public int compareTo(Project project) {
259                 return name.compareToIgnoreCase(project.name);
260         }
261
262         /**
263          * Returns the edition of the project.
264          * 
265          * @return The edition of the project
266          */
267         public int getEdition() {
268                 return edition;
269         }
270
271         /**
272          * Sets the edition of the project.
273          * 
274          * @param edition
275          *            The edition to set
276          */
277         public void setEdition(int edition) {
278                 this.edition = edition;
279         }
280
281         /**
282          * Constructs the final request URI including the edition number.
283          * 
284          * @return The final request URI
285          */
286         public String getFinalRequestURI(int offset) {
287                 return "USK@" + requestURI + "/" + path + "/" + (edition + offset) + "/";
288         }
289
290 }