aa28b54a027143d87e2258c316626907bb932093
[jSite.git] / src / main / java / de / todesbaum / jsite / application / FileOption.java
1 /*
2  * jSite - FileOption.java - Copyright © 2006–2012 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 import static com.google.common.base.Optional.absent;
22 import static com.google.common.base.Optional.of;
23
24 import com.google.common.base.Optional;
25
26 /**
27  * Container for various file options.
28  *
29  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
30  */
31 public class FileOption {
32
33         /** The default for the insert state. */
34         private static final boolean DEFAULT_INSERT = true;
35
36         /** The default for the insert redirect state. */
37         private static final boolean DEFAULT_INSERT_REDIRECT = true;
38
39         /** The default for the custom key. */
40         private static final String DEFAULT_CUSTOM_KEY = "CHK@";
41
42         /** The insert state. */
43         private boolean insert;
44
45         /** Whether to force an insert. */
46         private boolean forceInsert;
47
48         /** Whether to insert a redirect. */
49         private boolean insertRedirect;
50
51         /** The hash of the last insert. */
52         private String lastInsertHash;
53
54         /** The edition of the last insert. */
55         private int lastInsertEdition;
56
57         /** The filename of the last insert. */
58         private String lastInsertFilename;
59
60         /** The current hash of the file. */
61         private String currentHash;
62
63         /** The custom key. */
64         private String customKey;
65
66         /** The changed name. */
67         private Optional<String> changedName = absent();
68
69         /** The default MIME type. */
70         private final String defaultMimeType;
71
72         /** The current MIME type. */
73         private String mimeType;
74
75         /**
76          * Creates new file options.
77          *
78          * @param defaultMimeType
79          *            The default MIME type of the file
80          */
81         public FileOption(String defaultMimeType) {
82                 insert = DEFAULT_INSERT;
83                 insertRedirect = DEFAULT_INSERT_REDIRECT;
84                 customKey = DEFAULT_CUSTOM_KEY;
85                 this.defaultMimeType = defaultMimeType;
86                 mimeType = defaultMimeType;
87         }
88
89         /**
90          * Returns the custom key. The custom key is only used when
91          * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
92          * true}.
93          *
94          * @return The custom key
95          */
96         public String getCustomKey() {
97                 return customKey;
98         }
99
100         /**
101          * Sets the custom key. The custom key is only used when {@link #isInsert()}
102          * and {@link #isInsertRedirect()} both return {@code true}.
103          *
104          * @param customKey
105          *            The custom key
106          */
107         public void setCustomKey(String customKey) {
108                 if (customKey == null) {
109                         this.customKey = "";
110                 } else {
111                         this.customKey = customKey;
112                 }
113         }
114
115         /**
116          * Returns whether the file should be inserted. If a file is not inserted
117          * and {@link #isInsertRedirect()} is also {@code false}, the file will not
118          * be inserted at all.
119          *
120          * @see #setCustomKey(String)
121          * @return <code>true</code> if the file should be inserted,
122          *         <code>false</code> otherwise
123          */
124         public boolean isInsert() {
125                 return insert;
126         }
127
128         /**
129          * Sets whether the file should be inserted. If a file is not inserted and
130          * {@link #isInsertRedirect()} is also {@code false}, the file will not be
131          * inserted at all.
132          *
133          * @param insert
134          *            <code>true</code> if the file should be inserted,
135          *            <code>false</code> otherwise
136          */
137         public void setInsert(boolean insert) {
138                 this.insert = insert;
139         }
140
141         /**
142          * Returns whether the insert of this file should be forced, even if its
143          * current hash matches the last insert hash.
144          *
145          * @return {@code true} to force the insert of this file, {@code false}
146          *         otherwise
147          */
148         public boolean isForceInsert() {
149                 return forceInsert;
150         }
151
152         /**
153          * Sets whether to force the insert of this file, even if its current hash
154          * matches the last insert hash.
155          *
156          * @param forceInsert
157          *            {@code true} to force the insert of this file, {@code false}
158          *            otherwise
159          * @return These file options
160          */
161         public FileOption setForceInsert(boolean forceInsert) {
162                 this.forceInsert = forceInsert;
163                 return this;
164         }
165
166         /**
167          * Returns whether a redirect to a different key should be inserted. This
168          * will only matter if {@link #isInsert()} returns {@code false}. The key
169          * that should be redirected to still needs to be specified via
170          * {@link #setCustomKey(String)}.
171          *
172          * @return {@code true} if a redirect should be inserted, {@code false}
173          *         otherwise
174          */
175         public boolean isInsertRedirect() {
176                 return insertRedirect;
177         }
178
179         /**
180          * Sets whether a redirect should be inserted. This will only matter if
181          * {@link #isInsert()} returns {@code false}, i.e. it has been
182          * {@link #setInsert(boolean)} to {@code false}. The key that should be
183          * redirected to still needs to be specified via
184          * {@link #setCustomKey(String)}.
185          *
186          * @param insertRedirect
187          *            {@code true} if a redirect should be inserted, {@code false}
188          *            otherwise
189          */
190         public void setInsertRedirect(boolean insertRedirect) {
191                 this.insertRedirect = insertRedirect;
192         }
193
194         /**
195          * Returns the hash of the file when it was last inserted
196          *
197          * @return The last hash of the file
198          */
199         public String getLastInsertHash() {
200                 return lastInsertHash;
201         }
202
203         /**
204          * Sets the hash of the file when it was last inserted.
205          *
206          * @param lastInsertHash
207          *            The last hash of the file
208          * @return These file options
209          */
210         public FileOption setLastInsertHash(String lastInsertHash) {
211                 this.lastInsertHash = lastInsertHash;
212                 return this;
213         }
214
215         /**
216          * Returns the last edition at which this file was inserted.
217          *
218          * @return The last insert edition of this file
219          */
220         public int getLastInsertEdition() {
221                 return lastInsertEdition;
222         }
223
224         /**
225          * Sets the last insert edition of this file.
226          *
227          * @param lastInsertEdition
228          *            The last insert edition of this file
229          * @return These file options
230          */
231         public FileOption setLastInsertEdition(int lastInsertEdition) {
232                 this.lastInsertEdition = lastInsertEdition;
233                 return this;
234         }
235
236         /**
237          * Returns the name of the file when it was last inserted.
238          *
239          * @return The name of the file at the last insert
240          */
241         public String getLastInsertFilename() {
242                 return lastInsertFilename;
243         }
244
245         /**
246          * Sets the name of the file when it was last inserted.
247          *
248          * @param lastInsertFilename
249          *            The name of the file at the last insert.
250          * @return These file options
251          */
252         public FileOption setLastInsertFilename(String lastInsertFilename) {
253                 this.lastInsertFilename = lastInsertFilename;
254                 return this;
255         }
256
257         /**
258          * Returns the current hash of the file. This value is ony a temporary value
259          * that is copied to {@link #getLastInsertHash()} when a project has
260          * finished inserting.
261          *
262          * @see Project#onSuccessfulInsert()
263          * @return The current hash of the file
264          */
265         public String getCurrentHash() {
266                 return currentHash;
267         }
268
269         /**
270          * Sets the current hash of the file.
271          *
272          * @param currentHash
273          *            The current hash of the file
274          * @return These file options
275          */
276         public FileOption setCurrentHash(String currentHash) {
277                 this.currentHash = currentHash;
278                 return this;
279         }
280
281         /**
282          * Returns the changed name for this file. This method will return {@code
283          * null} or an empty {@link String} if this file should not be renamed.
284          *
285          * @return The changed name, or {@code null} if this file should not be
286          *         renamed
287          */
288         public Optional<String> getChangedName() {
289                 return changedName;
290         }
291
292         /**
293          * Sets the changed name for this file. Setting the changed file to {@code
294          * null} or an empty {@link String} will disable renaming.
295          *
296          * @param changedName
297          *            The new changed name for this file
298          */
299         public void setChangedName(String changedName) {
300                 this.changedName = ((changedName != null) && (changedName.length() > 0)) ? of(changedName) : Optional.<String>absent();
301         }
302
303         /**
304          * Sets the MIME type of the file. Setting the MIME type to
305          * <code>null</code> will set the MIME type to the default MIME type.
306          *
307          * @param mimeType
308          *            The MIME type of the file
309          */
310         public void setMimeType(String mimeType) {
311                 if (mimeType == null) {
312                         this.mimeType = defaultMimeType;
313                 } else {
314                         this.mimeType = mimeType;
315                 }
316         }
317
318         /**
319          * Returns the MIME type of the file. If no custom MIME type has been set,
320          * the default MIME type is returned.
321          *
322          * @return The MIME type of the file
323          */
324         public String getMimeType() {
325                 return mimeType;
326         }
327
328         /**
329          * Returns whether the options for this file have been modified, i.e. are
330          * not at their default values.
331          *
332          * @return <code>true</code> if the options have been modified,
333          *         <code>false</code> if they are at default values
334          */
335         public boolean isCustom() {
336                 if (insert != DEFAULT_INSERT) {
337                         return true;
338                 }
339                 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
340                         return true;
341                 }
342                 if (changedName.isPresent()) {
343                         return true;
344                 }
345                 if (!defaultMimeType.equals(mimeType)) {
346                         return true;
347                 }
348                 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {
349                         return true;
350                 }
351                 return false;
352         }
353
354 }