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