com.baidu.rigel.biplatform.tesseract.meta.impl.TimeDimensionMemberServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.baidu.rigel.biplatform.tesseract.meta.impl.TimeDimensionMemberServiceImpl.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.tesseract.meta.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baidu.rigel.biplatform.ac.minicube.MiniCubeMember;
import com.baidu.rigel.biplatform.ac.model.Cube;
import com.baidu.rigel.biplatform.ac.model.Level;
import com.baidu.rigel.biplatform.ac.model.LevelType;
import com.baidu.rigel.biplatform.ac.model.Member;
import com.baidu.rigel.biplatform.ac.query.data.DataSourceInfo;
import com.baidu.rigel.biplatform.ac.util.TimeRangeDetail;
import com.baidu.rigel.biplatform.ac.util.TimeUtils;
import com.baidu.rigel.biplatform.tesseract.exception.MetaException;
import com.baidu.rigel.biplatform.tesseract.meta.DimensionMemberService;
import com.google.common.collect.Lists;

/**
 * 
 * ?
 *
 * @author david.wang
 * @version 1.0.0.1
 */
@Service(DimensionMemberService.TIME_MEMBER_SERVICE)
public class TimeDimensionMemberServiceImpl implements DimensionMemberService {

    /**
     * ??
     */
    private static final String[] QUARTER_NAMES = new String[] { "Q1", "Q2", "Q3", "Q4" };

    /**
     * 
     */
    private static final String[][] QUARTER_MONTH_MAPPING = new String[][] {
            new String[] { "0101", "0201", "0301" }, new String[] { "0401", "0501", "0601" },
            new String[] { "0701", "0801", "0901" }, new String[] { "1001", "1101", "1201" } };

    /**
     * 
     * @param cube
     * @param name
     * @param level
     * @param dataSourceInfo
     * @param parentMember
     * @param params
     * @return
     * @throws MetaException
     */
    private List<MiniCubeMember> getMembers(Cube cube, String name, Level level, DataSourceInfo dataSourceInfo,
            MiniCubeMember parentMember, Map<String, String> params) throws MetaException {
        List<MiniCubeMember> members = Lists.newArrayList();
        // ??????

        if (parentMember != null) {
            return getMembers(cube, level, dataSourceInfo, parentMember, params);
        }
        // ??level???
        // ??????????
        try {
            getMembersByStartDate(level, name, members);
        } catch (Exception e) {
            throw new MetaException(e.getMessage(), e);
        }
        return members;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<MiniCubeMember> getMembers(Cube cube, Level level, DataSourceInfo dataSourceInfo,
            Member parentMember, Map<String, String> params) throws MetaException {
        List<MiniCubeMember> members = Lists.newArrayList();
        // ??????

        if (parentMember != null) {
            switch (parentMember.getLevel().getType()) {
            case TIME_YEARS:
                List<MiniCubeMember> membersWithYearParent = genMembersWithYearParent(level, parentMember);
                members.addAll(membersWithYearParent);
                return members;
            case TIME_QUARTERS:
                List<MiniCubeMember> membersWithQuarterParent = genMembersWithQuarterParent(level, parentMember);
                members.addAll(membersWithQuarterParent);
                return members;
            case TIME_MONTHS:
                List<MiniCubeMember> membersWithMonthParent = genMembersWithMonthParent(level, parentMember);
                members.addAll(membersWithMonthParent);
                return members;
            case TIME_WEEKS:
                List<MiniCubeMember> membersWithWeekParent = genMembersWithWeekParent(level, parentMember);
                members.addAll(membersWithWeekParent);
                return members;
            case TIME_DAYS:
                List<MiniCubeMember> membersWithDayParent = genMembersWithDayParent(level, parentMember);
                members.addAll(membersWithDayParent);
                return members;
            default:
                throw new IllegalArgumentException(
                        "Invalidate time dimension level type : " + parentMember.getLevel().getType());
            }
        }
        // ??level???
        // ??????????
        genDefaultMembers(level, parentMember, members);
        return members;
    }

    /**
     * 
     * @param level
     * @param parentMember
     * @return
     */
    private List<MiniCubeMember> genMembersWithDayParent(Level level, Member parentMember) {
        // All??
        if (parentMember.isAll()) {
            return genMembersWithDayParentForAll(level, parentMember);
        } else {
            // ?All??
            try {
                return genDayMembersWithParent(level, parentMember, TimeUtils.getMonthDays(null));
            } catch (Exception e) {
                return Lists.newArrayList();
            }
        }
    }

    /**
     * ???
     * 
     * @param level
     * @param parentMember
     * @return
     */
    private List<MiniCubeMember> genMembersWithDayParentForAll(Level level, Member parentMember) {
        List<MiniCubeMember> members = Lists.newArrayList();
        Calendar calNow = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        while (cal.before(calNow) || (cal.compareTo(calNow) == 0)) {
            String day = sf.format(cal.getTime());
            MiniCubeMember dayMember = new MiniCubeMember(day);
            dayMember.setCaption(day);
            dayMember.setLevel(level);
            dayMember.setParent(parentMember);
            dayMember.setName(day);
            dayMember.setVisible(true);
            dayMember.getQueryNodes().add(day);
            members.add(dayMember);
            cal.add(Calendar.DATE, 1);
        }
        return members;
    }

    /**
     * ????
     * 
     * @param level
     * @param parentMember
     * @return
     */
    private List<MiniCubeMember> genMembersWithWeekParent(Level level, Member parentMember) {
        // All??
        if (parentMember.isAll()) {
            try {
                // TODO ?? jiangyichao at 2014-11-11
                return genDayMembersWithWeekParentForAll(level, parentMember);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        } else {
            // ?
            switch (level.getType()) {
            case TIME_DAYS:
                try {
                    String name = parentMember.getName();
                    Date date = TimeRangeDetail.getTime(name);
                    TimeRangeDetail monthRange = TimeUtils.getWeekDays(date);
                    return genDayMembersWithParent(level, parentMember, monthRange);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            case TIME_MONTHS:
            case TIME_YEARS:
            case TIME_QUARTERS:
            case TIME_WEEKS:
            default:
                throw new IllegalArgumentException("Invalidate level type : " + level.getType()
                        + " with parent type : " + parentMember.getLevel().getType());
            }
        }
    }

    /**
     * ??
     * 
     * @param year
     * @return
     */
    private List<MiniCubeMember> genDayMembersWithWeekParentForAll(Level level, Member parentMember)
            throws Exception {
        List<MiniCubeMember> members = Lists.newArrayList();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int weekNow = cal.get(Calendar.WEEK_OF_YEAR);
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);
        Date firstWeek = getFirstDayOfWeek(cal.getTime());
        cal.setTime(firstWeek);
        int week = 1;
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        while (week <= weekNow) {
            String day = sf.format(cal.getTime());
            MiniCubeMember dayMember = new MiniCubeMember(day);
            String caption = year + "" + week + "-" + day;
            dayMember.setCaption(caption);
            dayMember.setLevel(level);
            dayMember.setParent(parentMember);
            dayMember.setName(day);
            dayMember.setVisible(true);
            for (int i = 0; i <= 6; i++) {
                day = sf.format(cal.getTime());
                dayMember.getQueryNodes().add(day);
                cal.add(Calendar.DATE, 1);
            }
            members.add(dayMember);
            // cal.add(Calendar.DATE, 1);
            week++;
        }
        return members;
    }

    /**
     * ??
     * 
     * @param date
     * @return
     */
    private Date getFirstDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        return cal.getTime();
    }

    /**
     * ????
     * 
     * @param level
     * @param parentMember
     * @return
     */
    private List<MiniCubeMember> genMembersWithMonthParent(Level level, Member parentMember) {
        if (parentMember.isAll()) {
            return genMembersWithMonthParentForAll(level, parentMember);
        } else {
            // [Time].[year].[month]"
            // eg:[Time].[2014].[01]
            String parentName = parentMember.getName();
            switch (level.getType()) {
            // ??
            case TIME_DAYS:
                String year = parentName.substring(0, 4);
                String month = parentName.substring(4, 6);
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.YEAR, Integer.valueOf(year));
                cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                TimeRangeDetail monthRange = TimeUtils.getMonthDays(cal.getTime(), 0, 0);
                try {
                    return genDayMembersWithParent(level, parentMember, monthRange);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                // TODO ??
            case TIME_MONTHS:
            case TIME_YEARS:
            case TIME_QUARTERS:
            case TIME_WEEKS:
                // TODO ??
                return genMembersFromOtherToWeek(level, parentMember);
            default:
                throw new IllegalArgumentException("Invalidate level type : " + level.getType()
                        + " with parent type : " + parentMember.getLevel().getType());
            }
        }
    }

    /**
     * 
     * @param level
     * @param parentMember
     * @param members
     * 
     */
    private List<MiniCubeMember> genMembersWithMonthParentForAll(Level level, Member parentMember) {
        List<MiniCubeMember> members = Lists.newArrayList();
        Calendar cal = Calendar.getInstance();
        int nowMonth = cal.get(Calendar.MONTH); // ?-1
        int year = cal.get(Calendar.YEAR); // ?
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        for (int i = 0; i <= nowMonth; i++) {
            cal.set(year, i, 1); // ?
            String day = sf.format(cal.getTime());
            String month = day.substring(4, 6);
            String caption = year + "" + month + "";
            MiniCubeMember firstDayOfMonth = new MiniCubeMember(day);
            firstDayOfMonth.setCaption(caption);
            firstDayOfMonth.setLevel(level);
            firstDayOfMonth.setParent(parentMember);
            firstDayOfMonth.setName(day);
            firstDayOfMonth.setVisible(true);
            int daysOfMonth = cal.getActualMaximum(Calendar.DATE);
            for (int j = 0; j < daysOfMonth; j++) {
                firstDayOfMonth.getQueryNodes().add(sf.format(cal.getTime()));
                cal.add(Calendar.DATE, 1);
            }
            members.add(firstDayOfMonth);
        }
        return members;
    }

    /**
     * ?????
     * 
     * @param level
     * @param parentMember
     * @return
     * 
     */
    private List<MiniCubeMember> genMembersWithQuarterParent(Level level, Member parentMember) {
        if (parentMember.isAll()) {
            return genMembersWithQuarterParentForAll(level, parentMember);
        } else {
            // [Time].[year].[quarter]"
            // eg:[Time].[2014].[Q1]
            String name = parentMember.getName();
            String year = name.substring(0, 4);
            String month = name.substring(4, 6);
            String quarterStr = "Q" + (Integer.valueOf(month) / 3 + 1);
            String[] tmpArray = { "[Time]", "[" + year + "]", "[" + quarterStr + "]" };
            int quarterIndex = Integer.valueOf(tmpArray[2].substring(2, 3)) - 1;
            switch (level.getType()) {
            case TIME_MONTHS:
                return genMonthMemberWithQuarterParent(level, parentMember, name, tmpArray, quarterIndex);
            case TIME_DAYS:
                String time = tmpArray[1].substring(1, 5) + QUARTER_MONTH_MAPPING[quarterIndex][1];
                try {
                    TimeRangeDetail monthRange = TimeUtils.getMonthDays(TimeRangeDetail.getTime(time), 1, 1);
                    try {
                        return genDayMembersWithParent(level, parentMember, monthRange);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            case TIME_YEARS:
            case TIME_QUARTERS:
            case TIME_WEEKS:
                return genMembersFromOtherToWeek(level, parentMember);
            default:
                throw new IllegalArgumentException("Invalidate level type : " + level.getType()
                        + " with parent type : " + parentMember.getLevel().getType());
            }
        }
    }

    /**
     * ??
     * 
     * @param level
     * @param parentMember
     * @return
     */
    private List<MiniCubeMember> genMembersWithQuarterParentForAll(Level level, Member parentMember) {
        List<MiniCubeMember> members = Lists.newArrayList();
        Calendar cal = Calendar.getInstance(); // ?
        cal.set(Calendar.DAY_OF_MONTH, 1); // 1?
        int nowMonth = cal.get(Calendar.MONTH) + 1; // ?
        int quarterIndex = nowMonth / 3; // 
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        for (int i = 0; i <= quarterIndex; i++) {
            cal.set(Calendar.MONTH, i * 3);// 
            cal.set(Calendar.DATE, 1);
            Calendar calEnd = Calendar.getInstance();
            calEnd.setTime(cal.getTime());
            calEnd.add(Calendar.MONTH, 2);
            calEnd.add(Calendar.DATE, calEnd.getActualMaximum(Calendar.DATE) - 1); // 
            String day = sf.format(cal.getTime());
            String year = day.substring(0, 4);
            String caption = year + "" + (i + 1) + "";
            MiniCubeMember firstDayOfQuarter = new MiniCubeMember(day);
            firstDayOfQuarter.setCaption(caption);
            firstDayOfQuarter.setLevel(level);
            firstDayOfQuarter.setParent(parentMember);
            firstDayOfQuarter.setName(day);
            firstDayOfQuarter.setVisible(true);
            while (cal.before(calEnd) || (cal.compareTo(calEnd) == 0)) {
                firstDayOfQuarter.getQueryNodes().add(sf.format(cal.getTime()));
                cal.add(Calendar.DATE, 1);
            }
            members.add(firstDayOfQuarter);
        }
        return members;
    }

    /**
     * ???
     * 
     * @param level
     * @param parentMember
     * @param time
     * @return
     * @throws Exception
     * 
     */
    private List<MiniCubeMember> genDayMembersWithParent(Level level, Member parentMember, TimeRangeDetail range)
            throws Exception {
        String[] days = range.getDays();
        List<MiniCubeMember> members = Lists.newArrayList();
        for (String day : days) {
            MiniCubeMember dayMember = new MiniCubeMember(day);
            dayMember.setCaption(day);
            dayMember.setLevel(level);
            dayMember.setParent(parentMember);
            dayMember.setName(day);
            dayMember.setVisible(true);
            dayMember.getQueryNodes().add(day);
            members.add(dayMember);
        }
        return members;
    }

    /**
     * ?????
     * 
     * @param level
     * @param parentMember
     * @param name
     * @param tmpArray
     * @param quarterIndex
     * 
     */
    private List<MiniCubeMember> genMonthMemberWithQuarterParent(Level level, Member parentMember, String name,
            String[] tmpArray, int quarterIndex) {
        List<MiniCubeMember> members = Lists.newArrayList();
        Calendar cal = Calendar.getInstance();
        String[] months = QUARTER_MONTH_MAPPING[quarterIndex];
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        for (String month : months) {
            String memberName = "[Time]." + tmpArray[1] + ".[" + month.substring(0, 2) + "]";
            MiniCubeMember monthMember = new MiniCubeMember(memberName);
            monthMember.setCaption(tmpArray[1].substring(1, 5) + "" + month.substring(0, 2) + "");
            monthMember.setLevel(level);
            monthMember.setName(tmpArray[1].substring(1, 5) + month);
            monthMember.setParent(parentMember);
            monthMember.setVisible(true);
            cal.set(Calendar.YEAR, Integer.valueOf(tmpArray[1].substring(1, 5)));
            cal.set(Calendar.MONTH, Integer.valueOf(month.substring(0, 2)) - 1);
            cal.set(Calendar.DATE, 1);
            int daysOfMonth = cal.getActualMaximum(Calendar.DATE);
            for (int i = 0; i < daysOfMonth; i++) {
                String day = sf.format(cal.getTime());
                monthMember.getQueryNodes().add(day);
                cal.add(Calendar.DATE, 1);
            }
            members.add(monthMember);
        }
        return members;
    }

    /**
     * ?????
     * 
     * @param level
     * @param parentMember
     */
    private List<MiniCubeMember> genMembersWithYearParent(Level level, Member parentMember) {
        int year = Integer.valueOf(parentMember.getCaption());
        switch (level.getType()) {
        case TIME_QUARTERS:
            return genQuarterMembersWithYear(year, level, parentMember);
        case TIME_MONTHS:
            return genMonthMembersWithYear(year, level, parentMember);
        case TIME_DAYS:
            return genDayOfYearMembers(level, parentMember, year);
        case TIME_WEEKS:
        case TIME_YEARS:
        default:
            throw new IllegalArgumentException("Invalidate level type : " + level.getType() + " with parent type : "
                    + parentMember.getLevel().getType());

        }
    }

    /**
     * 
     * @param level
     * @param parentMember
     * @param year
     * @return
     * 
     */
    private List<MiniCubeMember> genDayOfYearMembers(Level level, Member parentMember, int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        TimeRangeDetail yearDays = TimeUtils.getYearDays(cal.getTime());
        String[] days = yearDays.getDays();
        List<MiniCubeMember> members = Lists.newArrayList();
        for (String day : days) {
            MiniCubeMember dayMember = new MiniCubeMember(day);
            dayMember.setCaption(day);
            dayMember.setLevel(level);
            dayMember.setParent(parentMember);
            dayMember.setName(day);
            dayMember.setVisible(true);
            members.add(dayMember);
        }
        return members;
    }

    /**
     * 
     * @param level
     * @param parentMember
     * @param members
     * 
     */
    private void genDefaultMembers(Level level, Member parentMember, List<MiniCubeMember> members) {
        switch (level.getType()) {
        case TIME_YEARS:
            MiniCubeMember member = genMemberWithCurrentYear(level, parentMember);
            members.add(member);
            break;
        case TIME_QUARTERS:
            int quarterYear = TimeUtils.getCurrentYear();
            List<MiniCubeMember> quarterMembers = genQuarterMembersWithYear(quarterYear, level, parentMember);
            members.addAll(quarterMembers);
            break;
        case TIME_MONTHS:
            int year = TimeUtils.getCurrentYear();
            List<MiniCubeMember> monthMembers = genMonthMembersWithYear(year, level, parentMember);
            members.addAll(monthMembers);
            break;
        case TIME_WEEKS:
            MiniCubeMember weekMember = genWeekMemberWithCurrentYear(level, parentMember);
            members.add(weekMember);
            break;
        case TIME_DAYS:
            MiniCubeMember dayMember = genDayMemberWithCurrentTime(level, parentMember);
            members.add(dayMember);
            break;
        default:
            throw new IllegalStateException("Invalidate time dimension level type : " + level.getType());
        }
    }

    /**
     * 
     * @param level
     * @param parentMember
     * @return
     * 
     */
    private MiniCubeMember genDayMemberWithCurrentTime(Level level, Member parentMember) {
        String current = TimeUtils.getDays(0, 0).getStart();
        MiniCubeMember dayMember = new MiniCubeMember(current);
        dayMember.setCaption(current);
        dayMember.setLevel(level);
        dayMember.setParent(parentMember);
        dayMember.setName(current);
        dayMember.setVisible(true);
        return dayMember;
    }

    /**
     * 
     * @param level
     * @param parentMember
     * @return
     * 
     */
    private MiniCubeMember genWeekMemberWithCurrentYear(Level level, Member parentMember) {
        String firstDayOfWeek = TimeUtils.getWeekDays(null).getStart();
        MiniCubeMember weekMember = new MiniCubeMember(firstDayOfWeek);
        try {
            weekMember.setCaption(
                    firstDayOfWeek.substring(0, 4) + "-" + TimeUtils.getWeekIndex(firstDayOfWeek) + "W");
        } catch (Exception e) {
            throw new RuntimeException(
                    "Invalidate date formate, expected [yyyyMMdd] " + "partten, but was : " + firstDayOfWeek);
        }
        weekMember.setLevel(level);
        weekMember.setParent(parentMember);
        weekMember.setVisible(true);
        return weekMember;
    }

    /**
     * 
     * @param level
     * @param parentMember
     * 
     */
    private List<MiniCubeMember> genMonthMembersWithYear(int year, Level level, Member parentMember) {
        List<MiniCubeMember> rs = Lists.newArrayList();
        for (int i = 1; i <= 12; ++i) {
            String name = "[Time].[" + year + "].[";
            if (i < 10) {
                name += 0;
            }
            name = name + i + "]";
            MiniCubeMember monthMember = new MiniCubeMember(name);
            monthMember.setCaption(i + "");
            monthMember.setLevel(level);
            monthMember.setName(name);
            monthMember.setParent(parentMember);
            monthMember.setVisible(true);
            rs.add(monthMember);
        }
        return rs;
    }

    /**
     * 
     * @param level
     * @param parentMember
     * 
     */
    private List<MiniCubeMember> genQuarterMembersWithYear(int year, Level level, Member parentMember) {
        List<MiniCubeMember> rs = Lists.newArrayList();
        for (int i = 0; i < 4; ++i) {
            String name = "[Time].[" + year + "].[" + QUARTER_NAMES[i] + "]";
            MiniCubeMember quarterMember = new MiniCubeMember(name);
            quarterMember.setCaption(QUARTER_NAMES[i]);
            quarterMember.setLevel(level);
            quarterMember.setName(name);
            quarterMember.setParent(parentMember);
            quarterMember.setVisible(true);
            rs.add(quarterMember);
        }
        return rs;
    }

    /**
     * 
     * ??????
     * 
     * @param level
     * @param parentMember
     * @return
     * 
     */
    private MiniCubeMember genMemberWithCurrentYear(Level level, Member parentMember) {
        int year = TimeUtils.getCurrentYear();
        MiniCubeMember member = new MiniCubeMember("");
        member.setCaption(String.valueOf(year));
        member.setLevel(level);
        member.setParent(parentMember);
        member.setVisible(true);
        member.setName("[Time].[" + year + "]");
        return member;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MiniCubeMember getMemberFromLevelByName(DataSourceInfo dataSourceInfo, Cube cube, Level level,
            String name, MiniCubeMember parent, Map<String, String> params) throws MetaException {
        if (StringUtils.isBlank(name)) {
            throw new IllegalArgumentException("name can not be null");
        }
        /**
         * TODO ?
         */
        if (level.getType() == LevelType.TIME_DAYS) {
            MiniCubeMember dayMember = new MiniCubeMember(name);
            dayMember.setCaption(name);
            dayMember.setLevel(level);
            dayMember.setParent(null);
            dayMember.setName(name);
            dayMember.setVisible(true);
            return dayMember;
        }
        List<MiniCubeMember> members = getMembers(cube, name, level, dataSourceInfo, parent, params);
        for (MiniCubeMember m : members) {
            if (name.equals(m.getName()) || m.getQueryNodes().contains(name)) {
                return m;
            }
        }
        return null;
    }

    /**
     * 
     * @param level
     * @param name
     * @param members
     * @throws Exception
     */
    private void getMembersByStartDate(Level level, String name, List<MiniCubeMember> members) throws Exception {
        Date date = TimeRangeDetail.getTime(name);
        switch (level.getType()) {
        case TIME_YEARS:
            TimeRangeDetail detail = TimeUtils.getYearDays(date);
            MiniCubeMember yearDayMember = new MiniCubeMember(detail.getStart());
            yearDayMember.setCaption(detail.getStart());
            yearDayMember.setLevel(level);
            yearDayMember.setParent(null);
            yearDayMember.setName(detail.getStart());
            yearDayMember.setVisible(true);
            for (String day : detail.getDays()) {
                yearDayMember.getQueryNodes().add(day);
            }
            members.add(yearDayMember);
            break;
        case TIME_QUARTERS:
            TimeRangeDetail qurterDetail = TimeUtils.getQuarterDays(date);
            MiniCubeMember quarterDayMember = new MiniCubeMember(qurterDetail.getStart());
            quarterDayMember.setCaption(name);
            quarterDayMember.setLevel(level);
            quarterDayMember.setParent(null);
            quarterDayMember.setName(name);
            quarterDayMember.setVisible(true);
            for (String day : qurterDetail.getDays()) {
                quarterDayMember.getQueryNodes().add(day);
            }
            members.add(quarterDayMember);
            break;
        case TIME_MONTHS:
            TimeRangeDetail monthDetail = TimeUtils.getMonthDays(date);
            MiniCubeMember monthDayMember = new MiniCubeMember(monthDetail.getStart());
            monthDayMember.setCaption(monthDetail.getStart());
            monthDayMember.setLevel(level);
            monthDayMember.setParent(null);
            monthDayMember.setName(monthDetail.getStart());
            monthDayMember.setVisible(true);
            for (String day : monthDetail.getDays()) {
                monthDayMember.getQueryNodes().add(day);
            }
            members.add(monthDayMember);
            break;
        case TIME_WEEKS:
            TimeRangeDetail weekDetail = TimeUtils.getWeekDays(date);
            MiniCubeMember weekDayMember = new MiniCubeMember(weekDetail.getStart());
            weekDayMember.setLevel(level);
            weekDayMember.setVisible(true);
            weekDayMember.setParent(null);
            weekDayMember.setCaption(weekDetail.getStart());
            weekDayMember.setName(weekDetail.getStart());
            for (String day : weekDetail.getDays()) {
                weekDayMember.getQueryNodes().add(day);
            }
            members.add(weekDayMember);
            break;
        case TIME_DAYS:
            MiniCubeMember dayMember = new MiniCubeMember(name);
            dayMember.setCaption(name);
            dayMember.setLevel(level);
            dayMember.setParent(null);
            dayMember.setName(name);
            dayMember.setVisible(true);
            members.add(dayMember);
            break;
        default:
            throw new IllegalStateException("Invalidate time dimension level type : " + level.getType());
        }
    }

    /**
     * ?
     * 
     * @param level
     * @param parentMember
     * @return
     */
    private List<MiniCubeMember> genMembersFromOtherToWeek(Level level, Member parentMember) {
        List<MiniCubeMember> members = Lists.newArrayList();
        String parentName = parentMember.getName();
        String year = parentName.substring(0, 4);
        String month = parentName.substring(4, 6);
        int quarterIndex = Integer.valueOf(month) - 1; // 
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime(); // ?
        Date nowWeek = this.getFirstDayOfWeek(now); // ?

        cal.set(Calendar.YEAR, Integer.valueOf(year));
        cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
        cal.set(Calendar.DATE, 1);
        cal.setFirstDayOfWeek(Calendar.MONDAY);

        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        int count = 1; // 
        Date firstDay = cal.getTime();
        Date firstWeek = this.getFirstDayOfWeek(firstDay); // ?
        Date lastDay;
        Date lastWeek;
        int daysOfMonth;
        switch (parentMember.getLevel().getType()) {
        case TIME_YEARS:
            break;
        case TIME_QUARTERS:
            cal.add(Calendar.MONTH, 2); // ??
            daysOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // ?
            cal.add(Calendar.DATE, daysOfMonth);
            lastDay = cal.getTime(); // 
            lastWeek = this.getFirstDayOfWeek(lastDay); // 
            // ???
            if (nowWeek.before(lastWeek) || (nowWeek.compareTo(lastWeek) == 0)) {
                lastWeek = nowWeek;
            }
            cal.setTime(firstWeek); // ?
            while (cal.getTime().before(lastWeek) || (cal.getTime().compareTo(lastWeek) == 0)) {
                String day = sf.format(cal.getTime());
                String caption = "" + (quarterIndex + 1) + "" + count + "";
                caption = caption + day;
                MiniCubeMember dayMember = new MiniCubeMember(day);
                dayMember.setCaption(caption);
                dayMember.setLevel(level);
                dayMember.setParent(parentMember);
                dayMember.setName(day);
                dayMember.setVisible(true);
                for (int i = 0; i <= 6; i++) {
                    dayMember.getQueryNodes().add(sf.format(cal.getTime()));
                    cal.add(Calendar.DATE, 1);
                }
                members.add(dayMember);
                // cal.add(Calendar.DATE, 1);
                count++;
            }
            break;
        case TIME_MONTHS:
            daysOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 
            cal.add(Calendar.DATE, daysOfMonth);
            lastDay = cal.getTime(); // 
            lastWeek = this.getFirstDayOfWeek(lastDay); // 
            // ???
            if (nowWeek.before(lastWeek) || (nowWeek.compareTo(lastWeek) == 0)) {
                lastWeek = nowWeek;
            }
            cal.setTime(firstWeek); // ?
            while (cal.getTime().before(lastWeek) || (cal.getTime().compareTo(lastWeek) == 0)) {
                String day = sf.format(cal.getTime());
                String caption = month + "" + count + "";
                MiniCubeMember dayMember = new MiniCubeMember(day);
                dayMember.setCaption(caption);
                dayMember.setLevel(level);
                dayMember.setParent(parentMember);
                dayMember.setName(day);
                dayMember.setVisible(true);
                for (int i = 0; i <= 6; i++) {
                    dayMember.getQueryNodes().add(sf.format(cal.getTime()));
                    cal.add(Calendar.DATE, 1);
                }
                members.add(dayMember);
                // cal.add(Calendar.DATE, 1);
                count++;
            }
            break;
        case TIME_WEEKS:
            break;
        case TIME_DAYS:
            break;
        default:
        }
        return members;
    }
}