/**
  * 获取维度中的级别定义
  *
  * @param dimension 维度定义
  * @return 级别定义
  */
 private Level getLevel(Dimension dimension) {
   Level level = null;
   //        for (Map.Entry<String, Level> entry : dimension.getLevels().entrySet()) {
   //            level = entry.getValue();
   //        }
   if (dimension.getLevels() == null || dimension.getLevels().size() == 0) {
     return null;
   }
   level = (Level) dimension.getLevels().values().toArray()[0];
   return level;
 }
 /**
  * 构建引用参照
  *
  * @param factTable 事实表名称
  * @param dimension 维度定义
  * @param level 级别
  * @return
  */
 private ReferenceDefine buildReference(String factTable, Dimension dimension, Level level) {
   ReferenceDefine reference = new ReferenceDefine();
   reference.setMajorColumn(dimension.getFacttableColumn());
   reference.setSalveColumn(level.getPrimaryKey());
   reference.setMajorTable(factTable); // 事实表名称
   return reference;
 }
  /**
   * 构建回调维度表定义
   *
   * @param factTable 事实表名称
   * @param dimension 维度定义
   * @param level 级别定义
   * @return
   */
  private CallbackDimTableMetaDefine buildCallDimTable(
      String factTable, Dimension dimension, Level level) {
    CallbackDimTableMetaDefine dimTable = new CallbackDimTableMetaDefine();
    dimTable.setName(dimension.getTableName());
    ReferenceDefine reference = buildReference(factTable, dimension, level);
    dimTable.setReference(reference);
    CallbackLevel tmp = (CallbackLevel) level;
    Map<String, String> params = Maps.newHashMap();
    params.putAll(tmp.getCallbackParams());
    dimTable.setParams(params);
    dimTable.setUrl(tmp.getCallbackUrl());
    int interval = tmp.getRefreshInterval();
    int refreshType = 0;
    if (interval == 0) {
      refreshType = 1;
    } else if (interval == -1) {
      refreshType = 2;
    } else {
      refreshType = 3;
    }
    // 设置刷新时间间隔
    dimTable.addConfigItem(CallbackDimTableMetaDefine.REF_INTERNAL_KEY, String.valueOf(interval));

    // 设置刷新时间类型
    dimTable.addConfigItem(CallbackDimTableMetaDefine.REFRESH_KEY, String.valueOf(refreshType));
    // TODO 添加刷新类型
    dimTable.addColumn(this.buildColumn(level));
    return dimTable;
  }
 /**
  * 构建时间维度表
  *
  * @param factTable 事实表
  * @param dimension 维度定义
  * @param level 级别定义
  * @return 时间维度表定义
  */
 private DimTableMetaDefine buildTimeDimTable(String factTable, Dimension dimension, Level level) {
   TimeDimTableMetaDefine dimTable = new TimeDimTableMetaDefine(TimeDimType.STANDARD_TIME);
   dimTable.setName(dimension.getTableName());
   ReferenceDefine reference = buildReference(factTable, dimension, level);
   dimTable.setReference(reference);
   dimTable.setFormat(((TimeDimension) dimension).getTimeFormat());
   dimTable.addColumn(this.buildColumn(level));
   return dimTable;
 }
 /**
  * 构建维度表
  *
  * @param cube 立方体
  * @return 维度表定义
  */
 private List<DimTableMetaDefine> buildDimTables(MiniCube cube) {
   Collection<Dimension> dimensions = cube.getDimensions().values();
   if (dimensions == null || dimensions.size() == 0) {
     return new ArrayList<DimTableMetaDefine>();
   }
   Map<String, DimTableMetaDefine> dimTableCache = new HashMap<String, DimTableMetaDefine>();
   List<DimTableMetaDefine> rs = new ArrayList<DimTableMetaDefine>();
   String factTableName = cube.getSource();
   for (Dimension dimension : dimensions) {
     if (dimension.getLevels().size() > 1) { // 组合维度 不处理
       continue;
     }
     String dimTableName = dimension.getTableName();
     if (dimTableName == null || dimTableName.equals(factTableName)) {
       continue;
     }
     // 同一个维度表中的列,只需要加入维度表中就可以了
     if (dimTableCache.containsKey(dimension.getTableName())) {
       DimTableMetaDefine dimTable = dimTableCache.get(dimension.getTableName());
       ColumnMetaDefine column = buildColumn(this.getLevel(dimension));
       dimTable.addColumn(column);
     } else { // 构建新的维度表,并加入当前列
       DimTableMetaDefine dimTable = getDimTableDefineWithLevelType(cube.getSource(), dimension);
       ColumnMetaDefine column = buildColumn(this.getLevel(dimension));
       dimTable.addColumn(column);
       dimTableCache.put(dimension.getTableName(), dimTable);
     }
   }
   rs.addAll(dimTableCache.values());
   return rs;
 }
  /**
   * 构建维度表定义
   *
   * @param dimension 维度
   * @return 维度表定义
   */
  private DimTableMetaDefine getDimTableDefineWithLevelType(String factTable, Dimension dimension) {
    Level level = getLevel(dimension);
    if (level == null) {
      throw new RuntimeException("dimension's level is empty : " + dimension);
    }
    switch (level.getType()) {
      case REGULAR:
        {
          StandardDimTableMetaDefine dimTable = new StandardDimTableMetaDefine();
          dimTable.setName(dimension.getTableName());
          ReferenceDefine reference = buildReference(factTable, dimension, level);
          dimTable.setReference(reference);
          dimTable.addColumn(buildColumn(level));
          return dimTable;
        }
      case CALL_BACK:
        {
          CallbackDimTableMetaDefine dimTable = buildCallDimTable(factTable, dimension, level);
          return dimTable;
        }
      case PARENT_CHILD:
        // TODO 目前没有处理
        return null;
      case USER_CUSTOM:
        {
          // TODO 目前没有处理
          //                UserDefineDimTableMetaDefine dimTable = buildUserDefineTable(factTable,
          // dimension,
          //                    level);
          //                return dimTable;
          return null;
        }
      default:
        break;
    }

    return buildTimeDimTable(factTable, dimension, level);
  }
Example #7
0
 /**
  * @param dim -- Dimension
  * @return Dimension
  */
 public static Dimension convertDim2Dim(Dimension dim) {
   StandardDimension rs = new StandardDimension(dim.getName());
   rs.setCaption(dim.getCaption());
   rs.setDescription(dim.getDescription());
   rs.setTableName(dim.getTableName());
   rs.setFacttableCaption(dim.getFacttableCaption());
   rs.setFacttableColumn(dim.getFacttableColumn());
   rs.setPrimaryKey(dim.getPrimaryKey());
   rs.setType(dim.getType());
   rs.setVisible(true);
   rs.setId(dim.getId());
   rs.setName(dim.getName());
   LinkedHashMap<String, Level> levels = Maps.newLinkedHashMap();
   dim.getLevels()
       .values()
       .forEach(
           level -> {
             level.setDimension(dim);
             levels.put(level.getName(), level);
           });
   rs.setLevels(levels);
   return rs;
 }
Example #8
0
  /**
   * 获取扩展区域包含的立方体定义
   *
   * @param reportModel 报表模型
   * @param area 扩展区域
   * @return 立方体定义
   * @throws QueryModelBuildException
   */
  public static Cube getCubeWithExtendArea(ReportDesignModel reportModel, ExtendArea area)
      throws QueryModelBuildException {
    Cube oriCube = getCubeFromReportModel(reportModel, area);
    // 对于可选指标,默认将事实表包含的所有列作为查询条件
    if ("true".equals(area.getOtherSetting().get(Constants.CAN_CHANGED_MEASURE))) {
      Cube rs = transformCube(oriCube);
      modifyMeasures(rs.getMeasures(), rs);
      return rs;
    }
    Map<String, List<Dimension>> filterDims = collectFilterDim(reportModel);
    MiniCube cube = new MiniCube(area.getCubeId());
    String areaId = area.getId();
    LogicModel logicModel = area.getLogicModel();
    if (area.getType() == ExtendAreaType.SELECTION_AREA
        || area.getType() == ExtendAreaType.LITEOLAP_CHART
        || area.getType() == ExtendAreaType.LITEOLAP_TABLE) {
      LiteOlapExtendArea liteOlapArea =
          (LiteOlapExtendArea) reportModel.getExtendById(area.getReferenceAreaId());
      logicModel = liteOlapArea.getLogicModel();
      areaId = area.getReferenceAreaId();
    }
    if (logicModel == null) {
      throw new QueryModelBuildException("logic model is empty");
    }
    Item[] items = logicModel.getItems(area.getType() != ExtendAreaType.TABLE);
    Map<String, Dimension> dimensions = new HashMap<String, Dimension>();
    Map<String, Measure> measures = new HashMap<String, Measure>();

    for (Item item : items) {
      OlapElement olapElement = oriCube.getDimensions().get(item.getOlapElementId());
      if (olapElement == null) { // 维度不存在或者可能是指标信息
        olapElement = oriCube.getMeasures().get(item.getOlapElementId());
        if (olapElement != null) {
          Measure measure = (Measure) olapElement;
          measures.put(measure.getName(), measure);
        }
      } else {
        MiniCubeDimension dim = (MiniCubeDimension) DeepcopyUtils.deepCopy(olapElement);
        dim.setLevels(Maps.newLinkedHashMap());
        ;
        ((Dimension) olapElement)
            .getLevels()
            .values()
            .forEach(
                level -> {
                  level.setDimension(dim);
                  dim.getLevels().put(level.getName(), level);
                });
        dimensions.put(dim.getName(), dim);
      }
    }
    if (area.getType() == ExtendAreaType.LITEOLAP) {
      /** TODO 把liteOlap中候选的维度和指标加入到items里面 */
      Map<String, Item> candDims = ((LiteOlapExtendArea) area).getCandDims();
      Schema schema = reportModel.getSchema();
      String cubeId = area.getCubeId();
      for (String elementId : candDims.keySet()) {
        OlapElement element =
            ReportDesignModelUtils.getDimOrIndDefineWithId(schema, cubeId, elementId);
        MiniCubeDimension dim = (MiniCubeDimension) DeepcopyUtils.deepCopy(element);
        dim.setLevels(Maps.newLinkedHashMap());
        ((Dimension) element)
            .getLevels()
            .values()
            .forEach(
                level -> {
                  level.setDimension(dim);
                  dim.getLevels().put(level.getName(), level);
                });
        dimensions.put(element.getName(), (Dimension) element);
      }
      Map<String, Item> candInds = ((LiteOlapExtendArea) area).getCandInds();
      for (String elementId : candInds.keySet()) {
        OlapElement element =
            ReportDesignModelUtils.getDimOrIndDefineWithId(schema, cubeId, elementId);
        if (element instanceof CallbackMeasure) {
          CallbackMeasure m = DeepcopyUtils.deepCopy((CallbackMeasure) element);
          String url = ((CallbackMeasure) element).getCallbackUrl();
          m.setCallbackUrl(HttpUrlUtils.getBaseUrl(url));
          m.setCallbackParams(HttpUrlUtils.getParams(url));
          measures.put(m.getName(), m);
        } else {
          measures.put(element.getName(), (Measure) element);
        }
      }
    }
    if (filterDims != null) {
      List<Dimension> dims = filterDims.get(area.getCubeId());
      if (dims != null) {
        for (Dimension dim : dims) {
          if (dim != null) {
            dimensions.put(dim.getName(), dim);
          }
        }
      }

      // TODO 处理不同cube共用同一查询条件情况
      filterDims.forEach(
          (key, dimArray) -> {
            if (key != null && !key.equals(area.getCubeId())) {
              dimArray
                  .stream()
                  .filter(
                      dim -> {
                        return dim instanceof TimeDimension;
                      })
                  .forEach(
                      dim -> {
                        for (Dimension tmp : oriCube.getDimensions().values()) {
                          if (dim.getName().equals(tmp.getName())) {
                            MiniCubeDimension tmpDim =
                                (MiniCubeDimension) DeepcopyUtils.deepCopy(dim);
                            tmpDim.setLevels((LinkedHashMap<String, Level>) tmp.getLevels());
                            tmpDim.setFacttableColumn(tmp.getFacttableColumn());
                            tmpDim.setFacttableCaption(tmp.getFacttableCaption());
                            dimensions.put(tmpDim.getName(), tmpDim);
                          }
                        }
                      });
            }
          });
    }
    cube.setDimensions(dimensions);
    modifyMeasures(measures, oriCube);
    cube.setMeasures(measures);
    cube.setSource(((MiniCube) oriCube).getSource());
    cube.setPrimaryKey(((MiniCube) oriCube).getPrimaryKey());
    cube.setId(oriCube.getId() + "_" + areaId);
    return cube;
  }
Example #9
0
  /**
   * 判断是否需要汇总
   *
   * @param questionModel
   * @return
   */
  private static boolean needSummary(QuestionModel questionModel) {
    ConfigQuestionModel configQuestionModel = (ConfigQuestionModel) questionModel;
    for (AxisMeta meta : questionModel.getAxisMetas().values()) {
      if (meta.getAxisType() == AxisType.ROW) {
        for (String str : meta.getCrossjoinDims()) {
          DimensionCondition condition =
              (DimensionCondition) questionModel.getQueryConditions().get(str);
          Dimension dim =
              configQuestionModel.getCube().getDimensions().get(condition.getMetaName());

          if (dim != null && dim.getType() == DimensionType.CALLBACK) {
            if (condition.getQueryDataNodes() != null
                && condition.getQueryDataNodes().size() == 1) {
              List<Member> members =
                  dim.getLevels()
                      .values()
                      .toArray(new Level[0])[0]
                      .getMembers(
                          configQuestionModel.getCube(),
                          configQuestionModel.getDataSourceInfo(),
                          configQuestionModel.getRequestParams());
              String uniqueName = condition.getQueryDataNodes().get(0).getUniqueName();
              if (MetaNameUtil.isAllMemberUniqueName(uniqueName)) {
                return false;
              } else if (meta.getCrossjoinDims().size() > 1) {
                return false;
              } else if (members.size() == 1) {
                MiniCubeMember m = (MiniCubeMember) members.get(0);
                if (m.getChildren() != null && m.getChildren().size() > 1) {
                  return false;
                } else if (m.getChildren() == null) {
                  return false;
                }
              }
            } else if (CollectionUtils.isEmpty(condition.getQueryDataNodes())) {
              return false;
            }
            return true;
          }
          if (condition.getQueryDataNodes() == null || condition.getQueryDataNodes().isEmpty()) {
            return false;
          } else {
            List<QueryData> queryDatas = condition.getQueryDataNodes();
            for (QueryData queryData : queryDatas) {
              if (MetaNameUtil.isAllMemberName(queryData.getUniqueName())) {
                return false;
              } else {
                // TODO 这里需要修改 需要修改为可配置方式
                String[] tmp = MetaNameUtil.parseUnique2NameArray(queryData.getUniqueName());
                if (tmp[tmp.length - 1].contains(":")) {
                  return false;
                }
              }
            }
          }
        }
        break;
      }
    }
    return true;
  }