remove Id keyword
[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  */
35 public class Project extends AbstractBean {
36
37         /** Name of the “name” property. */
38         public static final String PROPERTY_NAME = "name";
39
40         /** Name of the “description” property. */
41         public static final String PROPERTY_DESCRIPTION = "description";
42
43         /** Name of the “public key” property. */
44         public static final String PROPERTY_PUBLIC_KEY = "publicKey";
45
46         /** Name of the “private key” property. */
47         public static final String PROPERTY_PRIVATE_KEY = "privateKey";
48
49         /** Name of the “base path” property. */
50         public static final String PROPERTY_BASE_PATH = "basePath";
51
52         /** Name of the “base path entries” property. */
53         public static final String PROPERTY_BASE_PATH_ENTRIES = "basePathEntries";
54
55         /** Internal ID. */
56         private String id;
57
58         /** The name of the project. */
59         private String name;
60
61         /** The description of the project. */
62         private String description;
63
64         /** The public key. */
65         private String publicKey;
66
67         /** The private key. */
68         private String privateKey;
69
70         /** The base path of the project. */
71         private String basePath;
72
73         /** The list of files from the base path. */
74         private List<Entry> basePathEntries = new ArrayList<Entry>();
75
76         /** The list of virtual files. */
77         private List<Entry> virtualEntries = new ArrayList<Entry>();
78
79         /**
80          * Returns the internal ID.
81          * 
82          * @return The internal ID
83          */
84         String getId() {
85                 return id;
86         }
87
88         /**
89          * Sets the internal ID.
90          * 
91          * @param id
92          *            The internal ID
93          */
94         void setId(String id) {
95                 this.id = id;
96         }
97
98         /**
99          * Returns the name of the project.
100          * 
101          * @return The name of the project
102          */
103         public String getName() {
104                 return name;
105         }
106
107         /**
108          * Sets the name of the project.
109          * 
110          * @param name
111          *            The name of the project
112          */
113         public void setName(String name) {
114                 String oldName = this.name;
115                 this.name = name;
116                 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
117         }
118
119         /**
120          * Returns the description of the project.
121          * 
122          * @return The description of the project
123          */
124         public String getDescription() {
125                 return description;
126         }
127
128         /**
129          * Sets the description of the project
130          * 
131          * @param description
132          *            The description of the project
133          */
134         public void setDescription(String description) {
135                 String oldDescription = this.description;
136                 this.description = description;
137                 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
138         }
139
140         /**
141          * Returns the public key of the project.
142          * 
143          * @return The public key of the project
144          */
145         public String getPublicKey() {
146                 return publicKey;
147         }
148
149         /**
150          * Sets the public key of the project.
151          * 
152          * @param publicKey
153          *            The public key of the project
154          */
155         void setPublicKey(String publicKey) {
156                 String oldPublicKey = this.publicKey;
157                 this.publicKey = publicKey;
158                 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
159         }
160
161         /**
162          * Returns the private key of the project.
163          * 
164          * @return The private key of the project
165          */
166         public String getPrivateKey() {
167                 return privateKey;
168         }
169
170         /**
171          * Sets the private key of the project.
172          * 
173          * @param privateKey
174          *            The private key of the project
175          */
176         void setPrivateKey(String privateKey) {
177                 String oldPrivateKey = this.privateKey;
178                 this.privateKey = privateKey;
179                 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
180         }
181
182         /**
183          * Returns the base path of the project.
184          * 
185          * @return The base path of the project
186          */
187         public String getBasePath() {
188                 return basePath;
189         }
190
191         /**
192          * Sets the base path of the project.
193          * 
194          * @param basePath
195          *            The base path of the project
196          */
197         public void setBasePath(String basePath) {
198                 String oldBasePath = this.basePath;
199                 this.basePath = basePath;
200                 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
201         }
202
203         /**
204          * Rescans the base path for new or changed files.
205          */
206         public void rescanBasePath() {
207                 List<Entry> entries = new ArrayList<Entry>();
208                 scanPath("", entries);
209                 basePathEntries.clear();
210                 basePathEntries.addAll(entries);
211                 firePropertyChange(PROPERTY_BASE_PATH_ENTRIES, null, null);
212         }
213
214         /**
215          * Returns the list of files from the base path.
216          * 
217          * @return The list of files from the base path
218          */
219         public List<Entry> getBasePathEntries() {
220                 return basePathEntries;
221         }
222
223         /**
224          * Returns the list of visual entries.
225          * 
226          * @return The visual entries
227          */
228         public List<Entry> getVirtualEntries() {
229                 return virtualEntries;
230         }
231
232         /**
233          * Adds a virtual entry that redirects to the given target.
234          * 
235          * @param name
236          *            The name of the entry
237          * @param contentType
238          *            The content type of the entry, or <code>null</code> for
239          *            auto-detection
240          * @param target
241          *            The target URI of the redirect
242          */
243         public void addVirtualEntry(String name, String contentType, String target) {
244                 RedirectEntry redirectEntry = new RedirectEntry();
245                 redirectEntry.setName(name);
246                 redirectEntry.setContentType(contentType);
247                 redirectEntry.setTarget(target);
248                 redirectEntry.setInsert(true);
249         }
250
251         //
252         // PRIVATE METHODS
253         //
254
255         /**
256          * Scans the given path relative to {@link #basePath} for files and adds
257          * them to the given list of entries.
258          * 
259          * @param currentPath
260          *            The current path, relative to the base path
261          * @param entries
262          *            The list of entries
263          */
264         private void scanPath(String currentPath, List<Entry> entries) {
265                 File currentDirectory = new File(basePath + File.separatorChar + currentPath);
266                 if (!currentDirectory.isDirectory()) {
267                         return;
268                 }
269                 for (File file: currentDirectory.listFiles()) {
270                         String fileName = currentPath + file.getName();
271                         if (file.isDirectory()) {
272                                 scanPath(fileName + File.separatorChar, entries);
273                                 continue;
274                         }
275                         PhysicalEntry entry = new PhysicalEntry();
276                         entry.setName(fileName);
277                         entry.setPath(file.getPath());
278                         entry.setInsert(!file.isHidden());
279                         entries.add(entry);
280                 }
281         }
282
283 }