-
Notifications
You must be signed in to change notification settings - Fork 0
/
DimensionService.go
116 lines (105 loc) · 2.9 KB
/
DimensionService.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package tm1go
import (
"encoding/json"
"fmt"
)
type DimensionService struct {
rest *RestService
object *ObjectService
hierarchy *HierarchyService
}
// NewDimensionService creates a new instance of DimensionService
func NewDimensionService(rest *RestService, object *ObjectService, hierarchy *HierarchyService) *DimensionService {
return &DimensionService{rest: rest, object: object, hierarchy: hierarchy}
}
// Create a new dimension
func (ds *DimensionService) Create(dimension *Dimension) error {
url := "/Dimensions"
dimensionBody, err := dimension.getBody(false)
if err != nil {
return err
}
jsonBody, err := json.Marshal(dimensionBody)
if err != nil {
return err
}
_, err = ds.rest.POST(url, string(jsonBody), nil, 0, nil)
return err
}
// Get retrieves a dimension
func (ds *DimensionService) Get(dimensionName string) (*Dimension, error) {
url := fmt.Sprintf("/Dimensions('%v')?$expand=Hierarchies($expand=*)", dimensionName)
response, err := ds.rest.GET(url, nil, 0, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
dimension := &Dimension{}
err = json.NewDecoder(response.Body).Decode(dimension)
if err != nil {
return nil, err
}
return dimension, nil
}
// Delete a dimension
func (ds *DimensionService) Delete(dimensionName string) error {
url := fmt.Sprintf("/Dimensions('%v')", dimensionName)
_, err := ds.rest.DELETE(url, nil, 0, nil)
return err
}
// Exists checks if a dimension exists
func (ds *DimensionService) Exists(dimensionName string) (bool, error) {
url := fmt.Sprintf("/Dimensions('%v')", dimensionName)
return ds.object.Exists(url)
}
// GetAllNames retrieves all dimension names
func (dx *DimensionService) GetAllNames() ([]string, error) {
url := "/Dimensions?$select=Name"
response, err := dx.rest.GET(url, nil, 0, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
result := ValueArray[Dimension]{}
err = json.NewDecoder(response.Body).Decode(&result)
if err != nil {
return nil, err
}
names := make([]string, 0, len(result.Value))
for _, dimension := range result.Value {
names = append(names, dimension.Name)
}
return names, nil
}
// GetNumberOfDimensions retrieves the number of dimensions in TM1
func (ds *DimensionService) GetNumberOfDimensions(skipControlDims bool) (int, error) {
url := "/Dimensions/$count"
if skipControlDims {
url = "/ModelDimensions()?$select=Name&$top=0&$count"
}
response, err := ds.rest.GET(url, nil, 0, nil)
if err != nil {
return 0, err
}
defer response.Body.Close()
count := 0
if skipControlDims {
result := ValueArray[Dimension]{}
err = json.NewDecoder(response.Body).Decode(&result)
if err != nil {
return 0, err
}
count = result.Count
} else {
err = json.NewDecoder(response.Body).Decode(&count)
if err != nil {
return 0, err
}
}
return count, nil
}
//execute_mdx
//create_element_attributes_through_ti
//uses_alternate_hierarchies
//update
//update_or_create