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