reformat
[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.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import net.pterodactylus.util.beans.AbstractBean;
31
32 /**
33  * Container for project information. A Project is capable of notifying
34  * {@link PropertyChangeListener}s if any of the contained properties change.
35  * 
36  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
37  */
38 public class Project extends AbstractBean {
39
40         /** Name of the “name” property. */
41         public static final String PROPERTY_NAME = "name";
42
43         /** Name of the “description” property. */
44         public static final String PROPERTY_DESCRIPTION = "description";
45
46         /** Name of the “public key” property. */
47         public static final String PROPERTY_PUBLIC_KEY = "publicKey";
48
49         /** Name of the “private key” property. */
50         public static final String PROPERTY_PRIVATE_KEY = "privateKey";
51
52         /** Name of the “base path” property. */
53         public static final String PROPERTY_BASE_PATH = "basePath";
54
55         /** Name of the “default file” property. */
56         public static final String PROPERTY_DEFAULT_FILE = "defaultFile";
57
58         /** Internal ID. */
59         private String id;
60
61         /** The name of the project. */
62         private String name;
63
64         /** The description of the project. */
65         private String description;
66
67         /** The public key. */
68         private String publicKey;
69
70         /** The private key. */
71         private String privateKey;
72
73         /** The base path of the project. */
74         private String basePath;
75
76         /** The default file. */
77         private String defaultFile;
78
79         /** The overrides. */
80         private final Map<String, FileOverride> fileOverrides = new HashMap<String, FileOverride>();
81
82         /** The current root project file. */
83         private ProjectFileImpl rootProjectFile;
84
85         /**
86          * Creates a new project.
87          */
88         public Project() {
89                 /* do nothing. */
90         }
91
92         /**
93          * Clones the given project.
94          * 
95          * @param project
96          */
97         Project(Project project) {
98                 this.name = project.name;
99                 this.description = project.description;
100                 this.publicKey = project.publicKey;
101                 this.privateKey = project.privateKey;
102                 this.basePath = project.basePath;
103         }
104
105         /**
106          * Returns the internal ID.
107          * 
108          * @return The internal ID
109          */
110         String getId() {
111                 return id;
112         }
113
114         /**
115          * Sets the internal ID.
116          * 
117          * @param id
118          *            The internal ID
119          */
120         void setId(String id) {
121                 this.id = id;
122         }
123
124         /**
125          * Returns the name of the project.
126          * 
127          * @return The name of the project
128          */
129         public String getName() {
130                 return name;
131         }
132
133         /**
134          * Sets the name of the project.
135          * 
136          * @param name
137          *            The name of the project
138          */
139         public void setName(String name) {
140                 String oldName = this.name;
141                 this.name = name;
142                 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
143         }
144
145         /**
146          * Returns the description of the project.
147          * 
148          * @return The description of the project
149          */
150         public String getDescription() {
151                 return description;
152         }
153
154         /**
155          * Sets the description of the project
156          * 
157          * @param description
158          *            The description of the project
159          */
160         public void setDescription(String description) {
161                 String oldDescription = this.description;
162                 this.description = description;
163                 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
164         }
165
166         /**
167          * Returns the public key of the project.
168          * 
169          * @return The public key of the project
170          */
171         public String getPublicKey() {
172                 return publicKey;
173         }
174
175         /**
176          * Sets the public key of the project.
177          * 
178          * @param publicKey
179          *            The public key of the project
180          */
181         void setPublicKey(String publicKey) {
182                 String oldPublicKey = this.publicKey;
183                 this.publicKey = publicKey;
184                 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
185         }
186
187         /**
188          * Returns the private key of the project.
189          * 
190          * @return The private key of the project
191          */
192         public String getPrivateKey() {
193                 return privateKey;
194         }
195
196         /**
197          * Sets the private key of the project.
198          * 
199          * @param privateKey
200          *            The private key of the project
201          */
202         void setPrivateKey(String privateKey) {
203                 String oldPrivateKey = this.privateKey;
204                 this.privateKey = privateKey;
205                 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
206         }
207
208         /**
209          * Returns the base path of the project.
210          * 
211          * @return The base path of the project
212          */
213         public String getBasePath() {
214                 return basePath;
215         }
216
217         /**
218          * Sets the base path of the project.
219          * 
220          * @param basePath
221          *            The base path of the project
222          */
223         public void setBasePath(String basePath) {
224                 String oldBasePath = this.basePath;
225                 this.basePath = basePath;
226                 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
227         }
228
229         /**
230          * Returns the default file.
231          * 
232          * @return The default file
233          */
234         public String getDefaultFile() {
235                 return defaultFile;
236         }
237
238         /**
239          * Sets the default file.
240          * 
241          * @param defaultFile
242          *            The default file
243          */
244         public void setDefaultFile(String defaultFile) {
245                 String oldDefaultFile = this.defaultFile;
246                 this.defaultFile = defaultFile;
247                 fireIfPropertyChanged(PROPERTY_DEFAULT_FILE, oldDefaultFile, defaultFile);
248         }
249
250         /**
251          * Adds a file override for the given file.
252          * 
253          * @param projectFile
254          *            The file
255          * @param override
256          *            The override for the file
257          */
258         public void addFileOverride(ProjectFile projectFile, FileOverride override) {
259                 addFileOverride(projectFile.getCompletePath(), override);
260         }
261
262         /**
263          * Adds a file override for the given file.
264          * 
265          * @param filePath
266          *            The file path
267          * @param override
268          *            The override for the file
269          */
270         public void addFileOverride(String filePath, FileOverride override) {
271                 fileOverrides.put(filePath, override);
272         }
273
274         /**
275          * Removes the file override for the given file.
276          * 
277          * @param projectFile
278          *            The file for which to remove the override
279          */
280         public void removeFileOverride(ProjectFile projectFile) {
281                 removeFileOverride(projectFile.getCompletePath());
282         }
283
284         /**
285          * Removes the file override for the given file.
286          * 
287          * @param filePath
288          *            The file path for which to remove the override
289          */
290         public void removeFileOverride(String filePath) {
291                 fileOverrides.remove(filePath);
292         }
293
294         /**
295          * Returns the file override for the given file.
296          * 
297          * @param projectFile
298          *            The file for which to get the override
299          * @return The file override, or <code>null</code> if the given file does
300          *         not have an override
301          */
302         public FileOverride getFileOverride(ProjectFile projectFile) {
303                 return getFileOverride(projectFile.getCompletePath());
304         }
305
306         /**
307          * Returns the file override for the given file.
308          * 
309          * @param filePath
310          *            The file path for which to get the override
311          * @return The file override, or <code>null</code> if the given file does
312          *         not have an override
313          */
314         public FileOverride getFileOverride(String filePath) {
315                 return fileOverrides.get(filePath);
316         }
317
318         /**
319          * Returns the list of {@link FileOverride}s.
320          * 
321          * @return All file overrides
322          */
323         public Map<String, FileOverride> getFileOverrides() {
324                 return fileOverrides;
325         }
326
327         /**
328          * Scans the base path for files and returns the {@link ProjectFile} for the
329          * base path. From this file it is possible to reach all files in the base
330          * path. This method is disk-intensive and may take some time on larger
331          * directories!
332          * 
333          * @return The file for the base path, or <code>null</code> if the base
334          *         path does not denote an existing directory
335          */
336         public ProjectFile getBaseFile() {
337                 File basePathFile = new File(basePath);
338                 if (!basePathFile.exists() || !basePathFile.isDirectory()) {
339                         return null;
340                 }
341                 rootProjectFile = new ProjectFileImpl(null, "", 0, true, false);
342                 scanDirectory(basePathFile, rootProjectFile);
343                 return rootProjectFile;
344         }
345
346         /**
347          * Returns the file that is specified by its complete path.
348          * 
349          * @param completePath
350          *            The complete path of the file
351          * @return The project file at the given path, or <code>null</code> if
352          *         there is no such file
353          */
354         public ProjectFile getFile(String completePath) {
355                 if (rootProjectFile == null) {
356                         getBaseFile();
357                 }
358                 if ((rootProjectFile == null) || (completePath.length() == 0)) {
359                         return rootProjectFile;
360                 }
361                 String[] pathParts = completePath.split("\\" + File.separator);
362                 ProjectFileImpl currentProjectFile = rootProjectFile;
363                 for (String pathPart : pathParts) {
364                         currentProjectFile = currentProjectFile.getFile(pathPart);
365                         if (currentProjectFile == null) {
366                                 return null;
367                         }
368                 }
369                 return currentProjectFile;
370         }
371
372         //
373         // PRIVATE METHODS
374         //
375
376         /**
377          * Scans the given directory and recreates the file and directory structure
378          * in the given project file.
379          * 
380          * @param directory
381          *            The directory to scan
382          * @param projectFile
383          *            The project file in which to recreate the directory and file
384          *            structure
385          */
386         private void scanDirectory(File directory, ProjectFileImpl projectFile) {
387                 if (!directory.isDirectory()) {
388                         return;
389                 }
390                 for (File file : directory.listFiles()) {
391                         ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
392                         if (file.isDirectory()) {
393                                 scanDirectory(file, projectFileChild);
394                         }
395                 }
396                 projectFile.sort();
397         }
398
399         /**
400          * Implementation of a {@link ProjectFile}.
401          * 
402          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
403          */
404         private static class ProjectFileImpl implements ProjectFile, Comparable<ProjectFileImpl> {
405
406                 /** The parent of this project file. */
407                 private final ProjectFileImpl parentProjectFile;
408
409                 /** The name of this project file. */
410                 private final String name;
411
412                 /** The size of the file. */
413                 private final long size;
414
415                 /** Whether this project file is a directory. */
416                 private final boolean directory;
417
418                 /** Whether this file is hidden. */
419                 private final boolean hidden;
420
421                 /** This project file’s children. */
422                 private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
423
424                 /**
425                  * Creates a new project fie.
426                  * 
427                  * @param parentProjectFile
428                  *            The parent of the project file, or <code>null</code> if
429                  *            the new project file does not have a parent
430                  * @param name
431                  *            The name of the project file
432                  * @param size
433                  *            The size of the file
434                  * @param isDirectory
435                  *            <code>true</code> if this project file is a directory,
436                  *            <code>false</code> otherwise
437                  * @param isHidden
438                  *            <code>true</code> if this project file is hidden,
439                  *            <code>false</code> otherwise
440                  */
441                 ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, long size, boolean isDirectory, boolean isHidden) {
442                         this.parentProjectFile = parentProjectFile;
443                         this.name = name;
444                         this.size = size;
445                         this.directory = isDirectory;
446                         this.hidden = isHidden;
447                 }
448
449                 //
450                 // INTERFACE ProjectFile
451                 //
452
453                 /**
454                  * @see net.pterodactylus.jsite.project.ProjectFile#getName()
455                  */
456                 public String getName() {
457                         return name;
458                 }
459
460                 /**
461                  * @see net.pterodactylus.jsite.project.ProjectFile#getParent()
462                  */
463                 public ProjectFile getParent() {
464                         return parentProjectFile;
465                 }
466
467                 /**
468                  * {@inheritDoc}
469                  */
470                 public long getSize() {
471                         return size;
472                 }
473
474                 /**
475                  * @see net.pterodactylus.jsite.project.ProjectFile#getParents()
476                  */
477                 public List<ProjectFile> getParents() {
478                         List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
479                         ProjectFileImpl currentProjectFile = this;
480                         do {
481                                 parentProjectFiles.add(0, currentProjectFile);
482                         } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
483                         return parentProjectFiles;
484                 }
485
486                 /**
487                  * {@inheritDoc}
488                  */
489                 /* TODO - caching? */
490                 public String getCompletePath() {
491                         StringBuilder completePath = new StringBuilder();
492                         ProjectFileImpl currentProjectFile = this;
493                         while ((currentProjectFile != null) && (currentProjectFile.parentProjectFile != null)) {
494                                 completePath.insert(0, currentProjectFile.getName()).insert(0, File.separatorChar);
495                                 currentProjectFile = currentProjectFile.parentProjectFile;
496                         }
497                         return (completePath.length() > 0) ? completePath.substring(1) : "";
498                 }
499
500                 /**
501                  * @see net.pterodactylus.jsite.project.ProjectFile#isFile()
502                  */
503                 public boolean isFile() {
504                         return !directory;
505                 }
506
507                 /**
508                  * @see net.pterodactylus.jsite.project.ProjectFile#isDirectory()
509                  */
510                 public boolean isDirectory() {
511                         return directory;
512                 }
513
514                 /**
515                  * @see net.pterodactylus.jsite.project.ProjectFile#isHidden()
516                  */
517                 public boolean isHidden() {
518                         return hidden;
519                 }
520
521                 /**
522                  * Returns the project file with the given name. The project file has to
523                  * be a direct child of this project file.
524                  * 
525                  * @param name
526                  *            The name of the file to get
527                  * @return The project file, or <code>null</code> if there is no
528                  *         project file by that name
529                  */
530                 public ProjectFileImpl getFile(String name) {
531                         if (!isDirectory()) {
532                                 return null;
533                         }
534                         for (ProjectFileImpl projectFile : childProjectFiles) {
535                                 if (projectFile.getName().equals(name)) {
536                                         return projectFile;
537                                 }
538                         }
539                         return null;
540                 }
541
542                 /**
543                  * @see net.pterodactylus.jsite.project.ProjectFile#getFiles()
544                  */
545                 public List<ProjectFile> getFiles() {
546                         List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
547                         return projectFiles;
548                 }
549
550                 //
551                 // ACTIONS
552                 //
553
554                 /**
555                  * Adds a new project file as child to this project file.
556                  * 
557                  * @param name
558                  *            The name of the file
559                  * @param size
560                  *            The size of the file
561                  * @param isDirectory
562                  *            <code>true</code> if the new file is a directory,
563                  *            <code>false</code> otherwise
564                  * @param isHidden
565                  *            <code>true</code> if the new file is hidden,
566                  *            <code>false</code> otherwise
567                  * @return The created project file
568                  */
569                 public ProjectFileImpl addFile(String name, long size, boolean isDirectory, boolean isHidden) {
570                         ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, size, isDirectory, isHidden);
571                         childProjectFiles.add(newProjectFile);
572                         return newProjectFile;
573                 }
574
575                 /**
576                  * Sorts the children of this file.
577                  */
578                 public void sort() {
579                         Collections.sort(childProjectFiles);
580                 }
581
582                 //
583                 // INTERFACE Comparable
584                 //
585
586                 /**
587                  * {@inheritDoc}
588                  */
589                 public int compareTo(ProjectFileImpl otherProjectFileImpl) {
590                         return name.compareTo(otherProjectFileImpl.name);
591                 }
592
593         }
594
595 }