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