001: package org.enhydra.dm.presentation;
002:
003: import java.io.IOException;
004: import java.text.SimpleDateFormat;
005: import java.util.Date;
006:
007: import javax.activation.MimetypesFileTypeMap;
008:
009: import org.enhydra.dm.EnhydraDM;
010: import org.enhydra.dm.api.DocumentVersion;
011: import org.enhydra.dm.api.exceptions.BaseException;
012: import org.enhydra.dm.business.exceptions.ActionNotAllowedException;
013: import org.enhydra.dm.util.EnhydraDMConstants;
014: import org.w3c.dom.Node;
015: import org.w3c.dom.html.HTMLAnchorElement;
016: import org.w3c.dom.html.HTMLImageElement;
017:
018: import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
019: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
020: import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
021: import com.lutris.appserver.server.httpPresentation.HttpPresentationOutputStream;
022: import com.lutris.appserver.server.httpPresentation.HttpPresentationResponse;
023: import com.lutris.logging.Logger;
024:
025: public class Versions extends Base {
026:
027: VersionsHTML page;
028:
029: /*
030: * There is the only function needed in order to be a presentation object.
031: */
032: public void run(HttpPresentationComms comms) throws Exception {
033:
034: page = (VersionsHTML) comms.xmlcFactory
035: .create(VersionsHTML.class);
036:
037: comms.request.getHttpServletRequest().setCharacterEncoding(
038: EnhydraDMConstants.ENCODING);
039:
040: String documentId = comms.request
041: .getParameter(EnhydraDMConstants.DOCUMENT_ID);
042: String versionId = comms.request
043: .getParameter(EnhydraDMConstants.VERSION_ID);
044: String action = comms.request
045: .getParameter(EnhydraDMConstants.ACTION);
046: String mode = comms.request
047: .getParameter(EnhydraDMConstants.MODE);
048:
049: if (!EnhydraDMConstants.MODE_TEMPLATE.equals(mode)) {
050: mode = EnhydraDMConstants.MODE_DOCUMENT;
051: }
052:
053: String user = getUser(comms);
054:
055: boolean doCommit = true;
056: try {
057: initTransaction();
058: documentManager = ((EnhydraDM) comms.application)
059: .getDocumentManager();
060: documentStore = ((EnhydraDM) comms.application)
061: .getDocumentStore();
062:
063: if (null == user) {
064: if (null != comms.application.getLogChannel()) {
065: comms.application.getLogChannel()
066: .write(Logger.ERROR,
067: "User Id parameter in empty!");
068: }
069: throw new ActionNotAllowedException(
070: "User oid parameter is empty;");
071: }
072: if (action != null && !"".equals(action)) {
073: if (EnhydraDMConstants.ACTION_DOWNLOAD.equals(action)) {
074: downloadVersionFile(comms, versionId);
075: throw new ClientPageRedirectException(
076: EnhydraDMConstants.NAVIG_VERSIONS + "?"
077: + EnhydraDMConstants.DOCUMENT_ID
078: + "=" + documentId + "&"
079: + EnhydraDMConstants.MODE + "="
080: + mode);
081: } else if (EnhydraDMConstants.ACTION_RESTORE
082: .equals(action)) {
083: restoreDocument(comms, versionId, user);
084: throw new ClientPageRedirectException(
085: EnhydraDMConstants.NAVIG_MANAGER + "?"
086: + EnhydraDMConstants.MODE + "="
087: + mode);
088: }
089: }
090:
091: page.setTextUser("User: " + user);
092: fillFilesetTable(comms, documentId, user, mode);
093: comms.response.writeDOM(page);
094: } catch (BaseException ex) {
095: doCommit = false;
096: if (null != comms.application.getLogChannel()) {
097: comms.application.getLogChannel().write(
098: Logger.ERROR,
099: "Action :" + action + " failure! Reason: "
100: + ex.getMessage());
101: }
102: throw new Exception(ex);
103: } finally {
104: if (doCommit) {
105: commitTransaction();
106: } else {
107: rollbackTransaction();
108: }
109: }
110: }
111:
112: private void fillFilesetTable(HttpPresentationComms comms,
113: String id, String user, String mode) throws BaseException {
114:
115: Node fileRow = page.getElementDocumentRow();
116: DocumentVersion[] documentVers = null;
117: documentVers = documentManager.getDocumentVersions(id);
118:
119: HTMLAnchorElement readLink = page.getElementReadLink();
120: HTMLAnchorElement downloadLink = page.getElementDownloadLink();
121: HTMLAnchorElement logoutLink = page.getElementLogoutLink();
122: HTMLAnchorElement restoreLink = page.getElementRestoreLink();
123: HTMLAnchorElement c2cLink = page.getElementC2cLink();
124: HTMLAnchorElement saaLink = page.getElementSaaLink();
125: HTMLAnchorElement sabLink = page.getElementSabLink();
126: HTMLAnchorElement backLink = page.getElementBackLink();
127:
128: HTMLImageElement restoreImage = page.getElementRestoreImage();
129:
130: logoutLink.setHref(EnhydraDMConstants.NAVIG_MANAGER + "?"
131: + EnhydraDMConstants.ACTION + "="
132: + EnhydraDMConstants.ACTION_LOGOUT);
133: backLink.setHref(EnhydraDMConstants.NAVIG_MANAGER + "?"
134: + EnhydraDMConstants.MODE + "=" + mode);
135:
136: SimpleDateFormat sdf = new SimpleDateFormat(
137: EnhydraDMConstants.DATE_FORMAT);
138: if (documentVers != null) {
139: for (int i = 0; i < documentVers.length; i++) {
140: page.setTextDocumentName(documentVers[i].getDocument()
141: .getName());
142: page.setTextDocumentId(id);
143: page.setTextVersionId(documentVers[i].getNumber());
144: page.setTextDocumentSize(String.valueOf(documentVers[i]
145: .getSize()));
146: page.setTextVersionNumber(documentVers[i]
147: .getVersionNumber());
148:
149: Date mainDate = new Date(documentVers[i]
150: .getLastModifiedDate());
151: if (mainDate != null) {
152: String strMainDate = sdf.format(mainDate);
153: page.setTextDocumentLastModifiedDate(strMainDate);
154: } else {
155: page.setTextDocumentLastModifiedDate("-");
156: }
157:
158: Date versDate = new Date(documentVers[i]
159: .getVersionDate());
160: if (versDate != null) {
161: String strVersDate = sdf.format(versDate);
162: page.setTextVersionDate(strVersDate);
163: } else {
164: page.setTextVersionDate("-");
165: }
166:
167: String parameters = null;
168:
169: parameters = EnhydraDMConstants.ACTION + "="
170: + EnhydraDMConstants.ACTION_READ_VERSION + "&"
171: + EnhydraDMConstants.VERSION_ID + "="
172: + documentVers[i].getNumber();
173: if (EnhydraDM.desenc != null) {
174: parameters = EnhydraDM.desenc.encrypt(parameters);
175: parameters = EnhydraDMConstants.PARAM + "="
176: + parameters;
177: }
178:
179: readLink.setHref(EnhydraDMConstants.NAVIG_WEBDAV + "/"
180: + documentVers[i].getDocument().getName() + "?"
181: + parameters);
182: downloadLink.setHref(EnhydraDMConstants.NAVIG_VERSIONS
183: + "?" + EnhydraDMConstants.ACTION + "="
184: + EnhydraDMConstants.ACTION_DOWNLOAD + "&"
185: + EnhydraDMConstants.VERSION_ID + "="
186: + documentVers[i].getNumber() + "&"
187: + EnhydraDMConstants.DOCUMENT_ID + "=" + id
188: + "&" + EnhydraDMConstants.MODE + "=" + mode);
189:
190: c2cLink.setHref("javascript: copy2clipboard('"
191: + EnhydraDMConstants.NAVIG_WEBDAV + "/"
192: + documentVers[i].getDocument().getName() + "?"
193: + parameters + "');");
194: saaLink.setHref("javascript: sendasattachement('"
195: + EnhydraDMConstants.NAVIG_WEBDAV + "/"
196: + documentVers[i].getDocument().getName() + "?"
197: + parameters + "');");
198: sabLink.setHref("javascript: sendShortcut('"
199: + EnhydraDMConstants.NAVIG_WEBDAV + "/"
200: + documentVers[i].getDocument().getName() + "?"
201: + parameters + "');");
202:
203: if (documentVers[i].getDocument().getLockedBy() != null
204: || documentVers[i].getDocument()
205: .getCheckedOutBy() != null) {
206:
207: restoreLink.removeAttribute("href");
208: restoreImage.setSrc("media/restoreDis.gif");
209:
210: } else {
211: String value = EnhydraDMConstants.NAVIG_VERSIONS
212: + "?" + EnhydraDMConstants.ACTION + "="
213: + EnhydraDMConstants.ACTION_RESTORE + "&"
214: + EnhydraDMConstants.VERSION_ID + "="
215: + documentVers[i].getNumber() + "&"
216: + EnhydraDMConstants.DOCUMENT_ID + "=" + id
217: + "&" + EnhydraDMConstants.MODE + "="
218: + mode;
219: restoreLink.setAttribute("href", value);
220:
221: restoreImage.setSrc("media/restore.gif");
222:
223: }
224:
225: fileRow.getParentNode().insertBefore(
226: fileRow.cloneNode(true), fileRow);
227: }
228: }
229: fileRow.getParentNode().removeChild(fileRow);
230: }
231:
232: private void restoreDocument(HttpPresentationComms comms,
233: String versionId, String user) throws BaseException {
234: documentManager.restoreDocumentVersion(versionId, user);
235: }
236:
237: private void downloadVersionFile(HttpPresentationComms comms,
238: String versionId) throws BaseException {
239:
240: try {
241: if (null != comms.application.getLogChannel()) {
242: comms.application.getLogChannel().write(
243: Logger.ERROR,
244: "Download document version for id: "
245: + versionId + "!");
246: }
247: DocumentVersion documentVersion = documentManager
248: .getDocumentVersion(versionId);
249: byte[] fileForDownload = documentStore
250: .loadDocumentContent(documentVersion.getFilepath());
251:
252: HttpPresentationResponse response = comms.response;
253: HttpPresentationOutputStream out = response
254: .getOutputStream();
255: response.setContentType(new MimetypesFileTypeMap()
256: .getContentType(documentVersion.getDocument()
257: .getName()));
258: response.setHeader(
259: EnhydraDMConstants.HEAD_CONTENT_DISPOSITION,
260: "attachment; filename=\""
261: + documentVersion.getDocument().getName()
262: + "\"");
263: response
264: .setHeader(EnhydraDMConstants.HEAD_PRAGMA, "public");
265: response.setHeader(EnhydraDMConstants.HEAD_CACHE_CONTROL,
266: "max-age=0");
267: response.setStatus(HttpPresentationResponse.SC_OK);
268: out.write(fileForDownload);
269: out.flush();
270: } catch (BaseException e) {
271: throw e;
272: } catch (HttpPresentationException e) {
273: throw new ActionNotAllowedException(e);
274: } catch (IOException e) {
275: throw new ActionNotAllowedException(e);
276: }
277:
278: }
279: }
|