PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/webccr/src/com/emis/caesar/senao/local/TExecuteApplication.java

https://bitbucket.org/sanliou/synccr
Java | 122 lines | 94 code | 9 blank | 19 comment | 20 complexity | b1b9964a13356639d773b0d99c92428f MD5 | raw file
  1package com.emis.caesar.senao.local;
  2
  3import java.io.BufferedWriter;
  4import java.io.File;
  5import java.util.ArrayList;
  6
  7import com.emis.caesar.emisLog;
  8import com.emis.caesar.senao.event.TEventContext;
  9import com.emis.caesar.senao.unit.TCcr;
 10import com.emis.caesar.senao.unit.TCcrConsole;
 11import com.emis.caesar.senao.unit.sale.TInvoice;
 12import com.emis.caesarui.webccr.webccr;
 13import com.emis.util.emisUtil;
 14import com.vaadin.ui.JavaScript;
 15import com.vaadin.ui.JavaScriptFunction;
 16import com.vaadin.ui.UI;
 17import de.steinwedel.messagebox.ButtonOption;
 18import de.steinwedel.messagebox.MessageBox;
 19import elemental.json.JsonArray;
 20
 21public class TExecuteApplication {
 22  //建構子
 23  public TExecuteApplication() {
 24
 25  }
 26
 27  /**
 28   * 呼叫前台發票列印外掛程式
 29   * @param tInvoice
 30   * @param aInvoiceData_
 31   * @return
 32   */
 33  public static boolean runElecInvoicePrinter(TEventContext _tEventContext,TInvoice tInvoice,
 34                                              String aInvoiceData_, String TYPE) {
 35      /* !!Assigned to Jenny!!
 36    	 1. 將 aInvoiceData_ 存至local的 @sInvNo_.dat 
 37    	 2. 呼叫前台local程式執行
 38    	 3. 讀取前台執行檔產出的result.dat, 判斷是否成功, 失敗則彈出訊息
 39    	 註:可參考SME後台呼叫範例
 40    	 emisServerX 亂碼問題 外掛解析UTF8不用轉UTF8不然會有亂碼
 41    	 目前是直接讀寫 by SENAO 
 42    	 */
 43    ((webccr) UI.getCurrent()).getlog().info(webccr.class, "InvNo:" + tInvoice.getNo());
 44    ((webccr) UI.getCurrent()).getlog().info(webccr.class, "Inv String:" + aInvoiceData_);
 45    ((webccr) UI.getCurrent()).getlog().info(webccr.class, "TYPE:" + TYPE);
 46
 47    JavaScript.getCurrent().addFunction("getFileContext", new JavaScriptFunction() {
 48      public void call(JsonArray arguments) {
 49        String _sContent = arguments.getString(0);
 50        ((webccr) UI.getCurrent()).getlog().info(webccr.class, "INVNO RESULT.DAT content :" + _sContent);
 51        if (!emisUtil.isEmpty(_sContent)) {
 52          String _sSetup[] = _sContent.split("\r\n");
 53          if (_sSetup.length == 1) {
 54            ((webccr) UI.getCurrent()).getFoCCR().UInotify.swErrMsg("ERROR:格式異常" );
 55            _tEventContext.runEventMethod("OK");
 56            return;
 57          }
 58
 59          for (int i = 0; i < _sSetup.length; i++) {
 60            String _sStr = _sSetup[i];
 61            if (_sStr.indexOf("=") >= 0) {
 62              String _sKey = _sStr.substring(0, _sStr.indexOf("="));
 63              String _sVal = _sStr.substring(_sStr.indexOf("=") + 1);
 64              String [] _sResult = _sVal.split(",");
 65              if ("RESULT".equals(_sKey) && tInvoice != null) {
 66                if (_sResult.length >= 2) {
 67                  if ("P".equals(_sResult[0]) && tInvoice.getNo().equals(_sResult[1])) {
 68                    tInvoice.setChkPrint(true);
 69                    _tEventContext.runEventMethod("OK");
 70                  } else if ("Y".equals(_sResult[0]) && tInvoice.getNo().equals(_sResult[1])) {
 71                    tInvoice.setChkPrint(true);
 72                    _tEventContext.runEventMethod("OK");
 73                  } else {
 74                    ((webccr) UI.getCurrent()).getFoCCR().UInotify.swErrMsg("ERROR:" + _sResult[1]);
 75                    _tEventContext.runEventMethod("OK");
 76                  }
 77                } else {
 78                  ((webccr) UI.getCurrent()).getFoCCR().UInotify.swErrMsg("ERROR:格式異常" );
 79                  _tEventContext.runEventMethod("OK");
 80                }
 81                return;
 82              }
 83            } else if (i == _sSetup.length - 1) {
 84              ((webccr) UI.getCurrent()).getFoCCR().UInotify.swErrMsg("ERROR:格式異常" );
 85              _tEventContext.runEventMethod("OK");
 86              return;
 87            }
 88          }
 89        } else {
 90          ((webccr) UI.getCurrent()).getFoCCR().UInotify.swMessage("無外掛或是沒有回覆檔!");
 91          ((webccr) UI.getCurrent()).getlog().error(webccr.class, "invoice : result.dat not found!");
 92          _tEventContext.runEventMethod("OK");
 93        }
 94      }
 95    });
 96    String path = ((webccr) UI.getCurrent()).getFoCCR().getPLUG_DISK() + "emis/senao/ePrintInv/";
 97
 98    JavaScript.getCurrent().execute(
 99        " var util = new emisUtility(); " +
100            " var a = '" + aInvoiceData_ + "';" +
101            " var c = '" + path + "RESULT.DAT';" +
102            " var d = '" + path + tInvoice.getNo() +".DAT';" +
103            // 刪檔
104            " console.log('deletefile => RESULT.DAT'); \n" +
105            " util.deletefile(c);" +
106            // 寫檔 
107            " console.log('write => INVOICE.DAT'); \n" +
108            " util.writeStringToUTF8File(d, a);"+
109            // 呼叫外掛
110            " console.log('executewait => SenaoElecInvoicePrinter'); \n" +
111            " util.executewait('" + path + "SenaoElecInvoicePrinter','" + tInvoice.getNo() + " " + TYPE +"'); " +
112            // 讀回應檔
113            " console.log('read => RESULT.DAT'); \n" +
114            " var _sStr = ''; " +
115            " if (util.exists(c)) " +
116            " {_sStr = util.readFileAsString(c);} " +
117            " getFileContext(_sStr); "
118    );
119    return true;
120  }
121
122}