@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;
  }
  @Override
  public GeneratorInstance get(GeneratorInstanceGetRequest request) {
    Long id = request.getId();
    GeneratorInstance generatorInstancePersistence = generatorInstanceRepository.selectById(id);
    if (generatorInstancePersistence == null) {
      throw new AppException("实例不存在");
    }

    GeneratorInstance generatorInstance = new GeneratorInstance();
    generatorInstance.setId(generatorInstancePersistence.getId());
    generatorInstance.setName(generatorInstancePersistence.getName());
    generatorInstance.setCreateDate(generatorInstancePersistence.getCreateDate());
    generatorInstance.setModifyDate(generatorInstancePersistence.getModifyDate());
    generatorInstance.setIsDelete(generatorInstancePersistence.getIsDelete());

    User userPersistence =
        userRepository.selectById(generatorInstancePersistence.getUser().getId());
    generatorInstance.setUser(userPersistence);

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

    generatorInstance.setDataModel(generatorInstancePersistence.getDataModel());
    generatorInstance.setVersion(generatorInstancePersistence.getVersion());

    return generatorInstance;
  }
  @Override
  public PagerResult<GeneratorInstance> search(GeneratorInstanceSearchRequest request) {
    Long userId = request.getUserId();
    String name = StringUtils.hasText(request.getName()) ? request.getName() : null;
    List<GeneratorInstance> records =
        generatorInstanceRepository.filter(
            generatorInstance -> {
              if (generatorInstance.getIsDelete()) {
                return false;
              }
              if (name != null) {
                if (!generatorInstance.getName().contains(name)) {
                  return false;
                }
              }
              if (userId != null) {
                if (!userId.equals(generatorInstance.getUser().getId())) {
                  return false;
                }
              }
              return true;
            });
    Integer page = request.getPage();
    Integer pageSize = request.getPageSize();
    Integer fromIndex = (page - 1) * pageSize;
    Integer toIndex = fromIndex + pageSize > records.size() ? records.size() : fromIndex + pageSize;
    List<GeneratorInstance> limitRecords = records.subList(fromIndex, toIndex);
    List<GeneratorInstance> result = new ArrayList<>();
    for (GeneratorInstance g : limitRecords) {
      GeneratorInstance generatorInstance = new GeneratorInstance();
      generatorInstance.setId(g.getId());
      generatorInstance.setName(g.getName());
      generatorInstance.setCreateDate(g.getCreateDate());
      generatorInstance.setModifyDate(g.getModifyDate());
      generatorInstance.setIsDelete(g.getIsDelete());
      User userPersistence = userRepository.selectById(g.getUser().getId());
      generatorInstance.setUser(userPersistence);
      Generator generatorPersistence = generatorRepository.selectById(g.getGenerator().getId());
      if (generatorPersistence == null) {
        throw new AppException("生成器不存在");
      }
      generatorInstance.setGenerator(generatorPersistence);
      generatorInstance.setDataModel(g.getDataModel());
      generatorInstance.setVersion(g.getVersion());
      result.add(generatorInstance);
    }

    String sortField = request.getSortField();
    String sortDirection = request.getSortDirection();
    if ("modifyDate".equals(sortField)) {
      if ("DESC".equalsIgnoreCase(sortDirection)) {
        result.sort(
            (g1, g2) -> (int) (g2.getModifyDate().getTime() - g1.getModifyDate().getTime()));
      }
    }

    return new PagerResult<>(result, (long) records.size());
  }
 @Override
 public User updateUser(
     long userId, String username, String password, String firstName, String lastName) {
   User user = userRepository.findOne(userId);
   user.setUsername(username);
   user.setFirstName(firstName);
   user.setLastName(lastName);
   user.setPassword(password);
   return this.userRepository.save(user);
 }
 @Override
 public Customer removeCustomer(long userId, long customerId) {
   User user = userRepository.findOne(userId);
   Customer customer = customerRepository.findOne(customerId);
   user.getCustomers().remove(customer);
   this.userRepository.save(user);
   customer.setUser(null);
   this.customerRepository.delete(customer);
   return customer;
 }
  @Override
  public void setUp() throws Exception {
    super.setUp();
    userRepository = new UserRepository(em);
    user = new User(TEST_USER_ID, TEST_PASSWORD);
    userRepository.create(user);
    em.flush();

    repository = new AlbumRepository(em);
  }
  @Override
  public void writeUserProfilePhoto(long userId, MediaType ext, byte[] bytesForProfilePhoto) {

    User user = findById(userId);
    user.setProfilePhotoMediaType(ext.toString());
    user.setProfilePhotoImported(true);
    userRepository.save(user);

    ByteArrayInputStream byteArrayInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
      fileOutputStream = new FileOutputStream(fileForPhoto(userId));
      byteArrayInputStream = new ByteArrayInputStream(bytesForProfilePhoto);
      IOUtils.copy(byteArrayInputStream, fileOutputStream);
    } catch (IOException e) {
      throw new UserProfilePhotoWriteException(userId, e);
    } finally {
      IOUtils.closeQuietly(fileOutputStream);
      IOUtils.closeQuietly(byteArrayInputStream);
    }
  }
 @Override
 public User removeUser(long userId) {
   User u = userRepository.findOne(userId);
   this.userRepository.delete(userId);
   return u;
 }
 @Override
 public User findById(long userId) {
   User user = userRepository.findOne(userId);
   if (null == user) throw new UserNotFoundException(userId);
   return user;
 }
 @Override
 public User findUserByUsername(String username) {
   return userRepository.findByUsername(username);
 }
  /** Stores to persistence layer initial data. */
  @PostConstruct
  public void init() {
    if (roleRepository.findByName("ROLE_ADMIN") == null) {
      Role roleUser = new Role();
      roleUser.setName("ROLE_USER");
      roleRepository.save(roleUser);

      Role roleAdmin = new Role();
      roleAdmin.setName("ROLE_ADMIN");
      roleRepository.save(roleAdmin);

      User user = new User();
      user.setEnabled(true);
      user.setEmail("admin@admin");

      BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
      user.setPassword(encoder.encode("admin"));
      List<Role> roles = new ArrayList<>();
      roles.add(roleAdmin);
      roles.add(roleUser);
      user.setRoles(roles);
      userRepository.save(user);

      // Create account for wallet
      Account walletAccount = new Account();
      walletAccount.setName(context.getMessage("Name.default.account", null, Locale.ENGLISH));
      walletAccount.setUser(user);
      walletAccount.setAmount(new BigDecimal(0));
      walletAccount.setCurrency(Currency.getInstance("UAH"));
      accountRepository.save(walletAccount);

      Account bankAccount = new Account();
      bankAccount.setName("Bank");
      bankAccount.setUser(user);
      bankAccount.setAmount(new BigDecimal(500));
      bankAccount.setCurrency(Currency.getInstance("UAH"));
      accountRepository.save(bankAccount);

      // Create categories for expenses
      for (int i = 1; i < 6; i++) {
        Category category = new Category();
        category.setName(
            context.getMessage("Name" + i + ".default.category", null, Locale.ENGLISH));
        category.setType(Operation.EXPENSE);
        category.setUser(user);
        categoryRepository.save(category);
      }

      // Create categories for incomes
      for (int i = 6; i < 8; i++) {
        Category category = new Category();
        category.setName(
            context.getMessage("Name" + i + ".default.category", null, Locale.ENGLISH));
        category.setType(Operation.INCOME);
        category.setUser(user);
        categoryRepository.save(category);
      }

      Transaction transaction1 = new Transaction();
      transaction1.setDate(new Date());
      transaction1.setAccount(walletAccount);
      transaction1.setAmount(new BigDecimal(50));
      transaction1.setCurrency(Currency.getInstance("UAH"));
      transaction1.setCategory(categoryRepository.findOne(3));
      transaction1.setType(Operation.EXPENSE);
      transaction1.setComment("McDonalds");
      transaction1.setUser(user);
      transactionRepository.save(transaction1);

      Transaction transaction2 = new Transaction();
      Calendar calendar = new GregorianCalendar();
      calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
      transaction2.setDate(calendar.getTime());
      transaction2.setAccount(bankAccount);
      transaction2.setAmount(new BigDecimal(45));
      transaction2.setCurrency(Currency.getInstance("UAH"));
      transaction2.setCategory(categoryRepository.findOne(7));
      transaction2.setType(Operation.INCOME);
      transaction2.setComment("Festo");
      transaction2.setUser(user);
      transactionRepository.save(transaction2);

      List<Transaction> transactions = new ArrayList<>();
      transactions.add(transaction1);
      user.setTransactions(transactions);
    }
  }