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