001: /**
002: *
003: */package org.enhydra.dm.business;
004:
005: import java.io.File;
006: import java.math.BigDecimal;
007: import java.sql.SQLException;
008: import java.sql.Timestamp;
009: import java.util.Properties;
010:
011: import org.enhydra.dm.api.Document;
012: import org.enhydra.dm.api.DocumentManager;
013: import org.enhydra.dm.api.DocumentVersion;
014: import org.enhydra.dm.api.exceptions.BaseException;
015: import org.enhydra.dm.api.loggers.Log;
016: import org.enhydra.dm.business.exceptions.ActionNotAllowedException;
017: import org.enhydra.dm.business.exceptions.DatabaseException;
018: import org.enhydra.dm.business.exceptions.DocumentStoreException;
019: import org.enhydra.dm.business.exceptions.LockException;
020: import org.enhydra.dm.data.DocumentDO;
021: import org.enhydra.dm.data.DocumentQuery;
022: import org.enhydra.dm.data.DocumentVersionDO;
023: import org.enhydra.dm.data.DocumentVersionQuery;
024: import org.enhydra.dm.data.FoDocumentDO;
025:
026: import com.lutris.appserver.server.sql.DBRowUpdateException;
027: import com.lutris.appserver.server.sql.DatabaseManagerException;
028: import com.lutris.dods.builder.generator.query.DataObjectException;
029: import com.lutris.dods.builder.generator.query.NonUniqueQueryException;
030: import com.lutris.dods.builder.generator.query.QueryBuilder;
031: import com.lutris.dods.builder.generator.query.QueryException;
032: import com.lutris.dods.builder.generator.query.RefAssertionException;
033:
034: /**
035: * Default implementation of DocumentManager interface.
036: *
037: * @author Slobodan Vujasinovic
038: */
039: public class DocumentManagerImpl implements DocumentManager {
040:
041: private Log logger = null;
042:
043: /**
044: * Default constructor
045: */
046:
047: public DocumentManagerImpl() {
048: }
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see org.enhydra.dm.api.DocumentManager#checkOut(java.lang.String, java.lang.String)
054: */
055:
056: public Document checkOut(String documentId, String userName)
057: throws BaseException {
058: try {
059: DocumentDO documentDO = getDocumentDOByNumber(documentId);
060:
061: if (documentDO.getLOCKEDBY() != null
062: || documentDO.getCHECKEDOUTBY() != null) {
063: throw new LockException(
064: "Document can't be checked out (id)! "
065: + documentId);
066: }
067:
068: documentDO.setLOCKEDBY(userName);
069: documentDO.setCHECKEDOUTBY(userName);
070: documentDO.save();
071: return new DocumentImpl(documentDO);
072: } catch (DataObjectException e) {
073: throw new DatabaseException(e);
074: } catch (RefAssertionException e) {
075: throw new DatabaseException(e);
076: } catch (DBRowUpdateException e) {
077: throw new DatabaseException(e);
078: } catch (QueryException e) {
079: throw new DatabaseException(e);
080: } catch (SQLException e) {
081: throw new DatabaseException(e);
082: } catch (DatabaseManagerException e) {
083: throw new DatabaseException(e);
084: }
085: }
086:
087: /*
088: * (non-Javadoc)
089: *
090: * @see org.enhydra.dm.api.DocumentManager#unCheckOut(java.lang.String,
091: * java.lang.String)
092: */
093: public void unCheckOut(String documentId, String userName)
094: throws BaseException {
095: try {
096: DocumentDO documentDO = getDocumentDOByNumber(documentId);
097:
098: String coUserName = documentDO.getCHECKEDOUTBY();
099: if (coUserName == null) {
100: throw new LockException(
101: "Document isn't checked out (id)! "
102: + documentId);
103: }
104:
105: if (!coUserName.equals(userName)) {
106: throw new LockException(
107: "User can't un check out document (id)! "
108: + documentId);
109: }
110:
111: documentDO.setLOCKEDBY(null);
112: documentDO.setCHECKEDOUTBY(null);
113: documentDO.save();
114:
115: if (null != logger) {
116: logger.log(Log.DEBUG,
117: "Document is Un Checked Out (id):" + documentId
118: + " by user(id):" + userName);
119: }
120: } catch (DataObjectException e) {
121: throw new DatabaseException(e);
122: } catch (DatabaseManagerException e) {
123: throw new DatabaseException(e);
124: } catch (RefAssertionException e) {
125: throw new DatabaseException(e);
126: } catch (DBRowUpdateException e) {
127: throw new DatabaseException(e);
128: } catch (QueryException e) {
129: throw new DatabaseException(e);
130: } catch (SQLException e) {
131: throw new DatabaseException(e);
132: }
133: }
134:
135: /*
136: * (non-Javadoc)
137: *
138: * @see org.enhydra.dm.api.DocumentManager#checkIn(java.lang.String, java.lang.String,
139: * java.lang.String)
140: */
141: public void checkIn(String documentId, String documentPath,
142: String userName) throws BaseException {
143: try {
144: DocumentDO documentDO = getDocumentDOByNumber(documentId);
145: String coUserName = documentDO.getCHECKEDOUTBY();
146: if (coUserName == null) {
147: throw new LockException(
148: "Document isn't checked out (id)! "
149: + documentId);
150: }
151:
152: if (!coUserName.equals(userName)) {
153: throw new LockException(
154: "User can't check in document (id)! "
155: + documentId);
156: }
157:
158: if (null == documentPath) {
159: throw new DocumentStoreException(
160: "Error while saving document content for document (id): "
161: + documentId);
162: }
163:
164: File documentFile = new File(documentPath);
165: if (documentFile == null || !documentFile.exists()) {
166: throw new DocumentStoreException(
167: "Error while saving document content for document (id): "
168: + documentId);
169: }
170:
171: if (documentDO.getAUTOVERSIONABLE()) {
172: DocumentVersionDO dvDO = createDocumentVersion(
173: documentDO, userName);
174: if (null == dvDO) {
175: throw new DatabaseException(
176: "Document Version creation error!");
177: }
178: dvDO.save();
179: }
180:
181: documentDO.setFILEPATH(documentPath);
182: documentDO.setSIZE(new BigDecimal(documentFile.length()));
183: documentDO.setLASTMODIFIEDDATE(new Timestamp(System
184: .currentTimeMillis()));
185: documentDO.setLASTMODIFIEDBY(userName);
186: documentDO.setLOCKEDBY(null);
187: documentDO.setCHECKEDOUTBY(null);
188: documentDO.save();
189: if (null != logger) {
190: logger.log(Log.DEBUG, "Document is Checked In (id):"
191: + documentId + " by user(id):" + userName);
192: }
193: } catch (DataObjectException e) {
194: throw new DatabaseException(e);
195: } catch (DatabaseManagerException e) {
196: throw new DatabaseException(e);
197: } catch (RefAssertionException e) {
198: throw new DatabaseException(e);
199: } catch (DBRowUpdateException e) {
200: throw new DatabaseException(e);
201: } catch (QueryException e) {
202: throw new DatabaseException(e);
203: } catch (SQLException e) {
204: throw new DatabaseException(e);
205: }
206: }
207:
208: /*
209: * (non-Javadoc)
210: *
211: * @see org.enhydra.dm.api.DocumentManager#unlock(java.lang.String, java.lang.String)
212: */
213: public void unlock(String documentId, String userName)
214: throws BaseException {
215: try {
216: DocumentDO documentDO = getDocumentDOByNumber(documentId);
217:
218: String lockUserName = documentDO.getLOCKEDBY();
219: if (lockUserName == null) {
220: throw new LockException("Document isn't locked (id)! "
221: + documentId);
222: }
223:
224: if (!lockUserName.equals(userName)) {
225: throw new LockException(
226: "User can't unlock document (id)! "
227: + documentId);
228: }
229:
230: documentDO.setLOCKEDBY(null);
231: documentDO.save();
232: if (null != logger) {
233: logger.log(Log.DEBUG, "Document is Un Locked (id):"
234: + documentId + " by user(id):" + userName);
235: }
236: } catch (DataObjectException e) {
237: throw new DatabaseException(e);
238: } catch (DatabaseManagerException e) {
239: throw new DatabaseException(e);
240: } catch (RefAssertionException e) {
241: throw new DatabaseException(e);
242: } catch (DBRowUpdateException e) {
243: throw new DatabaseException(e);
244: } catch (QueryException e) {
245: throw new DatabaseException(e);
246: } catch (SQLException e) {
247: throw new DatabaseException(e);
248: }
249: }
250:
251: /*
252: * (non-Javadoc)
253: *
254: * @see org.enhydra.dm.api.DocumentManager#lock(java.lang.String, java.lang.String)
255: */
256: public void lock(String documentId, String userName)
257: throws BaseException {
258: try {
259: DocumentDO documentDO = getDocumentDOByNumber(documentId);
260:
261: if (documentDO.getLOCKEDBY() != null
262: || documentDO.getCHECKEDOUTBY() != null) {
263: throw new LockException(
264: "Document allready locked (id)! " + documentId);
265: }
266:
267: documentDO.setLOCKEDBY(userName);
268: documentDO.save();
269: if (null != logger) {
270: logger.log(Log.DEBUG, "Document is Locked (id):"
271: + documentId + " by user(id):" + userName);
272: }
273: } catch (DataObjectException e) {
274: throw new DatabaseException(e);
275: } catch (DatabaseManagerException e) {
276: throw new DatabaseException(e);
277: } catch (RefAssertionException e) {
278: throw new DatabaseException(e);
279: } catch (DBRowUpdateException e) {
280: throw new DatabaseException(e);
281: } catch (QueryException e) {
282: throw new DatabaseException(e);
283: } catch (SQLException e) {
284: throw new DatabaseException(e);
285: }
286: }
287:
288: /*
289: * (non-Javadoc)
290: *
291: * @see org.enhydra.dm.api.DocumentManager#update(java.lang.String, java.lang.String,
292: * java.lang.String, java.lang.String)
293: */
294: public void update(String documentId, String path, String userName,
295: String foDocumentId) throws BaseException {
296: try {
297: DocumentDO documentDO = getDocumentDOByNumber(documentId);
298:
299: String lockUserName = documentDO.getLOCKEDBY();
300: if (lockUserName == null) {
301: throw new LockException("Document isn't locked (id)! "
302: + documentId);
303: }
304:
305: if (!lockUserName.equals(userName)) {
306: throw new LockException(
307: "User can't update document (id)! "
308: + documentId);
309: }
310:
311: File file = new File(path);
312: if (!file.exists()) {
313: throw new DocumentStoreException(
314: "File not found on Document Store path for document (id): "
315: + documentId);
316: }
317:
318: if (documentDO.getAUTOVERSIONABLE()) {
319: DocumentVersionDO dvDO = createDocumentVersion(
320: documentDO, userName);
321: if (null == dvDO) {
322: throw new DatabaseException(
323: "Document Version creation error!");
324: }
325: dvDO.save();
326: }
327:
328: FoDocumentDO foDocumentRefDO = null;
329: if (foDocumentId != null) {
330: foDocumentRefDO = getFoDocumentRefDO(foDocumentId);
331: }
332:
333: documentDO.setFILEPATH(path);
334: documentDO.setLASTMODIFIEDDATE(new Timestamp(System
335: .currentTimeMillis()));
336: documentDO.setSIZE(new BigDecimal(file.length()));
337: documentDO.setLASTMODIFIEDBY(userName);
338: documentDO.setLOCKEDBY(null);
339: documentDO.setFODOCUMENTREF(foDocumentRefDO);
340: documentDO.save();
341: if (null != logger) {
342: logger.log(Log.DEBUG, "Document is Updated (id):"
343: + documentId + " by user(id):" + userName);
344: }
345: } catch (NumberFormatException e) {
346: throw new DatabaseException(e);
347: } catch (DataObjectException e) {
348: throw new DatabaseException(e);
349: } catch (DatabaseManagerException e) {
350: throw new DatabaseException(e);
351: } catch (RefAssertionException e) {
352: throw new DatabaseException(e);
353: } catch (DBRowUpdateException e) {
354: throw new DatabaseException(e);
355: } catch (QueryException e) {
356: throw new DatabaseException(e);
357: } catch (SQLException e) {
358: throw new DatabaseException(e);
359: }
360: }
361:
362: /*
363: * (non-Javadoc)
364: *
365: * @see org.enhydra.dm.api.DocumentManager#getDocument(java.lang.String)
366: */
367: public Document getDocument(String documentId) throws BaseException {
368: DocumentDO documentDO = getDocumentDOByNumber(documentId);
369: if (null != logger) {
370: logger.log(Log.DEBUG, "Returning document (id):"
371: + documentId);
372: }
373: return new DocumentImpl(documentDO);
374: }
375:
376: /*
377: * (non-Javadoc)
378: *
379: * @see org.enhydra.dm.api.DocumentManager#getDocumentVersion(java.lang.String)
380: */
381: public DocumentVersion getDocumentVersion(String documentVersionId)
382: throws BaseException {
383: DocumentVersionDO documentVersionDO = getDocumentVersionDOByNumber(documentVersionId);
384: if (null != logger) {
385: logger.log(Log.DEBUG, "Returning document version (id):"
386: + documentVersionId);
387: }
388: return new DocumentVersionImpl(documentVersionDO);
389: }
390:
391: /*
392: * (non-Javadoc)
393: *
394: * @see org.enhydra.dm.api.DocumentManager#delete(java.lang.String, java.lang.String)
395: */
396: public void delete(String documentId, String userName)
397: throws BaseException {
398: try {
399: DocumentDO documentDO = getDocumentDOByNumber(documentId);
400: if (documentDO.getLOCKEDBY() != null
401: || documentDO.getCHECKEDOUTBY() != null) {
402: throw new LockException("Document is locked (id)! "
403: + documentId);
404: }
405: if (documentDO.getDELETEDBY() != null) {
406: throw new ActionNotAllowedException(
407: "Document allready deleted (id)! " + documentId);
408: }
409:
410: documentDO.setDELETEDBY(userName);
411: documentDO.setDELETEDDATE(new Timestamp(System
412: .currentTimeMillis()));
413: documentDO.save();
414: if (null != logger) {
415: logger.log(Log.DEBUG, "Document is deleted (id):"
416: + documentId + " by user " + userName);
417: }
418: } catch (DataObjectException e) {
419: throw new DatabaseException(e);
420: } catch (DatabaseManagerException e) {
421: throw new DatabaseException(e);
422: } catch (RefAssertionException e) {
423: throw new DatabaseException(e);
424: } catch (DBRowUpdateException e) {
425: throw new DatabaseException(e);
426: } catch (QueryException e) {
427: throw new DatabaseException(e);
428: } catch (SQLException e) {
429: throw new DatabaseException(e);
430: }
431: }
432:
433: /*
434: * (non-Javadoc)
435: *
436: * @see org.enhydra.dm.api.DocumentManager#create(java.lang.String, java.lang.String,
437: * java.lang.String, java.lang.String, java.lang.String, boolean,
438: * java.lang.String)
439: */
440: public String create(String documentName, String documentPath,
441: String mimeType, String userName, String foDocumentId,
442: boolean isTemplate, String templateId) throws BaseException {
443:
444: FoDocumentDO foDocumentRefDO = null;
445: if (foDocumentId != null) {
446: foDocumentRefDO = getFoDocumentRefDO(foDocumentId);
447: }
448:
449: DocumentDO templateRefDO = null;
450: if (templateId != null) {
451: templateRefDO = getDocumentDOByNumber(templateId);
452: }
453:
454: Document document = new DocumentImpl();
455: document.setCurrentVersionNumber("1");
456: document.setLastModifiedDate(System.currentTimeMillis());
457: document.setCreatedDate(System.currentTimeMillis());
458: document.setMimeType(mimeType);
459: document.setName(documentName);
460: if (null == documentPath) {
461: throw new DocumentStoreException(
462: "Error while saving document content!");
463: }
464: File documentFile = new File(documentPath);
465: if (documentFile == null || !documentFile.exists()) {
466: throw new DocumentStoreException(
467: "Error while saving document content!");
468: }
469:
470: document.setFilepath(documentPath);
471: document.setSize(documentFile.length());
472: document.setAutoversionable(true);
473: document.setTemplate(isTemplate);
474: try {
475: DocumentDO documentDO = document.createDO();
476: documentDO.setCREATEDBY(userName);
477: documentDO.setLASTMODIFIEDBY(userName);
478: documentDO.setFODOCUMENTREF(foDocumentRefDO);
479: documentDO.setTEMPLATEREF(templateRefDO);
480: documentDO.save();
481: if (null != logger) {
482: logger.log(Log.DEBUG, "Document :" + documentName
483: + "is created by " + userName);
484: }
485:
486: return documentDO.get_Handle();
487: } catch (DatabaseManagerException e) {
488: throw new DatabaseException(e);
489: } catch (DataObjectException e) {
490: throw new DatabaseException(e);
491: } catch (RefAssertionException e) {
492: throw new DatabaseException(e);
493: } catch (DBRowUpdateException e) {
494: throw new DatabaseException(e);
495: } catch (QueryException e) {
496: throw new DatabaseException(e);
497: } catch (SQLException e) {
498: throw new DatabaseException(e);
499: }
500: }
501:
502: /*
503: * (non-Javadoc)
504: *
505: * @see org.enhydra.dm.api.DocumentManager#getDocumentVersions(java.lang.String)
506: */
507: public DocumentVersion[] getDocumentVersions(String id)
508: throws BaseException {
509: DocumentVersionImpl[] documentVersArray = null;
510: try {
511:
512: DocumentDO documentDO = getDocumentDOByNumber(id);
513: if (documentDO == null) {
514: throw new DatabaseException(
515: "Document not found (id) - " + id);
516: }
517:
518: DocumentVersionQuery query = new DocumentVersionQuery();
519: query.setQueryDOCUMENTOID(documentDO);
520:
521: DocumentVersionDO[] documentVersDOArray = query
522: .getDOArray();
523: if (null != documentVersDOArray) {
524: documentVersArray = new DocumentVersionImpl[documentVersDOArray.length];
525: for (int i = 0; i < documentVersDOArray.length; i++) {
526: documentVersArray[i] = new DocumentVersionImpl(
527: documentVersDOArray[i]);
528: }
529: } else {
530: throw new DatabaseException(
531: "Error while loading document versions array for document (id) - "
532: + id);
533: }
534: } catch (DataObjectException e) {
535: throw new DatabaseException(e);
536: } catch (QueryException e) {
537: throw new DatabaseException(e);
538: } catch (NonUniqueQueryException e) {
539: throw new DatabaseException(e);
540: }
541: if (null != logger) {
542: logger
543: .log(Log.DEBUG,
544: "Getting Document versions for Document (id):"
545: + id);
546: }
547: return documentVersArray;
548: }
549:
550: /*
551: * (non-Javadoc)
552: *
553: * @see org.enhydra.dm.api.DocumentManager#getDocuments(boolean)
554: */
555: public Document[] getDocuments(boolean isTemplate)
556: throws BaseException {
557: DocumentImpl[] documentArray = null;
558: try {
559: DocumentQuery query = new DocumentQuery();
560: query.setQueryDELETEDBY(null);
561: query.setQueryISTEMPLATE(isTemplate);
562: DocumentDO[] documentDOArray = query.getDOArray();
563: if (null != documentDOArray) {
564: documentArray = new DocumentImpl[documentDOArray.length];
565: for (int i = 0; i < documentDOArray.length; i++) {
566: documentArray[i] = new DocumentImpl(
567: documentDOArray[i]);
568: }
569: } else {
570: throw new DatabaseException(
571: "Error while loading documents!");
572: }
573: } catch (DataObjectException e) {
574: throw new DatabaseException(e);
575: } catch (NonUniqueQueryException e) {
576: throw new DatabaseException(e);
577: } catch (QueryException e) {
578: throw new DatabaseException(e);
579: }
580: if (null != logger) {
581: logger.log(Log.DEBUG, "Getting Documents!");
582: }
583: return documentArray;
584: }
585:
586: /**
587: * Method that creates document version and increment version number
588: *
589: * @param documentDO
590: * @param userName
591: * @return
592: * @throws BaseException
593: */
594: private DocumentVersionDO createDocumentVersion(
595: DocumentDO documentDO, String userName)
596: throws BaseException {
597: try {
598: Document doc = new DocumentImpl(documentDO);
599: DocumentVersion dv = (DocumentVersionImpl) doc
600: .createVersion();
601: DocumentVersionDO dvDO = dv.createVersionDO();
602: dvDO.setDOCUMENTOID(documentDO);
603: dvDO.setLASTMODIFIEDBY(userName);
604: BigDecimal bd = documentDO.getCURRENTVERSIONNUMBER();
605: int currentVersion = bd.intValue();
606: currentVersion++;
607: documentDO.setCURRENTVERSIONNUMBER(new BigDecimal(
608: currentVersion));
609: if (null != logger) {
610: logger.log(Log.DEBUG,
611: "Document version Created for document "
612: + documentDO.get_OId() + " by user "
613: + userName);
614: }
615: return dvDO;
616: } catch (Exception e) {
617: throw new DatabaseException(e);
618: }
619: }
620:
621: /*
622: * (non-Javadoc)
623: *
624: * @see org.enhydra.dm.api.DocumentManager#restoreDocumentVersion(java.lang.String,
625: * java.lang.String)
626: */
627: public void restoreDocumentVersion(String versionId, String userName)
628: throws BaseException {
629: DocumentVersion documentVersion = getDocumentVersion(versionId);
630: try {
631: String id = documentVersion.getDocument().getId();
632: DocumentDO documentDO = getDocumentDO(id);
633: if (null == documentDO) {
634: throw new DatabaseException("Document not found (ID) "
635: + id);
636: }
637: if (documentDO.getLOCKEDBY() != null
638: || documentDO.getCHECKEDOUTBY() != null) {
639: throw new LockException("Document is locked (ID)! "
640: + id);
641: }
642:
643: DocumentVersionDO dvDO = createDocumentVersion(documentDO,
644: userName);
645: documentDO.setFILEPATH(documentVersion.getFilepath());
646: documentDO.setLASTMODIFIEDDATE(new Timestamp(
647: documentVersion.getLastModifiedDate()));
648: documentDO
649: .setSIZE(new BigDecimal(documentVersion.getSize()));
650: if (documentVersion.getFoDocumentRef() != null)
651: documentDO.oid_setFODOCUMENTREF(documentVersion
652: .getFoDocumentRef().getId());
653: documentDO.setLASTMODIFIEDBY(userName);
654: documentDO.save();
655: dvDO.save();
656:
657: if (null != logger) {
658: logger.log(Log.DEBUG,
659: "Restoring Document Version (DOC_VERS_ID)",
660: documentVersion.getNumber());
661: }
662: } catch (Exception e) {
663: throw new DatabaseException(e);
664: }
665: }
666:
667: /*
668: * (non-Javadoc)
669: *
670: * @see org.enhydra.dm.api.DocumentManager#getLogger()
671: */
672: public Log getLogger() {
673: return logger;
674: }
675:
676: /*
677: * (non-Javadoc)
678: *
679: * @see org.enhydra.dm.api.DocumentManager#setLogger(org.enhydra.dm.api.loggers.Log)
680: */
681: public void setLogger(Log logger) {
682: this .logger = logger;
683: }
684:
685: /*
686: * (non-Javadoc)
687: *
688: * @see org.enhydra.dm.api.DocumentManager#configure(java.util.Properties)
689: */
690: public void configure(Properties properties) throws BaseException {
691: // do some property initialization
692: }
693:
694: /*
695: * (non-Javadoc)
696: *
697: * @see org.enhydra.dm.api.DocumentManager#restore(java.lang.String, java.lang.String)
698: */
699: public void restore(String documentId, String userName)
700: throws BaseException {
701: try {
702: DocumentDO documentDO = getDocumentDOByNumber(documentId);
703: if (documentDO.getDELETEDBY() == null) {
704: throw new ActionNotAllowedException(
705: "Document Allready Restored (ID)! "
706: + documentId);
707: }
708:
709: documentDO.setDELETEDBY(null);
710: documentDO.setDELETEDDATE(null);
711: documentDO.save();
712: if (null != logger) {
713: logger.log(Log.DEBUG,
714: "Document is successfully restored(ID):"
715: + documentId + " by user " + userName);
716: }
717: } catch (DataObjectException e) {
718: throw new DatabaseException(e);
719: } catch (DatabaseManagerException e) {
720: throw new DatabaseException(e);
721: } catch (RefAssertionException e) {
722: throw new DatabaseException(e);
723: } catch (DBRowUpdateException e) {
724: throw new DatabaseException(e);
725: } catch (QueryException e) {
726: throw new DatabaseException(e);
727: } catch (SQLException e) {
728: throw new DatabaseException(e);
729: }
730: }
731:
732: /*
733: * (non-Javadoc)
734: *
735: * @see org.enhydra.dm.api.DocumentManager#getDeleted(boolean)
736: */
737: public Document[] getDeleted(boolean isTemplate)
738: throws BaseException {
739: DocumentImpl[] documentArray = null;
740: try {
741: DocumentQuery query = new DocumentQuery();
742: query.setQueryDELETEDBY(null, QueryBuilder.NOT_EQUAL);
743: query.setQueryISTEMPLATE(isTemplate);
744: DocumentDO[] documentDOArray = query.getDOArray();
745: if (null != documentDOArray) {
746: documentArray = new DocumentImpl[documentDOArray.length];
747: for (int i = 0; i < documentDOArray.length; i++) {
748: documentArray[i] = new DocumentImpl(
749: documentDOArray[i]);
750: }
751: } else {
752: throw new DatabaseException(
753: "Error while loading archive documents!");
754: }
755: } catch (DataObjectException e) {
756: throw new DatabaseException(e);
757: } catch (NonUniqueQueryException e) {
758: throw new DatabaseException(e);
759: } catch (QueryException e) {
760: throw new DatabaseException(e);
761: }
762: if (null != logger) {
763: logger.log(Log.DEBUG,
764: "Getting deleted documents from database!");
765: }
766: return documentArray;
767: }
768:
769: /*
770: * (non-Javadoc)
771: *
772: * @see org.enhydra.dm.api.DocumentManager#switchAutoVersionable(java.lang.String,
773: * java.lang.String)
774: */
775: public void switchAutoVersionable(String documentId, String userName)
776: throws BaseException {
777: try {
778: DocumentDO documentDO = getDocumentDOByNumber(documentId);
779: if (documentDO.getLOCKEDBY() != null
780: || documentDO.getCHECKEDOUTBY() != null) {
781: throw new LockException("Document is locked (id)! "
782: + documentId);
783: }
784:
785: if (documentDO.getAUTOVERSIONABLE()) {
786: documentDO.setAUTOVERSIONABLE(false);
787: } else {
788: documentDO.setAUTOVERSIONABLE(true);
789: }
790:
791: documentDO.save();
792: if (null != logger) {
793: logger.log(Log.DEBUG,
794: "Versionable switched for Document (id):"
795: + documentId + " by user " + userName);
796: }
797: } catch (DataObjectException e) {
798: throw new DatabaseException(e);
799: } catch (DatabaseManagerException e) {
800: throw new DatabaseException(e);
801: } catch (RefAssertionException e) {
802: throw new DatabaseException(e);
803: } catch (DBRowUpdateException e) {
804: throw new DatabaseException(e);
805: } catch (QueryException e) {
806: throw new DatabaseException(e);
807: } catch (SQLException e) {
808: throw new DatabaseException(e);
809: }
810:
811: }
812:
813: /*
814: * (non-Javadoc)
815: *
816: * @see org.enhydra.dm.api.DocumentManager#switchArchived(java.lang.String,
817: * java.lang.String)
818: */
819: public void switchArchived(String documentId, String userName)
820: throws BaseException {
821: try {
822: DocumentDO documentDO = getDocumentDOByNumber(documentId);
823: if (documentDO.getLOCKEDBY() != null
824: || documentDO.getCHECKEDOUTBY() != null) {
825: throw new LockException("Document is locked (id)! "
826: + documentId);
827: }
828:
829: if (documentDO.getARCHIVED()) {
830: documentDO.setARCHIVED(false);
831: } else {
832: documentDO.setARCHIVED(true);
833: }
834:
835: documentDO.save();
836:
837: if (null != logger) {
838: logger.log(Log.DEBUG,
839: "Archived switched for Document (id):"
840: + documentId + " by user " + userName);
841: }
842: } catch (DataObjectException e) {
843: throw new DatabaseException(e);
844: } catch (DatabaseManagerException e) {
845: throw new DatabaseException(e);
846: } catch (RefAssertionException e) {
847: throw new DatabaseException(e);
848: } catch (DBRowUpdateException e) {
849: throw new DatabaseException(e);
850: } catch (QueryException e) {
851: throw new DatabaseException(e);
852: } catch (SQLException e) {
853: throw new DatabaseException(e);
854: }
855:
856: }
857:
858: /**
859: * Return DocumentDO object by document id
860: *
861: * @param documentId
862: * @return
863: * @throws BaseException
864: */
865: private DocumentDO getDocumentDO(String documentId)
866: throws BaseException {
867: DocumentDO documentDO = DocumentDO.createExisting(documentId);
868: if (documentDO == null) {
869: throw new DatabaseException("Document not found (ID) - "
870: + documentId);
871: }
872: return documentDO;
873: }
874:
875: /**
876: * Return DocumentDO object by document number
877: *
878: * @param documentNumber
879: * @return
880: * @throws BaseException
881: */
882: private DocumentDO getDocumentDOByNumber(String documentNumber)
883: throws BaseException {
884:
885: DocumentDO documentDO = null;
886: DocumentQuery dq = new DocumentQuery();
887: try {
888: dq.requireUniqueInstance();
889: dq.setQueryLOGNUM(Integer.parseInt(documentNumber));
890: documentDO = dq.getNextDO();
891:
892: } catch (DataObjectException e) {
893: throw new DatabaseException(e);
894: } catch (QueryException e) {
895: throw new DatabaseException(e);
896: } catch (NonUniqueQueryException e) {
897: throw new DatabaseException(e);
898: }
899: if (documentDO == null) {
900: throw new DatabaseException(
901: "Document not found (NUMBER) - " + documentNumber);
902: }
903:
904: return documentDO;
905: }
906:
907: /**
908: * Search for DocumentVersionDO by documentVersionNumber
909: *
910: * @param documentVersionNumber
911: * @return
912: * @throws BaseException
913: */
914: private DocumentVersionDO getDocumentVersionDOByNumber(
915: String documentVersionNumber) throws BaseException {
916:
917: DocumentVersionDO documentVersionDO = null;
918: DocumentVersionQuery dvq = new DocumentVersionQuery();
919: try {
920: dvq.requireUniqueInstance();
921: dvq.setQueryLOGNUM(Integer.parseInt(documentVersionNumber));
922: documentVersionDO = dvq.getNextDO();
923:
924: } catch (DataObjectException e) {
925: throw new DatabaseException(e);
926: } catch (QueryException e) {
927: throw new DatabaseException(e);
928: } catch (NonUniqueQueryException e) {
929: throw new DatabaseException(e);
930: }
931: if (documentVersionDO == null) {
932: throw new DatabaseException(
933: "DocumentVersion not found (NUMBER) - "
934: + documentVersionNumber);
935: }
936:
937: return documentVersionDO;
938: }
939:
940: /**
941: * Search for FoDocumentDO by foDocumentId
942: *
943: * @param foDocumentId
944: * @return
945: * @throws BaseException
946: */
947: private FoDocumentDO getFoDocumentRefDO(String foDocumentId)
948: throws BaseException {
949: FoDocumentDO foDocumentDO = FoDocumentDO
950: .createExisting(foDocumentId);
951: if (foDocumentDO == null) {
952: throw new DatabaseException("FoDocument not found (id) - "
953: + foDocumentId);
954: }
955: return foDocumentDO;
956: }
957:
958: /*
959: * (non-Javadoc)
960: *
961: * @see org.enhydra.dm.api.DocumentManager#createVersion(java.lang.String,
962: * java.lang.String)
963: */
964: public void createVersion(String documentId, String userName)
965: throws BaseException {
966: DocumentDO documentDO = getDocumentDOByNumber(documentId);
967: if (documentDO == null) {
968: throw new DatabaseException("Document not found (id) - "
969: + documentId);
970: }
971: DocumentVersionDO dvDO = createDocumentVersion(documentDO,
972: userName);
973: if (dvDO == null) {
974: throw new DatabaseException(
975: "Can't create document version for document (id) - "
976: + documentId);
977: }
978: try {
979: dvDO.save();
980: } catch (DatabaseManagerException e) {
981: throw new DatabaseException(e);
982: } catch (DataObjectException e) {
983: throw new DatabaseException(e);
984: } catch (RefAssertionException e) {
985: throw new DatabaseException(e);
986: } catch (DBRowUpdateException e) {
987: throw new DatabaseException(e);
988: } catch (QueryException e) {
989: throw new DatabaseException(e);
990: } catch (SQLException e) {
991: throw new DatabaseException(e);
992: }
993: }
994: }
|