forked from gooddata/gooddata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataService.java
More file actions
207 lines (189 loc) · 8.58 KB
/
MetadataService.java
File metadata and controls
207 lines (189 loc) · 8.58 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
*/
package com.gooddata.md;
import com.gooddata.AbstractService;
import com.gooddata.GoodDataException;
import com.gooddata.GoodDataRestException;
import com.gooddata.gdc.UriResponse;
import com.gooddata.md.report.ReportDefinition;
import com.gooddata.project.Project;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.Collection;
import static com.gooddata.Validate.notNull;
/**
* Query, create and update project metadata - attributes, facts, metrics, reports,...
*/
public class MetadataService extends AbstractService {
public MetadataService(RestTemplate restTemplate) {
super(restTemplate);
}
/**
* Create metadata object in given project
*
* @param project project
* @param obj metadata object to be created
* @param <T> type of the object to be created
* @return new metadata object
* @throws com.gooddata.md.ObjCreateException if creation failed
* @throws com.gooddata.md.ObjNotFoundException if new metadata object not found after creation
* @throws com.gooddata.GoodDataRestException if GoodData REST API returns unexpected status code when getting
* the new object
* @throws com.gooddata.GoodDataException if it encounters client-side HTTP errors when getting the new object
*/
@SuppressWarnings("unchecked")
public <T extends Obj> T createObj(Project project, T obj) {
notNull(project, "project");
notNull(obj, "obj");
final UriResponse response;
try {
response = restTemplate.postForObject(Obj.URI, obj, UriResponse.class, project.getId());
} catch (GoodDataRestException | RestClientException e) {
throw new ObjCreateException(obj, e);
}
if (response == null) {
throw new ObjCreateException("empty response from API call", obj);
}
return getObjByUri(response.getUri(), (Class<T>) obj.getClass());
}
/**
* Get metadata object by URI (format is <code>/gdc/md/{PROJECT_ID}/obj/{OBJECT_ID}</code>)
*
* @param uri URI in format <code>/gdc/md/{PROJECT_ID}/obj/{OBJECT_ID}</code>
* @param cls class of the resulting object
* @param <T> type of the object to be returned
* @return the metadata object
* @throws com.gooddata.md.ObjNotFoundException if metadata object not found
* @throws com.gooddata.GoodDataRestException if GoodData REST API returns unexpected status code
* @throws com.gooddata.GoodDataException if it encounters client-side HTTP errors
*/
public <T extends Obj> T getObjByUri(String uri, Class<T> cls) {
notNull(uri, "uri");
notNull(cls, "cls");
try {
final T result = restTemplate.getForObject(uri, cls);
if (result != null) {
return result;
} else {
throw new GoodDataException("empty response from API call");
}
} catch (GoodDataRestException e) {
if (HttpStatus.NOT_FOUND.value() == e.getStatusCode()) {
throw new ObjNotFoundException(uri, cls, e);
} else {
throw e;
}
} catch (RestClientException e) {
throw new GoodDataException("Unable to get " + cls.getSimpleName().toLowerCase() + " " + uri, e);
}
}
/**
* Get metadata object by id.
*
* @param project project where to search for the object
* @param id id of the object
* @param cls class of the resulting object
* @param <T> type of the object to be returned
* @return the metadata object
* @throws com.gooddata.md.ObjNotFoundException if metadata object not found
* @throws com.gooddata.GoodDataRestException if GoodData REST API returns unexpected status code
* @throws com.gooddata.GoodDataException if it encounters client-side HTTP errors
*/
public <T extends Obj> T getObjById(Project project, String id, Class<T> cls) {
notNull(project, "project");
notNull(id, "id");
notNull(cls, "cls");
return getObjByUri(Obj.OBJ_TEMPLATE.expand(project.getId(), id).toString(), cls);
}
/**
* Get metadata object URI by restrictions like identifier, title or summary.
*
* @param project project where to search for the object
* @param cls class of the resulting object
* @param restrictions query restrictions
* @param <T> type of the object to be returned
* @return the URI of metadata object
* @throws com.gooddata.md.ObjNotFoundException if metadata object not found
* @throws com.gooddata.md.NonUniqueObjException if more than one object corresponds to search restrictions
*/
public <T extends Queryable> String findObjUri(Project project, Class<T> cls, Restriction... restrictions) {
final Collection<String> results = findUris(project, cls, restrictions);
if (results == null || results.isEmpty()) {
throw new ObjNotFoundException(cls);
} else if (results.size() != 1) {
throw new NonUniqueObjException(cls, results);
}
return results.iterator().next();
}
/**
* Find metadata by restrictions like identifier, title or summary.
*
* @param project project where to search for the metadata
* @param cls class of searched metadata
* @param restrictions query restrictions
* @param <T> type of the metadata referenced in returned entries
* @return the collection of metadata entries
* @throws com.gooddata.GoodDataException if unable to query metadata
*/
public <T extends Queryable> Collection<Entry> find(Project project, Class<T> cls, Restriction... restrictions) {
notNull(project, "project");
notNull(cls, "cls");
final String type = cls.getSimpleName().toLowerCase() +
(cls.isAssignableFrom(ReportDefinition.class) ? "" : "s");
try {
final Query queryResult = restTemplate.getForObject(Query.URI, Query.class, project.getId(), type);
if (queryResult != null && queryResult.getEntries() != null) {
return filterEntries(queryResult.getEntries(), restrictions);
} else {
throw new GoodDataException("empty response from API call");
}
} catch (RestClientException e) {
throw new GoodDataException("Unable to query metadata: " + type, e);
}
}
private Collection<Entry> filterEntries(Collection<Entry> entries, Restriction... restrictions) {
if (restrictions == null || restrictions.length == 0) {
return entries;
}
final Collection<Entry> result = new ArrayList<>(entries.size());
for (Entry entry : entries) {
for (Restriction restriction : restrictions) {
switch (restriction.getType()) {
case IDENTIFIER:
if (restriction.getValue().equals(entry.getIdentifier())) result.add(entry);
break;
case TITLE:
if (restriction.getValue().equals(entry.getTitle())) result.add(entry);
break;
case SUMMARY:
if (restriction.getValue().equals(entry.getSummary())) result.add(entry);
break;
}
}
}
return result;
}
/**
* Find metadata URIs by restrictions like identifier, title or summary.
*
* @param project project where to search for the metadata
* @param cls class of searched metadata
* @param restrictions query restrictions
* @param <T> type of the metadata referenced by returned URIs
* @return the collection of metadata URIs
* @throws com.gooddata.GoodDataException if unable to query metadata
*/
public <T extends Queryable> Collection<String> findUris(Project project,
Class<T> cls,
Restriction... restrictions) {
final Collection<Entry> entries = find(project, cls, restrictions);
final Collection<String> result = new ArrayList<>(entries.size());
for (Entry entry : entries) {
result.add(entry.getLink());
}
return result;
}
}