/** {@inheritDoc} */
 public boolean deleteModel(String id, boolean removeFromDisk)
     throws ReportModelOperationException {
   try {
     ReportDesignModel model = this.getModelByIdOrName(id, false);
     if (model != null) {
       boolean result = true;
       // 如果存在发布态报表,删除发布态报表
       if (this.getModelByIdOrName(id, true) != null) {
         /** 已经发布了,不能删除 TODO 应该有下线操作,下线以后把发布的报表删除,才能够进一步删除正在开发的报表 */
         return false;
       }
       result = fileService.rm(generateDevReportLocation(model));
       // 尝试删除原有命名方式命名的报表
       if (!result) {
         result = fileService.rm(generateOriDevReportLocation(model));
       }
       logger.info("delete report " + (result ? "successfully" : "failed"));
       return result;
     }
     return false;
   } catch (FileServiceException e) {
     logger.error(e.getMessage(), e);
     throw new ReportModelOperationException(e);
   }
 }
 /** {@inheritDoc} */
 @Override
 public boolean isNameExist(String name) {
   if (name == null) {
     return false;
   }
   String[] listFile = null;
   try {
     listFile = fileService.ls(this.getDevReportDir());
   } catch (FileServiceException e) {
     logger.debug(e.getMessage(), e);
   }
   if (listFile == null || listFile.length == 0) {
     return false;
   }
   for (String file : listFile) {
     if (file.contains(".")) {
       continue;
     }
     String[] tmpArray = file.split(FILE_SPLIT_REG);
     if (name.equals(tmpArray[0])
         || name.equals(tmpArray[1])
         || String.valueOf(name.hashCode()).equals(tmpArray[1])) {
       return true;
     }
   }
   return false;
 }
 @Deprecated
 private String getOriReleaseReportLocation(ReportDesignModel model) {
   if (model == null) {
     return null;
   }
   String name = model.getName();
   String productLine = ContextManager.getProductLine();
   try {
     String[] listFile = fileService.ls(this.getReleaseReportDir());
     for (String file : listFile) {
       if (file.startsWith(model.getId())) {
         return productLine
             + File.separator
             + reportBaseDir
             + File.separator
             + "release"
             + File.separator
             + file;
       }
     }
   } catch (FileServiceException e) {
     logger.error(e.getMessage(), e);
   }
   return productLine
       + File.separator
       + reportBaseDir
       + File.separator
       + "release"
       + File.separator
       + model.getId()
       + Constants.FILE_NAME_SEPERATOR
       + name;
 }
  /**
   * 升级方法
   *
   * @param model
   * @return String
   */
  @Deprecated
  private String generateOriDevReportLocation(ReportDesignModel model) {
    if (model == null) {
      return null;
    }
    StringBuilder builder = new StringBuilder();
    builder.append(getDevReportDir());
    builder.append(File.separator);
    String name = null;
    try {
      String[] listFile = fileService.ls(this.getDevReportDir());
      for (String file : listFile) {

        if (file.startsWith(model.getId())) {
          name = file;
          builder.append(name);
          break;
        }
      }
    } catch (FileServiceException e) {
      logger.error(e.getMessage(), e);
    }
    if (name == null) {
      builder.append(model.getId());
      builder.append(Constants.FILE_NAME_SEPERATOR);
      builder.append(name);
      builder.append(Constants.FILE_NAME_SEPERATOR);
      builder.append(model.getDsId());
    }
    return builder.toString();
  }
 /**
  * @param model
  * @param removeFromDisk
  */
 private void deleteModel(ReportDesignModel model, boolean removeFromDisk)
     throws ReportModelOperationException {
   try {
     // TODO 升级兼容 后续考虑删除
     fileService.rm(generateDevReportLocation(model));
     logger.info("delete report successfully");
   } catch (FileServiceException e) {
     logger.warn(e.getMessage(), e);
     throw new ReportModelOperationException(e);
   }
 }
  /** {@inheritDoc} */
  @Override
  public ReportDesignModel saveOrUpdateModel(ReportDesignModel model)
      throws ReportModelOperationException {
    if (model == null) {
      logger.warn("current model is null");
      throw new ReportModelOperationException("model can not be null");
    }
    if (StringUtils.isEmpty(model.getName())) {
      logger.debug("model's name can not be empty");
      throw new ReportModelOperationException("model's name can not be empty");
    }
    if (StringUtils.isEmpty(model.getId())) {
      model.setId(UuidGeneratorUtils.generate());
    }
    try {
      ReportDesignModel oldReport = getModelByIdOrName(model.getId(), false);
      if (oldReport != null) {
        try {
          this.deleteModel(oldReport, true);
        } catch (Exception e) {
          try {
            fileService.rm(generateOriDevReportLocation(model));
          } catch (Exception e1) {
            logger.error(e.getMessage(), e);
          }
        }
      }
      boolean rs =
          fileService.write(generateDevReportLocation(model), SerializationUtils.serialize(model));
      if (rs) {
        return model;
      }
    } catch (FileServiceException e) {
      logger.error(e.getMessage(), e);
    }

    return null;
  }
  /** {@inheritDoc} */
  @Override
  public ReportDesignModel getModelByIdOrName(String idOrName, boolean isRelease) {
    String baseDir = null;
    if (!isRelease) {
      baseDir = getDevReportDir();
    } else {
      baseDir = getReleaseReportDir();
    }
    String[] modelFileList = null;
    try {
      modelFileList = fileService.ls(baseDir);
    } catch (FileServiceException e) {
      logger.debug(e.getMessage(), e);
    }
    if (modelFileList == null || modelFileList.length == 0) {
      logger.warn("can not get report model define in directory: " + baseDir);
      return null;
    }

    try {
      for (String modelFile : modelFileList) {
        if (modelFile.contains(".")) {
          continue;
        }
        String[] tmpArray = modelFile.split(FILE_SPLIT_REG);
        if (idOrName.equals(tmpArray[0])
            || tmpArray[1].equals(idOrName)
            || String.valueOf(idOrName.hashCode()).equals(tmpArray[1])) {
          byte[] content = fileService.read(baseDir + File.separator + modelFile);
          ReportDesignModel model = (ReportDesignModel) SerializationUtils.deserialize(content);
          return model;
        }
      }
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
    return null;
  }
 /** {@inheritDoc} */
 @Override
 public List<String> lsReportWithDsId(String id) {
   String[] modelFileList = null;
   try {
     modelFileList = fileService.ls(getDevReportDir());
   } catch (FileServiceException e) {
     logger.debug(e.getMessage(), e);
     return Lists.newArrayList();
   }
   if (modelFileList == null || modelFileList.length == 0) {
     return Lists.newArrayList();
   }
   List<String> rs = Lists.newArrayList();
   for (String str : modelFileList) {
     if (str.contains(id)) {
       rs.add(
           str.substring(
                   str.indexOf(Constants.FILE_NAME_SEPERATOR),
                   str.lastIndexOf(Constants.FILE_NAME_SEPERATOR))
               .replace(Constants.FILE_NAME_SEPERATOR, ""));
     }
   }
   return rs;
 }
 public boolean isNameExist(String name, String id) {
   if (name == null) {
     return false;
   }
   String[] listFile = null;
   try {
     listFile = fileService.ls(this.getDevReportDir());
   } catch (FileServiceException e) {
     logger.debug(e.getMessage(), e);
   }
   if (listFile == null || listFile.length == 0) {
     return false;
   }
   String idTarget = id + Constants.FILE_NAME_SEPERATOR;
   String nameTarget =
       Constants.FILE_NAME_SEPERATOR + name.hashCode() + Constants.FILE_NAME_SEPERATOR;
   for (String file : listFile) {
     // 名称相同,id不相同
     if (!file.startsWith(idTarget) && file.contains(nameTarget)) {
       return true;
     }
   }
   return false;
 }
  /** {@inheritDoc} */
  @Override
  public ReportDesignModel[] queryAllModels(boolean released) {
    try {
      String[] listFile = null;
      if (released) {
        listFile = fileService.ls(this.getReleaseReportDir());
      } else {
        listFile = fileService.ls(this.getDevReportDir());
      }
      if (listFile == null || listFile.length == 0) {
        return new ReportDesignModel[0];
      }
      ReportDesignModel[] modelList = buildResult(listFile, released);
      List<ReportDesignModel> reportList = Arrays.asList(modelList);
      Collections.sort(
          reportList,
          new Comparator<ReportDesignModel>() {

            @Override
            public int compare(ReportDesignModel o1, ReportDesignModel o2) {
              if (o1 == null || StringUtils.isEmpty(o1.getName())) {
                return -1;
              }
              if (o2 == null) {
                return 0;
              }
              return o1.getName().compareTo(o2.getName());
            }
          });
      return reportList.toArray(new ReportDesignModel[0]);

    } catch (FileServiceException e) {
      logger.error(e.getMessage(), e);
    }
    return new ReportDesignModel[0];
  }
  /**
   * {@inheritDoc}
   *
   * @throws DataSourceOperationException
   */
  @Override
  public boolean publishReport(ReportDesignModel model, String securityKey)
      throws ReportModelOperationException, DataSourceOperationException {

    boolean result = false;
    String devReportLocation = this.generateDevReportLocation(model);
    String realeaseLocation = this.getReleaseReportLocation(model);
    try {
      // 删除原来已经发布的报表,如果不存在,忽略此处异常
      ReportDesignModel tmp = this.getModelByIdOrName(model.getId(), true);
      if (tmp != null) {
        try {
          fileService.rm(getReleaseReportLocation(tmp));
        } catch (Exception e) {
          fileService.rm(getOriReleaseReportLocation(tmp));
        }
      }
    } catch (FileServiceException e1) {
      logger.info(e1.getMessage(), e1);
    }
    try {
      result = this.fileService.copy(devReportLocation, realeaseLocation);
    } catch (FileServiceException e) {
      logger.error(e.getMessage(), e);
      throw new ReportModelOperationException("发布报表失败!");
    }
    if (!result) {
      logger.error("拷贝报表失败!");
      throw new ReportModelOperationException("发布报表失败!");
    }
    /** 发布 */
    DataSourceDefine dsDefine;
    DataSourceInfo dsInfo;
    try {
      dsDefine = dsService.getDsDefine(model.getDsId());
      DataSourceConnectionService<?> dsConnService =
          DataSourceConnectionServiceFactory.getDataSourceConnectionServiceInstance(
              dsDefine.getDataSourceType().name());
      dsInfo = dsConnService.parseToDataSourceInfo(dsDefine, securityKey);
    } catch (DataSourceOperationException e) {
      logger.error("Fail in Finding datasource define. ", e);
      throw e;
    } catch (DataSourceConnectionException e) {
      logger.error("Fail in parse datasource to datasourceInfo.", e);
      throw new DataSourceOperationException(e);
    }
    List<Cube> cubes = Lists.newArrayList();
    for (ExtendArea area : model.getExtendAreaList()) {
      try {
        if ((area.getType() != ExtendAreaType.TABLE
                && area.getType() != ExtendAreaType.LITEOLAP_TABLE
                && area.getType() != ExtendAreaType.CHART
                && area.getType() != ExtendAreaType.LITEOLAP_CHART)
            || area.getType() == ExtendAreaType.PLANE_TABLE
            || QueryUtils.isFilterArea(area.getType())) {
          continue;
        }
        Cube cube = QueryUtils.getCubeWithExtendArea(model, area);
        cubes.add(cube);
      } catch (QueryModelBuildException e) {
        logger.warn("It seems that logicmodel of area is null. Ingore this area. ");
        continue;
      }
    }
    if (cubes.size() == 0) {
      logger.info("cube is empty, don't need to create index!");
      return true;
    }
    new Thread() {
      public void run() {
        MiniCubeConnection connection = MiniCubeDriverManager.getConnection(dsInfo);
        connection.publishCubes(cubes, dsInfo);
      }
    }.start();
    return true;
  }