001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.value;
034:
035: import com.flexive.shared.FxSharedUtils;
036: import com.flexive.shared.ObjectWithLabel;
037: import com.flexive.shared.exceptions.FxStreamException;
038: import com.flexive.shared.stream.BinaryUploadPayload;
039: import com.flexive.shared.stream.FxStreamUtils;
040: import com.flexive.stream.ServerLocation;
041:
042: import java.io.*;
043: import java.util.List;
044:
045: /**
046: * Descriptor for binaries
047: *
048: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
049: */
050: public class BinaryDescriptor implements Serializable {
051: private static final long serialVersionUID = -416186902840155773L;
052:
053: public final static int SYS_UNKNOWN = -1;
054: public final static int SYS_NOACCESS = -2;
055: public final static int SYS_AUDIO = -3;
056: public final static int SYS_DOC = -4;
057: public final static int SYS_ICAL = -5;
058: public final static int SYS_INFO = -6;
059: public final static int SYS_PDF = -7;
060: public final static int SYS_TXT = -8;
061: public final static int SYS_VIDEO = -9;
062: public final static int SYS_XLS = -10;
063: public final static int SYS_PPT = -6; //TODO: find a fitting thumbnail!
064: public final static int SYS_HTML = -8; //TODO: find a fitting thumbnail!
065:
066: public final static int SYS_SELECTLIST_DEFAULT = -11;
067:
068: public final static String EMPTY = "[EMPTY]";
069: /**
070: * For images: box scaled size for preview 1
071: */
072: public final static int PREVIEW1_BOX = 42;
073: /**
074: * For images: box scaled size for preview 2
075: */
076: public final static int PREVIEW2_BOX = 85;
077: /**
078: * For images: box scaled size for preview 3
079: */
080: public final static int PREVIEW3_BOX = 232;
081:
082: /**
083: * Enumeration of all available preview sizes.
084: */
085: public static enum PreviewSizes implements ObjectWithLabel {
086: PREVIEW1(1, PREVIEW1_BOX), PREVIEW2(2, PREVIEW2_BOX), PREVIEW3(
087: 3, PREVIEW3_BOX), ORIGINAL(0, -1);
088:
089: private final int blobIndex;
090: private final int size;
091:
092: PreviewSizes(int blobIndex, int size) {
093: this .blobIndex = blobIndex;
094: this .size = size;
095: }
096:
097: public int getSize() {
098: return size;
099: }
100:
101: public int getBlobIndex() {
102: return blobIndex;
103: }
104:
105: /** {@inheritDoc} */
106: public FxString getLabel() {
107: return FxSharedUtils.getEnumLabel(this , size, size);
108: }
109: }
110:
111: private String handle = null;
112: private boolean newBinary;
113: private List<ServerLocation> server;
114:
115: private long id = -1;
116: private int version;
117: private int quality;
118: private long creationTime;
119: private String name;
120: private long size;
121: private String metadata;
122: private String mimeType;
123: private boolean image;
124: private double resolution;
125: private int width;
126: private int height;
127:
128: /**
129: * Constructor (for new Binaries)
130: *
131: * @param handle binary_transit handle
132: */
133: public BinaryDescriptor(String handle) {
134: this .handle = handle;
135: this .newBinary = true;
136: }
137:
138: /**
139: * Constructor for new Binaries in prepareSave process ..
140: *
141: * @param handle handle
142: * @param name name of the binary
143: * @param size size in bytes
144: * @param mimeType MIME type
145: * @param metadata xml meta data
146: */
147: public BinaryDescriptor(String handle, String name, long size,
148: String mimeType, String metadata) {
149: this .handle = handle;
150: this .newBinary = true;
151: this .name = name;
152: this .size = size;
153: this .metadata = metadata;
154: this .mimeType = mimeType;
155: }
156:
157: /**
158: * Constructor for a new empty binary
159: */
160: public BinaryDescriptor() {
161: this .handle = EMPTY;
162: this .newBinary = true;
163: }
164:
165: /**
166: * Constructor (for new Binaries)
167: *
168: * @param name name of the binary
169: * @param streamLength expected size of the binary
170: * @param stream an open input stream for the binary to upload
171: * @throws FxStreamException on upload errors
172: */
173: public BinaryDescriptor(String name, long streamLength,
174: InputStream stream) throws FxStreamException {
175: this .name = name;
176: this .size = streamLength;
177: BinaryUploadPayload payload = FxStreamUtils.uploadBinary(
178: streamLength, stream);
179: this .handle = payload.getHandle();
180: this .newBinary = true;
181: }
182:
183: /**
184: * Constructor (for new Binaries with unknown length - use with care since it will have to create a temp file to determine length!)
185: *
186: * @param name name of the binary
187: * @param stream an open input stream for the binary to upload
188: * @throws FxStreamException on upload errors
189: */
190: public BinaryDescriptor(String name, InputStream stream)
191: throws FxStreamException {
192: this .name = name;
193:
194: File tmp = null;
195: FileOutputStream fos = null;
196: FileInputStream fin = null;
197: final int BUF_SIZE = 4096;
198: try {
199: tmp = File.createTempFile("FxBinary", ".bin");
200: fos = new FileOutputStream(tmp);
201: byte[] buffer = new byte[BUF_SIZE];
202: int read;
203: while ((read = stream.read(buffer)) != -1) {
204: fos.write(buffer, 0, read);
205: }
206: fos.flush();
207: fos.close();
208: fos = null;
209: fin = new FileInputStream(tmp);
210: this .size = tmp.length();
211: BinaryUploadPayload payload = FxStreamUtils.uploadBinary(
212: tmp.length(), fin);
213: this .handle = payload.getHandle();
214: } catch (IOException e) {
215: throw new FxStreamException(e, "ex.stream", e.getMessage());
216: } finally {
217: try {
218: if (fos != null)
219: fos.close();
220: } catch (IOException e) {
221: //ignore
222: }
223: try {
224: if (fin != null)
225: fin.close();
226: } catch (IOException e) {
227: //ignore
228: }
229: if (tmp != null) {
230: if (!tmp.delete())
231: tmp.deleteOnExit();
232: }
233: }
234: this .newBinary = true;
235: }
236:
237: /**
238: * Constructor - used for loading from the content engine
239: *
240: * @param server server location
241: * @param id binary id
242: * @param version binary version
243: * @param quality quality
244: * @param creationTime timestamp when the binary data was created in the storage
245: * @param name name
246: * @param size size
247: * @param metadata xml metadata
248: * @param mimeType mime type
249: * @param image binary is an image?
250: * @param resolution resoltion (if image and detected)
251: * @param width width (if image and detected)
252: * @param height height (if image and detected)
253: */
254: public BinaryDescriptor(List<ServerLocation> server, long id,
255: int version, int quality, long creationTime, String name,
256: long size, String metadata, String mimeType, boolean image,
257: double resolution, int width, int height) {
258: this .server = server;
259: this .id = id;
260: this .version = version;
261: this .quality = quality;
262: this .creationTime = creationTime;
263: this .name = name;
264: this .size = size;
265: this .metadata = metadata;
266: this .mimeType = mimeType;
267: this .image = image;
268: this .resolution = resolution;
269: this .width = width;
270: this .height = height;
271: }
272:
273: /**
274: * Downloads the binary to the given stream.
275: * The stream won't be flushed or closed!
276: *
277: * @param stream stream used for download
278: * @throws FxStreamException on errors
279: */
280: public void download(OutputStream stream) throws FxStreamException {
281: FxStreamUtils.downloadBinary(server, stream, this );
282: }
283:
284: public String getHandle() {
285: return handle;
286: }
287:
288: public boolean isNewBinary() {
289: return newBinary;
290: }
291:
292: public long getId() {
293: return id;
294: }
295:
296: public int getVersion() {
297: return version;
298: }
299:
300: public int getQuality() {
301: return quality;
302: }
303:
304: public long getCreationTime() {
305: return creationTime;
306: }
307:
308: public String getName() {
309: return name;
310: }
311:
312: public long getSize() {
313: return size;
314: }
315:
316: public String getMetadata() {
317: return metadata;
318: }
319:
320: public String getMimeType() {
321: return mimeType;
322: }
323:
324: public boolean isImage() {
325: return image;
326: }
327:
328: public double getResolution() {
329: return resolution;
330: }
331:
332: public int getWidth() {
333: return width;
334: }
335:
336: public int getHeight() {
337: return height;
338: }
339:
340: /**
341: * {@inheritDoc}
342: */
343: @Override
344: public boolean equals(Object obj) {
345: if (!(obj instanceof BinaryDescriptor))
346: return false;
347: BinaryDescriptor b = (BinaryDescriptor) obj;
348: return !(b.getMetadata() != null && !b.getMetadata().equals(
349: this .getMetadata()))
350: && !(this .getMetadata() != null && !this .getMetadata()
351: .equals(b.getMetadata()))
352: && !(this .getMetadata() == null && b.getMetadata() != null)
353: && !(b.getMetadata() == null && this .getMetadata() != null)
354: && !(b.getMimeType() != null && !b.getMimeType()
355: .equals(this .getMimeType()))
356: && !(this .getMimeType() != null && !this .getMimeType()
357: .equals(b.getMimeType()))
358: && !(this .getMimeType() == null && b.getMimeType() != null)
359: && !(b.getMimeType() == null && this .getMimeType() != null)
360: && !(this .handle != null && !this .handle
361: .equals(b.handle))
362: && !(this .id != -1 && this .id != b.id);
363: }
364:
365: /**
366: * {@inheritDoc}
367: */
368: @Override
369: public int hashCode() {
370: return (int) this .id
371: + (this .handle != null ? 31 * this .handle.hashCode()
372: : 0);
373: }
374:
375: /**
376: * {@inheritDoc}
377: */
378: @Override
379: public String toString() {
380: return this .getName() + " " + this.mimeType;
381: }
382: }
|