示例#1
0
 /**
  * trans cube
  *
  * @param cube
  * @return new Cube
  */
 public static Cube transformCube(Cube cube) {
   MiniCube newCube = (MiniCube) DeepcopyUtils.deepCopy(cube);
   final Map<String, Measure> measures = Maps.newConcurrentMap();
   cube.getMeasures()
       .values()
       .forEach(
           m -> {
             measures.put(m.getName(), m);
           });
   newCube.setMeasures(measures);
   final Map<String, Dimension> dimensions = Maps.newLinkedHashMap();
   cube.getDimensions()
       .values()
       .forEach(
           dim -> {
             MiniCubeDimension tmp = (MiniCubeDimension) DeepcopyUtils.deepCopy(dim);
             LinkedHashMap<String, Level> tmpLevel = Maps.newLinkedHashMap();
             dim.getLevels()
                 .values()
                 .forEach(
                     level -> {
                       level.setDimension(dim);
                       tmpLevel.put(level.getName(), level);
                     });
             tmp.setLevels(tmpLevel);
             dimensions.put(tmp.getName(), tmp);
           });
   newCube.setDimensions(dimensions);
   return newCube;
 }
示例#2
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;
  }