# CAP4无流程表单应用绑定自定义按钮开发文档


# 概要说明

自V7.1版本开始,CAP4无流程表单应用绑定设置可以支持自定义按钮(以下简称按钮),客开伙伴可以用此通路在CAP4无流程数据列表页面开发一些想要的功能。

总体来说,应用绑定自定义按钮开发模式和表单编辑器中的自定义控件类似,是以插件或者组件的形式融入产品,插件或者组件开发规范请见http://open.seeyon.com/book/ctp/sdk/ctpbackendspec.html#插件化 (opens new window)

# 应用绑定自定义按钮代码目录结构说明

开发按钮第一步自然是创建项目,创建项目之后目录结构可以参照以下示例创建各目录(以下目录结构是一个自定义按钮的示例):

 ─seeyon
..├─apps_res
..│..└─cap
..│......└─customCtrlResources
..│..........└─newFormDataBtnResources
..│..............├─css
..│..............│......setTargetFormInfo.css
..│..............│
..│..............├─html
..│..............│......setTargetFormInfo.html
..│..............│
..│..............├─images
..│..............│......dash-arrow.png
..│..............│
..│..............└─js
..│......................customBtn8714694276131171133.common.js
..│......................customBtn8714694276131171133.umd.js
..│......................customBtn8714694276131171133.umd.min.js
..│......................setTargetFormInfo.js
..│
..└─WEB-INF
......├─cfgHome
......│..└─component
......│......└─newFormDataBtn
......│..........│..pluginCfg.xml
......│..........│
......│..........├─i18n
......│..........│......newFormDataBtn_en.properties
......│..........│......newFormDataBtn_zh_CN.properties
......│..........│......newFormDataBtn_zh_TW.properties
......│..........│
......│..........└─spring
......│..................spring-newformdatabtn-manager.xml
......│
......├─classes
......│..└─com
......│......└─seeyon
......│..........└─cap4
......│..............└─form
......│..................└─bean
......│......................└─button
......│..............................NewFormDataBtn.class
......│
......└─jsp

# 后端开发说明

要实现一个自定义按钮,需要写一个java类继承com.seeyon.cap4.form.bean.button.CommonBtn,并且实现/重写其中的接口,例如:

package com.seeyon.cap4.form.bean.button;

import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormSaveAsBean;
import com.seeyon.cap4.form.modules.importandexport.BusinessDataBean;
import com.seeyon.cap4.form.util.Enums;
import com.seeyon.ctp.common.i18n.ResourceUtil;
import com.seeyon.ctp.util.Strings;
import com.seeyon.ctp.util.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by weijh on 2018-12-26.
 * 应用绑定新建按钮实现类
 */
public class NewFormDataBtn extends CommonBtn {

@Override
public void init() {
    this.setPluginId("newFormDataBtn");//设置插件或者组件id,和pluginCfg.xml中的id一致
    this.setIcon("cap-icon-custom-button");

    BtnParamDefinition targetFormInfoParam = new BtnParamDefinition();
    targetFormInfoParam.setDialogUrl("apps_res/cap/customCtrlResources/newFormDataBtnResources/html/setTargetFormInfo.html");
    targetFormInfoParam.setDisplay("com.cap.btn.newFormDataBtn.param1.display");
    targetFormInfoParam.setName("targetFormInfo");
    targetFormInfoParam.setParamType(Enums.BtnParamType.button);
    targetFormInfoParam.setDialogWidth("640");
    targetFormInfoParam.setDialogHeight("415");

    addDefinition(targetFormInfoParam);
}

@Override
public String getKey() {
 return "8714694276131171133";//给按钮设置一个key,可以随便取,只是不要和已有按钮冲突
}

@Override
public String getNameSpace() {
 return "customBtn" + this.getKey();
}

@Override
public String getText() {
 return ResourceUtil.getString("com.cap.btn.newFormDataBtn.text");//设置按钮名称
}

/*
/**
 * 获取PC端自定义控件运行态资源注入信息
 * jsUri:定义PC端表单运行态加载第三方JavaScript的路径
 * cssUri:定义PC端表单运行态加载第三方CSS的路径
 * initMethod:定义PC端表单运行态第三方js入口方法名称
 *
 * @return
 */
@Override
public String getPCInjectionInfo() {
 return "{\"path\":\"apps_res/cap/customCtrlResources/newFormDataBtnResources/\",\"jsUri\":\"js/" + this.getNameSpace() + ".umd.min.js\",\"initMethod\":\"init\",\"nameSpace\":\"" + this.getNameSpace() + "\"}";
}

@Override
public String getMBInjectionInfo() {
 return null;
}

/**
 * 导出的扩展接口,应用绑定自定义按钮用,有需要的重写该方法
 *
 * @param formBean         当前表单
 * @param businessDataBean 导出中间对象,如果有附件,可以放到对象中的unifiedExportAttachment中
 * @param resultMap        按鈕json
 */
@SuppressWarnings("unchecked")
@Override
public void getJson4Export(FormBean formBean, String customParam, BusinessDataBean businessDataBean, Map<String, Object> resultMap) {
    if (Strings.isNotEmpty(customParam)) {
     Map<String, Object> customParamMap = (Map<String, Object>) JSONUtil.parseJSONString(customParam);
     if(customParamMap.size() > 0){
      resultMap.putAll(customParamMap);
      Map<String, Object> targetFormInfo = (Map<String, Object>) customParamMap.get("targetFormInfo");
      Map<String, Object> targetFormMap = (Map<String, Object>) targetFormInfo.get("targetForm");
      String formId = (String) targetFormMap.get("formId");
      targetFormMap.put("formId", businessDataBean.getRealId4Export(Long.valueOf(formId)).toString());
      String bindId = (String) targetFormMap.get("bindId");
      targetFormMap.put("bindId", businessDataBean.getRealId4Export(Long.valueOf(bindId)).toString());
     }
    }
}
/**
 * 在业务导入完之后,应用绑定自定义按钮的处理接口
 * @param formBean
 * @param customParam
 * @param businessDataBean
 * @param btnInfoMap
 */
@Override
public void importInfoAfterBizImport(FormBean formBean, String customParam, BusinessDataBean businessDataBean, Map<String, Object> btnInfoMap) {
    if (Strings.isNotEmpty(customParam)) {
     Map<String, Object> customParamMap = (Map<String, Object>) JSONUtil.parseJSONString(customParam);
     btnInfoMap.putAll(customParamMap);
     Map<String, Object> targetFormInfo = (Map<String, Object>) customParamMap.get("targetFormInfo");
     Map<String, Object> targetFormMap = (Map<String, Object>) targetFormInfo.get("targetForm");
     int targetType = Integer.parseInt(String.valueOf(targetFormMap.get("targetType")));
     if (targetType == 0) {
      //业务内表单新建
      String formId = (String) targetFormMap.get("formId");
      targetFormMap.put("formId", businessDataBean.getNewIdByOldId(Long.valueOf(formId)).toString());
      String bindId = (String) targetFormMap.get("bindId");
      targetFormMap.put("bindId", businessDataBean.getNewIdByOldId(Long.valueOf(bindId)).toString());
     } else {
      btnInfoMap.putAll(new HashMap<String, Object>());
     }
    } else {
     btnInfoMap.putAll(new HashMap<String, Object>());
    }
}

/**
 * 表单另存为应用绑定自定义按钮另存为接口,各个应用绑定自定义按钮需要处理自己的逻辑
 * @param formSaveAsBean
 * @param formBean
 * @param btnInfoMap
 */
public void otherSave(FormSaveAsBean formSaveAsBean, FormBean formBean, Map<String, Object> btnInfoMap){
    Long saveToBizId = formSaveAsBean.getSaveToBizId();
    Long oldBizId = formSaveAsBean.getOldBizId();
    //另存为选择为空说明是存为单表
    if(null == saveToBizId){
        //原来是应用中的表,清空
        if(null != oldBizId){
            btnInfoMap.put("customParam","");
        }
    }else{//存为应用中的表单
        if(null == oldBizId){//原来是单表
            btnInfoMap.put("customParam","");
        }else{//原来是应用中的表单
            if(!saveToBizId.equals(oldBizId)){//跨应用另存,清空
                btnInfoMap.put("customParam","");
            }
        }
    }

 }
}
编撰人:yinyanting