001: /**
002: *
003: */package org.enhydra.dm.business;
004:
005: import java.io.IOException;
006: import java.math.BigDecimal;
007: import java.security.MessageDigest;
008: import java.security.NoSuchAlgorithmException;
009: import java.sql.Timestamp;
010: import java.text.DateFormat;
011: import java.text.SimpleDateFormat;
012: import java.util.Date;
013: import java.util.Locale;
014:
015: import org.enhydra.dm.api.Document;
016: import org.enhydra.dm.api.DocumentVersion;
017: import org.enhydra.dm.api.FoDocument;
018: import org.enhydra.dm.api.exceptions.BaseException;
019: import org.enhydra.dm.business.exceptions.DatabaseException;
020: import org.enhydra.dm.data.DocumentDO;
021: import org.enhydra.dm.data.FoDocumentDO;
022: import org.enhydra.dm.util.Numerator;
023:
024: /**
025: * Implementation of Document interface.
026: *
027: * @author Slobodan Vujasinovic
028: */
029: public class DocumentImpl implements Document {
030:
031: private static final DateFormat LAST_MODIFIED_FORMAT = new SimpleDateFormat(
032: "E, dd MMM yyyy HH:mm:ss z", Locale.US);
033:
034: private static final String DOCUMENT_NUMBER_COLUMN_NAME = "DOCUMENT";
035:
036: private static MessageDigest digest;
037:
038: String id;
039:
040: String number;
041:
042: String name;
043:
044: String filepath;
045:
046: long size;
047:
048: String mimeType;
049:
050: String createdBy = null;
051:
052: String lockedBy = null;
053:
054: boolean archived;
055:
056: boolean autoversionable;
057:
058: String currentVersionNumber;
059:
060: String checkedOutBy = null;
061:
062: long createdDate;
063:
064: String lastModifiedBy = null;
065:
066: long lastModifiedDate;
067:
068: String deletedBy = null;
069:
070: long deletedDate;
071:
072: boolean template;
073:
074: Document templateRef = null;
075:
076: FoDocument foDocumentRef = null;
077:
078: DocumentImpl() {
079:
080: }
081:
082: public DocumentImpl(DocumentDO documentDO) throws BaseException {
083: try {
084: setId(documentDO.get_Handle());
085: setNumber(String.valueOf(documentDO.getLOGNUM()));
086: setName(documentDO.getNAME());
087: setFilepath(documentDO.getFILEPATH());
088: setSize(documentDO.getSIZE().intValue());
089: setMimeType(documentDO.getMIMETYPE());
090: setCreatedDate(documentDO.getCREATEDDATE().getTime());
091: setArchived(documentDO.getARCHIVED());
092: setAutoversionable(documentDO.getAUTOVERSIONABLE());
093: setCurrentVersionNumber(documentDO
094: .getCURRENTVERSIONNUMBER().toString());
095: setLastModifiedDate(documentDO.getLASTMODIFIEDDATE()
096: .getTime());
097: setTemplate(documentDO.getISTEMPLATE());
098:
099: setCreatedBy(documentDO.getCREATEDBY());
100: setCheckedOutBy(documentDO.getCHECKEDOUTBY());
101: setLockedBy(documentDO.getLOCKEDBY());
102: setLastModifiedBy(documentDO.getLASTMODIFIEDBY());
103: setDeletedBy(documentDO.getDELETEDBY());
104:
105: /*
106: * Check for possible null values
107: */
108:
109: DocumentDO templateDocDO = documentDO.getTEMPLATEREF();
110: if (templateDocDO != null) {
111: setTemplateRef(new DocumentImpl(templateDocDO));
112: }
113:
114: FoDocumentDO foDocDO = documentDO.getFODOCUMENTREF();
115: if (foDocDO != null) {
116: setFoDocumentRef(new FoDocumentImpl(foDocDO));
117: }
118:
119: Timestamp delDate = documentDO.getDELETEDDATE();
120: if (delDate != null) {
121: setDeletedDate(delDate.getTime());
122: }
123: } catch (Exception e) {
124: throw new DatabaseException(e);
125: }
126: }
127:
128: /**
129: * Returns the entity tag for the specified resource. The returned string uniquely
130: * identifies the current incarnation of the given resource.
131: *
132: * @param file The resource whose entity tag is to be retrieved.
133: * @return A <code>String</code> containing the entity tag for the resource.
134: */
135: public String getETag() {
136:
137: try {
138: digest = MessageDigest.getInstance("MD5");
139:
140: String key = this .getName() + ":"
141: + Long.toHexString(this .getLastModifiedDate());
142: byte[] hashBytes = null;
143: synchronized (digest) {
144: hashBytes = digest.digest(key.getBytes("UTF-8"));
145: }
146: StringBuffer hash = new StringBuffer();
147: int count = hashBytes.length;
148: for (int i = 0; i < count; i++) {
149: hash.append(Integer
150: .toHexString((hashBytes[i] >> 4) & 0x0f));
151: hash.append(Integer.toHexString(hashBytes[i] & 0x0f));
152: }
153: return "\"" + hash.toString() + "\"";
154: } catch (IOException ex) {
155: return null;
156: } catch (NoSuchAlgorithmException e) {
157: return null;
158: }
159: }
160:
161: /**
162: * Formats a timestamp (representing milliseconds since the epoch) as used in the
163: * WebDAV <code>getlastmodified</code> property.
164: *
165: * @return A <code>String</code> containing the formatted result.
166: */
167: public String getLastModifiedFormated() {
168: synchronized (LAST_MODIFIED_FORMAT) {
169: return LAST_MODIFIED_FORMAT.format(new Date(this
170: .getLastModifiedDate()));
171: }
172: }
173:
174: public DocumentDO createDO() throws BaseException {
175: try {
176: DocumentDO documentDO = DocumentDO.createVirgin();
177: documentDO.setNAME(this .getName());
178: documentDO.setFILEPATH(this .getFilepath());
179: documentDO.setSIZE(new BigDecimal(this .getSize()));
180: documentDO.setMIMETYPE(this .getMimeType());
181: documentDO.setCREATEDDATE(new Timestamp(this
182: .getCreatedDate()));
183: documentDO.setDELETEDDATE(new Timestamp(this
184: .getDeletedDate()));
185: documentDO.setARCHIVED(this .isArchived());
186: documentDO.setAUTOVERSIONABLE(this .isAutoversionable());
187: documentDO.setCURRENTVERSIONNUMBER(new BigDecimal(this
188: .getCurrentVersionNumber()));
189: documentDO.setLASTMODIFIEDDATE(new Timestamp(this
190: .getLastModifiedDate()));
191: documentDO.setISTEMPLATE(this .isTemplate());
192: documentDO.setLOGNUM(Numerator.getNext(
193: DOCUMENT_NUMBER_COLUMN_NAME).intValue());
194:
195: return documentDO;
196: } catch (Exception e) {
197: throw new DatabaseException(e);
198: }
199: }
200:
201: public DocumentVersion createVersion() {
202: DocumentVersion dv = (DocumentVersion) new DocumentVersionImpl();
203: dv.setFilepath(getFilepath());
204: dv.setLastModifiedDate(getLastModifiedDate());
205: dv.setSize(getSize());
206: dv.setFoDocumentRef(getFoDocumentRef());
207: dv.setVersionDate(System.currentTimeMillis());
208: dv.setVersionNumber(getCurrentVersionNumber());
209:
210: return dv;
211: }
212:
213: public boolean isArchived() {
214: return archived;
215: }
216:
217: public void setArchived(boolean archived) {
218: this .archived = archived;
219: }
220:
221: public boolean isAutoversionable() {
222: return autoversionable;
223: }
224:
225: public void setAutoversionable(boolean autoversionable) {
226: this .autoversionable = autoversionable;
227: }
228:
229: public String getCheckedOutBy() {
230: return checkedOutBy;
231: }
232:
233: public void setCheckedOutBy(String checkedOutBy) {
234: this .checkedOutBy = checkedOutBy;
235: }
236:
237: public String getCreatedBy() {
238: return createdBy;
239: }
240:
241: public void setCreatedBy(String createdBy) {
242: this .createdBy = createdBy;
243: }
244:
245: public long getCreatedDate() {
246: return createdDate;
247: }
248:
249: public void setCreatedDate(long createdDate) {
250: this .createdDate = createdDate;
251: }
252:
253: public String getCurrentVersionNumber() {
254: return currentVersionNumber;
255: }
256:
257: public void setCurrentVersionNumber(String currentVersionNumber) {
258: this .currentVersionNumber = currentVersionNumber;
259: }
260:
261: public String getFilepath() {
262: return filepath;
263: }
264:
265: public void setFilepath(String filepath) {
266: this .filepath = filepath;
267: }
268:
269: public FoDocument getFoDocumentRef() {
270: return foDocumentRef;
271: }
272:
273: public void setFoDocumentRef(FoDocument foDocumentRef) {
274: this .foDocumentRef = foDocumentRef;
275: }
276:
277: public String getLastModifiedBy() {
278: return lastModifiedBy;
279: }
280:
281: public void setLastModifiedBy(String lastModifiedBy) {
282: this .lastModifiedBy = lastModifiedBy;
283: }
284:
285: public long getLastModifiedDate() {
286: return lastModifiedDate;
287: }
288:
289: public void setLastModifiedDate(long lastModifiedDate) {
290: this .lastModifiedDate = lastModifiedDate;
291: }
292:
293: public String getLockedBy() {
294: return lockedBy;
295: }
296:
297: public void setLockedBy(String lockedBy) {
298: this .lockedBy = lockedBy;
299: }
300:
301: public String getMimeType() {
302: return mimeType;
303: }
304:
305: public void setMimeType(String mimeType) {
306: this .mimeType = mimeType;
307: }
308:
309: public String getName() {
310: return name;
311: }
312:
313: public void setName(String name) {
314: this .name = name;
315: }
316:
317: public String getNumber() {
318: return number;
319: }
320:
321: public String getId() {
322: return id;
323: }
324:
325: public void setNumber(String number) {
326: this .number = number;
327: }
328:
329: public void setId(String id) {
330: this .id = id;
331: }
332:
333: public long getSize() {
334: return size;
335: }
336:
337: public void setSize(long size) {
338: this .size = size;
339: }
340:
341: public boolean isTemplate() {
342: return template;
343: }
344:
345: public void setTemplate(boolean template) {
346: this .template = template;
347: }
348:
349: public Document getTemplateRef() {
350: return templateRef;
351: }
352:
353: public void setTemplateRef(Document templateRef) {
354: this .templateRef = templateRef;
355: }
356:
357: public String getDeletedBy() {
358: return deletedBy;
359: }
360:
361: public void setDeletedBy(String deletedBy) {
362: this .deletedBy = deletedBy;
363: }
364:
365: public long getDeletedDate() {
366: return deletedDate;
367: }
368:
369: public void setDeletedDate(long deletedDate) {
370: this.deletedDate = deletedDate;
371: }
372:
373: }
|