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