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