@Test
  public void test() throws Exception {
    // 数据库连接基本信息,并构建内存数据库H2
    String username = "******";
    String password = "******";
    String dbInstance = "testDB";
    String securityKey = "0000000000000000";
    String passwordEncrypt = AesUtil.getInstance().encryptAndUrlEncoding(password, securityKey);
    String url = "jdbc:h2:mem:" + dbInstance + ";";
    try {
      Class.forName("org.h2.Driver");
      Connection conn = DriverManager.getConnection(url, username, password);
      if (conn != null) {
        LOG.error("get H2 datasource by username:"******"jdbc:h2:mem://");
    ds.setDataSourceType(DataSourceType.H2);
    ds.setDbInstance(dbInstance);
    ds.setDbPwd(passwordEncrypt);
    ds.setDbUser(username);
    ds.setHostAndPort("127.0.0.1:3306");
    // 测试打开和关闭连接
    Connection conn = dsConnService.createConnection(ds, securityKey);
    Assert.assertNotNull(conn);
    Assert.assertTrue(dsConnService.closeConnection(conn));

    try {
      dsConnService.getDataSourceConnUrl(null);
    } catch (DataSourceConnectionException e) {
      Assert.assertNotNull(e);
    }

    String connUrl =
        DataSourceType.H2.getPrefix()
            + ds.getHostAndPort()
            + DataSourceType.H2.getDiv()
            + ds.getDbInstance();
    Assert.assertEquals(connUrl, dsConnService.getDataSourceConnUrl(ds));

    // 测试数据源连接是否有效
    Assert.assertTrue(dsConnService.isValidateDataSource(ds, securityKey));
    DataSourceType.H2.setDriver("this is not database driver");
    ds.setDataSourceType(DataSourceType.H2);
    Assert.assertFalse(dsConnService.isValidateDataSource(ds, securityKey));
    // 设置正确的数据库前缀
    DataSourceType.H2.setPrefix("jdbc:h2:tcp://");
    DataSourceType.H2.setDriver("org.h2.Driver");
  }
  /**
   * {@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;
  }
예제 #3
0
  /**
   * 将查询动作转化成问题模型
   *
   * @param dsDefine
   * @param queryAction 查询动作
   * @return 问题模型
   * @throws QueryModelBuildException 构建失败异常
   */
  public static QuestionModel convert2QuestionModel(
      DataSourceDefine dsDefine,
      ReportDesignModel reportModel,
      QueryAction queryAction,
      Map<String, Object> requestParams,
      PageInfo pageInfo,
      String securityKey)
      throws QueryModelBuildException {
    if (queryAction == null) {
      throw new QueryModelBuildException("query action is null");
    }
    ConfigQuestionModel questionModel = new ConfigQuestionModel();
    String areaId = queryAction.getExtendAreaId();
    if (StringUtils.isEmpty(areaId)) {
      throw new QueryModelBuildException("area id is empty");
    }
    ExtendArea area = reportModel.getExtendById(areaId);
    if (area == null) {
      throw new QueryModelBuildException("can not get area with id : " + areaId);
    }
    Cube cube = getCubeFromReportModel(reportModel, area);
    if (cube == null) {
      throw new QueryModelBuildException("can not get cube define in area : " + areaId);
    }
    if (area.getType() == ExtendAreaType.PLANE_TABLE) {
      cube = transformCube(cube);
      MiniCube miniCube = (MiniCube) cube;
      DivideTableStrategyVo divideVo = miniCube.getDivideTableStrategyVo();
      DivideTableContext divideContext = new DivideTableContext();
      DivideTableService divideTableService = null;
      if (divideVo != null) {
        switch (divideVo.getCondition()) {
          case "yyyyMMdd":
            divideTableService = new DayDivideTableStrategyServiceImpl();
            break;
          case "yyyyMM":
            divideTableService = new MonthDivideTableStrategyServiceImpl();
            break;
          case "yyyy":
            divideTableService = new YearDivideTableStrategyServiceImpl();
            break;
          default:
            throw new UnsupportedOperationException("暂时不支持该分表策略");
        }
        divideContext.setDivideTableService(divideTableService);
        if (divideContext.getAllFactTableName(divideVo, requestParams) != null) {
          miniCube.setSource(divideContext.getAllFactTableName(divideVo, requestParams));
        }
        MiniCubeSchema schema = (MiniCubeSchema) reportModel.getSchema();
        schema.getCubes().put(miniCube.getId(), miniCube);
        reportModel.setSchema(schema);
      }
      miniCube.setProductLine(dsDefine.getProductLine());
      questionModel.setCube(miniCube);
    } else {
      cube = getCubeWithExtendArea(reportModel, area);
      ((MiniCube) cube).setProductLine(dsDefine.getProductLine());
      questionModel.setCube(cube);
    }
    // 设置轴信息
    questionModel.setAxisMetas(buildAxisMeta(reportModel.getSchema(), area, queryAction));

    questionModel.setCubeId(area.getCubeId());
    // TODO 动态更新cube 针对查询过程中动态添加的属性 需要仔细考虑此处逻辑
    Set<Item> tmp = Sets.newHashSet();
    tmp.addAll(queryAction.getSlices().keySet());
    tmp.addAll(queryAction.getRows().keySet());
    try {
      DataSourceInfo dataSource =
          DataSourceConnectionServiceFactory.getDataSourceConnectionServiceInstance(
                  dsDefine.getDataSourceType().name())
              .parseToDataSourceInfo(dsDefine, securityKey);
      questionModel.setDataSourceInfo(dataSource);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    OrderDesc orderDesc = queryAction.getOrderDesc();
    if (orderDesc != null) {
      SortType sortType = SortType.valueOf(orderDesc.getOrderType());
      String uniqueName = "";
      if (DataModelUtils.isMeasure(orderDesc.getName(), cube)) {
        uniqueName = uniqueName + "[Measure].";
      } else {
        uniqueName = uniqueName + "[Dimension].";
      }
      uniqueName = uniqueName + "[" + orderDesc.getName() + "]";
      SortRecord sortRecord = new SortRecord(sortType, uniqueName, orderDesc.getRecordSize());
      questionModel.setSortRecord(sortRecord);
    }
    // TODO 此处没有考虑指标、维度交叉情况,如后续有指标维度交叉情况,此处需要调整
    questionModel.getQueryConditionLimit().setWarningAtOverFlow(false);
    if (queryAction.isNeedOthers()) {
      // TODO 需要开发通用工具包 将常量定义到通用工具包中
      questionModel.getRequestParams().put("NEED_OTHERS", "1");
    }
    // 设置请求参数信息
    if (requestParams != null) {
      for (String key : requestParams.keySet()) {
        Object value = requestParams.get(key);
        if (value != null && value instanceof String) {
          questionModel.getRequestParams().put(key, (String) value);
        }
      }
      // 设计器中, 设置分页信息
      if (requestParams.get(Constants.IN_EDITOR) != null
          && Boolean.valueOf(requestParams.get(Constants.IN_EDITOR).toString())) {
        questionModel.setPageInfo(pageInfo);
      }
    }
    if (area.getType() == ExtendAreaType.PLANE_TABLE) {
      questionModel.setQuerySource("SQL");
      // 对于平面表不使用汇总方式
      questionModel.setNeedSummary(false);
      // 设置分页信息
      questionModel.setPageInfo(pageInfo);
      // 针对平面表构建查询条件
      Map<String, MetaCondition> conditionsForPlaneTable =
          QueryConditionUtils.buildQueryConditionsForPlaneTable(reportModel, area, queryAction);
      questionModel.setQueryConditions(conditionsForPlaneTable);
    } else {
      questionModel.setQuerySource("TESSERACT");
      // 针对其他情况构建查询条件
      Map<String, MetaCondition> conditionsForPivotTable =
          QueryConditionUtils.buildQueryConditionsForPivotTable(reportModel, area, queryAction);
      questionModel.setQueryConditions(conditionsForPivotTable);
      if (queryAction.getDrillDimValues() == null
          || !queryAction.getDrillDimValues().isEmpty()
          || queryAction.isChartQuery()) {
        questionModel.setNeedSummary(false);
      } else {
        ExtendAreaType areaType =
            reportModel.getExtendById(queryAction.getExtendAreaId()).getType();
        if (areaType != ExtendAreaType.TABLE) {
          questionModel.setNeedSummary(false);
        } else {
          questionModel.setNeedSummary(needSummary(questionModel));
        }
      }

      if (questionModel.isNeedSummary()
          && "false".equals(area.getOtherSetting().get("needSummary"))) {
        questionModel.setNeedSummary(false);
      }
    }
    questionModel.setUseIndex(true);

    putSliceConditionIntoParams(queryAction, questionModel);
    questionModel.setFilterBlank(queryAction.isFilterBlank());
    return questionModel;
  }
  @Test
  public void testGetConnUrl() throws Exception {
    DataSourceDefine ds = new DataSourceDefine();
    ds.setDataSourceType(DataSourceType.H2);
    ds.setDbInstance("test");
    ds.setHostAndPort("127.0.0.1:3306");
    String connUrl =
        DataSourceType.H2.getPrefix()
            + ds.getHostAndPort()
            + DataSourceType.H2.getDiv()
            + ds.getDbInstance();
    Assert.assertEquals(connUrl, dsConnService.getDataSourceConnUrl(ds));

    ds.setDataSourceType(DataSourceType.MYSQL);
    ds.setDbInstance("test");
    ds.setHostAndPort("127.0.0.1:3306");
    ds.setEncoding("utf8");
    connUrl =
        DataSourceType.MYSQL.getPrefix()
            + ds.getHostAndPort()
            + DataSourceType.MYSQL.getDiv()
            + ds.getDbInstance()
            + "?useUniCode=true&characterEncoding="
            + ds.getEncoding();
    Assert.assertEquals(connUrl, dsConnService.getDataSourceConnUrl(ds));
  }
  @Test
  public void testParseToDataSourceInfo() throws Exception {
    // 数据库连接基本信息,并构建内存数据库H2
    String username = "******";
    String password = "******";
    String dbInstance = "testDB";
    String securityKey = "0000000000000000";
    String passwordEncrypt = AesUtil.getInstance().encryptAndUrlEncoding(password, securityKey);

    DataSourceDefine ds = new DataSourceDefine();
    DataSourceType.H2.setPrefix("jdbc:h2:mem://");
    ds.setDataSourceType(DataSourceType.H2);
    ds.setDbInstance(dbInstance);
    ds.setDbPwd(passwordEncrypt);
    ds.setDbUser(username);
    ds.setHostAndPort("127.0.0.1:3306");

    Assert.assertNotNull(dsConnService.parseToDataSourceInfo(ds, securityKey));

    ds.setDataSourceType(DataSourceType.MYSQL);
    ds.setDbInstance(dbInstance);
    ds.setDbPwd(passwordEncrypt);
    ds.setDbUser(username);
    ds.setHostAndPort("127.0.0.1:3306");

    Assert.assertNotNull(dsConnService.parseToDataSourceInfo(ds, securityKey));

    ds.setDataSourceType(DataSourceType.ORACLE);
    ds.setDbInstance(dbInstance);
    ds.setDbPwd(passwordEncrypt);
    ds.setDbUser(username);
    ds.setHostAndPort("127.0.0.1:3306");

    Assert.assertNotNull(dsConnService.parseToDataSourceInfo(ds, securityKey));

    ds.setDataSourceType(DataSourceType.TXT);
    ds.setDbInstance(dbInstance);
    ds.setDbPwd(passwordEncrypt);
    ds.setDbUser(username);
    ds.setHostAndPort("127.0.0.1:3306");

    Assert.assertNotNull(dsConnService.parseToDataSourceInfo(ds, securityKey));
    // 设置正确的数据库前缀
    DataSourceType.H2.setPrefix("jdbc:h2:tcp://");
    DataSourceType.H2.setDriver("org.h2.Driver");
  }