dimension.ts 3.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
 * Departamento de Informatica - Universidade Federal do Parana
 *
 * This file is part of blend.
 *
 * blend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * blend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with blend.  If not, see <http://www.gnu.org/licenses/>.
 */

21 22
import { RelationType, DataType } from "../common/types";
import { EnumType } from "./enumType";
23

24 25
export interface DimensionOptions {
    name: string;
26
    dataType: DataType;
27
    parent?: Dimension;
28
    relation?: RelationType;
29
    description?: string;
30
    enumType?: string;
31 32 33 34 35 36 37 38
}

export interface DimensionStrOptions {
    name: string;
    dataType: string;
    parent?: string;
    relation?: string;
    description?: string;
39 40 41 42
}

export class Dimension {
    public readonly name: string;
43
    public readonly dataType: DataType;
44
    public readonly parent: Dimension;
45
    public readonly relation: RelationType;
46
    public readonly description: string;
47
    public readonly enumType: string;
48 49 50

    constructor(options: DimensionOptions) {
        this.name = options.name;
51
        this.dataType = options.dataType;
52
        this.relation = (options.relation) ? options.relation : RelationType.NONE;
53
        this.parent = (options.parent) ? options.parent : null;
54
        this.description = (options.description) ? options.description : "";
55
        this.enumType = (options.enumType) ? (options.enumType) : "";
56 57 58 59 60 61
    }

    public strOptions(): DimensionStrOptions {
        if (this.relation === RelationType.NONE) {
            return {
                name: this.name,
62
                dataType: (this.dataType !== DataType.NONE) ? EnumType.stringfyDataType(this.dataType) : this.enumType ,
63 64 65 66 67 68
                description: this.description
            };
        }
        else {
            return {
                name: this.name,
69
                dataType: (this.dataType !== DataType.NONE) ? EnumType.stringfyDataType(this.dataType) : this.enumType ,
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
                parent: this.parent.name,
                relation: Dimension.stringifyRelationType(this.relation),
                description: this.description
            };
        }
    }

    public static parseRelationType(r: string): RelationType {
        switch (r) {
            case "day":
                return RelationType.DAY;
            case "month":
                return RelationType.MONTH;
            case "year":
                return RelationType.YEAR;
            default:
                return RelationType.NONE;
        }
    }

    public static stringifyRelationType(r: RelationType): string {
        switch (r) {
            case RelationType.DAY:
                return "day";
            case RelationType.MONTH:
                return "month";
            case RelationType.YEAR:
                return "year";
            default:
                return "";
        }
101 102
    }
}