d250af1a437d6c40c360767d6d49140ac91720e3
[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         // PRIVATE METHODS
404         //
405
406         /**
407          * Scans the given directory and recreates the file and directory structure
408          * in the given project file.
409          * 
410          * @param directory
411          *            The directory to scan
412          * @param projectFile
413          *            The project file in which to recreate the directory and file
414          *            structure
415          */
416         private void scanDirectory(File directory, ProjectFileImpl projectFile) {
417                 if (!directory.isDirectory()) {
418                         return;
419                 }
420                 for (File file : directory.listFiles()) {
421                         ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
422                         if (file.isDirectory()) {
423                                 scanDirectory(file, projectFileChild);
424                         }
425                 }
426                 projectFile.sort();
427         }
428
429         /**
430          * Implementation of a {@link ProjectFile}.
431          * 
432          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
433          */
434         private static class ProjectFileImpl implements ProjectFile, Comparable<ProjectFileImpl> {
435
436                 /** The parent of this project file. */
437                 private final ProjectFileImpl parentProjectFile;
438
439                 /** The name of this project file. */
440                 private final String name;
441
442                 /** The size of the file. */
443                 private final long size;
444
445                 /** Whether this project file is a directory. */
446                 private final boolean directory;
447
448                 /** Whether this file is hidden. */
449                 private final boolean hidden;
450
451                 /** This project file’s children. */
452                 private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
453
454                 /**
455                  * Creates a new project fie.
456                  * 
457                  * @param parentProjectFile
458                  *            The parent of the project file, or <code>null</code> if
459                  *            the new project file does not have a parent
460                  * @param name
461                  *            The name of the project file
462                  * @param size
463                  *            The size of the file
464                  * @param isDirectory
465                  *            <code>true</code> if this project file is a directory,
466                  *            <code>false</code> otherwise
467                  * @param isHidden
468                  *            <code>true</code> if this project file is hidden,
469                  *            <code>false</code> otherwise
470                  */
471                 ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, long size, boolean isDirectory, boolean isHidden) {
472                         this.parentProjectFile = parentProjectFile;
473                         this.name = name;
474                         this.size = size;
475                         this.directory = isDirectory;
476                         this.hidden = isHidden;
477                 }
478
479                 //
480                 // INTERFACE ProjectFile
481                 //
482
483                 /**
484                  * @see net.pterodactylus.jsite.core.ProjectFile#getName()
485                  */
486                 public String getName() {
487                         return name;
488                 }
489
490                 /**
491                  * @see net.pterodactylus.jsite.core.ProjectFile#getParent()
492                  */
493                 public ProjectFile getParent() {
494                         return parentProjectFile;
495                 }
496
497                 /**
498                  * {@inheritDoc}
499                  */
500                 public long getSize() {
501                         return size;
502                 }
503
504                 /**
505                  * @see net.pterodactylus.jsite.core.ProjectFile#getParents()
506                  */
507                 public List<ProjectFile> getParents() {
508                         List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
509                         ProjectFileImpl currentProjectFile = this;
510                         do {
511                                 parentProjectFiles.add(0, currentProjectFile);
512                         } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
513                         return parentProjectFiles;
514                 }
515
516                 /**
517                  * {@inheritDoc}
518                  */
519                 /* TODO - caching? */
520                 public String getCompletePath() {
521                         StringBuilder completePath = new StringBuilder();
522                         ProjectFileImpl currentProjectFile = this;
523                         while ((currentProjectFile != null) && (currentProjectFile.parentProjectFile != null)) {
524                                 completePath.insert(0, currentProjectFile.getName()).insert(0, File.separatorChar);
525                                 currentProjectFile = currentProjectFile.parentProjectFile;
526                         }
527                         return (completePath.length() > 0) ? completePath.substring(1) : "";
528                 }
529
530                 /**
531                  * @see net.pterodactylus.jsite.core.ProjectFile#isFile()
532                  */
533                 public boolean isFile() {
534                         return !directory;
535                 }
536
537                 /**
538                  * @see net.pterodactylus.jsite.core.ProjectFile#isDirectory()
539                  */
540                 public boolean isDirectory() {
541                         return directory;
542                 }
543
544                 /**
545                  * @see net.pterodactylus.jsite.core.ProjectFile#isHidden()
546                  */
547                 public boolean isHidden() {
548                         return hidden;
549                 }
550
551                 /**
552                  * Returns the project file with the given name. The project file has to
553                  * be a direct child of this project file.
554                  * 
555                  * @param name
556                  *            The name of the file to get
557                  * @return The project file, or <code>null</code> if there is no
558                  *         project file by that name
559                  */
560                 public ProjectFileImpl getFile(String name) {
561                         if (!isDirectory()) {
562                                 return null;
563                         }
564                         for (ProjectFileImpl projectFile : childProjectFiles) {
565                                 if (projectFile.getName().equals(name)) {
566                                         return projectFile;
567                                 }
568                         }
569                         return null;
570                 }
571
572                 /**
573                  * @see net.pterodactylus.jsite.core.ProjectFile#getFiles()
574                  */
575                 public List<ProjectFile> getFiles() {
576                         List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
577                         return projectFiles;
578                 }
579
580                 //
581                 // ACTIONS
582                 //
583
584                 /**
585                  * Adds a new project file as child to this project file.
586                  * 
587                  * @param name
588                  *            The name of the file
589                  * @param size
590                  *            The size of the file
591                  * @param isDirectory
592                  *            <code>true</code> if the new file is a directory,
593                  *            <code>false</code> otherwise
594                  * @param isHidden
595                  *            <code>true</code> if the new file is hidden,
596                  *            <code>false</code> otherwise
597                  * @return The created project file
598                  */
599                 public ProjectFileImpl addFile(String name, long size, boolean isDirectory, boolean isHidden) {
600                         ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, size, isDirectory, isHidden);
601                         childProjectFiles.add(newProjectFile);
602                         return newProjectFile;
603                 }
604
605                 /**
606                  * Sorts the children of this file.
607                  */
608                 public void sort() {
609                         Collections.sort(childProjectFiles);
610                 }
611
612                 //
613                 // INTERFACE Comparable
614                 //
615
616                 /**
617                  * {@inheritDoc}
618                  */
619                 public int compareTo(ProjectFileImpl otherProjectFileImpl) {
620                         return name.compareTo(otherProjectFileImpl.name);
621                 }
622
623         }
624
625 }