remove entries altogether
[jSite2.git] / src / net / pterodactylus / jsite / project / ProjectManager.java
index 5434842..69b0522 100644 (file)
@@ -43,7 +43,7 @@ import net.pterodactylus.util.number.Hex;
 /**
  * Manages projects, taking care of persistence, lifetime statistics, and other
  * things.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
 public class ProjectManager implements PropertyChangeListener {
@@ -66,7 +66,7 @@ public class ProjectManager implements PropertyChangeListener {
        /**
         * Creates a new project manager that saves and restores its state to/from
         * the given directory.
-        * 
+        *
         * @param directory
         *            The directory to save and restore states to/from
         */
@@ -80,7 +80,7 @@ public class ProjectManager implements PropertyChangeListener {
 
        /**
         * Returns the directory the projects are loaded from and saved to.
-        * 
+        *
         * @return The directory for storing the projects
         */
        public String getDirectory() {
@@ -89,7 +89,7 @@ public class ProjectManager implements PropertyChangeListener {
 
        /**
         * Returns a list of all projects.
-        * 
+        *
         * @return A list of all projects
         */
        public List<Project> getProjects() {
@@ -98,7 +98,7 @@ public class ProjectManager implements PropertyChangeListener {
 
        /**
         * Sets the node manager to use.
-        * 
+        *
         * @param nodeManager
         *            The node manager to use
         */
@@ -112,7 +112,7 @@ public class ProjectManager implements PropertyChangeListener {
 
        /**
         * Loads projects and statistics.
-        * 
+        *
         * @throws IOException
         *             if an I/O error occurs
         */
@@ -146,7 +146,6 @@ public class ProjectManager implements PropertyChangeListener {
                        project.setPrivateKey(projectPrivateKey);
                        project.setPublicKey(projectPublicKey);
                        project.setBasePath(projectBasePath);
-                       project.rescanBasePath();
                        projects.add(project);
                        logger.fine("loaded project “" + project.getName() + "”.");
                        projectIndex++;
@@ -155,7 +154,7 @@ public class ProjectManager implements PropertyChangeListener {
 
        /**
         * Saves projects and statistics.
-        * 
+        *
         * @throws IOException
         *             if an I/O error occurs
         */
@@ -191,7 +190,7 @@ public class ProjectManager implements PropertyChangeListener {
        /**
         * Creates a new project. The returned {@link Project} will contain a newly
         * generated key pair.
-        * 
+        *
         * @return A newly created project
         * @throws IOException
         *             if an I/O error occured communicating with the node
@@ -201,9 +200,7 @@ public class ProjectManager implements PropertyChangeListener {
        public Project createProject() throws IOException, JSiteException {
                Project project = new Project();
                String[] keyPair = nodeManager.generateKeyPair();
-               byte[] idBytes = new byte[16];
-               random.nextBytes(idBytes);
-               project.setId(Hex.toHex(idBytes));
+               project.setId(generateId());
                project.setName("");
                project.setDescription("");
                project.setPrivateKey(keyPair[0]);
@@ -219,6 +216,59 @@ public class ProjectManager implements PropertyChangeListener {
                return project;
        }
 
+       /**
+        * Clones the given project and returns the clone. The clone will be
+        * identical in all user-exposed fields, except for the project’s
+        * {@link Project#getId ID}.
+        *
+        * @param project
+        *            The project to clone
+        * @return The cloned project
+        */
+       public Project cloneProject(Project project) {
+               Project projectClone = new Project(project);
+               projects.add(projectClone);
+               projectClone.setId(generateId());
+               projectClone.addPropertyChangeListener(this);
+               try {
+                       save();
+               } catch (IOException ioe1) {
+                       /* ignore. */
+               }
+               return projectClone;
+       }
+
+       /**
+        * Removes the given project.
+        *
+        * @param project
+        *            The project to remove
+        */
+       public void removeProject(Project project) {
+               projects.remove(project);
+               try {
+                       save();
+               } catch (IOException ioe1) {
+                       /* ignore. */
+               }
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Generates a new random ID, consisting of 16 random bytes converted to a
+        * hexadecimal number.
+        *
+        * @return The new ID
+        */
+       private static String generateId() {
+               byte[] idBytes = new byte[16];
+               random.nextBytes(idBytes);
+               return Hex.toHex(idBytes);
+       }
+
        //
        // INTERFACE PropertyChangeListener
        //