com.baidu.rigel.biplatform.ac.util.JsonUnSeriallizableUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.baidu.rigel.biplatform.ac.util.JsonUnSeriallizableUtils.java

Source

/**
 * Copyright (c) 2014 Baidu, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.baidu.rigel.biplatform.ac.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import com.baidu.rigel.biplatform.ac.exception.MiniCubeQueryException;
import com.baidu.rigel.biplatform.ac.minicube.MiniCube;
import com.baidu.rigel.biplatform.ac.minicube.MiniCubeMeasure;
import com.baidu.rigel.biplatform.ac.minicube.MiniCubeMember;
import com.baidu.rigel.biplatform.ac.model.Cube;
import com.baidu.rigel.biplatform.ac.model.Dimension;
import com.baidu.rigel.biplatform.ac.model.Level;
import com.baidu.rigel.biplatform.ac.model.Member;
import com.baidu.rigel.biplatform.ac.query.data.DataModel;
import com.baidu.rigel.biplatform.ac.query.data.HeadField;
import com.baidu.rigel.biplatform.ac.query.data.vo.MetaJsonDataInfo;
import com.baidu.rigel.biplatform.ac.query.data.vo.MetaJsonDataInfo.MetaType;

/**
 * json?????? 
 * 
 * @author xiaoming.chen
 *
 */
public class JsonUnSeriallizableUtils {

    /**
     * DataModeljson??DataModel???
     * 
     * @param dataModelJson DataModeljson
     * @return json??DataModel
     */
    public static DataModel dataModelFromJson(String dataModelJson) {
        if (StringUtils.isBlank(dataModelJson)) {
            throw new IllegalArgumentException("datamodel json is empty.");
        }
        DataModel dataModel = AnswerCoreConstant.GSON.fromJson(dataModelJson, DataModel.class);
        setHeadFieldParent(dataModel.getRowHeadFields(), null, null);
        setHeadFieldParent(dataModel.getColumnHeadFields(), null, null);
        return dataModel;

    }

    /**
     * ??
     * 
     * @param listFields ?
     * @param parentField 
     * @param parentLevelField 
     */
    public static void setHeadFieldParent(List<HeadField> listFields, HeadField parentField,
            HeadField parentLevelField) {
        if (CollectionUtils.isNotEmpty(listFields)) {
            listFields.forEach((field) -> {
                field.setParent(parentField);
                field.setParentLevelField(parentLevelField);
                // ??
                setHeadFieldParent(field.getChildren(), field, parentLevelField);
                // ??null
                setHeadFieldParent(field.getNodeList(), null, field);

            });
        }
    }

    /**
     * cubejson??cubecube?Dimensionleveldimension
     * 
     * @param cubeJson cubejson
     * @return ?cube
     */
    public static Cube parseCubeJson(String cubeJson) {
        if (StringUtils.isBlank(cubeJson)) {
            throw new IllegalArgumentException("cube json is blank.");
        }
        MiniCube cube = AnswerCoreConstant.GSON.fromJson(cubeJson, MiniCube.class);
        // ??DimensionlevelDimension?
        cube.getDimensions().values().forEach((dim) -> {
            dim.getLevels().forEach((k, v) -> {
                v.setDimension(dim);
            });
        });
        return cube;
    }

    /**
     * CubelevelDimension?
     * 
     * @param cube
     */
    public static void fillCubeInfo(Cube cube) {
        if (cube == null) {
            throw new IllegalArgumentException("cube is null");
        }
        if (MapUtils.isNotEmpty(cube.getDimensions())) {
            cube.getDimensions().values().forEach((dim) -> {
                dim.getLevels().forEach((k, v) -> {
                    v.setDimension(dim);
                });
            });
        }
        if (MapUtils.isNotEmpty(cube.getMeasures())) {
            cube.getMeasures().values().forEach((measure) -> {
                MiniCubeMeasure miniCubeMeasure = (MiniCubeMeasure) measure;
                miniCubeMeasure.setCube(cube);
            });
        }
    }

    /**
     * metaDataJson???Member member???Dimensionlevel??
     * 
     * @param cube ?membercube
     * @param metaDataJson member?
     * @return ??member
     * @throws IllegalArgumentException ?
     */
    public static Member parseMetaJson2Member(Cube cube, String metaDataJson) {
        if (StringUtils.isBlank(metaDataJson) || cube == null) {
            throw new IllegalArgumentException("param is illegal. cube:" + cube + " metaDataJson:" + metaDataJson);
        }
        MetaJsonDataInfo metaJsonDataInfo = AnswerCoreConstant.GSON.fromJson(metaDataJson, MetaJsonDataInfo.class);
        return parseMetaJson2Member(cube, metaJsonDataInfo);
    }

    /**
     * @param cube
     * @param metaJsonData
     * @return
     */
    public static MiniCubeMember parseMetaJson2Member(Cube cube, MetaJsonDataInfo metaJsonData) {
        if (metaJsonData == null || !metaJsonData.validate()) {
            throw new IllegalArgumentException("meta data json is blank.");
        }
        if (metaJsonData.getMetaType().equals(MetaType.Member)) {
            MiniCubeMember member = new MiniCubeMember(metaJsonData.getMemberName());
            member.generateUniqueName(metaJsonData.getMemberUniqueName());
            member.setCaption(metaJsonData.getMemberCaption());
            Dimension dimension = cube.getDimensions().get(metaJsonData.getDimensionName());
            Level level = dimension.getLevels().get(metaJsonData.getLevelName());
            member.setQueryNodes(metaJsonData.getQueryNodes());
            member.setLevel(level);
            if (CollectionUtils.isNotEmpty(metaJsonData.getChildren())) {
                List<MiniCubeMember> children = new ArrayList<MiniCubeMember>();
                for (MetaJsonDataInfo child : metaJsonData.getChildren()) {
                    children.add(parseMetaJson2Member(cube, child));
                }
                member.setChildren(children);
            }

            return member;
        }
        throw new UnsupportedOperationException("unsupported meta data type:" + metaJsonData.getMetaType());
    }

    /**
     * Member??metaDataJsonInfo??json
     * 
     * @param member ?member
     * @return ??member??
     */
    public static MetaJsonDataInfo parseMember2MetaJson(MiniCubeMember member) {
        if (member == null) {
            throw new IllegalArgumentException("member is illegal. member:" + member);
        }
        MetaJsonDataInfo metaJsonDataInfo = new MetaJsonDataInfo(MetaType.Member);
        metaJsonDataInfo.setDimensionName(member.getLevel().getDimension().getName());
        metaJsonDataInfo.setLevelName(member.getLevel().getName());
        metaJsonDataInfo.setMemberCaption(member.getCaption());
        metaJsonDataInfo.setMemberName(member.getName());
        metaJsonDataInfo.setMemberUniqueName(member.getUniqueName());
        metaJsonDataInfo.setQueryNodes(member.getQueryNodes());
        try {
            if (CollectionUtils.isNotEmpty(member.getChildren())) {
                for (Member child : member.getChildMembers(null, null, null)) {
                    metaJsonDataInfo.getChildren().add(parseMember2MetaJson((MiniCubeMember) child));
                }
            }
        } catch (MiniCubeQueryException e) {
            e.printStackTrace();
        }

        return metaJsonDataInfo;
    }

}