001: package org.enhydra.dm.presentation;
002:
003: import java.io.ByteArrayInputStream;
004: import java.util.Date;
005: import java.util.Enumeration;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.Map;
009: import java.util.Properties;
010:
011: import javax.xml.parsers.DocumentBuilder;
012: import javax.xml.parsers.DocumentBuilderFactory;
013:
014: import org.enhydra.dm.EnhydraDM;
015: import org.enhydra.dm.api.FoDocument;
016: import org.enhydra.dm.api.FoDocumentManager;
017: import org.enhydra.dm.api.FoDocumentParam;
018: import org.enhydra.dm.api.exceptions.BaseException;
019: import org.enhydra.dm.util.EnhydraDMConstants;
020: import org.enhydra.dm.util.TransformUtility;
021: import org.w3c.dom.Document;
022: import org.w3c.dom.Node;
023: import org.w3c.dom.NodeList;
024:
025: import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
026: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
027: import com.lutris.appserver.server.httpPresentation.PageRedirectException;
028: import com.lutris.logging.LogChannel;
029: import com.lutris.logging.Logger;
030:
031: public class FoEditor extends Base {
032:
033: /**
034: * Corresponding XMLC object
035: */
036: private FoEditorHTML page;
037:
038: private static final String PROPFILENAME = "foeditor";
039:
040: protected FoDocumentManager foDocumentManager = null;
041:
042: public void run(HttpPresentationComms comms) throws Exception {
043: page = (FoEditorHTML) comms.xmlcFactory
044: .create(FoEditorHTML.class);
045:
046: comms.request.getHttpServletRequest().setCharacterEncoding(
047: EnhydraDMConstants.ENCODING);
048:
049: String action = comms.request
050: .getParameter(EnhydraDMConstants.ACTION);
051: String lang = comms.request
052: .getParameter(EnhydraDMConstants.PARAM_LANG);
053: String name = comms.request
054: .getParameter(EnhydraDMConstants.PARAM_NAME);
055:
056: if (name != null && !name.equals("")) {
057: name = name.endsWith(".pdf") ? name : name + ".pdf";
058: }
059: String documentId = comms.request
060: .getParameter(EnhydraDMConstants.DOCUMENT_ID);
061: String templateId = comms.request
062: .getParameter(EnhydraDMConstants.TEMPLATE_ID);
063:
064: documentManager = ((EnhydraDM) comms.application)
065: .getDocumentManager();
066: documentStore = ((EnhydraDM) comms.application)
067: .getDocumentStore();
068: foDocumentManager = ((EnhydraDM) comms.application)
069: .getFoDocumentManager();
070:
071: boolean doCommit = true;
072: try {
073: initTransaction();
074: String user = getUser(comms);
075:
076: page.setTextUser(user);
077:
078: page.getElementLogoutLink().setHref(
079: EnhydraDMConstants.NAVIG_MANAGER + "?"
080: + EnhydraDMConstants.ACTION + "="
081: + EnhydraDMConstants.ACTION_LOGOUT);
082:
083: page.getElementName().setValue(name);
084: page.getElementNameLabelValue().setTextContent(name);
085:
086: Enumeration paramNames = comms.request.getParameterNames();
087:
088: // Store editor content, "Document properties" and "PDF properties",
089: // embed editor content and "Document properties" and create pdf file,
090: // on the end store new pdf file or new pdf file version.
091: if (EnhydraDMConstants.ACTION_SAVE.equals(action)) {
092: // Create new or update existing PDF document
093: try {
094: HashMap foParams = new HashMap();
095: HashMap pdfParams = new HashMap();
096:
097: String paramName = null;
098: String paramValue = null;
099:
100: while (paramNames.hasMoreElements()) {
101: paramName = paramNames.nextElement().toString();
102: paramValue = comms.request
103: .getParameter(paramName);
104: if (paramName
105: .startsWith(EnhydraDMConstants.FO_PARAM_PREFIX)) {
106: foParams.put(paramName, paramValue);
107: } else if (paramName
108: .startsWith(EnhydraDMConstants.PDF_PARAM_PREFIX)) {
109: if (!paramName
110: .endsWith(EnhydraDMConstants.PDF_PARAM_USERPASSWORD)
111: && !paramName
112: .endsWith(EnhydraDMConstants.PDF_PARAM_OWNERPASSWORD)) {
113: pdfParams.put(paramName, new Boolean(
114: paramValue));
115: } else {
116: pdfParams.put(paramName, paramValue);
117: }
118: }
119: }
120:
121: // html2xhtml for TinyMCE submission
122: Iterator entries = foParams.entrySet().iterator();
123: while (entries.hasNext()) {
124: Map.Entry me = (Map.Entry) entries.next();
125: paramName = (String) me.getKey();
126:
127: if (paramName
128: .endsWith(EnhydraDMConstants.NODE_PARAM_SUFIX)) {
129: String content = (String) me.getValue();
130: content = TransformUtility
131: .html2xhtml(content);
132: foParams.put(paramName, content);
133: }
134: }
135:
136: // Save fo document and its parameters for futer edit option
137: String foDocumentId = this .createFoDocument(
138: foParams, pdfParams);
139:
140: // xhtml2fo for transformed TinyMCE submission
141: entries = foParams.entrySet().iterator();
142: while (entries.hasNext()) {
143: Map.Entry me = (Map.Entry) entries.next();
144: paramName = (String) me.getKey();
145:
146: if (paramName
147: .endsWith(EnhydraDMConstants.NODE_PARAM_SUFIX)) {
148: String content = (String) me.getValue();
149: content = TransformUtility
150: .xhtml2fo(content);
151: foParams.put(paramName, content);
152: }
153: }
154:
155: // prepare XSLFast transformation input
156: byte[] cont = TransformUtility.params2xml(foParams)
157: .getBytes(EnhydraDMConstants.ENCODING);
158:
159: // Setting optional PDF attributes
160: pdfParams.put("producer", "Together Serbia");
161: pdfParams.put("creator",
162: "Enhydra Document Management Application");
163: pdfParams.put("author", "Enhydra Development Team");
164: pdfParams.put("title", "Demonstration Document");
165: pdfParams.put("keywords", "");
166: pdfParams.put("resolution", new Integer(300));
167: pdfParams.put("date", new Date());
168:
169: org.enhydra.dm.api.Document template = documentManager
170: .getDocument(templateId);
171:
172: // create PDF from input
173: cont = TransformUtility.xml2pdf(cont, pdfParams,
174: template.getFilepath());
175:
176: String mimeType = "application/pdf";
177: if (documentId != null && !documentId.equals("")) {
178: // create new version of PDF document
179: this .updateDocument(documentId, name, cont,
180: user, foDocumentId);
181: } else {
182: // create new PDF document
183: this .createDocument(name, cont, mimeType, user,
184: foDocumentId, templateId);
185: }
186: throw new ClientPageRedirectException(
187: EnhydraDMConstants.NAVIG_MANAGER + "?"
188: + EnhydraDMConstants.MODE + "="
189: + EnhydraDMConstants.MODE_DOCUMENT);
190: } catch (BaseException e) {
191: throw new Exception(e);
192: }
193: } else {
194: Properties langProp = ((EnhydraDM) comms.application)
195: .getProperties(PROPFILENAME + "_" + lang,
196: PROPFILENAME);
197: setI18n(langProp, comms.application.getLogChannel(),
198: lang);
199:
200: if (EnhydraDMConstants.ACTION_NEW.equals(action)) {
201: // Open new
202: page.getElementTemplateId().setValue(templateId);
203: comms.response.writeDOM(page);
204: } else if (EnhydraDMConstants.ACTION_EDIT
205: .equals(action)) {
206: // Edit existing
207: org.enhydra.dm.api.Document document = documentManager
208: .getDocument(documentId);
209: String foDocumentId = document.getFoDocumentRef()
210: .getId();
211: templateId = document.getTemplateRef().getNumber();
212:
213: page.getElementDocumentId().setValue(documentId);
214: page.getElementTemplateId().setValue(templateId);
215:
216: // lock document
217: if (documentId != null) {
218: page
219: .getElementBackLink()
220: .setHref(
221: EnhydraDMConstants.NAVIG_FOEDITOR
222: + "?"
223: + EnhydraDMConstants.ACTION
224: + "="
225: + EnhydraDMConstants.ACTION_UNLOCK
226: + "&"
227: + EnhydraDMConstants.DOCUMENT_ID
228: + "=" + documentId);
229: this .lockDocument(documentId, user);
230: }
231:
232: FoDocument foDocument = foDocumentManager
233: .getFoDocumentById(foDocumentId);
234: FoDocumentParam[] foDocumentParams = foDocumentManager
235: .getFoDocumentParametersById(foDocument
236: .getId());
237:
238: // load document content
239: for (int i = 0; i < foDocumentParams.length; i++) {
240: String paramName = foDocumentParams[i]
241: .getName();
242: String patramValue = foDocumentParams[i]
243: .getContent();
244:
245: if (paramName
246: .endsWith(EnhydraDMConstants.NODE_PARAM_SUFIX)) {
247: DocumentBuilder db = DocumentBuilderFactory
248: .newInstance().newDocumentBuilder();
249: byte[] b = patramValue
250: .getBytes(EnhydraDMConstants.ENCODING);
251: Document documentNode = db
252: .parse(new ByteArrayInputStream(b));
253:
254: if (documentNode != null) {
255: Node bodyNode = documentNode
256: .getElementsByTagName("body")
257: .item(0);
258: NodeList bodyChildren = bodyNode
259: .getChildNodes();
260:
261: for (int j = 0; j < bodyChildren
262: .getLength(); j++) {
263: Node contentNode = bodyChildren
264: .item(j);
265: if (contentNode != null) {
266: contentNode = page.importNode(
267: contentNode, true);
268: page.getElementById(paramName)
269: .appendChild(
270: contentNode);
271: }
272: }
273: }
274: } else {
275: page.getElementById(paramName)
276: .setAttribute("value", patramValue);
277: }
278: }
279:
280: // load pdf properties
281: page
282: .getElementById(
283: EnhydraDMConstants.PDF_PARAM_PREFIX
284: + EnhydraDMConstants.PDF_PARAM_USERPASSWORD)
285: .setAttribute("value",
286: foDocument.getUserPassword());
287: page
288: .getElementById(
289: EnhydraDMConstants.PDF_PARAM_PREFIX
290: + EnhydraDMConstants.PDF_PARAM_OWNERPASSWORD)
291: .setAttribute("value",
292: foDocument.getOwnerPassword());
293:
294: if (foDocument.isAllowPrint())
295: page
296: .getElementById(
297: EnhydraDMConstants.PDF_PARAM_PREFIX
298: + EnhydraDMConstants.PDF_PARAM_ALLOWPRINT)
299: .setAttribute("checked", "true");
300:
301: if (foDocument.isAllowCopyContent())
302: page
303: .getElementById(
304: EnhydraDMConstants.PDF_PARAM_PREFIX
305: + EnhydraDMConstants.PDF_PARAM_ALLOWCOPYCONTENT)
306: .setAttribute("checked", "true");
307:
308: if (foDocument.isAllowEditContent())
309: page
310: .getElementById(
311: EnhydraDMConstants.PDF_PARAM_PREFIX
312: + EnhydraDMConstants.PDF_PARAM_ALLOWEDITCONTENT)
313: .setAttribute("checked", "true");
314:
315: if (foDocument.isAllowEditAnnotation())
316: page
317: .getElementById(
318: EnhydraDMConstants.PDF_PARAM_PREFIX
319: + EnhydraDMConstants.PDF_PARAM_ALLOWEDITANNOTATION)
320: .setAttribute("checked", "true");
321:
322: comms.response.writeDOM(page);
323: } else if (EnhydraDMConstants.ACTION_UNLOCK
324: .equals(action)) {
325: try {
326: this .unlockDocument(documentId, user);
327:
328: throw new ClientPageRedirectException(
329: EnhydraDMConstants.NAVIG_MANAGER
330: + "?"
331: + EnhydraDMConstants.MODE
332: + "="
333: + EnhydraDMConstants.MODE_DOCUMENT);
334: } catch (BaseException e) {
335: throw new Exception(e);
336: }
337: }
338: }
339: } catch (PageRedirectException e) {
340: throw e;
341: } catch (Throwable e) {
342: doCommit = false;
343: comms.application.getLogChannel().write(Logger.ERROR,
344: "just failed", e);
345: throw new Exception(e);
346:
347: } finally {
348: if (doCommit) {
349: commitTransaction();
350: } else {
351: rollbackTransaction();
352: }
353: }
354: }
355:
356: /**
357: * Method enable mulilanguage on FoEditor page. It recives propertie file with
358: * translated label names and sets them on page. If ptoperties file is null, label
359: * names will be in English.
360: *
361: * @param langProp
362: * @param logChannel
363: * @param lang
364: */
365: private void setI18n(Properties langProp, LogChannel logChannel,
366: String lang) {
367: try {
368: super .setI18n(langProp, page);
369:
370: String language = langProp.getProperty("language");
371: if (language != null) {
372: page.getElementLang().setTextContent(
373: "var langId = \"" + language + "\";");
374: }
375: } catch (Exception e) {
376: if (null != logChannel) {
377: lang = lang == null ? PROPFILENAME : PROPFILENAME + "_"
378: + lang;
379: logChannel.write(Logger.WARNING, lang
380: + ".properties not found");
381: }
382: }
383: }
384:
385: // ------ Document Management Methods
386:
387: /**
388: * Method store pdf file created from FoEditor content.
389: *
390: * @param documentName
391: * @param documentContent
392: * @param mimeType
393: * @param user
394: * @param foDocumentId
395: * @param templateId
396: * @throws BaseException
397: */
398: private void createDocument(String documentName,
399: byte[] documentContent, String mimeType, String user,
400: String foDocumentId, String templateId)
401: throws BaseException {
402: String documentPath = documentStore.saveDocumentContent(
403: documentName, documentContent);
404: documentManager.create(documentName, documentPath, mimeType,
405: user, foDocumentId, false, templateId);
406: }
407:
408: /**
409: * Method update pdf file created and changed using FoEditor.
410: *
411: * @param documentId
412: * @param documentName
413: * @param documentContent
414: * @param user
415: * @param foDocumentId
416: * @throws BaseException
417: */
418: private void updateDocument(String documentId, String documentName,
419: byte[] documentContent, String user, String foDocumentId)
420: throws BaseException {
421: String documentPath = documentStore.saveDocumentContent(
422: documentName, documentContent);
423: documentManager.update(documentId, documentPath, user,
424: foDocumentId);
425: }
426:
427: /**
428: * Method lock pdf file created from FoEditor content and edited in FoEditor.
429: *
430: * @param documentId
431: * @param user
432: * @throws BaseException
433: */
434: private void lockDocument(String documentId, String user)
435: throws BaseException {
436: documentManager.lock(documentId, user);
437: }
438:
439: /**
440: * Method unlock pdf file edited in FoEditor
441: *
442: * @param documentId
443: * @param user
444: * @throws BaseException
445: */
446: private void unlockDocument(String documentId, String user)
447: throws BaseException {
448: documentManager.unlock(documentId, user);
449: }
450:
451: /**
452: * Metod store editor content.
453: *
454: * @param foParams
455: * @param pdfParams
456: * @return
457: * @throws BaseException
458: */
459: private String createFoDocument(HashMap foParams, HashMap pdfParams)
460: throws BaseException {
461:
462: return foDocumentManager
463: .create(
464: foParams,
465: (String) pdfParams
466: .get(EnhydraDMConstants.PDF_PARAM_PREFIX
467: + EnhydraDMConstants.PDF_PARAM_USERPASSWORD),
468: (String) pdfParams
469: .get(EnhydraDMConstants.PDF_PARAM_PREFIX
470: + EnhydraDMConstants.PDF_PARAM_OWNERPASSWORD),
471: ((Boolean) pdfParams
472: .get(EnhydraDMConstants.PDF_PARAM_PREFIX
473: + EnhydraDMConstants.PDF_PARAM_ALLOWPRINT))
474: .booleanValue(),
475: ((Boolean) pdfParams
476: .get(EnhydraDMConstants.PDF_PARAM_PREFIX
477: + EnhydraDMConstants.PDF_PARAM_ALLOWCOPYCONTENT))
478: .booleanValue(),
479: ((Boolean) pdfParams
480: .get(EnhydraDMConstants.PDF_PARAM_PREFIX
481: + EnhydraDMConstants.PDF_PARAM_ALLOWEDITCONTENT))
482: .booleanValue(),
483: ((Boolean) pdfParams
484: .get(EnhydraDMConstants.PDF_PARAM_PREFIX
485: + EnhydraDMConstants.PDF_PARAM_ALLOWEDITANNOTATION))
486: .booleanValue());
487: }
488: }
|