add default content type notion
[jSite2.git] / src / net / pterodactylus / jsite / project / Project.java
1 /*
2  * jSite2 - Project.java -
3  * Copyright © 2008 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 net.pterodactylus.jsite.project;
21
22 import java.beans.PropertyChangeListener;
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import net.pterodactylus.util.beans.AbstractBean;
28 import net.pterodactylus.util.io.MimeTypes;
29
30 /**
31  * Container for project information. A Project is capable of notifying
32  * {@link PropertyChangeListener}s if any of the contained properties change.
33  * 
34  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
35  */
36 public class Project extends AbstractBean {
37
38         /** Name of the “name” property. */
39         public static final String PROPERTY_NAME = "name";
40
41         /** Name of the “description” property. */
42         public static final String PROPERTY_DESCRIPTION = "description";
43
44         /** Name of the “public key” property. */
45         public static final String PROPERTY_PUBLIC_KEY = "publicKey";
46
47         /** Name of the “private key” property. */
48         public static final String PROPERTY_PRIVATE_KEY = "privateKey";
49
50         /** Name of the “base path” property. */
51         public static final String PROPERTY_BASE_PATH = "basePath";
52
53         /** Name of the “base path entries” property. */
54         public static final String PROPERTY_BASE_PATH_ENTRIES = "basePathEntries";
55
56         /** Internal ID. */
57         private String id;
58
59         /** The name of the project. */
60         private String name;
61
62         /** The description of the project. */
63         private String description;
64
65         /** The public key. */
66         private String publicKey;
67
68         /** The private key. */
69         private String privateKey;
70
71         /** The base path of the project. */
72         private String basePath;
73
74         /** The list of files from the base path. */
75         private List<Entry> basePathEntries = new ArrayList<Entry>();
76
77         /** The list of virtual files. */
78         private List<Entry> virtualEntries = new ArrayList<Entry>();
79
80         /**
81          * Creates a new project.
82          */
83         public Project() {
84                 /* do nothing. */
85         }
86
87         /**
88          * Clones the given project.
89          * 
90          * @param project
91          */
92         Project(Project project) {
93                 this.name = project.name;
94                 this.description = project.description;
95                 this.publicKey = project.publicKey;
96                 this.privateKey = project.privateKey;
97                 this.basePath = project.basePath;
98                 this.basePathEntries.addAll(project.basePathEntries);
99                 this.virtualEntries.addAll(project.virtualEntries);
100         }
101
102         /**
103          * Returns the internal ID.
104          * 
105          * @return The internal ID
106          */
107         String getId() {
108                 return id;
109         }
110
111         /**
112          * Sets the internal ID.
113          * 
114          * @param id
115          *            The internal ID
116          */
117         void setId(String id) {
118                 this.id = id;
119         }
120
121         /**
122          * Returns the name of the project.
123          * 
124          * @return The name of the project
125          */
126         public String getName() {
127                 return name;
128         }
129
130         /**
131          * Sets the name of the project.
132          * 
133          * @param name
134          *            The name of the project
135          */
136         public void setName(String name) {
137                 String oldName = this.name;
138                 this.name = name;
139                 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
140         }
141
142         /**
143          * Returns the description of the project.
144          * 
145          * @return The description of the project
146          */
147         public String getDescription() {
148                 return description;
149         }
150
151         /**
152          * Sets the description of the project
153          * 
154          * @param description
155          *            The description of the project
156          */
157         public void setDescription(String description) {
158                 String oldDescription = this.description;
159                 this.description = description;
160                 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
161         }
162
163         /**
164          * Returns the public key of the project.
165          * 
166          * @return The public key of the project
167          */
168         public String getPublicKey() {
169                 return publicKey;
170         }
171
172         /**
173          * Sets the public key of the project.
174          * 
175          * @param publicKey
176          *            The public key of the project
177          */
178         void setPublicKey(String publicKey) {
179                 String oldPublicKey = this.publicKey;
180                 this.publicKey = publicKey;
181                 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
182         }
183
184         /**
185          * Returns the private key of the project.
186          * 
187          * @return The private key of the project
188          */
189         public String getPrivateKey() {
190                 return privateKey;
191         }
192
193         /**
194          * Sets the private key of the project.
195          * 
196          * @param privateKey
197          *            The private key of the project
198          */
199         void setPrivateKey(String privateKey) {
200                 String oldPrivateKey = this.privateKey;
201                 this.privateKey = privateKey;
202                 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
203         }
204
205         /**
206          * Returns the base path of the project.
207          * 
208          * @return The base path of the project
209          */
210         public String getBasePath() {
211                 return basePath;
212         }
213
214         /**
215          * Sets the base path of the project.
216          * 
217          * @param basePath
218          *            The base path of the project
219          */
220         public void setBasePath(String basePath) {
221                 String oldBasePath = this.basePath;
222                 this.basePath = basePath;
223                 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
224         }
225
226         /**
227          * Rescans the base path for new or changed files.
228          */
229         public void rescanBasePath() {
230                 List<Entry> entries = new ArrayList<Entry>();
231                 scanPath("", entries);
232                 basePathEntries.clear();
233                 basePathEntries.addAll(entries);
234                 firePropertyChange(PROPERTY_BASE_PATH_ENTRIES, null, null);
235         }
236
237         /**
238          * Returns the list of files from the base path.
239          * 
240          * @return The list of files from the base path
241          */
242         public List<Entry> getBasePathEntries() {
243                 return basePathEntries;
244         }
245
246         /**
247          * Returns the list of visual entries.
248          * 
249          * @return The visual entries
250          */
251         public List<Entry> getVirtualEntries() {
252                 return virtualEntries;
253         }
254
255         /**
256          * Adds a virtual entry that redirects to the given target.
257          * 
258          * @param name
259          *            The name of the entry
260          * @param contentType
261          *            The content type of the entry, or <code>null</code> for
262          *            auto-detection
263          * @param target
264          *            The target URI of the redirect
265          */
266         public void addVirtualEntry(String name, String contentType, String target) {
267                 RedirectEntry redirectEntry = new RedirectEntry();
268                 redirectEntry.setName(name);
269                 redirectEntry.setContentType(contentType);
270                 redirectEntry.setTarget(target);
271                 redirectEntry.setInsert(true);
272         }
273
274         //
275         // PRIVATE METHODS
276         //
277
278         /**
279          * Scans the given path relative to {@link #basePath} for files and adds
280          * them to the given list of entries.
281          * 
282          * @param currentPath
283          *            The current path, relative to the base path
284          * @param entries
285          *            The list of entries
286          */
287         private void scanPath(String currentPath, List<Entry> entries) {
288                 File currentDirectory = new File(basePath + File.separatorChar + currentPath);
289                 if (!currentDirectory.isDirectory()) {
290                         return;
291                 }
292                 for (File file: currentDirectory.listFiles()) {
293                         String fileName = currentPath + file.getName();
294                         if (file.isDirectory()) {
295                                 scanPath(fileName + File.separatorChar, entries);
296                                 continue;
297                         }
298                         PhysicalEntry entry = new PhysicalEntry();
299                         entry.setName(fileName);
300                         entry.setPath(file.getPath());
301                         entry.setDefaultInsert(!file.isHidden());
302                         entry.setInsert(!file.isHidden());
303                         String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
304                         List<String> mimeTypes = MimeTypes.getMimeTypes(extension);
305                         if (!mimeTypes.isEmpty()) {
306                                 entry.setDefaultContentType(mimeTypes.get(0));
307                                 entry.setContentType(mimeTypes.get(0));
308                         }
309                         entries.add(entry);
310                 }
311         }
312
313 }