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