Activiti Modeler和业务系统整合

前言

本博客是将modeler单独出来和业务系统整合的方案,希望对使用activiti的童鞋有所帮助

文件整合

在src/main/resource中添加modeler配置文件
editor.html,plugins.xml,stencilset.json,ui.properties,ui.properties.alfresco

在src/main/webapp中添加modeler文件夹
文件夹中包括modeler中的api,diagram-viewer,editor,explorer,libs文件夹

修改web.xml

<servlet >
        <servlet-name> ExplorerRestletServlet</servlet-name >
        <servlet-class> org.restlet.ext.servlet.ServerServlet</servlet-class >
        <init-param>
            <!-- Application class name -->
            <param-name> org.restlet.application</param-name >
            <param-value> com.isprint.ssf.controller.ExplorerRestApplication</param-value >
        </init-param>
    </servlet >

    <servlet >
        <servlet-name> RestletServlet</servlet-name >
        <servlet-class> org.restlet.ext.servlet.ServerServlet</servlet-class >
        <init-param>
            <!-- Application class name -->
            <param-name> org.restlet.application</param-name >
            <param-value> org.activiti.rest.service.application.ActivitiRestServicesApplication </param-value>
        </init-param>
    </servlet >

        <!-- Catch all service requests -->
        <servlet-mapping>
        <servlet-name> RestletServlet</servlet-name >
        <url-pattern> /rest/*</ url-pattern>
    </servlet-mapping >
        <servlet-mapping>
               <servlet-name> ExplorerRestletServlet</servlet-name >
               <url-pattern> /modeler/service/*</url-pattern >
        </servlet-mapping>

封装API

1、添加ExplorerRestApplication 类

public class ExplorerRestApplication extends ActivitiRestApplication {

  public ExplorerRestApplication() {
    super();
  }
  /** * Creates a root Restlet that will receive all incoming calls. */
  @Override
  public synchronized Restlet createInboundRoot() {
    Router router = new Router(getContext());
    router.attachDefault(DefaultResource.class);
    ModelerServicesInit.attachResources(router);
    JsonpFilter jsonpFilter = new JsonpFilter(getContext());
    jsonpFilter.setNext(router);
    return jsonpFilter;
  }

}

2、添加ModelController类,完成流程创建、部署、编辑、导出、删除操作。

/** * 流程模型控制器 * * @author */
@Controller
@RequestMapping(value = "/model")
public class ModelController {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    RepositoryService repositoryService;

    @Autowired
     ModelService modelService;

    @RequestMapping(value = "list")
    @ResponseBody
    public Map<String, Object>  modelList(ModelDto modelDto, HttpServletRequest request) {

         return modelService.queryModel(modelDto);
    }


    /** * 创建模型 */
    @RequestMapping("create")
    public void create(ModelDto modelDto, HttpServletRequest request, HttpServletResponse response) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode editorNode = objectMapper.createObjectNode();
            editorNode.put("id", "canvas");
            editorNode.put("resourceId", "canvas");
            ObjectNode stencilSetNode = objectMapper.createObjectNode();
            stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
            editorNode.put("stencilset", stencilSetNode);
            Model modelData = repositoryService.newModel();

            ObjectNode modelObjectNode = objectMapper.createObjectNode();
            modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, modelDto.getName());
            modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
            String description = StringUtils.defaultString(modelDto.getDescription());
            modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
            modelData.setMetaInfo(modelObjectNode.toString());
            modelData.setName(modelDto.getName());
            modelData.setKey(StringUtils.defaultString(modelDto.getKey()));

            //保存模型
            repositoryService.saveModel(modelData);
            repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));
            response.sendRedirect(request.getContextPath() + "/modeler/service/editor?id=" + modelData.getId());
        } catch (Exception e) {
            logger.error("创建模型失败:", e);
        }
    }

    /** * 根据Model部署流程 */
    @RequestMapping(value = "deploy/{modelId}")
    @ResponseBody
    public String deploy(@PathVariable("modelId") String modelId, RedirectAttributes redirectAttributes) {
        try {
            Model modelData = repositoryService.getModel(modelId);
            ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));
            byte[] bpmnBytes = null;

            BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
            bpmnBytes = new BpmnXMLConverter().convertToXML(model);
            String processName = modelData.getName() + ".bpmn20.xml";
            repositoryService.createDeployment().name(modelData.getName()).addString(processName, new String(bpmnBytes,"UTF-8")).deploy();
            //redirectAttributes.addFlashAttribute("message", "部署成功,部署ID=" + deployment.getId());
        } catch (Exception e) {
            logger.error("根据模型部署流程失败:modelId={}", modelId, e);
        }
        return "deployesuccess";
    }

    /** * 导出model的xml文件 */
    @RequestMapping(value = "export/{modelId}")
    public void export(@PathVariable("modelId") String modelId, HttpServletResponse response) {
        try {
            Model modelData = repositoryService.getModel(modelId);
            BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
            JsonNode editorNode = new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));
            BpmnModel bpmnModel = jsonConverter.convertToBpmnModel(editorNode);
            BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
            byte[] bpmnBytes = xmlConverter.convertToXML(bpmnModel);

            ByteArrayInputStream in = new ByteArrayInputStream(bpmnBytes);
            IOUtils.copy(in, response.getOutputStream());
            String filename = bpmnModel.getMainProcess().getId() + ".bpmn20.xml";
            response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            response.flushBuffer();
        } catch (Exception e) {
            logger.error("导出model的xml文件失败:modelId={}", modelId, e);
        }
    }

    @RequestMapping(value = "delete/{modelId}")
    @ResponseBody
    public String delete(@PathVariable("modelId") String modelId) {
        repositoryService.deleteModel(modelId);
        return "deletesuccess";
    }


}

3、添加Service、Dao类
ModelService接口


public interface ModelService {

     public Map<String,Object> queryModel(ModelDto modelDto);
}

Service实现


@Service
@Transactional
public class ModelServiceImpl implements ModelService {

     public static final Logger logger = Logger
               .getLogger(ModelServiceImpl.class);

     @Autowired
     private IModelDao modelDao;

     public Map<String,Object> queryModel(ModelDto modelDto){
          Map<String,Object> map = new HashMap<String,Object>();
          Map<String,String> paramMap=new HashMap<String,String>();
          if(StringUtils.isNotBlank(modelDto.getId())){
               paramMap.put("id", modelDto.getId());
          }
          if(StringUtils.isNotBlank(modelDto.getKey())){
               paramMap.put("key", modelDto.getKey());
          }
          if(StringUtils.isNotBlank(modelDto.getName())){
               paramMap.put("name", modelDto.getName());
          }

          List<ModelDto> modelDtoList=modelDao.getModelByQuery(paramMap, modelDto.getPage(), modelDto.getRows());
          map.put("total", modelDao.getModelCountByQuery(paramMap));
          map.put("rows", modelDtoList);
          return map;
     }
}

Dao接口

import java.util.List;
import java.util.Map;

public interface IModelDao {

     public List<ModelDto> getModelByQuery(Map<String,String> paramMap, int page, int rows);

     public int getModelCountByQuery(Map<String,String> paramMap);
}

Dao实现

@Autowired
     DirectlyDBService db;

     public static final Logger log = Logger.getLogger(ModelDaoImpl.class);

     @Override
     public List<ModelDto> getModelByQuery(Map<String, String> paramMap,
               int page, int rows) {
          int firstRow;
          int maxRow;
          if (page > 1) {
               firstRow = (page - 1) * rows + 1;
               maxRow = page * rows;
          } else {
               firstRow = 1;
               maxRow = firstRow * rows;
          }

          StringBuilder sb = new StringBuilder();
          sb.append("select * from ( ");
          sb.append(" select t.*,ROWNUM rn from (");
          sb.append(" select * from ACT_RE_MODEL where 1=1 ");

          if (paramMap.containsKey("id")) {
               sb.append(" and id_ ='" + paramMap.get("id") + "'");
          }
          if (paramMap.containsKey("name")) {
               sb.append(" and name_ like '%" + paramMap.get("name") + "%'");
          }
          if (paramMap.containsKey("key")) {
               sb.append(" and key_ like '%" + paramMap.get("key") + "%'");
          }

          sb.append(" ORDER BY create_time_ DESC) t)");
          sb.append(" where rn >=" + firstRow + " and rn<=" + maxRow);
          List<ModelDto> modelDtoList = new ArrayList<ModelDto>();
          try {

               List<Map<String, Object>> list = db.listBySql(sb.toString());

               for (Map<String, Object> dbMap : list) {
                    ModelDto modelDto = new ModelDto();

                    modelDto.setId(dbMap.get("ID_").toString());
                    modelDto.setName(dbMap.get("NAME_") == null ? "" : dbMap.get(
                              "NAME_").toString());
                    modelDto.setRev(dbMap.get("REV_") == null ? 0 : Integer
                              .parseInt(dbMap.get("REV_").toString()));
                    modelDto.setKey(dbMap.get("KEY_") == null ? "" : dbMap.get(
                              "KEY_").toString());
                    modelDto.setCategory(dbMap.get("CATEGORY_") == null ? ""
                              : dbMap.get("CATEGORY_").toString());
                    modelDto.setVersion(dbMap.get("VERSION_") == null ? 0 : Integer
                              .parseInt(dbMap.get("VERSION_").toString()));
                    modelDto.setMetaInfo(dbMap.get("META_INFO_") == null ? ""
                              : dbMap.get("META_INFO_").toString());
                    modelDto.setDeploymentId(dbMap.get("DEPLOYMENT_ID_") == null ? ""
                              : dbMap.get("DEPLOYMENT_ID_").toString());
                    modelDto.setEditorSourceValueId(dbMap
                              .get("EDITOR_SOURCE_VALUE_ID_") == null ? "" : dbMap
                              .get("EDITOR_SOURCE_VALUE_ID_").toString());
                    modelDto.setEditorSourceExtraValueId(dbMap
                              .get("EDITOR_SOURCE_EXTRA_VALUE_ID_") == null ? ""
                              : dbMap.get("EDITOR_SOURCE_EXTRA_VALUE_ID_").toString());
                    modelDto.setTenantId(dbMap.get("TENANT_ID_") == null ? ""
                              : dbMap.get("TENANT_ID_").toString());
                    modelDto.setCreateTime(dbMap.get("CREATE_TIME_") == null ? ""
                              : dbMap.get("CREATE_TIME_").toString());
                    modelDto.setLastUpdateTime(dbMap.get("LAST_UPDATE_TIME_") == null ? ""
                              :dbMap.get("LAST_UPDATE_TIME_").toString());

                    modelDtoList.add(modelDto);
               }
          } catch (Exception e) {
               log.error("ModelDao-getModelByQuery", e);
          }
          return modelDtoList;
     }

     @Override
     public int getModelCountByQuery(Map<String, String> paramMap) {
          StringBuilder sb = new StringBuilder();
          sb.append("select count(*) as MODELCOUNT from ( ");
          sb.append(" select t.* from (");
          sb.append(" select * from ACT_RE_MODEL where 1=1 ");

          if (paramMap.containsKey("id")) {
               sb.append(" and id_ ='" + paramMap.get("id") + "'");
          }
          if (paramMap.containsKey("name")) {
               sb.append(" and name_ like '%" + paramMap.get("name") + "%'");
          }
          if (paramMap.containsKey("key")) {
               sb.append(" and key_ like '%" + paramMap.get("key") + "%'");
          }

          sb.append(" ORDER BY id_ DESC) t)");

          System.out.println(sb);
          List<Map<String, Object>> list = db.listBySql(sb.toString());
          if (list != null && !list.isEmpty()) {
               Map<String, Object> listMap = list.get(0);
               return Integer.valueOf(listMap.get("MODELCOUNT").toString())
                         .intValue();
          } else {
               return 0;
          }
     }

总结

通过上面的操作之后,就已经准备好Modeler使用的环境了,如果业务系统想集成此流程设计器的话,就需要调用提供的方法即可。

你可能感兴趣的:(Activiti)