All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.activiti.service.editor.AppDefinitionImportService Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
package com.activiti.service.editor;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.servlet.http.HttpServletRequest;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import org.activiti.editor.language.json.converter.util.CollectionUtils;
import org.activiti.editor.language.json.model.ModelInfo;
import org.activiti.engine.identity.User;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.activiti.domain.editor.AbstractModel;
import com.activiti.domain.editor.AppDefinition;
import com.activiti.domain.editor.AppModelDefinition;
import com.activiti.domain.editor.Model;
import com.activiti.model.editor.AppDefinitionPublishRepresentation;
import com.activiti.model.editor.AppDefinitionRepresentation;
import com.activiti.model.editor.AppDefinitionUpdateResultRepresentation;
import com.activiti.repository.editor.ModelRepository;
import com.activiti.security.SecurityUtils;
import com.activiti.service.api.ModelService;
import com.activiti.service.exception.BadRequestException;
import com.activiti.service.exception.InternalServerErrorException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service
public class AppDefinitionImportService {

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

  @Autowired
  protected AppDefinitionPublishService appDefinitionPublishService;
  
  @Autowired
  protected ModelService modelService;
  
  @Autowired
  protected ModelRepository modelRepository;

  @Autowired
  protected ObjectMapper objectMapper;
  
  protected BpmnJsonConverter bpmnJsonConverter = new BpmnJsonConverter();

  public AppDefinitionRepresentation importAppDefinition(HttpServletRequest request, MultipartFile file) {
    try {
      InputStream is = file.getInputStream();
      String fileName = file.getOriginalFilename();
      return importAppDefinition(request, is, fileName, null, null, null, null);

    } catch (IOException e) {
      throw new InternalServerErrorException("Error loading file", e);
    }
  }

  public AppDefinitionRepresentation importAppDefinitionNewVersion(HttpServletRequest request, MultipartFile file, Long appDefId) {
    try {
      InputStream is = file.getInputStream();
      String fileName = file.getOriginalFilename();
      Model appModel = modelService.getModel(appDefId);
      if (appModel.getModelType().equals(Model.MODEL_TYPE_APP) == false) {
        throw new BadRequestException("No app definition found for id " + appDefId);
      }

      AppDefinitionRepresentation appDefinition = createAppDefinitionRepresentation(appModel);

      Map existingProcessModelMap = new HashMap();
      Map existingFormModelMap = new HashMap();
      Map existingDecisionTableMap = new HashMap();
      if (appDefinition.getDefinition() != null && CollectionUtils.isNotEmpty(appDefinition.getDefinition().getModels())) {
        for (AppModelDefinition modelDef : appDefinition.getDefinition().getModels()) {
          Model processModel = modelService.getModel(modelDef.getId());
          
          List referencedModels = modelRepository.findModelsByParentModelId(processModel.getId());
          for (Model childModel : referencedModels) {
            if (Model.MODEL_TYPE_FORM == childModel.getModelType()) {
              existingFormModelMap.put(childModel.getKey(), childModel);
              
            } else if (Model.MODEL_TYPE_DECISION_TABLE == childModel.getModelType()) {
              existingDecisionTableMap.put(childModel.getKey(), childModel);
            }
          }

          existingProcessModelMap.put(processModel.getKey(), processModel);
        }
      }

      return importAppDefinition(request, is, fileName, appModel, existingProcessModelMap, existingFormModelMap, existingDecisionTableMap);

    } catch (IOException e) {
      throw new InternalServerErrorException("Error loading file", e);
    }
  }

  protected AppDefinitionRepresentation importAppDefinition(HttpServletRequest request, InputStream is, String fileName, Model existingAppModel, Map existingProcessModelMap,
      Map existingFormModelMap, Map existingDecisionTableModelMap) {

    if (fileName != null && (fileName.endsWith(".zip"))) {
      Map formMap = new HashMap();
      Map decisionTableMap = new HashMap();
      Map bpmnModelMap = new HashMap();
      Map thumbnailMap = new HashMap();

      Model appDefinitionModel = readZipFile(is, formMap, decisionTableMap, bpmnModelMap, thumbnailMap);
      if (StringUtils.isNotEmpty(appDefinitionModel.getKey()) && StringUtils.isNotEmpty(appDefinitionModel.getModelEditorJson())) {

        Map formKeyAndModelMap = importForms(formMap, thumbnailMap, existingFormModelMap);
        Map decisionTableKeyAndModelMap = importDecisionTables(decisionTableMap, thumbnailMap, existingDecisionTableModelMap);
        Map bpmnModelIdAndModelMap = importBpmnModels(bpmnModelMap, formKeyAndModelMap, decisionTableKeyAndModelMap, 
            thumbnailMap, existingProcessModelMap);

        AppDefinitionRepresentation result = importAppDefinitionModel(appDefinitionModel, existingAppModel, bpmnModelIdAndModelMap);
        return result;

      } else {
        throw new BadRequestException("Could not find app definition json");
      }

    } else {
      throw new BadRequestException("Invalid file name, only .zip files are supported not " + fileName);
    }
  }

  public AppDefinitionUpdateResultRepresentation publishAppDefinition(Long modelId, AppDefinitionPublishRepresentation publishModel) {

    User user = SecurityUtils.getCurrentUserObject();
    Model appModel = modelService.getModel(modelId);

    // Create pojo representation of the model and the json
    AppDefinitionRepresentation appDefinitionRepresentation = createAppDefinitionRepresentation(appModel);
    AppDefinitionUpdateResultRepresentation result = new AppDefinitionUpdateResultRepresentation();

    // Actual publication
    appDefinitionPublishService.publishAppDefinition(publishModel.getComment(), appModel, user);

    result.setAppDefinition(appDefinitionRepresentation);
    return result;

  }

  protected AppDefinitionRepresentation createAppDefinitionRepresentation(AbstractModel model) {
    AppDefinition appDefinition = null;
    try {
      appDefinition = objectMapper.readValue(model.getModelEditorJson(), AppDefinition.class);
    } catch (Exception e) {
      logger.error("Error deserializing app " + model.getId(), e);
      throw new InternalServerErrorException("Could not deserialize app definition");
    }
    AppDefinitionRepresentation result = new AppDefinitionRepresentation(model);
    result.setDefinition(appDefinition);
    return result;
  }

  protected Model readZipFile(InputStream inputStream, Map formMap, Map decisionTableMap,
      Map bpmnModelMap, Map thumbnailMap) {

    Model appDefinitionModel = null;
    ZipInputStream zipInputStream = null;
    try {
      zipInputStream = new ZipInputStream(inputStream);
      ZipEntry zipEntry = zipInputStream.getNextEntry();
      while (zipEntry != null) {
        String zipEntryName = zipEntry.getName();
        if (zipEntryName.endsWith("json") || zipEntryName.endsWith("png")) {

          String modelFileName = null;
          if (zipEntryName.contains("/")) {
            modelFileName = zipEntryName.substring(zipEntryName.indexOf("/") + 1);
          } else {
            modelFileName = zipEntryName;
          }

          if (modelFileName.endsWith(".png")) {
            thumbnailMap.put(modelFileName.replace(".png", ""), IOUtils.toByteArray(zipInputStream));

          } else {
            modelFileName = modelFileName.replace(".json", "");
            String json = IOUtils.toString(zipInputStream);

            if (zipEntryName.startsWith("bpmn-models/")) {
              bpmnModelMap.put(modelFileName, json);

            } else if (zipEntryName.startsWith("form-models/")) {
              formMap.put(modelFileName, json);
              
            } else if (zipEntryName.startsWith("decision-table-models/")) {
              decisionTableMap.put(modelFileName, json);

            } else if (zipEntryName.contains("/") == false) {
              appDefinitionModel = createModelObject(json, Model.MODEL_TYPE_APP);
            }
          }
        }

        zipEntry = zipInputStream.getNextEntry();
      }
    } catch (Exception e) {
      logger.error("Error reading app definition zip file", e);
      throw new InternalServerErrorException("Error reading app definition zip file");

    } finally {
      if (zipInputStream != null) {
        try {
          zipInputStream.closeEntry();
        } catch (Exception e) {
        }
        try {
          zipInputStream.close();
        } catch (Exception e) {
        }
      }
    }

    return appDefinitionModel;
  }

  protected Map importForms(Map formMap, Map thumbnailMap, Map existingFormModelMap) {

    Map oldFormIdAndModelMap = new HashMap();

    for (String formKey : formMap.keySet()) {

      Model formModel = createModelObject(formMap.get(formKey), Model.MODEL_TYPE_FORM);
      Long oldFormId = formModel.getId();

      Model existingModel = null;
      if (existingFormModelMap != null && existingFormModelMap.containsKey(formModel.getKey())) {
        existingModel = existingFormModelMap.get(formModel.getKey());
      }

      Model updatedFormModel = null;
      if (existingModel != null) {
        byte[] imageBytes = null;
        if (thumbnailMap.containsKey(formKey)) {
          imageBytes = thumbnailMap.get(formKey);
        }
        updatedFormModel = modelService.saveModel(existingModel, formModel.getModelEditorJson(), imageBytes, 
            true, "App definition import", SecurityUtils.getCurrentUserObject());

      } else {
        formModel.setId(null);
        updatedFormModel = modelService.createModel(formModel, SecurityUtils.getCurrentUserObject());

        if (thumbnailMap.containsKey(formKey)) {
          updatedFormModel.setThumbnail(thumbnailMap.get(formKey));
          modelRepository.save(updatedFormModel);
        }
      }

      oldFormIdAndModelMap.put(oldFormId, updatedFormModel);
    }
    
    return oldFormIdAndModelMap;
  }
  
  protected Map importDecisionTables(Map decisionTableMap, Map thumbnailMap, 
      Map existingDecisionTableMap) {

    Map oldDecisionTableIdAndModelMap = new HashMap();

    for (String decisionTableKey : decisionTableMap.keySet()) {

      Model decisionTableModel = createModelObject(decisionTableMap.get(decisionTableKey), Model.MODEL_TYPE_DECISION_TABLE);
      Long oldDecisionTableId = decisionTableModel.getId();

      Model existingModel = null;
      if (existingDecisionTableMap != null && existingDecisionTableMap.containsKey(decisionTableModel.getKey())) {
        existingModel = existingDecisionTableMap.get(decisionTableModel.getKey());
      }

      Model updatedDecisionTableModel = null;
      if (existingModel != null) {
        byte[] imageBytes = null;
        if (thumbnailMap.containsKey(decisionTableKey)) {
          imageBytes = thumbnailMap.get(decisionTableKey);
        }
        updatedDecisionTableModel = modelService.saveModel(existingModel, decisionTableModel.getModelEditorJson(), imageBytes, 
            true, "App definition import", SecurityUtils.getCurrentUserObject());

      } else {
        decisionTableModel.setId(null);
        updatedDecisionTableModel = modelService.createModel(decisionTableModel, SecurityUtils.getCurrentUserObject());
        
        if (thumbnailMap.containsKey(decisionTableKey)) {
          updatedDecisionTableModel.setThumbnail(thumbnailMap.get(decisionTableKey));
          modelRepository.save(updatedDecisionTableModel);
        }
      }

      oldDecisionTableIdAndModelMap.put(oldDecisionTableId, updatedDecisionTableModel);
    }
    
    return oldDecisionTableIdAndModelMap;
  }

  protected Map importBpmnModels(Map bpmnModelMap, Map formKeyAndModelMap,
      Map decisionTableKeyAndModelMap, Map thumbnailMap, Map existingProcessModelMap) {

    Map bpmnModelIdAndModelMap = new HashMap();
    for (String bpmnModelKey : bpmnModelMap.keySet()) {
      
      Model existingModel = null;
      if (existingProcessModelMap != null && existingProcessModelMap.containsKey(bpmnModelKey)) {
        existingModel = existingProcessModelMap.get(bpmnModelKey);
      }

      String bpmnModelJson = bpmnModelMap.get(bpmnModelKey);
      Model bpmnModelObject = createModelObject(bpmnModelJson, Model.MODEL_TYPE_BPMN);
      Long oldBpmnModelId = bpmnModelObject.getId();
      
      JsonNode bpmnModelNode = null;
      try {
        bpmnModelNode = objectMapper.readTree(bpmnModelObject.getModelEditorJson());
      } catch (Exception e) {
        logger.error("Error reading BPMN json for " + bpmnModelKey, e);
        throw new InternalServerErrorException("Error reading BPMN json for " + bpmnModelKey);
      }
      
      Map oldFormIdFormKeyMap = new HashMap();
      Map formKeyModelIdMap = new HashMap();
      for (Long oldFormId : formKeyAndModelMap.keySet()) {
        Model formModel = formKeyAndModelMap.get(oldFormId);
        oldFormIdFormKeyMap.put(oldFormId, formModel.getKey());
        formKeyModelIdMap.put(formModel.getKey(), new ModelInfo(formModel.getId(), formModel.getName(), formModel.getKey()));
      }
      
      Map oldDecisionTableIdDecisionTableKeyMap = new HashMap();
      Map decisionTableKeyModelIdMap = new HashMap();
      for (Long oldDecisionTableId : decisionTableKeyAndModelMap.keySet()) {
        Model decisionTableModel = decisionTableKeyAndModelMap.get(oldDecisionTableId);
        oldDecisionTableIdDecisionTableKeyMap.put(oldDecisionTableId, decisionTableModel.getKey());
        decisionTableKeyModelIdMap.put(decisionTableModel.getKey(), new ModelInfo(decisionTableModel.getId(), 
            decisionTableModel.getName(), decisionTableModel.getKey()));
      }

      BpmnModel bpmnModel = bpmnJsonConverter.convertToBpmnModel(bpmnModelNode, oldFormIdFormKeyMap, oldDecisionTableIdDecisionTableKeyMap);
      String updatedBpmnJson = bpmnJsonConverter.convertToJson(bpmnModel, formKeyModelIdMap, decisionTableKeyModelIdMap).toString();

      Model updatedProcessModel = null;
      if (existingModel != null) {
        byte[] imageBytes = null;
        if (thumbnailMap.containsKey(bpmnModelKey)) {
          imageBytes = thumbnailMap.get(bpmnModelKey);
        }

        existingModel.setModelEditorJson(updatedBpmnJson);

        updatedProcessModel = modelService.saveModel(existingModel, existingModel.getModelEditorJson(), imageBytes, true, "App definition import", SecurityUtils.getCurrentUserObject());

      } else {
        bpmnModelObject.setId(null);
        bpmnModelObject.setModelEditorJson(updatedBpmnJson);
        updatedProcessModel = modelService.createModel(bpmnModelObject, SecurityUtils.getCurrentUserObject());
        
        if (thumbnailMap.containsKey(bpmnModelKey)) {
          updatedProcessModel.setThumbnail(thumbnailMap.get(bpmnModelKey));
          modelService.saveModel(updatedProcessModel);
        }
      }

      bpmnModelIdAndModelMap.put(oldBpmnModelId, updatedProcessModel);
    }

    return bpmnModelIdAndModelMap;
  }

  protected AppDefinitionRepresentation importAppDefinitionModel(Model appDefinitionModel, Model existingAppModel, Map bpmnModelIdAndModelMap) {
    
    AppDefinition appDefinition = null;
    try {
      appDefinition = objectMapper.readValue(appDefinitionModel.getModelEditorJson(), AppDefinition.class);
    } catch (Exception e) {
      logger.error("Error reading app definition " + appDefinitionModel.getName(), e);
      throw new BadRequestException("Error reading app definition", e);
    }
    
    for (AppModelDefinition modelDef : appDefinition.getModels()) {
      if (bpmnModelIdAndModelMap.containsKey(modelDef.getId())) {
        Model newModel = bpmnModelIdAndModelMap.get(modelDef.getId());
        modelDef.setId(newModel.getId());
        modelDef.setName(newModel.getName());
        modelDef.setCreatedBy(newModel.getCreatedBy());
        modelDef.setLastUpdatedBy(newModel.getLastUpdatedBy());
        modelDef.setLastUpdated(newModel.getLastUpdated());
        modelDef.setVersion(newModel.getVersion());
      }
    }

    try {
      String updatedAppDefinitionJson = objectMapper.writeValueAsString(appDefinition);

      if (existingAppModel != null) {
        appDefinitionModel = modelService.saveModel(existingAppModel, updatedAppDefinitionJson, null, true, "App definition import", SecurityUtils.getCurrentUserObject());
      } else {
        appDefinitionModel.setId(null);
        appDefinitionModel.setModelEditorJson(updatedAppDefinitionJson);
        appDefinitionModel = modelService.createModel(appDefinitionModel, SecurityUtils.getCurrentUserObject());
      }
      
      AppDefinitionRepresentation result = new AppDefinitionRepresentation(appDefinitionModel);
      result.setDefinition(appDefinition);
      return result;

    } catch (Exception e) {
      logger.error("Error storing app definition", e);
      throw new InternalServerErrorException("Error storing app definition");
    }
  }

  protected Model createModelObject(String modelJson, int modelType) {
    try {
      JsonNode modelNode = objectMapper.readTree(modelJson);
      Model model = new Model();
      model.setId(modelNode.get("id").asLong());
      model.setName(modelNode.get("name").asText());
      model.setKey(modelNode.get("key").asText());
      
      JsonNode descriptionNode = modelNode.get("description");
      if (descriptionNode != null && descriptionNode.isNull() == false) {
        model.setDescription(descriptionNode.asText());
      }
      
      model.setModelEditorJson(modelNode.get("editorJson").toString());
      model.setModelType(modelType);
      
      return model;
      
    } catch (Exception e) {
      logger.error("Error reading model json", e);
      throw new InternalServerErrorException("Error reading model json");
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy