2 * jSite - FileOption.java - Copyright © 2006–2012 David Roden
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
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
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.
19 package de.todesbaum.jsite.application;
22 * Container for various file options.
24 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
26 public class FileOption {
28 /** The default for the insert state. */
29 private static final boolean DEFAULT_INSERT = true;
31 /** The default for the insert redirect state. */
32 private static final boolean DEFAULT_INSERT_REDIRECT = true;
34 /** The default for the custom key. */
35 private static final String DEFAULT_CUSTOM_KEY = "CHK@";
37 /** The default changed name. */
38 private static final String DEFAULT_CHANGED_NAME = null;
40 /** The insert state. */
41 private boolean insert;
43 /** Whether to force an insert. */
44 private boolean forceInsert;
46 /** Whether to insert a redirect. */
47 private boolean insertRedirect;
49 /** The hash of the last insert. */
50 private String lastInsertHash;
52 /** The edition of the last insert. */
53 private int lastInsertEdition;
55 /** The filename of the last insert. */
56 private String lastInsertFilename;
58 /** The current hash of the file. */
59 private String currentHash;
61 /** The custom key. */
62 private String customKey;
64 /** The changed name. */
65 private String changedName;
67 /** The default MIME type. */
68 private final String defaultMimeType;
70 /** The current MIME type. */
71 private String mimeType;
74 * Creates new file options.
76 * @param defaultMimeType
77 * The default MIME type of the file
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;
89 * Returns the custom key. The custom key is only used when
90 * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
93 * @return The custom key
95 public String getCustomKey() {
100 * Sets the custom key. The custom key is only used when {@link #isInsert()}
101 * and {@link #isInsertRedirect()} both return {@code true}.
106 public void setCustomKey(String customKey) {
107 if (customKey == null) {
110 this.customKey = customKey;
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.
119 * @see #setCustomKey(String)
120 * @return <code>true</code> if the file should be inserted,
121 * <code>false</code> otherwise
123 public boolean isInsert() {
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
133 * <code>true</code> if the file should be inserted,
134 * <code>false</code> otherwise
136 public void setInsert(boolean insert) {
137 this.insert = insert;
141 * Returns whether the insert of this file should be forced, even if its
142 * current hash matches the last insert hash.
144 * @return {@code true} to force the insert of this file, {@code false}
147 public boolean isForceInsert() {
152 * Sets whether to force the insert of this file, even if its current hash
153 * matches the last insert hash.
156 * {@code true} to force the insert of this file, {@code false}
158 * @return These file options
160 public FileOption setForceInsert(boolean forceInsert) {
161 this.forceInsert = forceInsert;
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)}.
171 * @return {@code true} if a redirect should be inserted, {@code false}
174 public boolean isInsertRedirect() {
175 return insertRedirect;
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)}.
185 * @param insertRedirect
186 * {@code true} if a redirect should be inserted, {@code false}
189 public void setInsertRedirect(boolean insertRedirect) {
190 this.insertRedirect = insertRedirect;
194 * Returns the hash of the file when it was last inserted
196 * @return The last hash of the file
198 public String getLastInsertHash() {
199 return lastInsertHash;
203 * Sets the hash of the file when it was last inserted.
205 * @param lastInsertHash
206 * The last hash of the file
207 * @return These file options
209 public FileOption setLastInsertHash(String lastInsertHash) {
210 this.lastInsertHash = lastInsertHash;
215 * Returns the last edition at which this file was inserted.
217 * @return The last insert edition of this file
219 public int getLastInsertEdition() {
220 return lastInsertEdition;
224 * Sets the last insert edition of this file.
226 * @param lastInsertEdition
227 * The last insert edition of this file
228 * @return These file options
230 public FileOption setLastInsertEdition(int lastInsertEdition) {
231 this.lastInsertEdition = lastInsertEdition;
236 * Returns the name of the file when it was last inserted.
238 * @return The name of the file at the last insert
240 public String getLastInsertFilename() {
241 return lastInsertFilename;
245 * Sets the name of the file when it was last inserted.
247 * @param lastInsertFilename
248 * The name of the file at the last insert.
249 * @return These file options
251 public FileOption setLastInsertFilename(String lastInsertFilename) {
252 this.lastInsertFilename = lastInsertFilename;
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.
261 * @see Project#onSuccessfulInsert()
262 * @return The current hash of the file
264 public String getCurrentHash() {
269 * Sets the current hash of the file.
272 * The current hash of the file
273 * @return These file options
275 public FileOption setCurrentHash(String currentHash) {
276 this.currentHash = currentHash;
281 * Returns whether this file has a changed name. Use
282 * {@link #getChangedName()} is this method returns {@code true}.
284 * @return {@code true} if this file has a changed name, {@code false}
287 public boolean hasChangedName() {
288 return (changedName != null) && (changedName.length() > 0);
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.
295 * @return The changed name, or {@code null} if this file should not be
298 public String getChangedName() {
303 * Sets the changed name for this file. Setting the changed file to {@code
304 * null} or an empty {@link String} will disable renaming.
307 * The new changed name for this file
309 public void setChangedName(String changedName) {
310 this.changedName = changedName;
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.
318 * The MIME type of the file
320 public void setMimeType(String mimeType) {
321 if (mimeType == null) {
322 this.mimeType = defaultMimeType;
324 this.mimeType = mimeType;
329 * Returns the MIME type of the file. If no custom MIME type has been set,
330 * the default MIME type is returned.
332 * @return The MIME type of the file
334 public String getMimeType() {
339 * Returns whether the options for this file have been modified, i.e. are
340 * not at their default values.
342 * @return <code>true</code> if the options have been modified,
343 * <code>false</code> if they are at default values
345 public boolean isCustom() {
346 if (insert != DEFAULT_INSERT) {
349 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
352 if (((changedName != null) && !changedName.equals(DEFAULT_CHANGED_NAME)) || ((DEFAULT_CHANGED_NAME != null) && !DEFAULT_CHANGED_NAME.equals(changedName))) {
355 if (!defaultMimeType.equals(mimeType)) {
358 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {