001: package org.enhydra.dm.presentation;
002:
003: import java.text.SimpleDateFormat;
004: import java.util.Date;
005:
006: import org.enhydra.dm.EnhydraDM;
007: import org.enhydra.dm.api.Document;
008: import org.enhydra.dm.api.exceptions.BaseException;
009: import org.enhydra.dm.business.exceptions.ActionNotAllowedException;
010: import org.enhydra.dm.util.EnhydraDMConstants;
011: import org.w3c.dom.Node;
012: import org.w3c.dom.html.HTMLAnchorElement;
013:
014: import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
015: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
016: import com.lutris.logging.Logger;
017:
018: public class RecycleBin extends Base {
019:
020: RecycleBinHTML page;
021:
022: /*
023: * There is the only function needed in order to be a presentation object.
024: */
025: public void run(HttpPresentationComms comms) throws Exception {
026:
027: page = (RecycleBinHTML) comms.xmlcFactory
028: .create(RecycleBinHTML.class);
029:
030: comms.request.getHttpServletRequest().setCharacterEncoding(
031: EnhydraDMConstants.ENCODING);
032:
033: String documentid = comms.request
034: .getParameter(EnhydraDMConstants.DOCUMENT_ID);
035: String action = comms.request
036: .getParameter(EnhydraDMConstants.ACTION);
037: String mode = comms.request
038: .getParameter(EnhydraDMConstants.MODE);
039:
040: boolean isTemplate = true;
041: if (!EnhydraDMConstants.MODE_TEMPLATE.equals(mode)) {
042: mode = EnhydraDMConstants.MODE_DOCUMENT;
043: isTemplate = false;
044: }
045:
046: boolean doCommit = true;
047: try {
048: initTransaction();
049: String user = getUser(comms);
050: documentManager = ((EnhydraDM) comms.application)
051: .getDocumentManager();
052:
053: if (null == user) {
054: throw new ActionNotAllowedException(
055: "User not registred;");
056:
057: }
058: if (null != action) {
059: if (EnhydraDMConstants.ACTION_RESTORE.equals(action)) {
060: restoreDocument(comms, documentid, user);
061: }
062: throw new ClientPageRedirectException(
063: EnhydraDMConstants.NAVIG_RECYCLEBIN + "?"
064: + EnhydraDMConstants.MODE + "=" + mode);
065: }
066:
067: page.setTextUser("User: " + user);
068:
069: fillFilesetTable(comms, user, isTemplate);
070:
071: comms.response.writeDOM(page);
072: } catch (BaseException ex) {
073: doCommit = false;
074: if (null != comms.application.getLogChannel()) {
075: comms.application.getLogChannel().write(
076: Logger.ERROR,
077: "Action :" + action + " failure! Reason: "
078: + ex.getMessage());
079: }
080:
081: throw new Exception(ex);
082: } finally {
083: if (doCommit) {
084: commitTransaction();
085: } else {
086: rollbackTransaction();
087: }
088: }
089: }
090:
091: private void fillFilesetTable(HttpPresentationComms comms,
092: String user, boolean isTemplate) throws BaseException {
093:
094: String mode = EnhydraDMConstants.MODE_DOCUMENT;
095: if (isTemplate) {
096: mode = EnhydraDMConstants.MODE_TEMPLATE;
097: }
098:
099: Node fileRow = page.getElementDocumentRow();
100: Document[] documents = null;
101: documents = documentManager.getDeleted(isTemplate);
102: SimpleDateFormat sdf = new SimpleDateFormat(
103: EnhydraDMConstants.DATE_FORMAT);
104:
105: HTMLAnchorElement restoreLink = page.getElementRestoreLink();
106: HTMLAnchorElement logoutLink = page.getElementLogoutLink();
107: HTMLAnchorElement backLink = page.getElementBackLink();
108: HTMLAnchorElement documentDetailLink = page
109: .getElementDocumentDetailLink();
110:
111: logoutLink.setHref(EnhydraDMConstants.NAVIG_MANAGER + "?"
112: + EnhydraDMConstants.ACTION + "="
113: + EnhydraDMConstants.ACTION_LOGOUT);
114:
115: if (isTemplate) {
116: page.setTextHeader("Deleted Templates");
117: } else {
118: page.setTextHeader("Deleted Documents");
119: }
120:
121: backLink.setHref(EnhydraDMConstants.NAVIG_MANAGER + "?"
122: + EnhydraDMConstants.MODE + "=" + mode);
123:
124: if (documents != null) {
125:
126: for (int i = 0; i < documents.length; i++) {
127: String documentName = documents[i].getName();
128: String documentId = documents[i].getNumber().toString();
129: long documentSize = documents[i].getSize();
130:
131: String appUser = documents[i].getDeletedBy();
132:
133: documentDetailLink
134: .setHref(EnhydraDMConstants.NAVIG_DETAILS + "?"
135: + EnhydraDMConstants.DOCUMENT_ID + "="
136: + documentId + "&"
137: + EnhydraDMConstants.MODE + "=" + mode);
138: page.setTextDocumentName(documentName);
139: page.setTextDocumentId(documentId);
140: page.setTextDocumentSize(String.valueOf(documentSize));
141:
142: page.setTextDeletedBy(appUser);
143:
144: Date mainDate = new Date(documents[i].getDeletedDate());
145: if (mainDate != null) {
146: String strMainDate = sdf.format(mainDate);
147: page.setTextDeletionDate(strMainDate);
148: } else {
149: page.setTextDeletionDate("-");
150: }
151:
152: restoreLink.setHref(EnhydraDMConstants.NAVIG_RECYCLEBIN
153: + "?" + EnhydraDMConstants.ACTION + "="
154: + EnhydraDMConstants.ACTION_RESTORE + "&"
155: + EnhydraDMConstants.DOCUMENT_ID + "="
156: + documents[i].getNumber() + "&"
157: + EnhydraDMConstants.MODE + "=" + mode);
158:
159: fileRow.getParentNode().insertBefore(
160: fileRow.cloneNode(true), fileRow);
161: }
162: }
163: fileRow.getParentNode().removeChild(fileRow);
164: }
165:
166: private void restoreDocument(HttpPresentationComms comms,
167: String documentId, String user) throws BaseException {
168:
169: documentManager.restore(documentId, user);
170: }
171: }
|