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