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