@Override
  public RunResult dataDictionary(GeneratorInstanceDataDictionaryRequest request) {
    RunResult runResult = new RunResult();
    Authentication authentication = request.getAuthentication();
    Long userId = authentication.getUserId();

    Long id = request.getId();
    GeneratorInstance generatorInstance = generatorInstanceRepository.selectById(id);
    if (generatorInstance == null) {
      throw new AppException("实例不存在");
    }

    if (!userId.equals(generatorInstance.getUser().getId())) {
      throw new AppException("权限不足");
    }

    Long generatorId = generatorInstance.getGenerator().getId();
    Generator generator = generatorRepository.selectById(generatorId);
    if (generator == null) {
      throw new AppException("生成器不存在");
    }

    if (generatorInstance.getVersion() < generator.getVersion()) {
      throw new AppException("当前生成器已升级发布,请刷新数据,重新操作!");
    }

    if (!generator.getIsOpen() && !generator.getDeveloper().getId().equals(userId)) {
      throw new AppException("当前生成器正在维护,请暂停操作,等待发布!");
    }

    DynamicModelQueryRequest dynamicModelQueryRequest = new DynamicModelQueryRequest();
    dynamicModelQueryRequest.setGeneratorId(generatorId);
    dynamicModelQueryRequest.setAuthentication(authentication);

    List<DynamicModel> dynamicModels = dynamicModelService.query(dynamicModelQueryRequest);
    Map<Long, Map<String, Object>> dynamicModelGroupData = new HashMap<>();
    Map<Long, DynamicModel> dynamicModelCache = new HashMap<>();
    Map<Long, Map<String, Map<String, Set<String>>>> dynamicModelKeysCache = new HashMap<>();
    dynamicModels.forEach(
        dynamicModel -> {
          Map<String, Object> groupData = new HashMap<>();
          List<Map<String, Object>> fromGroups = new ArrayList<>();
          groupData.put("fromGroups", fromGroups);
          List<Map<String, Object>> headGroups = new ArrayList<>();
          groupData.put("headGroups", headGroups);
          List<DynamicProperty> heads = new ArrayList<>();
          groupData.put("heads", heads);

          DynamicModel newDynamicModel = new DynamicModel();
          newDynamicModel.setId(dynamicModel.getId());
          newDynamicModel.setName(dynamicModel.getName());
          newDynamicModel.setIcon(dynamicModel.getIcon());

          Map<String, Map<String, Set<String>>> keysCache = new HashMap<>();
          Map<String, Set<String>> propertiesKeys = new HashMap<>();
          Set<String> propertiesKeys_dateTypeKeys = new HashSet<>();
          Set<String> propertiesKeys_dataModelTypeKeys = new HashSet<>();
          propertiesKeys.put("dateTypeKeys", propertiesKeys_dateTypeKeys);
          propertiesKeys.put("dataModelTypeKeys", propertiesKeys_dataModelTypeKeys);
          Map<String, Set<String>> associationKeys = new HashMap<>();
          Set<String> associationKeys_dateTypeKeys = new HashSet<>();
          Set<String> associationKeys_dataModelTypeKeys = new HashSet<>();
          associationKeys.put("dateTypeKeys", associationKeys_dateTypeKeys);
          associationKeys.put("dataModelTypeKeys", associationKeys_dataModelTypeKeys);
          keysCache.put("propertiesKeys", propertiesKeys);
          keysCache.put("associationKeys", associationKeys);
          dynamicModelKeysCache.put(dynamicModel.getId(), keysCache);

          List<DynamicProperty> properties = dynamicModel.getProperties();
          properties.forEach(
              property -> {
                if ("Date".equals(property.getType())) {
                  propertiesKeys_dateTypeKeys.add(property.getName());
                } else if ("Model".equals(property.getType())) {
                  propertiesKeys_dataModelTypeKeys.add(property.getName());
                }

                DynamicProperty newDynamicProperty = new DynamicProperty();
                newDynamicProperty.setId(property.getId());
                newDynamicProperty.setLabel(property.getLabel());
                newDynamicProperty.setName(property.getName());
                newDynamicProperty.setViewWidth(property.getViewWidth());
                newDynamicProperty.setType(property.getType());
                newDynamicProperty.setDefaultValue(property.getDefaultValue());
                newDynamicModel.getProperties().add(newDynamicProperty);

                String group = property.getGroup();
                Map<String, Object> groupMap = new HashMap<>();
                if (!StringUtils.hasText(group)) {
                  groupMap.put("property", property);
                  fromGroups.add(groupMap);
                } else {
                  Map<String, Object> prevFromGroup =
                      fromGroups.isEmpty() ? null : fromGroups.get(fromGroups.size() - 1);
                  String prevGroup =
                      prevFromGroup == null
                          ? null
                          : (prevFromGroup.get("group") == null
                              ? null
                              : prevFromGroup.get("group").toString());
                  if (prevFromGroup == null || !group.equals(prevGroup)) {
                    groupMap.put("group", group);
                    List<DynamicProperty> children = new ArrayList<>();
                    children.add(property);
                    groupMap.put("children", children);
                    fromGroups.add(groupMap);
                  }
                  if (prevFromGroup != null && group.equals(prevGroup)) {
                    List<DynamicProperty> children =
                        (List<DynamicProperty>) prevFromGroup.get("children");
                    children.add(property);
                  }
                }
              });

          List<DynamicProperty> association = dynamicModel.getAssociation();
          association.forEach(
              property -> {
                if ("Date".equals(property.getType())) {
                  associationKeys_dateTypeKeys.add(property.getName());
                } else if ("Model".equals(property.getType())) {
                  associationKeys_dataModelTypeKeys.add(property.getName());
                }

                DynamicProperty newDynamicProperty = new DynamicProperty();
                newDynamicProperty.setId(property.getId());
                newDynamicProperty.setLabel(property.getLabel());
                newDynamicProperty.setName(property.getName());
                newDynamicProperty.setViewWidth(property.getViewWidth());
                newDynamicProperty.setType(property.getType());
                newDynamicProperty.setDefaultValue(property.getDefaultValue());
                newDynamicModel.getAssociation().add(newDynamicProperty);

                String group = property.getGroup();
                Map<String, Object> groupMap = new HashMap<>();
                if (!StringUtils.hasText(group)) {
                  groupMap.put("property", property);
                  headGroups.add(groupMap);
                } else {
                  Map<String, Object> prevHeadGroup =
                      headGroups.isEmpty() ? null : headGroups.get(headGroups.size() - 1);
                  String prevGroup =
                      prevHeadGroup == null
                          ? null
                          : (prevHeadGroup.get("group") == null
                              ? null
                              : prevHeadGroup.get("group").toString());
                  if (prevHeadGroup == null || !group.equals(prevGroup)) {
                    groupMap.put("group", group);
                    groupMap.put("span", 1);
                    headGroups.add(groupMap);
                  }
                  if (prevHeadGroup != null && group.equals(prevGroup)) {
                    Integer span = (Integer) prevHeadGroup.get("span");
                    prevHeadGroup.put("span", span + 1);
                  }
                  heads.add(property);
                }
              });
          dynamicModelCache.put(newDynamicModel.getId(), newDynamicModel);
          dynamicModelGroupData.put(newDynamicModel.getId(), groupData);
        });

    List<Long> excludeIds = request.getExcludeIds();
    Map<Long, Long> excludeIdCache = new HashMap<>();
    excludeIds.forEach(excludeId -> excludeIdCache.put(excludeId, excludeId));

    Long dataModelId = generatorInstance.getDataModel().getId();
    DataModel rootDataModel = dataModelRepository.selectById(dataModelId);

    Map<Long, DataModel> dataModelSourceCache = new HashMap<>();
    Map<Long, DataModel> dataModelTargetCache = new HashMap<>();
    LinkedList<DataModel> stack = new LinkedList<>();
    stack.push(rootDataModel);
    while (!stack.isEmpty()) {
      DataModel dataModel = stack.pop();
      dataModelSourceCache.put(dataModel.getId(), dataModel);
      dataModelTargetCache.put(dataModel.getId(), new DataModel());
      dataModel.getChildren().forEach(stack::push);
    }
    dataModelSourceCache.forEach(
        (dataModelSourceId, dataModelSource) -> {
          DataModel dataModelTarget = dataModelTargetCache.get(dataModelSourceId);
          dataModelTarget.setId(dataModelSource.getId());
          dataModelTarget.setName(dataModelSource.getName());
          if (dataModelSource.getParent() != null) {
            Long parentId = dataModelSource.getParent().getId();
            DataModel parentSource = dataModelSourceCache.get(parentId);
            if (parentSource != null) {
              DataModel parentTarget = dataModelTargetCache.get(parentId);
              dataModelTarget.setParent(parentTarget);
            }
          }
          dataModelSource
              .getChildren()
              .forEach(
                  child -> {
                    Long childId = child.getId();
                    if (!excludeIdCache.containsKey(childId)) {
                      dataModelTarget.getChildren().add(dataModelTargetCache.get(childId));
                    }
                  });

          if (!dataModelSource.equals(rootDataModel)) {
            DynamicModel dynamicModel =
                dynamicModelCache.get(dataModelSource.getDynamicModel().getId());
            dataModelTarget.setDynamicModel(dynamicModel);

            Map<String, Map<String, Set<String>>> keysCache =
                dynamicModelKeysCache.get(dynamicModel.getId());
            Map<String, Set<String>> propertiesKeys = keysCache.get("propertiesKeys");
            Map<String, Set<String>> associationKeys = keysCache.get("associationKeys");
            Set<String> propertiesKeys_dateTypeKeys = propertiesKeys.get("dateTypeKeys");
            Set<String> propertiesKeys_dataModelTypeKeys = propertiesKeys.get("dataModelTypeKeys");
            Set<String> associationKeys_dateTypeKeys = associationKeys.get("dateTypeKeys");
            Set<String> associationKeys_dataModelTypeKeys =
                associationKeys.get("dataModelTypeKeys");
            dataModelSource
                .getProperties()
                .forEach(
                    (name, value) -> {
                      try {
                        if (propertiesKeys_dateTypeKeys.contains(name)) {
                          dataModelTarget.getProperties().put(name, new Date((Long) value));
                        } else if (propertiesKeys_dataModelTypeKeys.contains(name)) {
                          dataModelTarget
                              .getProperties()
                              .put(name, dataModelTargetCache.get(value));
                        } else {
                          dataModelTarget.getProperties().put(name, value);
                        }
                      } catch (Exception e) {
                        //
                      }
                    });
            dataModelSource
                .getAssociation()
                .forEach(
                    property -> {
                      Map<String, Object> newProperty = new LinkedHashMap<>();
                      property.forEach(
                          (name, value) -> {
                            try {
                              if (associationKeys_dateTypeKeys.contains(name)) {
                                newProperty.put(name, new Date((Long) value));
                              } else if (associationKeys_dataModelTypeKeys.contains(name)) {
                                newProperty.put(name, dataModelTargetCache.get(value));
                              } else {
                                newProperty.put(name, value);
                              }
                            } catch (Exception e) {
                              //
                            }
                          });
                      dataModelTarget.getAssociation().add(newProperty);
                    });
          }
        });

    Long generateId = idWorker.nextId();
    ComparisonDateTool comparisonDateTool = new ComparisonDateTool();
    Map<String, String> themeIndex = new HashMap<>();
    themeIndex.put("cerulean", "cerulean");
    themeIndex.put("cosmo", "cosmo");
    themeIndex.put("cyborg", "cyborg");
    themeIndex.put("darkly", "darkly");
    themeIndex.put("default", "default");
    themeIndex.put("flatly", "flatly");
    themeIndex.put("journal", "journal");
    themeIndex.put("lumen", "lumen");
    themeIndex.put("paper", "paper");
    themeIndex.put("readable", "readable");
    themeIndex.put("sandstone", "sandstone");
    themeIndex.put("simplex", "simplex");
    themeIndex.put("slate", "slate");
    themeIndex.put("spacelab", "spacelab");
    themeIndex.put("superhero", "superhero");
    themeIndex.put("united", "united");
    themeIndex.put("yeti", "yeti");
    String theme = themeIndex.get(request.getTheme());
    theme = theme == null ? "slate" : theme;

    Global global = new Global();
    global.setGenerateId(generateId);
    global.setTheme(theme);

    User userClone = new User();
    userClone.setId(userId);
    userClone.setUserName(authentication.getUserName());
    global.setUser(userClone);

    GeneratorInstance generatorInstanceClone = new GeneratorInstance();
    generatorInstanceClone.setId(generatorInstance.getId());
    generatorInstanceClone.setName(generatorInstance.getName());
    global.setGeneratorInstance(generatorInstanceClone);

    DataModel rootDataModelClone = dataModelTargetCache.get(rootDataModel.getId());
    String generatePath =
        ConfigProperties.TEMPORARY_PATH
            + ConfigProperties.fileSeparator
            + userId
            + ConfigProperties.fileSeparator
            + generatorInstance.getName()
            + "("
            + generateId
            + ")";

    String indexOutFile = generatePath + ConfigProperties.fileSeparator + "index.html";
    Map<String, Object> indexVarMap = new HashMap<>();
    indexVarMap.put("global", global);
    indexVarMap.put("date", comparisonDateTool);
    indexVarMap.put("data", rootDataModelClone);
    FileUtil.mkdirs(new File(indexOutFile).getParent());
    VelocityUtil.mergeWrite(
        ConfigProperties.ROOT_PATH + ConfigProperties.fileSeparator + "client/template",
        "index.html.vm",
        indexOutFile,
        indexVarMap);

    FileUtil.mkdirs(generatePath + ConfigProperties.fileSeparator + "library");
    FileUtil.copyFile(
        new File(
            ConfigProperties.ROOT_PATH
                + ConfigProperties.fileSeparator
                + "client/library/jquery/jquery.min.js"),
        new File(generatePath + ConfigProperties.fileSeparator + "library/jquery.min.js"));
    FileUtil.copyFile(
        new File(
            ConfigProperties.ROOT_PATH
                + ConfigProperties.fileSeparator
                + "client/library/bootstrap/js/bootstrap.min.js"),
        new File(generatePath + ConfigProperties.fileSeparator + "library/bootstrap.min.js"));
    FileUtil.copyFile(
        new File(
            ConfigProperties.ROOT_PATH
                + ConfigProperties.fileSeparator
                + "client/library/angular/angular.min.js"),
        new File(generatePath + ConfigProperties.fileSeparator + "library/angular.min.js"));

    FileUtil.copyFile(
        new File(
            ConfigProperties.ROOT_PATH
                + ConfigProperties.fileSeparator
                + "client/library/bootstrap/theme/"
                + theme
                + ".min.css"),
        new File(generatePath + ConfigProperties.fileSeparator + "library/" + theme + ".min.css"));

    FileUtil.mkdirs(generatePath + ConfigProperties.fileSeparator + "fonts");
    File[] fontFiles =
        new File(
                ConfigProperties.ROOT_PATH
                    + ConfigProperties.fileSeparator
                    + "client/library/bootstrap/fonts")
            .listFiles();
    for (File fontFile : fontFiles) {
      FileUtil.copyFile(
          new File(
              ConfigProperties.ROOT_PATH
                  + ConfigProperties.fileSeparator
                  + "client/library/bootstrap/fonts/"
                  + fontFile.getName()),
          new File(generatePath + ConfigProperties.fileSeparator + "fonts/" + fontFile.getName()));
    }

    LinkedList<DataModel> dataStack = new LinkedList<>();
    dataStack.push(rootDataModelClone);
    FileUtil.mkdirs(generatePath + ConfigProperties.fileSeparator + "data");
    while (!dataStack.isEmpty()) {
      DataModel dataModel = dataStack.pop();
      if (dataModel != rootDataModelClone) {
        String dataModelOutFile =
            generatePath
                + ConfigProperties.fileSeparator
                + "data"
                + ConfigProperties.fileSeparator
                + dataModel.getId()
                + ".html";
        Map<String, Object> dataModelVarMap = new HashMap<>();
        dataModelVarMap.put("global", global);
        dataModelVarMap.put("date", comparisonDateTool);
        dataModelVarMap.put("dataModel", dataModel);
        dataModelVarMap.put("dynamicModelGroupData", dynamicModelGroupData);
        VelocityUtil.mergeWrite(
            ConfigProperties.ROOT_PATH + ConfigProperties.fileSeparator + "client/template",
            "dataModel.html.vm",
            dataModelOutFile,
            dataModelVarMap);
      }
      dataModel.getChildren().forEach(dataStack::push);
    }

    File generateFolder = new File(generatePath);
    try {
      ZipUtil.compress(generateFolder);
    } catch (Exception e) {
      throw new AppException(e, "压缩文件失败");
    }
    FileUtil.deleteFile(generateFolder);
    runResult.setUrl(userId + "/" + generatorInstance.getName() + "(" + generateId + ").zip");
    runResult.setFileName(generatorInstance.getName() + "(" + generateId + ").zip");
    return runResult;
  }
  @Override
  public RunResult run(GeneratorInstanceRunRequest request) {
    RunResult runResult = new RunResult();
    Authentication authentication = request.getAuthentication();
    Long userId = authentication.getUserId();

    Long id = request.getId();
    GeneratorInstance generatorInstance = generatorInstanceRepository.selectById(id);
    if (generatorInstance == null) {
      throw new AppException("实例不存在");
    }

    if (!userId.equals(generatorInstance.getUser().getId())) {
      throw new AppException("权限不足");
    }

    Long generatorId = generatorInstance.getGenerator().getId();
    Generator generator = generatorRepository.selectById(generatorId);
    if (generator == null) {
      throw new AppException("生成器不存在");
    }

    if (generatorInstance.getVersion() < generator.getVersion()) {
      throw new AppException("当前生成器已升级发布,请刷新数据,重新操作!");
    }

    if (!generator.getIsOpen() && !generator.getDeveloper().getId().equals(userId)) {
      throw new AppException("当前生成器正在维护,请暂停操作,等待发布!");
    }

    Long templateStrategyId = request.getTemplateStrategyId();
    TemplateStrategy templateStrategy = templateStrategyRepository.selectById(templateStrategyId);

    DynamicModelQueryRequest dynamicModelQueryRequest = new DynamicModelQueryRequest();
    dynamicModelQueryRequest.setGeneratorId(generatorId);
    dynamicModelQueryRequest.setAuthentication(authentication);
    List<DynamicModel> dynamicModels = dynamicModelService.query(dynamicModelQueryRequest);
    Map<Long, DynamicModel> dynamicModelCache = new HashMap<>();
    Map<Long, Map<String, Map<String, Set<String>>>> dynamicModelKeysCache = new HashMap<>();
    dynamicModels.forEach(
        dynamicModel -> {
          DynamicModel newDynamicModel = new DynamicModel();
          newDynamicModel.setId(dynamicModel.getId());
          newDynamicModel.setName(dynamicModel.getName());
          newDynamicModel.setIcon(dynamicModel.getIcon());

          Map<String, Map<String, Set<String>>> keysCache = new HashMap<>();
          Map<String, Set<String>> propertiesKeys = new HashMap<>();
          Set<String> propertiesKeys_dateTypeKeys = new HashSet<>();
          Set<String> propertiesKeys_dataModelTypeKeys = new HashSet<>();
          propertiesKeys.put("dateTypeKeys", propertiesKeys_dateTypeKeys);
          propertiesKeys.put("dataModelTypeKeys", propertiesKeys_dataModelTypeKeys);
          Map<String, Set<String>> associationKeys = new HashMap<>();
          Set<String> associationKeys_dateTypeKeys = new HashSet<>();
          Set<String> associationKeys_dataModelTypeKeys = new HashSet<>();
          associationKeys.put("dateTypeKeys", associationKeys_dateTypeKeys);
          associationKeys.put("dataModelTypeKeys", associationKeys_dataModelTypeKeys);
          keysCache.put("propertiesKeys", propertiesKeys);
          keysCache.put("associationKeys", associationKeys);
          dynamicModelKeysCache.put(dynamicModel.getId(), keysCache);

          List<DynamicProperty> properties = dynamicModel.getProperties();
          properties.forEach(
              property -> {
                if ("Date".equals(property.getType())) {
                  propertiesKeys_dateTypeKeys.add(property.getName());
                } else if ("Model".equals(property.getType())) {
                  propertiesKeys_dataModelTypeKeys.add(property.getName());
                }

                DynamicProperty newDynamicProperty = new DynamicProperty();
                newDynamicProperty.setId(property.getId());
                newDynamicProperty.setLabel(property.getLabel());
                newDynamicProperty.setName(property.getName());
                newDynamicProperty.setViewWidth(property.getViewWidth());
                newDynamicProperty.setType(property.getType());
                newDynamicProperty.setDefaultValue(property.getDefaultValue());
                newDynamicModel.getProperties().add(newDynamicProperty);
              });
          List<DynamicProperty> association = dynamicModel.getAssociation();
          association.forEach(
              property -> {
                if ("Date".equals(property.getType())) {
                  associationKeys_dateTypeKeys.add(property.getName());
                } else if ("Model".equals(property.getType())) {
                  associationKeys_dataModelTypeKeys.add(property.getName());
                }

                DynamicProperty newDynamicProperty = new DynamicProperty();
                newDynamicProperty.setId(property.getId());
                newDynamicProperty.setLabel(property.getLabel());
                newDynamicProperty.setName(property.getName());
                newDynamicProperty.setViewWidth(property.getViewWidth());
                newDynamicProperty.setType(property.getType());
                newDynamicProperty.setDefaultValue(property.getDefaultValue());
                newDynamicModel.getAssociation().add(newDynamicProperty);
              });
          dynamicModelCache.put(newDynamicModel.getId(), newDynamicModel);
        });

    List<Long> excludeIds = request.getExcludeIds();
    Map<Long, Long> excludeIdCache = new HashMap<>();
    excludeIds.forEach(excludeId -> excludeIdCache.put(excludeId, excludeId));

    Long dataModelId = generatorInstance.getDataModel().getId();
    DataModel rootDataModel = dataModelRepository.selectById(dataModelId);

    Map<Long, DataModel> dataModelSourceCache = new HashMap<>();
    Map<Long, DataModel> dataModelTargetCache = new HashMap<>();
    LinkedList<DataModel> stack = new LinkedList<>();
    stack.push(rootDataModel);
    while (!stack.isEmpty()) {
      DataModel dataModel = stack.pop();
      dataModelSourceCache.put(dataModel.getId(), dataModel);
      dataModelTargetCache.put(dataModel.getId(), new DataModel());
      dataModel.getChildren().forEach(stack::push);
    }
    dataModelSourceCache.forEach(
        (dataModelSourceId, dataModelSource) -> {
          DataModel dataModelTarget = dataModelTargetCache.get(dataModelSourceId);
          dataModelTarget.setId(dataModelSource.getId());
          dataModelTarget.setName(dataModelSource.getName());
          if (dataModelSource.getParent() != null) {
            Long parentId = dataModelSource.getParent().getId();
            DataModel parentSource = dataModelSourceCache.get(parentId);
            if (parentSource != null) {
              DataModel parentTarget = dataModelTargetCache.get(parentId);
              dataModelTarget.setParent(parentTarget);
            }
          }
          dataModelSource
              .getChildren()
              .forEach(
                  child -> {
                    Long childId = child.getId();
                    if (!excludeIdCache.containsKey(childId)) {
                      dataModelTarget.getChildren().add(dataModelTargetCache.get(childId));
                    }
                  });

          if (!dataModelSource.equals(rootDataModel)) {
            DynamicModel dynamicModel =
                dynamicModelCache.get(dataModelSource.getDynamicModel().getId());
            dataModelTarget.setDynamicModel(dynamicModel);

            Map<String, Map<String, Set<String>>> keysCache =
                dynamicModelKeysCache.get(dynamicModel.getId());
            Map<String, Set<String>> propertiesKeys = keysCache.get("propertiesKeys");
            Map<String, Set<String>> associationKeys = keysCache.get("associationKeys");
            Set<String> propertiesKeys_dateTypeKeys = propertiesKeys.get("dateTypeKeys");
            Set<String> propertiesKeys_dataModelTypeKeys = propertiesKeys.get("dataModelTypeKeys");
            Set<String> associationKeys_dateTypeKeys = associationKeys.get("dateTypeKeys");
            Set<String> associationKeys_dataModelTypeKeys =
                associationKeys.get("dataModelTypeKeys");
            dataModelSource
                .getProperties()
                .forEach(
                    (name, value) -> {
                      try {
                        if (propertiesKeys_dateTypeKeys.contains(name)) {
                          dataModelTarget.getProperties().put(name, new Date((Long) value));
                        } else if (propertiesKeys_dataModelTypeKeys.contains(name)) {
                          dataModelTarget
                              .getProperties()
                              .put(name, dataModelTargetCache.get(value));
                        } else {
                          dataModelTarget.getProperties().put(name, value);
                        }
                      } catch (Exception e) {
                        //
                      }
                    });
            dataModelSource
                .getAssociation()
                .forEach(
                    property -> {
                      Map<String, Object> newProperty = new LinkedHashMap<>();
                      property.forEach(
                          (name, value) -> {
                            try {
                              if (associationKeys_dateTypeKeys.contains(name)) {
                                newProperty.put(name, new Date((Long) value));
                              } else if (associationKeys_dataModelTypeKeys.contains(name)) {
                                newProperty.put(name, dataModelTargetCache.get(value));
                              } else {
                                newProperty.put(name, value);
                              }
                            } catch (Exception e) {
                              //
                            }
                          });
                      dataModelTarget.getAssociation().add(newProperty);
                    });
          }
        });

    Template templateTemplate = new Template();
    templateTemplate.setIsDelete(false);
    templateTemplate.setGenerator(generator);
    List<Template> templates = templateRepository.selectList(templateTemplate);
    Map<Long, Template> templateCache = new HashMap<>();
    templates.forEach(
        template -> {
          Template newTemplate = new Template();
          newTemplate.setId(template.getId());
          newTemplate.setName(template.getName());
          newTemplate.setUrl(template.getUrl());
          templateCache.put(template.getId(), newTemplate);
        });

    Long generateId = idWorker.nextId();
    User developer = userRepository.selectById(generator.getDeveloper().getId());
    TemplateStrategy templateStrategyClone = templateStrategy.clone();

    Global global = new Global();
    global.setGenerateId(generateId);
    global.setTemplateCache(templateCache);

    User userClone = new User();
    userClone.setId(userId);
    userClone.setUserName(authentication.getUserName());
    global.setUser(userClone);

    Generator generatorClone = new Generator();
    generatorClone.setId(generator.getId());
    generatorClone.setName(generator.getName());
    global.setGenerator(generatorClone);

    GeneratorInstance generatorInstanceClone = new GeneratorInstance();
    generatorInstanceClone.setId(generatorInstance.getId());
    generatorInstanceClone.setName(generatorInstance.getName());
    global.setGeneratorInstance(generatorInstanceClone);

    User developerClone = new User();
    developerClone.setId(developer.getId());
    developerClone.setUserName(developer.getUserName());
    global.setDeveloper(developerClone);

    global.setTemplateStrategy(templateStrategyClone);

    Context context = new Context();
    context.setVariable("global", global);
    context.setVariable("data", dataModelTargetCache.get(rootDataModel.getId()));
    try {
      templateStrategyClone.execute(context);
    } catch (Throwable e) {
      List<String> messages = new ArrayList<>();
      LinkedList<Throwable> exceptionStack = new LinkedList<>();
      exceptionStack.push(e);
      while (!exceptionStack.isEmpty()) {
        Throwable exception = exceptionStack.pop();
        messages.add(exception.toString());
        if (exception.getCause() != null) {
          exceptionStack.push(exception.getCause());
        }
      }
      runResult.setMessages(messages);
      return runResult;
    }
    String generatePath =
        ConfigProperties.TEMPORARY_PATH
            + ConfigProperties.fileSeparator
            + userId
            + ConfigProperties.fileSeparator
            + generatorInstance.getName()
            + "("
            + generateId
            + ")";
    FileUtil.mkdirs(generatePath);
    File generateFolder = new File(generatePath);
    try {
      ZipUtil.compress(generateFolder);
    } catch (Exception e) {
      throw new AppException(e, "压缩文件失败");
    }
    FileUtil.deleteFile(generateFolder);
    runResult.setUrl(userId + "/" + generatorInstance.getName() + "(" + generateId + ").zip");
    runResult.setFileName(generatorInstance.getName() + "(" + generateId + ").zip");
    return runResult;
  }
Example #3
0
  /**
   * @param datacenterName The name of the Datacenter
   * @return ManagedObjectReference to the Datacenter
   */
  private static ManagedObjectReference getDatacenterByName(String datacenterName) {
    ManagedObjectReference retVal = null;
    ManagedObjectReference rootFolder = serviceContent.getRootFolder();
    ManagedObjectReference propCollector = serviceContent.getPropertyCollector();
    try {
      TraversalSpec tSpec = getDatacenterTraversalSpec();
      // Create Property Spec
      PropertySpec propertySpec = new PropertySpec();
      propertySpec.setAll(Boolean.FALSE);
      propertySpec.getPathSet().add("name");
      propertySpec.setType("Datacenter");

      // Now create Object Spec
      ObjectSpec objectSpec = new ObjectSpec();
      objectSpec.setObj(rootFolder);
      objectSpec.setSkip(Boolean.TRUE);
      objectSpec.getSelectSet().add(tSpec);

      // Create PropertyFilterSpec using the PropertySpec and ObjectPec
      // created above.
      PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
      propertyFilterSpec.getPropSet().add(propertySpec);
      propertyFilterSpec.getObjectSet().add(objectSpec);

      List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
      listpfs.add(propertyFilterSpec);
      List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);

      if (listobjcont != null) {
        for (ObjectContent oc : listobjcont) {
          ManagedObjectReference mr = oc.getObj();
          String dcnm = null;
          List<DynamicProperty> dps = oc.getPropSet();
          if (dps != null) {
            // Since there is only one property PropertySpec pathset
            // this array contains only one value
            for (DynamicProperty dp : dps) {
              dcnm = (String) dp.getVal();
            }
          }
          // This is done outside of the previous for loop to break
          // out of the loop as soon as the required datacenter is found.
          if (dcnm != null && dcnm.equals(datacenterName)) {
            retVal = mr;
            break;
          }
        }
      }
    } catch (SOAPFaultException sfe) {
      printSoapFaultException(sfe);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return retVal;
  }
Example #4
0
  /**
   * Retrieves the MOREF of the host.
   *
   * @param hostName :
   * @return
   */
  private static ManagedObjectReference getHostByHostName(String hostName) {
    ManagedObjectReference retVal = null;
    ManagedObjectReference rootFolder = serviceContent.getRootFolder();
    try {
      TraversalSpec tSpec = getHostSystemTraversalSpec();
      // Create Property Spec
      PropertySpec propertySpec = new PropertySpec();
      propertySpec.setAll(Boolean.FALSE);
      propertySpec.getPathSet().add("name");
      propertySpec.setType("HostSystem");

      // Now create Object Spec
      ObjectSpec objectSpec = new ObjectSpec();
      objectSpec.setObj(rootFolder);
      objectSpec.setSkip(Boolean.TRUE);
      objectSpec.getSelectSet().add(tSpec);

      // Create PropertyFilterSpec using the PropertySpec and ObjectPec
      // created above.
      PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
      propertyFilterSpec.getPropSet().add(propertySpec);
      propertyFilterSpec.getObjectSet().add(objectSpec);
      List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
      listpfs.add(propertyFilterSpec);
      List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);

      if (listobjcont != null) {
        for (ObjectContent oc : listobjcont) {
          ManagedObjectReference mr = oc.getObj();
          String hostnm = null;
          List<DynamicProperty> listDynamicProps = oc.getPropSet();
          DynamicProperty[] dps =
              listDynamicProps.toArray(new DynamicProperty[listDynamicProps.size()]);
          if (dps != null) {
            for (DynamicProperty dp : dps) {
              hostnm = (String) dp.getVal();
            }
          }
          if (hostnm != null && hostnm.equals(hostName)) {
            retVal = mr;
            break;
          }
        }
      } else {
        System.out.println("The Object Content is Null");
      }
    } catch (SOAPFaultException sfe) {
      printSoapFaultException(sfe);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return retVal;
  }
  /**
   * Demonstrate how to use the PropertyCollector to retrieve properties of a managed object.
   *
   * <p>Prints Virtual machine name and powerstate of all the VMs in the datacenter (Either VC or
   * ESX host).
   */
  public static void getVMInfo() {
    try {
      TraversalSpec tSpec = getVMTraversalSpec();
      // Create Property Spec
      PropertySpec propertySpec = new PropertySpec();
      propertySpec.setAll(Boolean.FALSE);
      propertySpec.setPathSet(new String[] {"name", "runtime.powerState"});
      propertySpec.setType("VirtualMachine");
      PropertySpec[] propertySpecs = new PropertySpec[] {propertySpec};

      // Now create Object Spec
      ObjectSpec objectSpec = new ObjectSpec();
      objectSpec.setObj(ROOT_FOLDER);
      objectSpec.setSkip(Boolean.TRUE);
      objectSpec.setSelectSet(new SelectionSpec[] {tSpec});
      ObjectSpec[] objectSpecs = new ObjectSpec[] {objectSpec};

      // Create PropertyFilterSpec using the PropertySpec and ObjectPec
      // created above.
      PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
      propertyFilterSpec.setPropSet(propertySpecs);
      propertyFilterSpec.setObjectSet(objectSpecs);

      PropertyFilterSpec[] propertyFilterSpecs = new PropertyFilterSpec[] {propertyFilterSpec};

      ObjectContent[] oCont = VIM_PORT.retrieveProperties(PROP_COLLECTOR, propertyFilterSpecs);
      if (oCont != null) {
        // System.out.println("ObjectContent Length : " + oCont.length);
        StringBuilder sb = new StringBuilder();
        for (ObjectContent oc : oCont) {
          DynamicProperty[] dps = oc.getPropSet();
          if (dps != null) {
            for (DynamicProperty dp : dps) {
              if (dp.getName().equalsIgnoreCase("name")) {
                sb.append(dp.getVal());
                sb.append(" : ");
              } else {
                sb.append(dp.getVal());
                sb.append("\n");
              }
              // System.out.println(dp.getName() + " : " +
              // dp.getVal());
            }
          }
        }
        System.out.println(sb.toString());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Example #6
0
  /**
   * Get the MOR of the Virtual Machine by its name.
   *
   * @param vmName The name of the Virtual Machine
   * @return The Managed Object reference for this VM
   */
  private static ManagedObjectReference getVmByVMname(String vmname) {
    ManagedObjectReference retVal = null;
    try {
      TraversalSpec tSpec = getVMTraversalSpec();
      // Create Property Spec
      PropertySpec propertySpec = new PropertySpec();
      propertySpec.setAll(Boolean.FALSE);
      propertySpec.getPathSet().add("name");
      propertySpec.setType("VirtualMachine");

      // Now create Object Spec
      ObjectSpec objectSpec = new ObjectSpec();
      objectSpec.setObj(rootRef);
      objectSpec.setSkip(Boolean.TRUE);
      objectSpec.getSelectSet().add(tSpec);

      // Create PropertyFilterSpec using the PropertySpec and ObjectPec
      // created above.
      PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
      propertyFilterSpec.getPropSet().add(propertySpec);
      propertyFilterSpec.getObjectSet().add(objectSpec);

      List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
      listpfs.add(propertyFilterSpec);
      List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);

      if (listobjcont != null) {
        for (ObjectContent oc : listobjcont) {
          ManagedObjectReference mr = oc.getObj();
          String vmnm = null;
          List<DynamicProperty> dps = oc.getPropSet();
          if (dps != null) {
            for (DynamicProperty dp : dps) {
              vmnm = (String) dp.getVal();
            }
          }
          if (vmnm != null && vmnm.equals(vmname)) {
            retVal = mr;
            break;
          }
        }
      }
    } catch (SOAPFaultException sfe) {
      printSoapFaultException(sfe);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return retVal;
  }
  /**
   * Demonstrate how to use the PropertyCollector to retrieve properties of a managed object when
   * the managed object reference is known.
   *
   * <p>Retrieves the Virtual machine property.
   *
   * @return String property of the VirtualMachine.
   */
  public static Object getVMProperty(ManagedObjectReference mor, String prop) {
    Object retVal = null;
    try {
      // Create Property Spec
      PropertySpec propertySpec = new PropertySpec();
      propertySpec.setAll(Boolean.FALSE);
      // propertySpec.setPathSet(new String[]{"name"});
      propertySpec.setPathSet(new String[] {prop});
      propertySpec.setType("VirtualMachine");
      PropertySpec[] propertySpecs = new PropertySpec[] {propertySpec};

      // Now create Object Spec
      ObjectSpec objectSpec = new ObjectSpec();
      objectSpec.setObj(mor);
      ObjectSpec[] objectSpecs = new ObjectSpec[] {objectSpec};

      // Create PropertyFilterSpec using the PropertySpec and ObjectPec
      // created above.
      PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
      propertyFilterSpec.setPropSet(propertySpecs);
      propertyFilterSpec.setObjectSet(objectSpecs);

      PropertyFilterSpec[] propertyFilterSpecs = new PropertyFilterSpec[] {propertyFilterSpec};

      ObjectContent[] oCont = VIM_PORT.retrieveProperties(PROP_COLLECTOR, propertyFilterSpecs);
      if (oCont != null) {
        // System.out.println("ObjectContent Length : " + oCont.length);
        for (ObjectContent oc : oCont) {
          // DynamicProperty[] dps = oc.getPropSet();
          DynamicProperty[] dps = oc.getPropSet();
          if (dps != null) {
            for (DynamicProperty dp : dps) {
              // System.out.println(dp.getName() + " : " +
              // dp.getVal());
              retVal = dp.getVal();
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return retVal;
  }
Example #8
0
  /** Prints the Host names. */
  private static void printHostProductDetails() {
    try {
      setHostSystemAttributesList();
      TraversalSpec tSpec = getHostSystemTraversalSpec();
      // Create Property Spec
      PropertySpec propertySpec = new PropertySpec();
      propertySpec.setAll(Boolean.FALSE);
      propertySpec.getPathSet().addAll(hostSystemAttributesArr);
      propertySpec.setType("HostSystem");

      // Now create Object Spec
      ObjectSpec objectSpec = new ObjectSpec();
      objectSpec.setObj(rootRef);
      objectSpec.setSkip(Boolean.TRUE);
      objectSpec.getSelectSet().add(tSpec);

      // Create PropertyFilterSpec using the PropertySpec and ObjectPec
      // created above.
      PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
      propertyFilterSpec.getPropSet().add(propertySpec);
      propertyFilterSpec.getObjectSet().add(objectSpec);
      List<PropertyFilterSpec> pfSpecList = new ArrayList<PropertyFilterSpec>(1);
      pfSpecList.add(propertyFilterSpec);

      List<ObjectContent> listobjcont = retrievePropertiesAllObjects(pfSpecList);

      if (listobjcont != null) {
        for (ObjectContent oc : listobjcont) {
          List<DynamicProperty> dpList = oc.getPropSet();
          if (dpList != null) {
            for (DynamicProperty dp : dpList) {
              System.out.println(dp.getName() + " : " + dp.getVal());
            }
          }
          System.out.println("\n\n***************************************************************");
        }
      }
    } catch (SOAPFaultException sfe) {
      printSoapFaultException(sfe);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Example #9
0
  /**
   * Returns all the MOREFs of the specified type that are present under the container
   *
   * @param folder {@link ManagedObjectReference} of the container to begin the search from
   * @param morefType Type of the managed entity that needs to be searched
   * @return Map of name and MOREF of the managed objects present. If none exist then empty Map is
   *     returned
   * @throws InvalidPropertyFaultMsg
   * @throws RuntimeFaultFaultMsg
   */
  Map<String, ManagedObjectReference> getMOREFsInContainerByType(
      ManagedObjectReference folder, String morefType)
      throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    String PROP_ME_NAME = "name";
    ManagedObjectReference viewManager = serviceContent.getViewManager();
    ManagedObjectReference containerView =
        vimPort.createContainerView(viewManager, folder, Arrays.asList(morefType), true);

    Map<String, ManagedObjectReference> tgtMoref = new HashMap<String, ManagedObjectReference>();

    // Create Property Spec
    PropertySpec propertySpec = new PropertySpec();
    propertySpec.setAll(Boolean.FALSE);
    propertySpec.setType(morefType);
    propertySpec.getPathSet().add(PROP_ME_NAME);

    TraversalSpec ts = new TraversalSpec();
    ts.setName("view");
    ts.setPath("view");
    ts.setSkip(false);
    ts.setType("ContainerView");

    // Now create Object Spec
    ObjectSpec objectSpec = new ObjectSpec();
    objectSpec.setObj(containerView);
    objectSpec.setSkip(Boolean.TRUE);
    objectSpec.getSelectSet().add(ts);

    // Create PropertyFilterSpec using the PropertySpec and ObjectPec
    // created above.
    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
    propertyFilterSpec.getPropSet().add(propertySpec);
    propertyFilterSpec.getObjectSet().add(objectSpec);

    List<PropertyFilterSpec> propertyFilterSpecs = new ArrayList<PropertyFilterSpec>();
    propertyFilterSpecs.add(propertyFilterSpec);

    RetrieveResult rslts =
        vimPort.retrievePropertiesEx(
            serviceContent.getPropertyCollector(), propertyFilterSpecs, new RetrieveOptions());
    List<ObjectContent> listobjcontent = new ArrayList<ObjectContent>();
    if (rslts != null && rslts.getObjects() != null && !rslts.getObjects().isEmpty()) {
      listobjcontent.addAll(rslts.getObjects());
    }
    String token = null;
    if (rslts != null && rslts.getToken() != null) {
      token = rslts.getToken();
    }
    while (token != null && !token.isEmpty()) {
      rslts = vimPort.continueRetrievePropertiesEx(serviceContent.getPropertyCollector(), token);
      token = null;
      if (rslts != null) {
        token = rslts.getToken();
        if (rslts.getObjects() != null && !rslts.getObjects().isEmpty()) {
          listobjcontent.addAll(rslts.getObjects());
        }
      }
    }
    for (ObjectContent oc : listobjcontent) {
      ManagedObjectReference mr = oc.getObj();
      String entityNm = null;
      List<DynamicProperty> dps = oc.getPropSet();
      if (dps != null) {
        for (DynamicProperty dp : dps) {
          entityNm = (String) dp.getVal();
        }
      }
      tgtMoref.put(entityNm, mr);
    }
    return tgtMoref;
  }