001: package com.xoetrope.print;
002:
003: import com.xoetrope.export.Exporter;
004: import com.xoetrope.export.TemplatePreprocessor;
005: import com.xoetrope.service.http.XHttpRequestProxy;
006: import com.xoetrope.xbb.appmgr.ApplicationManager;
007: import com.xoetrope.xbb.appmgr.Logger;
008: import java.util.ArrayList;
009:
010: import net.xoetrope.optional.service.ServiceContext;
011: import net.xoetrope.optional.service.ServiceProxyArgs;
012: import net.xoetrope.optional.service.XServiceModelNode;
013: import net.xoetrope.xui.XPage;
014: import net.xoetrope.xui.data.XBaseModel;
015: import net.xoetrope.xui.data.XModel;
016:
017: import java.io.File;
018: import java.io.FileInputStream;
019: import java.io.IOException;
020: import net.xoetrope.swing.XButton;
021: import sun.misc.BASE64Encoder;
022:
023: /**
024: *
025: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
026: * the GNU Public License (GPL), please see license.txt for more details. If
027: * you make commercial use of this software you must purchase a commercial
028: * license from Xoetrope.</p>
029: * @author luano
030: */
031: public class PrintHelper extends XPage {
032: public static final String CURRENT_EVENT_INDEX = "jobIndex/jobId";
033: public static final String EVENT_NAME = "job";
034: public static final String ROOT_PATH = EVENT_NAME + "/";
035: public static final String STATE_PATH = "/xui_state/" + EVENT_NAME
036: + "/";
037:
038: private String jobId = "0";
039: private XModel jobNode;
040: private XModel stateNode;
041: private TemplatePreprocessor preprocessor;
042: private ApplicationManager jobManager;
043: private Logger logger;
044: private XButton nextBtn;
045:
046: /** Creates a new instance of PrintHelper */
047: public PrintHelper() {
048: }
049:
050: /**
051: * Preview the letter
052: */
053: public void doPreview() {
054: printLetter(true);
055: }
056:
057: /**
058: * Print the letter
059: */
060: public void doPrint() {
061: printLetter(false);
062: }
063:
064: /**
065: * Prepare the letter for printing
066: */
067: protected void printLetter(boolean isPreview) {
068: // saveBoundComponentValues();
069:
070: String idStr = ((XBaseModel) rootModel.get(ROOT_PATH + "jobId"))
071: .getAttribValueAsString(XBaseModel.VALUE_ATTRIBUTE);
072: jobNode = (XModel) rootModel.get(ROOT_PATH + idStr);
073: stateNode = (XModel) rootModel.get(STATE_PATH + idStr);
074:
075: preprocessor = new TemplatePreprocessor(project);
076: XBaseModel model = (XBaseModel) rootModel.get("margins");
077: ArrayList marginList = (ArrayList) model.get();
078: preprocessor.setPdfProperties(marginList);
079:
080: String outputFileName = preprocessor.printDocument(
081: "job/jobTemplate",
082: isPreview ? TemplatePreprocessor.PREVIEW
083: : TemplatePreprocessor.PRINT,
084: Exporter.PDF_EXPORTER, jobNode, stateNode);
085:
086: if (logger == null)
087: logger = (Logger) ((XBaseModel) rootModel.get("Logger"))
088: .get();
089: if (logger != null)
090: logger.addMessage("File printed: " + outputFileName);
091:
092: if (!isPreview)
093: postDocument(outputFileName);
094: }
095:
096: private void postDocument(String documentName) {
097: XBaseModel model = (XBaseModel) rootModel.get("PhysioPost");
098: XServiceModelNode node = (XServiceModelNode) model.get();
099:
100: // Setup the context
101: ServiceContext context = new ServiceContext();
102: ServiceProxyArgs args = context.getArgs();
103:
104: // Setup the authorization
105: String token = ApplicationManager.userName + ":"
106: + ApplicationManager.password;
107: String authorization = new BASE64Encoder().encode(token
108: .getBytes());
109: args.setConfigParam(XHttpRequestProxy.PARAM_AUTHORIZATION,
110: authorization);
111: args.setConfigParam(XHttpRequestProxy.REQUEST_METHOD, "PUT");
112:
113: // Setup the document argument
114: byte[] fileBytes = readDocument(documentName);
115: //String encodedFile = new BASE64Encoder().encode( token.getBytes());
116: args.setPassParam(XHttpRequestProxy.PARAM_BODY, fileBytes);
117:
118: // Make the request
119: String urlStr = "";//jobManager.getEventUrlStr();
120: if ((urlStr == null) || (urlStr.length() == 0))
121: return;
122:
123: args.setConfigParam("url", urlStr);
124: String result = (String) node.get(context);
125: //System.out.println( result );
126:
127: if (logger != null)
128: logger.addMessage("File markered as printed: "
129: + documentName);
130:
131: //jobManager.selectedDocumentIssued();
132: }
133:
134: private byte[] readDocument(String fileName) {
135: try {
136: File file = new File(fileName);
137: FileInputStream fis = new FileInputStream(file);
138: byte[] b = new byte[(int) file.length()];
139:
140: // Read in the bytes
141: int offset = 0;
142: int numRead = 0;
143: while ((offset < b.length) && (numRead >= 0)) {
144: numRead = fis.read(b, offset, b.length - offset);
145: offset += numRead;
146: }
147:
148: // Ensure all the bytes have been read in
149: if (offset < b.length)
150: throw new IOException("Could not completely read file "
151: + fileName);
152:
153: fis.close();
154: return b;
155: } catch (Exception ex) {
156: ex.printStackTrace();
157: }
158:
159: return null;
160: }
161: }
|