add virtual entries
[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
29 /**
30  * Container for project information. A Project is capable of notifying
31  * {@link PropertyChangeListener}s if any of the contained properties change.
32  * 
33  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34  * @version $Id$
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          * Returns the internal ID.
82          * 
83          * @return The internal ID
84          */
85         String getId() {
86                 return id;
87         }
88
89         /**
90          * Sets the internal ID.
91          * 
92          * @param id
93          *            The internal ID
94          */
95         void setId(String id) {
96                 this.id = id;
97         }
98
99         /**
100          * Returns the name of the project.
101          * 
102          * @return The name of the project
103          */
104         public String getName() {
105                 return name;
106         }
107
108         /**
109          * Sets the name of the project.
110          * 
111          * @param name
112          *            The name of the project
113          */
114         public void setName(String name) {
115                 String oldName = this.name;
116                 this.name = name;
117                 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
118         }
119
120         /**
121          * Returns the description of the project.
122          * 
123          * @return The description of the project
124          */
125         public String getDescription() {
126                 return description;
127         }
128
129         /**
130          * Sets the description of the project
131          * 
132          * @param description
133          *            The description of the project
134          */
135         public void setDescription(String description) {
136                 String oldDescription = this.description;
137                 this.description = description;
138                 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
139         }
140
141         /**
142          * Returns the public key of the project.
143          * 
144          * @return The public key of the project
145          */
146         public String getPublicKey() {
147                 return publicKey;
148         }
149
150         /**
151          * Sets the public key of the project.
152          * 
153          * @param publicKey
154          *            The public key of the project
155          */
156         void setPublicKey(String publicKey) {
157                 String oldPublicKey = this.publicKey;
158                 this.publicKey = publicKey;
159                 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
160         }
161
162         /**
163          * Returns the private key of the project.
164          * 
165          * @return The private key of the project
166          */
167         public String getPrivateKey() {
168                 return privateKey;
169         }
170
171         /**
172          * Sets the private key of the project.
173          * 
174          * @param privateKey
175          *            The private key of the project
176          */
177         void setPrivateKey(String privateKey) {
178                 String oldPrivateKey = this.privateKey;
179                 this.privateKey = privateKey;
180                 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
181         }
182
183         /**
184          * Returns the base path of the project.
185          * 
186          * @return The base path of the project
187          */
188         public String getBasePath() {
189                 return basePath;
190         }
191
192         /**
193          * Sets the base path of the project.
194          * 
195          * @param basePath
196          *            The base path of the project
197          */
198         public void setBasePath(String basePath) {
199                 String oldBasePath = this.basePath;
200                 this.basePath = basePath;
201                 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
202         }
203
204         /**
205          * Rescans the base path for new or changed files.
206          */
207         public void rescanBasePath() {
208                 List<Entry> entries = new ArrayList<Entry>();
209                 scanPath("", entries);
210                 basePathEntries.clear();
211                 basePathEntries.addAll(entries);
212                 firePropertyChange(PROPERTY_BASE_PATH_ENTRIES, null, null);
213         }
214
215         /**
216          * Returns the list of files from the base path.
217          * 
218          * @return The list of files from the base path
219          */
220         public List<Entry> getBasePathEntries() {
221                 return basePathEntries;
222         }
223
224         /**
225          * Returns the list of visual entries.
226          * 
227          * @return The visual entries
228          */
229         public List<Entry> getVirtualEntries() {
230                 return virtualEntries;
231         }
232
233         /**
234          * Adds a virtual entry that redirects to the given target.
235          * 
236          * @param name
237          *            The name of the entry
238          * @param contentType
239          *            The content type of the entry, or <code>null</code> for
240          *            auto-detection
241          * @param target
242          *            The target URI of the redirect
243          */
244         public void addVirtualEntry(String name, String contentType, String target) {
245                 RedirectEntry redirectEntry = new RedirectEntry();
246                 redirectEntry.setName(name);
247                 redirectEntry.setContentType(contentType);
248                 redirectEntry.setTarget(target);
249                 redirectEntry.setInsert(true);
250         }
251
252         //
253         // PRIVATE METHODS
254         //
255
256         /**
257          * Scans the given path relative to {@link #basePath} for files and adds
258          * them to the given list of entries.
259          * 
260          * @param currentPath
261          *            The current path, relative to the base path
262          * @param entries
263          *            The list of entries
264          */
265         private void scanPath(String currentPath, List<Entry> entries) {
266                 File currentDirectory = new File(basePath + File.separatorChar + currentPath);
267                 if (!currentDirectory.isDirectory()) {
268                         return;
269                 }
270                 for (File file: currentDirectory.listFiles()) {
271                         String fileName = currentPath + file.getName();
272                         if (file.isDirectory()) {
273                                 scanPath(fileName + File.separatorChar, entries);
274                                 continue;
275                         }
276                         PhysicalEntry entry = new PhysicalEntry();
277                         entry.setName(fileName);
278                         entry.setPath(file.getPath());
279                         entry.setInsert(!file.isHidden());
280                         entries.add(entry);
281                 }
282         }
283
284 }