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