com.iisigroup.cap.rule.handler.RuleTbMntHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.iisigroup.cap.rule.handler.RuleTbMntHandler.java

Source

/* 
 * SampleHandler.java
 * 
 * Copyright (c) 2009-2012 International Integrated System, Inc. 
 * All Rights Reserved.
 * 
 * Licensed Materials - Property of International Integrated System, Inc.
 * 
 * This software is confidential and proprietary information of 
 * International Integrated System, Inc. ("Confidential Information").
 */
package com.iisigroup.cap.rule.handler;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.DecisionTableConfiguration;
import org.drools.builder.DecisionTableInputType;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderConfiguration;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatelessKnowledgeSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;

import com.iisigroup.cap.annotation.HandlerType;
import com.iisigroup.cap.annotation.HandlerType.HandlerTypeEnum;
import com.iisigroup.cap.component.Request;
import com.iisigroup.cap.component.Result;
import com.iisigroup.cap.component.impl.AjaxFormResult;
import com.iisigroup.cap.component.impl.BeanGridResult;
import com.iisigroup.cap.component.impl.ByteArrayDownloadResult;
import com.iisigroup.cap.constants.Constants;
import com.iisigroup.cap.db.constants.SearchMode;
import com.iisigroup.cap.db.dao.SearchSetting;
import com.iisigroup.cap.db.model.Page;
import com.iisigroup.cap.db.service.CommonService;
import com.iisigroup.cap.exception.CapException;
import com.iisigroup.cap.exception.CapFormatException;
import com.iisigroup.cap.exception.CapMessageException;
import com.iisigroup.cap.formatter.BeanFormatter;
import com.iisigroup.cap.formatter.Formatter;
import com.iisigroup.cap.mvc.handler.MFormHandler;
import com.iisigroup.cap.rule.constants.DroolsConstants;
import com.iisigroup.cap.rule.model.CaseInfo;
import com.iisigroup.cap.rule.model.DivCtDtl;
import com.iisigroup.cap.rule.model.DivCtItm;
import com.iisigroup.cap.rule.model.DivFtDtl;
import com.iisigroup.cap.rule.model.DivFtItm;
import com.iisigroup.cap.rule.model.DivRlDtl;
import com.iisigroup.cap.rule.model.DivRlItm;
import com.iisigroup.cap.rule.service.ConditionMntService;
import com.iisigroup.cap.rule.service.FactorMntService;
import com.iisigroup.cap.rule.service.RuleTbMntService;
import com.iisigroup.cap.security.CapSecurityContext;
import com.iisigroup.cap.utils.CapAppContext;
import com.iisigroup.cap.utils.CapBeanUtil;
import com.iisigroup.cap.utils.CapDate;
import com.iisigroup.cap.utils.CapString;
import com.iisigroup.cap.utils.GsonUtil;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * <pre>
 * Condition Maintain Handler
 * </pre>
 * 
 * @since 2013/12/24
 * @author TimChiang
 * @version
 *          <ul>
 *          <li>2013/12/24,TimChiang,new
 *          </ul>
 */
@Controller("ruleTbMnthandler")
public class RuleTbMntHandler extends MFormHandler {

    @Resource
    private ConditionMntService conditionMntService;
    @Resource
    private FactorMntService factorMntService;
    @Resource
    private RuleTbMntService ruleTbMntService;

    @Resource
    private CommonService commonService;

    private final Logger logger = LoggerFactory.getLogger(RuleTbMntHandler.class);

    /**
     * Condition
     * 
     * @param search
     * @param params
     * @return
     */
    @HandlerType(HandlerTypeEnum.FORM)
    public Result query(Request request) {
        String oid = request.get("mainOid");
        String divCtNo = request.get("divRlNo");
        AjaxFormResult result = new AjaxFormResult();
        DivRlItm rlItm = null;
        if (!CapString.isEmpty(oid)) {
            rlItm = ruleTbMntService.getById(oid);
        } else if (!CapString.isEmpty(divCtNo)) {
            rlItm = ruleTbMntService.findByDivRlNo(divCtNo);
        }
        if (rlItm != null) {
            result.putAll(rlItm.toJSONObject(new String[] { "divRlNo", "divRlNm", "oid" }, null));
            result.set("RadioGroup1", rlItm.getDivRlTyp());
        }

        return result;
    }

    /**
     * modify Condition Item and Detail
     * 
     * @param request
     *            request
     * @return IResult
     */
    public Result saveRuleTbDtl(Request request) {
        AjaxFormResult result = new AjaxFormResult();
        String type = request.get("type");
        String divCtNo = request.get("divRlNo");
        // Factor DETAIL
        String[] ftGridData = request.getParamsAsStringArray("grid[]");
        DivRlItm rlItm = ruleTbMntService.findByDivRlNo(divCtNo);

        if ("A".equals(type)) {
            if (rlItm != null) {
                // ?!
                throw new CapMessageException(CapAppContext.getMessage("ruleTb.0001"), getClass());
            }
            rlItm = new DivRlItm();
        } else {
            if (rlItm != null && !rlItm.getOid().equals(request.get("oid"))) {
                // ?!
                throw new CapMessageException(CapAppContext.getMessage("ruleTb.0001"), getClass());
            } else if (rlItm == null && !CapString.isEmpty(request.get("oid"))) {
                rlItm = ruleTbMntService.getById(request.get("oid"));
            } else if (rlItm == null) {
                rlItm = new DivRlItm();
            }
        }
        CapBeanUtil.map2Bean(request, rlItm);
        if ("A".equals(type)) {
            rlItm.setOid(null);
        }
        List<DivRlDtl> delRlDtl = new ArrayList<DivRlDtl>();
        if (ftGridData != null) {
            List<DivRlDtl> rlDtls = new LinkedList<DivRlDtl>();
            if (rlItm.getDivRlDtls() != null && !rlItm.getDivRlDtls().isEmpty()) {
                delRlDtl = rlItm.getDivRlDtls();
            }
            for (int i = 0; i < ftGridData.length; i++) {
                Map<String, Object> jsData = GsonUtil.jsonToMap(ftGridData[i]);
                DivRlDtl rlDtl = new DivRlDtl();
                // int j = 0;
                // boolean haveOld = false;
                // if(!CapString.isEmpty(jsData.getString("oid")) && !rlItm.getDivRlDtls().isEmpty()){
                // for(DivRlDtl rlDtl2 : rlDtls){
                // j++;
                // if(rlDtl2.getOid().equals(jsData.opt("oid"))){
                // rlDtl = rlDtl2;
                // haveOld = true;
                // break;
                // }
                // }
                // }
                CapBeanUtil.map2Bean(jsData, rlDtl);
                rlDtl.setDivRlNo(rlItm.getDivRlNo());
                rlDtl.setDivRlSor(new BigDecimal(i + 1));
                rlDtl.setDivCtNo(CapString.trimNull(jsData.get("divCtNo")));
                DivCtItm ctItm = conditionMntService.getById(CapString.trimNull(jsData.get("divCtOid")));
                // rlDtl.setDivCtItm(ctItm);
                rlDtl.setDivRlItm(rlItm);
                // if(haveOld){
                // rlDtls.set(j, rlDtl);
                // }else{
                rlDtls.add(rlDtl);
                // }
            }

            rlItm.setDivRlDtls(rlDtls);
        }
        String userId = CapSecurityContext.getUserId();
        if (userId.length() > 6)
            userId = userId.substring(0, 6);
        rlItm.setUpdater(userId);
        rlItm.setUpdateTime(CapDate.getCurrentTimestamp());
        String divRltyp = request.get("RadioGroup1");
        rlItm.setDivRlTyp(divRltyp);
        ruleTbMntService.saveDivRlItm(rlItm);
        if (!delRlDtl.isEmpty())
            ruleTbMntService.deleteRlDtlByList(delRlDtl);
        result.set(Constants.AJAX_NOTIFY_MESSAGE, CapAppContext.getMessage("ruleTb.0002"));
        return result;
    }

    /**
     * delete Division Condition Item and Detail
     * 
     * @param request
     *            request
     * @return IResult
     */
    public Result delete(Request request) {
        AjaxFormResult result = new AjaxFormResult();
        ruleTbMntService.deleteById(request.get("oid"));
        result.set(Constants.AJAX_NOTIFY_MESSAGE, CapAppContext.getMessage("ruleTb.0003"));
        return result;
    }

    public Result getFtSelOption(Request request) {
        AjaxFormResult result = new AjaxFormResult();
        List<DivFtItm> ftItms = factorMntService.findAllDivFtItm();
        if (ftItms != null) {
            result.set(" ", "? -");
            for (DivFtItm ftItm : ftItms) {
                result.set(ftItm.getFactorNo(), ftItm.getFactorNm());
            }
        }
        return result;
    }

    @HandlerType(HandlerTypeEnum.FileDownload)
    public Result dwnload(Request request) throws CapException {
        File file = createDecisionTable(request);
        FileInputStream is = null;
        try {
            is = FileUtils.openInputStream(file);
            // return new FileDownloadResult(request, file.getPath(), file.getName(), "xls/plain");
            return new ByteArrayDownloadResult(request, IOUtils.toByteArray(is), "xls/plain", file.getName());
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(is);
        }
        return new AjaxFormResult();
    }

    public File createDecisionTable(Request request) {
        // String packageName = "com.iisigroup.cap.service";
        // String importClass = "com.iisigroup.cap.base.model.CaseInfo,com.iisigroup.cap.utils.CapDroolsUtil";r
        // String variablesName = "com.iisigroup.cap.utils.CapDroolsUtil comUtil";
        String divRlNo = request.get("divRlNo");
        String oid = request.get("oid");
        DivRlItm rlItm = ruleTbMntService.getByRlItmNo(divRlNo);
        if (rlItm == null || rlItm.getDivRlDtls() == null) {
            return null;
        }
        // ??Rule
        List<DivRlDtl> rlDtls = ruleTbMntService.findRlDtlsByRlNoAndSort(rlItm.getDivRlNo());// rlItm.getDivRlDtls();
        List<DivCtItm> ctItms = null;
        WritableWorkbook workbook = null;
        File relFile = null;
        try {
            File fileDir = new File("../Temp");
            FileUtils.forceMkdir(fileDir);
            // Char(3.6) ?
            List<Map<String, Object>> lnf312Data = new ArrayList<Map<String, Object>>();
            String BRNO = "", BRNM = "";
            StringBuffer BRNoNm = new StringBuffer();
            if (!CollectionUtils.isEmpty(lnf312Data)) {
                for (Map<String, Object> map : lnf312Data) {
                    BRNO = MapUtils.getString(map, "BRNO") + " ";
                    BRNM = MapUtils.getString(map, "BRNM") + " ";
                    BRNoNm.append(BRNO).append(BRNM);
                }
            }

            relFile = new File(fileDir + File.separator + CapDate.getCurrentDate("yyyyMMdd") + "_ruleExport.xls");
            relFile.createNewFile();
            // 2.  workbook
            workbook = Workbook.createWorkbook(relFile);
            WritableSheet sheet1 = workbook.createSheet("Tables", 0);
            WritableFont myFont = new WritableFont(WritableFont.createFont("Arial"), 12);
            WritableFont boldWhiteFnt = new WritableFont(WritableFont.createFont("Arial"), 12, WritableFont.BOLD);
            boldWhiteFnt.setColour(Colour.WHITE);
            WritableFont blackFnt = new WritableFont(WritableFont.createFont("Arial"), 12, WritableFont.NO_BOLD);
            blackFnt.setColour(Colour.BLACK);
            // 1--->
            // DecisionTable?()
            WritableCellFormat headCellFmt = new WritableCellFormat();
            // ?
            headCellFmt.setWrap(false);
            // ?
            headCellFmt.setAlignment(Alignment.LEFT);
            // 
            headCellFmt.setVerticalAlignment(VerticalAlignment.TOP);
            // 
            headCellFmt.setFont(boldWhiteFnt);
            // ?()
            // headCellFmt.setBorder(Border.ALL,BorderLineStyle.THIN, Colour.BLACK);
            // ?
            headCellFmt.setBackground(Colour.GRAY_80);

            // DecisionTable
            WritableCellFormat setCellFmt = new WritableCellFormat();
            // ?
            setCellFmt.setWrap(true);
            // ?
            setCellFmt.setAlignment(Alignment.LEFT);
            // 
            setCellFmt.setVerticalAlignment(VerticalAlignment.TOP);
            // 
            setCellFmt.setFont(myFont);
            // ?()
            setCellFmt.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
            // ?()
            setCellFmt.setBackground(Colour.TAN);

            // DecisionTable
            WritableCellFormat bodyCellFmt = new WritableCellFormat();
            // ?
            bodyCellFmt.setWrap(false);
            // ?
            bodyCellFmt.setAlignment(Alignment.LEFT);
            // 
            bodyCellFmt.setVerticalAlignment(VerticalAlignment.TOP);
            // 
            bodyCellFmt.setFont(myFont);
            // ?()
            bodyCellFmt.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
            // ?
            // bodyCellFmt.setBackground(Colour.BLACK);

            // DecisionTable?
            WritableCellFormat blueCellFmt = new WritableCellFormat();
            // ?
            blueCellFmt.setWrap(false);
            // ?
            blueCellFmt.setAlignment(Alignment.LEFT);
            // 
            blueCellFmt.setVerticalAlignment(VerticalAlignment.TOP);
            // 
            blueCellFmt.setFont(myFont);
            // ?()
            blueCellFmt.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
            blueCellFmt.setBackground(Colour.LIGHT_TURQUOISE);

            // DecisionTable
            WritableCellFormat yellowCellFmt = new WritableCellFormat();
            // ?
            yellowCellFmt.setWrap(false);
            // ?
            yellowCellFmt.setAlignment(Alignment.LEFT);
            // 
            yellowCellFmt.setVerticalAlignment(VerticalAlignment.TOP);
            // 
            yellowCellFmt.setFont(myFont);
            // ?()
            yellowCellFmt.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
            yellowCellFmt.setBackground(Colour.YELLOW);

            // DecisionTable
            WritableCellFormat greenCellFmt = new WritableCellFormat();
            // ?
            greenCellFmt.setWrap(false);
            // ?
            greenCellFmt.setAlignment(Alignment.LEFT);
            // 
            greenCellFmt.setVerticalAlignment(VerticalAlignment.TOP);
            // 
            greenCellFmt.setFont(myFont);
            // ?()
            greenCellFmt.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
            greenCellFmt.setBackground(Colour.LIGHT_GREEN);

            // 
            // int row = 0;
            // sheet1.setRowView(row,450);
            // row100
            // sheet1.setColumnView(row, 100);
            // ?x,y, x1,y2
            // sheet1.mergeCells(0, row, 11, row);
            // sheet1.addCell(new Label(0, 0, "", bodyCellFmt));
            // sheet1.setColumnView(0, 26);

            for (int i = 0; i < 65535; i++) {
                sheet1.setRowView(i + 1, 450);
                sheet1.setColumnView(i, 23);
            }
            sheet1.setRowView(12, 900);

            // DecisionTable-?
            NumberFormat nf = new DecimalFormat("#.00");
            sheet1.addCell(new Label(1, 2, "RuleSet", headCellFmt));
            sheet1.addCell(new Label(1, 3, "Import", headCellFmt));
            sheet1.addCell(new Label(1, 4, "EscapeQuotes", headCellFmt));
            sheet1.addCell(new Label(1, 5, "Variables", headCellFmt));
            sheet1.addCell(new Label(1, 6, "Functions", headCellFmt));
            sheet1.addCell(new Label(1, 7, "Notes", headCellFmt));
            sheet1.addCell(new Label(1, 9, "RuleTable " + rlItm.getDivRlNm(), headCellFmt));

            // DecisionTable-
            sheet1.addCell(new Label(2, 2, DroolsConstants.PACKAGE_NAME, headCellFmt));
            sheet1.addCell(new Label(2, 3, DroolsConstants.IMPORT_CLASS, headCellFmt));
            sheet1.addCell(new Label(2, 4, "false", headCellFmt));
            sheet1.addCell(new Label(2, 5, DroolsConstants.VARIABLES_NAME, headCellFmt));
            // ???
            sheet1.addCell(new Label(2, 6, "", headCellFmt));
            sheet1.addCell(new Label(2, 7, "This is decision table for " + rlItm.getDivRlNm(), headCellFmt));
            sheet1.addCell(new Label(2, 9, "", headCellFmt));
            // excel?
            for (int i = 0; i < 6; i++) {
                sheet1.mergeCells(2, 2 + i, 7, 2 + i);
            }
            // ?
            sheet1.mergeCells(2, 9, 7, 9);
            /* ?, */
            // ?conditionaction
            // condition detail
            int conditionMaxCount = 0;
            // database table & column?
            List<String> tableNms = new ArrayList<String>();
            Map<String, DivCtDtl> colNms = new LinkedHashMap<String, DivCtDtl>();
            // ?????($param)
            List<DivCtItm> ctItmList = new ArrayList<DivCtItm>();
            for (DivRlDtl rlDtl : rlDtls) {
                DivCtItm ctItm = conditionMntService.findByDivCtItmNo(rlDtl.getDivCtNo());
                if (ctItm.getDivCtDtls() != null && ctItm.getDivCtDtls().size() > conditionMaxCount) {
                    conditionMaxCount = ctItm.getDivCtDtls().size();
                }
                // ??
                List<DivCtDtl> ctDtls = conditionMntService.findCtDtlsByCtNoAndSort(ctItm.getDivCtNo());
                // ?(??1to1)
                for (DivCtDtl ctDtl : ctDtls) {
                    if (ctDtl.getDivFtDtl() != null && ctDtl.getDivFtDtl().getDivFtItm() != null) {
                        DivFtItm ftItm = ctDtl.getDivFtDtl().getDivFtItm();
                        String tableNm = ftItm.getTableNm();
                        String colNm = ftItm.getColumnNm();
                        // ????CONDITION?
                        if (!tableNms.contains(tableNm)) {
                            tableNms.add(tableNm);
                        }
                        if (!colNms.containsKey(colNm)) {
                            colNms.put(colNm, ctDtl);
                        }
                    }
                }
                ctItmList.add(ctItm);
            } // rule detail

            // RuleTable()
            int i = 0, j = 0;
            Map<String, Integer> colSortNo = new HashMap<String, Integer>();
            for (String key : colNms.keySet()) {
                DivCtDtl ctDtl = colNms.get(key);
                // CONDITOIN?
                sheet1.addCell(new Label(1 + j, 10, DroolsConstants.CONDITION_COL, setCellFmt));
                // 
                sheet1.addCell(new Label(1 + j, 11, "beanClz:" + tableNms.get(0), setCellFmt));
                // ? . + "?" + param/$1,$2
                String operand = "";
                if (ctDtl.getDivFtDtl() != null) {
                    DivFtDtl ftDtl = ctDtl.getDivFtDtl();
                    if (!CapString.isEmpty(ftDtl.getRange1()) && !CapString.isEmpty(ftDtl.getRange2())) {
                        operand = "$1<" + key + "&&" + key + "<=$2";
                    } else {
                        operand = key + "==$param";
                    }
                }
                sheet1.addCell(new Label(1 + j, 12, operand, setCellFmt));
                sheet1.addCell(new Label(1 + j, 13, key, blueCellFmt));
                colSortNo.put(key, 1 + j);
                i++;
                j++;
            }
            // Bean ??
            sheet1.mergeCells(1, 11, j, 11);

            // CONDITION?ACTIVITION-GROUP&NO-LOOP&PRIORITY
            int actGpColNo = 1 + j;
            sheet1.addCell(new Label(actGpColNo, 10, DroolsConstants.ACTIVATION_GROUP, setCellFmt));
            sheet1.addCell(new Label(actGpColNo, 11, "", setCellFmt));
            sheet1.addCell(new Label(actGpColNo, 12, "", setCellFmt));
            sheet1.addCell(new Label(actGpColNo, 13, "activation group", blueCellFmt));
            int noLpColNo = actGpColNo + 1;
            sheet1.addCell(new Label(noLpColNo, 10, DroolsConstants.NO_LOOP, setCellFmt));
            sheet1.addCell(new Label(noLpColNo, 11, "", setCellFmt));
            sheet1.addCell(new Label(noLpColNo, 12, "", setCellFmt));
            sheet1.addCell(new Label(noLpColNo, 13, "no loop", blueCellFmt));
            int priColNo = noLpColNo + 1;
            sheet1.addCell(new Label(priColNo, 10, DroolsConstants.PRIORITY, setCellFmt));
            sheet1.addCell(new Label(priColNo, 11, "", setCellFmt));
            sheet1.addCell(new Label(priColNo, 12, "", setCellFmt));
            sheet1.addCell(new Label(priColNo, 13, "priority", blueCellFmt));

            // ACTION
            int actColNo1 = priColNo + 1;
            sheet1.addCell(new Label(actColNo1, 10, DroolsConstants.ACTION_COL, setCellFmt));
            sheet1.addCell(new Label(actColNo1, 11, "", setCellFmt));
            sheet1.addCell(new Label(actColNo1, 12, "beanClz.setGrpUppId(\"$param\");", setCellFmt));
            sheet1.addCell(new Label(actColNo1, 13, "", yellowCellFmt));

            int actColNo2 = actColNo1 + 1;
            sheet1.addCell(new Label(actColNo2, 10, DroolsConstants.ACTION_COL, setCellFmt));
            sheet1.addCell(new Label(actColNo2, 11, "", setCellFmt));
            sheet1.addCell(new Label(actColNo2, 12, "beanClz.setDocStatus(\"$param\");", setCellFmt));
            sheet1.addCell(new Label(actColNo2, 13, "", yellowCellFmt));

            int actColNo3 = actColNo2 + 1;
            sheet1.addCell(new Label(actColNo3, 10, DroolsConstants.ACTION_COL, setCellFmt));
            sheet1.addCell(new Label(actColNo3, 11, "", setCellFmt));
            sheet1.addCell(new Label(actColNo3, 12,
                    "System.out.println(\"$param\"+ \" CASEINFO IS "
                            + "\" +beanClz.getCasNo()+\" ** ? = " + "\" +beanClz.getGrpUppId() );",
                    setCellFmt));
            sheet1.addCell(new Label(actColNo3, 13, "", yellowCellFmt));
            sheet1.addCell(new Label(0, 13, "Base rules", greenCellFmt));

            /*
             * condition-->factorExcel
             */
            if (!ctItmList.isEmpty()) {
                int rowNo = 14, count = 0;
                for (DivCtItm ctItm : ctItmList) {
                    if (ctItm.getDivCtDtls() != null) {
                        // CtItm list?CtDtl,CtItmRule Row
                        for (DivCtDtl ctDtl : ctItm.getDivCtDtls()) {
                            ctItm = ctDtl.getDivCtItm();
                            // Action
                            sheet1.addCell(new Label(actColNo1, rowNo, ctItm.getDivCtAction(), bodyCellFmt));
                            sheet1.addCell(new Label(actColNo2, rowNo, ctItm.getDivCtSetting(), bodyCellFmt));
                            sheet1.addCell(new Label(actColNo3, rowNo, "Test Condition::" + ctItm.getDivCtNm(),
                                    bodyCellFmt));

                            DivFtDtl ftDtl = ctDtl.getDivFtDtl();
                            String colNm = ftDtl.getDivFtItm().getColumnNm();
                            // ??cell
                            Integer colNo = colSortNo.get(colNm);
                            String rangeVal = "";
                            if (!CapString.isEmpty(ftDtl.getRange1())) {
                                rangeVal = ftDtl.getRange1();
                            }
                            if (!CapString.isEmpty(ftDtl.getRange2())) {
                                rangeVal += "," + ftDtl.getRange2();
                            }
                            // row??(rule name)
                            sheet1.addCell(new Label(0, rowNo, ctDtl.getDivCtItm().getDivCtNm(), bodyCellFmt));
                            // 
                            sheet1.addCell(new Label(colNo, rowNo, rangeVal, bodyCellFmt));
                        }
                        sheet1.addCell(new Label(actGpColNo, rowNo, "group", bodyCellFmt));
                        sheet1.addCell(new Label(noLpColNo, rowNo, "true", bodyCellFmt));
                        sheet1.addCell(
                                new Label(priColNo, rowNo, String.valueOf(ctItmList.size() - count), bodyCellFmt));
                        rowNo++;
                        count++;
                    }
                }
            }

            /* END */

            workbook.write();
            workbook.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return relFile;
    }

    /**
     * 
     * 
     * @param request
     * @return IResult
     */
    public Result testDrools(Request request) {

        File ruleXls = new File("/Volumes/RamDisk/rule_export.xls");
        AjaxFormResult result = new AjaxFormResult();
        try {
            Properties props = new Properties();
            props.setProperty("drools.dialect.java.compiler", "JANINO");
            KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(props);
            KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(config);

            DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
            dtconf.setInputType(DecisionTableInputType.XLS);
            dtconf.setWorksheetName("Tables");
            kbuilder.add(ResourceFactory.newFileResource(ruleXls), ResourceType.DTABLE, dtconf);
            // kbuilder.add(ResourceFactory.newClassPathResource("rule_export.xls", getClass()), ResourceType.DTABLE, dtconf);

            if (kbuilder.hasErrors()) {
                KnowledgeBuilderErrors errs = kbuilder.getErrors();
                Iterator rr = errs.iterator();
                while (rr.hasNext()) {
                    Object obj = rr.next();
                    if (obj instanceof KnowledgeBuilderError) {
                        System.err.println(((KnowledgeBuilderError) obj).getMessage());
                    }
                }
                System.err.print(kbuilder.getErrors());
                result.set("tMsg", "" + kbuilder.getErrors());
                return result;
            }

            KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
            kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

            // typical decision tables are used statelessly
            StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
            List<CaseInfo> csinfoList = ruleTbMntService.getNoneDispatchCaseInfo();
            // ksession2.setGlobal("currDate", CommonUtil.getSysDate());
            if (csinfoList != null && !csinfoList.isEmpty()) {
                int i = 0;
                for (CaseInfo csinfo : csinfoList) {
                    ksession.execute(Arrays.asList(new Object[] { csinfo }));
                }
            }
            ruleTbMntService.updateCaseInfo(csinfoList);
            System.out.println(" test dispatch success !");
            result.set("tMsg", "?");
        } catch (Exception e) {
            result.set("tMsg", "" + e.getLocalizedMessage());
            e.printStackTrace();
        }

        return result;
    }

    /*********** Grid ***************************************************/
    /**
     * ViewPageRule Item Grid
     * 
     * @param search
     * @param params
     * @return
     */
    @HandlerType(HandlerTypeEnum.GRID)
    public BeanGridResult queryRuleItmByDivRlNo(SearchSetting search, Request params) {
        search.addSearchModeParameters(SearchMode.NOT_EQUALS, "divRlNo", "");

        Page<DivRlItm> page = commonService.findPage(DivRlItm.class, search);
        Map<String, Formatter> fmt = new HashMap<String, Formatter>();
        fmt.put("ruleCont", new RuleValFormatter(this.conditionMntService));
        return new BeanGridResult(page.getContent(), page.getTotalRow(), fmt);
    }

    /**
     * Condition Detail Grid
     * 
     * @param search
     * @param params
     * @return
     */
    @HandlerType(HandlerTypeEnum.GRID)
    public BeanGridResult queryConditionDetail(SearchSetting search, Request params) {
        // if (params.containsKey("divRlNo") && !CapString.isEmpty(params.get("divRlNo"))) {
        // search.addSearchModeParameters(SearchMode.EQUALS, "divRlNo",
        // params.get("divRlNo"));
        // }else{
        // return new GridResult();
        // }
        // search.addSearchModeParameters(SearchMode.EQUALS, "divCtNo", "C000001");
        Page<DivCtItm> page = commonService.findPage(DivCtItm.class, search);
        Map<String, Formatter> fmt = new HashMap<String, Formatter>();
        fmt.put("divCtCont", new CondValNmFormatter());
        return new BeanGridResult(page.getContent(), page.getTotalRow(), fmt);
    }

    /**
     * Rule Detail Grid
     * 
     * @param search
     * @param params
     * @return
     */
    @HandlerType(HandlerTypeEnum.GRID)
    public BeanGridResult queryRuleTbDetailByDivRlNo(SearchSetting search, Request params) {
        if (params.containsKey("divRlNo") && !CapString.isEmpty(params.get("divRlNo"))) {
            search.addSearchModeParameters(SearchMode.EQUALS, "divRlNo", params.get("divRlNo"));
        } else {
            return new BeanGridResult();
        }

        Page<DivRlDtl> page = commonService.findPage(DivRlDtl.class, search);
        Map<String, Formatter> fmt = new HashMap<String, Formatter>();
        fmt.put("divCtNm", new ConditionNmFormatter(conditionMntService));
        return new BeanGridResult(page.getContent(), page.getTotalRow(), fmt);
    }

    /********* Grid Formatter **********/

    /**
     * CondValNmFormatter formatter
     */
    class CondValNmFormatter implements BeanFormatter {
        private static final long serialVersionUID = 1L;

        @SuppressWarnings("unchecked")
        public String reformat(Object in) throws CapFormatException {
            StringBuffer result = new StringBuffer();
            if (in instanceof DivCtItm) {
                DivCtItm ctItm = ((DivCtItm) in);
                if (ctItm.getDivCtDtls() != null) {
                    for (DivCtDtl ctDtl : ctItm.getDivCtDtls()) {
                        result.append(ctDtl.getDivFtDtl().getRangeNm() + ",");
                    }
                    if (result.length() > 0)
                        result.deleteCharAt(result.lastIndexOf(","));
                }
            }
            return result.toString();
        }
    }

    /**
     * RuleDetailNmFormatter formatter
     */
    class RuleValFormatter implements BeanFormatter {
        private static final long serialVersionUID = 1L;
        private ConditionMntService conditionMntService;

        public RuleValFormatter(ConditionMntService conditionMntService) {
            this.conditionMntService = conditionMntService;
        }

        @SuppressWarnings("unchecked")
        public String reformat(Object in) throws CapFormatException {
            StringBuffer result = new StringBuffer();
            if (in instanceof DivRlItm) {
                DivRlItm rlItm = ((DivRlItm) in);
                if (rlItm.getDivRlDtls() != null && !rlItm.getDivRlDtls().isEmpty()) {
                    for (DivRlDtl rlDtl : rlItm.getDivRlDtls()) {
                        DivCtItm ctItm = conditionMntService.findByDivCtItmNo(rlDtl.getDivCtNo());
                        if (ctItm != null)
                            result.append(ctItm.getDivCtNm() + ",");
                    }
                    if (result.length() > 0)
                        result.deleteCharAt(result.lastIndexOf(","));
                }
            }
            return result.toString();
        }
    }

    /**
     * ConditionNmFormatter formatter
     */
    class ConditionNmFormatter implements BeanFormatter {
        private static final long serialVersionUID = 1L;
        private ConditionMntService conditionMntService;

        public ConditionNmFormatter(ConditionMntService conditionMntService) {
            this.conditionMntService = conditionMntService;
        }

        @SuppressWarnings("unchecked")
        public String reformat(Object in) throws CapFormatException {
            StringBuffer result = new StringBuffer();
            if (in instanceof DivRlDtl) {
                DivRlDtl rlDtl = ((DivRlDtl) in);
                DivCtItm ctItm = conditionMntService.findByDivCtItmNo(rlDtl.getDivCtNo());
                if (ctItm != null) {
                    result.append(ctItm.getDivCtNm());
                }
            }
            return result.toString();
        }
    }

    // /**
    // * CondRangeNmFormatter formatter
    // */
    // class CondRangeNmFormatter implements IBeanFormatter {
    // private static final long serialVersionUID = 1L;
    //
    // @SuppressWarnings("unchecked")
    // public String reformat(Object in) throws CapFormatException {
    // StringBuffer rangeNm = new StringBuffer();
    // if (in instanceof DivCtDtl) {
    // DivCtDtl ctDtl = ((DivCtDtl) in);
    // if(ctDtl.getDivFtDtl()!=null){
    // DivFtDtl ftDtl = ctDtl.getDivFtDtl();
    // rangeNm.append(ftDtl.getRangeNm());
    // }
    // }
    // return rangeNm.toString();
    // }
    // }

}