2e5830de3e133d0bfb52ef9af0521bb8fc72aeb4
[jSite.git] / src / de / todesbaum / jsite / application / FileOption.java
1 /*
2  * jSite - FileOption.java - Copyright © 2006–2011 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.application;
20
21 /**
22  * Container for various file options.
23  *
24  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
25  */
26 public class FileOption {
27
28         /** The default for the insert state. */
29         private static final boolean DEFAULT_INSERT = true;
30
31         /** The default for the insert redirect state. */
32         private static final boolean DEFAULT_INSERT_REDIRECT = true;
33
34         /** The default for the custom key. */
35         private static final String DEFAULT_CUSTOM_KEY = "CHK@";
36
37         /** The default changed name. */
38         private static final String DEFAULT_CHANGED_NAME = null;
39
40         /** The insert state. */
41         private boolean insert;
42
43         /** Whether to insert a redirect. */
44         private boolean insertRedirect;
45
46         /** The hash of the last insert. */
47         private String lastInsertHash;
48
49         /** The current hash of the file. */
50         private String currentHash;
51
52         /** The custom key. */
53         private String customKey;
54
55         /** The changed name. */
56         private String changedName;
57
58         /** The default MIME type. */
59         private final String defaultMimeType;
60
61         /** The current MIME type. */
62         private String mimeType;
63
64         /**
65          * Creates new file options.
66          *
67          * @param defaultMimeType
68          *            The default MIME type of the file
69          */
70         public FileOption(String defaultMimeType) {
71                 insert = DEFAULT_INSERT;
72                 insertRedirect = DEFAULT_INSERT_REDIRECT;
73                 customKey = DEFAULT_CUSTOM_KEY;
74                 changedName = DEFAULT_CHANGED_NAME;
75                 this.defaultMimeType = defaultMimeType;
76                 mimeType = defaultMimeType;
77         }
78
79         /**
80          * Returns the custom key. The custom key is only used when
81          * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
82          * true}.
83          *
84          * @return The custom key
85          */
86         public String getCustomKey() {
87                 return customKey;
88         }
89
90         /**
91          * Sets the custom key. The custom key is only used when {@link #isInsert()}
92          * and {@link #isInsertRedirect()} both return {@code true}.
93          *
94          * @param customKey
95          *            The custom key
96          */
97         public void setCustomKey(String customKey) {
98                 if (customKey == null) {
99                         this.customKey = "";
100                 } else {
101                         this.customKey = customKey;
102                 }
103         }
104
105         /**
106          * Returns whether the file should be inserted. If a file is not inserted
107          * and {@link #isInsertRedirect()} is also {@code false}, the file will not
108          * be inserted at all.
109          *
110          * @see #setCustomKey(String)
111          * @return <code>true</code> if the file should be inserted,
112          *         <code>false</code> otherwise
113          */
114         public boolean isInsert() {
115                 return insert;
116         }
117
118         /**
119          * Sets whether the file should be inserted. If a file is not inserted and
120          * {@link #isInsertRedirect()} is also {@code false}, the file will not be
121          * inserted at all.
122          *
123          * @param insert
124          *            <code>true</code> if the file should be inserted,
125          *            <code>false</code> otherwise
126          */
127         public void setInsert(boolean insert) {
128                 this.insert = insert;
129         }
130
131         /**
132          * Returns whether a redirect to a different key should be inserted. This
133          * will only matter if {@link #isInsert()} returns {@code false}. The key
134          * that should be redirected to still needs to be specified via
135          * {@link #setCustomKey(String)}.
136          *
137          * @return {@code true} if a redirect should be inserted, {@code false}
138          *         otherwise
139          */
140         public boolean isInsertRedirect() {
141                 return insertRedirect;
142         }
143
144         /**
145          * Sets whether a redirect should be inserted. This will only matter if
146          * {@link #isInsert()} returns {@code false}, i.e. it has been
147          * {@link #setInsert(boolean)} to {@code false}. The key that should be
148          * redirected to still needs to be specified via
149          * {@link #setCustomKey(String)}.
150          *
151          * @param insertRedirect
152          *            {@code true} if a redirect should be inserted, {@code false}
153          *            otherwise
154          */
155         public void setInsertRedirect(boolean insertRedirect) {
156                 this.insertRedirect = insertRedirect;
157         }
158
159         /**
160          * Returns the hash of the file when it was last inserted
161          *
162          * @return The last hash of the file
163          */
164         public String getLastInsertHash() {
165                 return lastInsertHash;
166         }
167
168         /**
169          * Sets the hash of the file when it was last inserted.
170          *
171          * @param lastInsertHash
172          *            The last hash of the file
173          * @return These file options
174          */
175         public FileOption setLastInsertHash(String lastInsertHash) {
176                 this.lastInsertHash = lastInsertHash;
177                 return this;
178         }
179
180         /**
181          * Returns the current hash of the file. This value is ony a temporary value
182          * that is copied to {@link #getLastInsertHash()} when a project has
183          * finished inserting.
184          *
185          * @see Project#copyHashes()
186          * @return The current hash of the file
187          */
188         public String getCurrentHash() {
189                 return currentHash;
190         }
191
192         /**
193          * Sets the current hash of the file.
194          *
195          * @param currentHash
196          *            The current hash of the file
197          * @return These file options
198          */
199         public FileOption setCurrentHash(String currentHash) {
200                 this.currentHash = currentHash;
201                 return this;
202         }
203
204         /**
205          * Returns whether this file has a changed name. Use
206          * {@link #getChangedName()} is this method returns {@code true}.
207          *
208          * @return {@code true} if this file has a changed name, {@code false}
209          *         otherwise
210          */
211         public boolean hasChangedName() {
212                 return (changedName != null) && (changedName.length() > 0);
213         }
214
215         /**
216          * Returns the changed name for this file. This method will return {@code
217          * null} or an empty {@link String} if this file should not be renamed.
218          *
219          * @return The changed name, or {@code null} if this file should not be
220          *         renamed
221          */
222         public String getChangedName() {
223                 return changedName;
224         }
225
226         /**
227          * Sets the changed name for this file. Setting the changed file to {@code
228          * null} or an empty {@link String} will disable renaming.
229          *
230          * @param changedName
231          *            The new changed name for this file
232          */
233         public void setChangedName(String changedName) {
234                 this.changedName = changedName;
235         }
236
237         /**
238          * Sets the MIME type of the file. Setting the MIME type to
239          * <code>null</code> will set the MIME type to the default MIME type.
240          *
241          * @param mimeType
242          *            The MIME type of the file
243          */
244         public void setMimeType(String mimeType) {
245                 if (mimeType == null) {
246                         this.mimeType = defaultMimeType;
247                 } else {
248                         this.mimeType = mimeType;
249                 }
250         }
251
252         /**
253          * Returns the MIME type of the file. If no custom MIME type has been set,
254          * the default MIME type is returned.
255          *
256          * @return The MIME type of the file
257          */
258         public String getMimeType() {
259                 return mimeType;
260         }
261
262         /**
263          * Returns whether the options for this file have been modified, i.e. are
264          * not at their default values.
265          *
266          * @return <code>true</code> if the options have been modified,
267          *         <code>false</code> if they are at default values
268          */
269         public boolean isCustom() {
270                 if (insert != DEFAULT_INSERT) {
271                         return true;
272                 }
273                 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
274                         return true;
275                 }
276                 if (((changedName != null) && !changedName.equals(DEFAULT_CHANGED_NAME)) || ((DEFAULT_CHANGED_NAME != null) && !DEFAULT_CHANGED_NAME.equals(changedName))) {
277                         return true;
278                 }
279                 if (!defaultMimeType.equals(mimeType)) {
280                         return true;
281                 }
282                 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {
283                         return true;
284                 }
285                 return false;
286         }
287
288 }