Clear force-insert flag after successful insert.
[jSite.git] / src / de / todesbaum / jsite / application / Project.java
index 7b4dc00..bf891b1 100644 (file)
@@ -1,20 +1,19 @@
 /*
- * jSite - a tool for uploading websites into Freenet
- * Copyright (C) 2006 David Roden
+ * jSite - Project.java - Copyright © 2006–2011 David Roden
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 2 of the License, or (at your option) any later
+ * version.
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
 package de.todesbaum.jsite.application;
@@ -28,7 +27,7 @@ import de.todesbaum.util.mime.DefaultMIMETypes;
 
 /**
  * Container for project information.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
 public class Project implements Comparable<Project> {
@@ -60,6 +59,9 @@ public class Project implements Comparable<Project> {
        /** The edition to insert to. */
        protected int edition;
 
+       /** Whether to ignore hidden directory. */
+       private boolean ignoreHiddenFiles;
+
        /** Options for files. */
        protected Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
 
@@ -72,7 +74,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Creates a new project from an existing one.
-        * 
+        *
         * @param project
         *            The project to clone
         */
@@ -86,12 +88,13 @@ public class Project implements Comparable<Project> {
                localPath = project.localPath;
                indexFile = project.indexFile;
                lastInsertionTime = project.lastInsertionTime;
+               ignoreHiddenFiles = project.ignoreHiddenFiles;
                fileOptions = new HashMap<String, FileOption>(project.fileOptions);
        }
 
        /**
         * Returns the name of the project.
-        * 
+        *
         * @return The name of the project
         */
        public String getName() {
@@ -100,7 +103,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Sets the name of the project.
-        * 
+        *
         * @param name
         *            The name of the project
         */
@@ -110,7 +113,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Returns the description of the project.
-        * 
+        *
         * @return The description of the project
         */
        public String getDescription() {
@@ -119,7 +122,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Sets the description of the project.
-        * 
+        *
         * @param description
         *            The description of the project
         */
@@ -129,7 +132,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Returns the local path of the project.
-        * 
+        *
         * @return The local path of the project
         */
        public String getLocalPath() {
@@ -138,7 +141,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Sets the local path of the project.
-        * 
+        *
         * @param localPath
         *            The local path of the project
         */
@@ -149,7 +152,7 @@ public class Project implements Comparable<Project> {
        /**
         * Returns the name of the index file of the project, relative to the
         * project’s local path.
-        * 
+        *
         * @return The name of the index file of the project
         */
        public String getIndexFile() {
@@ -159,7 +162,7 @@ public class Project implements Comparable<Project> {
        /**
         * Sets the name of the index file of the project, relative to the project’s
         * local path.
-        * 
+        *
         * @param indexFile
         *            The name of the index file of the project
         */
@@ -170,7 +173,7 @@ public class Project implements Comparable<Project> {
        /**
         * Returns the time the project was last inserted, in milliseconds since the
         * epoch.
-        * 
+        *
         * @return The time of the last insertion
         */
        public long getLastInsertionTime() {
@@ -180,7 +183,7 @@ public class Project implements Comparable<Project> {
        /**
         * Sets the time the project was last inserted, in milliseconds since the
         * last epoch.
-        * 
+        *
         * @param lastInserted
         *            The time of the last insertion
         */
@@ -191,7 +194,7 @@ public class Project implements Comparable<Project> {
        /**
         * Returns the remote path of the project. The remote path is the path that
         * directly follows the request URI of the project.
-        * 
+        *
         * @return The remote path of the project
         */
        public String getPath() {
@@ -201,7 +204,7 @@ public class Project implements Comparable<Project> {
        /**
         * Sets the remote path of the project. The remote path is the path that
         * directly follows the request URI of the project.
-        * 
+        *
         * @param path
         *            The remote path of the project
         */
@@ -211,7 +214,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Returns the insert URI of the project.
-        * 
+        *
         * @return The insert URI of the project
         */
        public String getInsertURI() {
@@ -220,7 +223,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Sets the insert URI of the project.
-        * 
+        *
         * @param insertURI
         *            The insert URI of the project
         */
@@ -230,7 +233,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Returns the request URI of the project.
-        * 
+        *
         * @return The request URI of the project
         */
        public String getRequestURI() {
@@ -239,7 +242,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Sets the request URI of the project.
-        * 
+        *
         * @param requestURI
         *            The request URI of the project
         */
@@ -248,6 +251,27 @@ public class Project implements Comparable<Project> {
        }
 
        /**
+        * Returns whether hidden files are ignored, i.e. not inserted.
+        *
+        * @return {@code true} if hidden files are not inserted, {@code false}
+        *         otherwise
+        */
+       public boolean isIgnoreHiddenFiles() {
+               return ignoreHiddenFiles;
+       }
+
+       /**
+        * Sets whether hidden files are ignored, i.e. not inserted.
+        *
+        * @param ignoreHiddenFiles
+        *            {@code true} if hidden files are not inserted, {@code false}
+        *            otherwise
+        */
+       public void setIgnoreHiddenFiles(boolean ignoreHiddenFiles) {
+               this.ignoreHiddenFiles = ignoreHiddenFiles;
+       }
+
+       /**
         * {@inheritDoc}
         * <p>
         * This method returns the name of the project.
@@ -259,31 +283,32 @@ public class Project implements Comparable<Project> {
 
        /**
         * Shortens the given URI by removing scheme and key-type prefixes.
-        * 
+        *
         * @param uri
         *            The URI to shorten
         * @return The shortened URI
         */
        private String shortenURI(String uri) {
-               if (uri.startsWith("freenet:")) {
-                       uri = uri.substring("freenet:".length());
+               String shortUri = uri;
+               if (shortUri.startsWith("freenet:")) {
+                       shortUri = shortUri.substring("freenet:".length());
                }
-               if (uri.startsWith("SSK@")) {
-                       uri = uri.substring("SSK@".length());
+               if (shortUri.startsWith("SSK@")) {
+                       shortUri = shortUri.substring("SSK@".length());
                }
-               if (uri.startsWith("USK@")) {
-                       uri = uri.substring("USK@".length());
+               if (shortUri.startsWith("USK@")) {
+                       shortUri = shortUri.substring("USK@".length());
                }
-               if (uri.endsWith("/")) {
-                       uri = uri.substring(0, uri.length() - 1);
+               if (shortUri.endsWith("/")) {
+                       shortUri = shortUri.substring(0, shortUri.length() - 1);
                }
-               return uri;
+               return shortUri;
        }
 
        /**
         * Shortens the name of the given file by removing the local path of the
         * project and leading file separators.
-        * 
+        *
         * @param file
         *            The file whose name should be shortened
         * @return The shortened name of the file
@@ -303,7 +328,7 @@ public class Project implements Comparable<Project> {
         * Returns the options for the file with the given name. If the file does
         * not yet have any options, a new set of default options is created and
         * returned.
-        * 
+        *
         * @param filename
         *            The name of the file, relative to the project root
         * @return The options for the file
@@ -319,7 +344,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Sets options for a file.
-        * 
+        *
         * @param filename
         *            The filename to set the options for, relative to the project
         *            root
@@ -337,7 +362,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Returns all file options.
-        * 
+        *
         * @return All file options
         */
        public Map<String, FileOption> getFileOptions() {
@@ -346,7 +371,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Sets all file options.
-        * 
+        *
         * @param fileOptions
         *            The file options
         */
@@ -366,7 +391,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Returns the edition of the project.
-        * 
+        *
         * @return The edition of the project
         */
        public int getEdition() {
@@ -375,7 +400,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Sets the edition of the project.
-        * 
+        *
         * @param edition
         *            The edition to set
         */
@@ -385,7 +410,7 @@ public class Project implements Comparable<Project> {
 
        /**
         * Constructs the final request URI including the edition number.
-        * 
+        *
         * @param offset
         *            The offset for the edition number
         * @return The final request URI
@@ -394,4 +419,20 @@ public class Project implements Comparable<Project> {
                return "USK@" + requestURI + "/" + path + "/" + (edition + offset) + "/";
        }
 
+       /**
+        * Performs some post-processing on the project after it was inserted
+        * successfully. At the moment it copies the current hashes of all file
+        * options to the last insert hashes, updating the hashes for the next
+        * insert.
+        */
+       public void onSuccessfulInsert() {
+               for (FileOption fileOption : fileOptions.values()) {
+                       if ((fileOption.getCurrentHash() != null) && (fileOption.getCurrentHash().length() > 0) && !fileOption.getCurrentHash().equals(fileOption.getLastInsertHash())) {
+                               fileOption.setLastInsertEdition(edition);
+                               fileOption.setLastInsertHash(fileOption.getCurrentHash());
+                       }
+                       fileOption.setForceInsert(false);
+               }
+       }
+
 }