forked from gooddata/gooddata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLcmService.java
More file actions
117 lines (105 loc) · 4.98 KB
/
Copy pathLcmService.java
File metadata and controls
117 lines (105 loc) · 4.98 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
/*
* Copyright (C) 2007-2018, GoodData(R) Corporation. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
package com.gooddata.lcm;
import com.gooddata.AbstractService;
import com.gooddata.GoodDataException;
import com.gooddata.GoodDataSettings;
import com.gooddata.account.Account;
import com.gooddata.collections.MultiPageList;
import com.gooddata.collections.Page;
import com.gooddata.collections.PageRequest;
import com.gooddata.collections.PageableList;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import static com.gooddata.util.Validate.notNull;
/**
* Service, which provides access to lifecycle management objects.
*/
public class LcmService extends AbstractService {
/**
* Constructs service for GoodData Life Cycle Management.
*
* @param restTemplate RESTful HTTP Spring template
* @param settings settings
*/
public LcmService(final RestTemplate restTemplate, final GoodDataSettings settings) {
super(restTemplate, settings);
}
/**
* Lists all {@link LcmEntities} for given {@link Account}.
* Returns empty list in case there is no {@link LcmEntity}.
* Returns only first page if there's more instances than page limit. Use {@link PageableList#stream()} to iterate
* over all pages, or {@link MultiPageList#collectAll()} to load the entire list.
*
* @param account account to list LCM entities for
* @return MultiPageList first page of list of lcm entities or empty list
*/
public PageableList<LcmEntity> listLcmEntities(final Account account) {
return listLcmEntities(account, new PageRequest());
}
/**
* Lists {@link LcmEntities} for given {@link Account} filtered according given {@link LcmEntityFilter}.
* Returns empty list in case there is no {@link LcmEntity}.
* Returns only first page if there's more instances than page limit. Use {@link PageableList#stream()} to iterate
* over all pages, or {@link MultiPageList#collectAll()} to load the entire list.
*
* @param account account to list LCM entities for
* @param filter filter of the entities
* @return MultiPageList first page of list of lcm entitiesor empty list
*/
public PageableList<LcmEntity> listLcmEntities(final Account account, final LcmEntityFilter filter) {
return listLcmEntities(account, filter, new PageRequest());
}
/**
* Lists all {@link LcmEntities} for given {@link Account}.
* Returns empty list in case there is no {@link LcmEntity}.
* Returns requested page (by page limit and offset). Use {@link #listLcmEntities(Account)} to get first page with default setting.
*
* @param account account to list LCM entities for
* @param startPage page to be listed
* @return MultiPageList requested page of list of lcm entities or empty list
*/
public PageableList<LcmEntity> listLcmEntities(final Account account, final Page startPage) {
return listLcmEntities(account, new LcmEntityFilter(), startPage);
}
/**
* Lists {@link LcmEntities} for given {@link Account} filtered according given {@link LcmEntityFilter}.
* Returns empty list in case there is no {@link LcmEntity}.
* Returns requested page (by page limit and offset). Use {@link #listLcmEntities(Account)} to get first page with default setting.
*
* @param account account to list LCM entities for
* @param filter filter of the entities
* @param startPage page to be listed
* @return MultiPageList requested page of list of lcm entities or empty list
*/
public PageableList<LcmEntity> listLcmEntities(final Account account, final LcmEntityFilter filter, final Page startPage) {
notNull(filter, "filter");
notNull(startPage, "startPage");
final String accountId = notNull(account, "account").getId();
return new MultiPageList<>(startPage, page -> listLcmEntities(getLcmEntitiesUri(accountId, filter, page)));
}
private URI getLcmEntitiesUri(final String accountId) {
return LcmEntities.TEMPLATE.expand(accountId);
}
private URI getLcmEntitiesUri(final String accountId, final LcmEntityFilter filter, final Page page) {
return page.getPageUri(
UriComponentsBuilder.fromUri(getLcmEntitiesUri(accountId))
.queryParams(filter.asQueryParams()));
}
private PageableList<LcmEntity> listLcmEntities(final URI uri) {
try {
final LcmEntities result = restTemplate.getForObject(uri, LcmEntities.class);
if (result == null) {
return new PageableList<>();
}
return result;
} catch (GoodDataException | RestClientException e) {
throw new GoodDataException("Unable to list LcmEntity", e);
}
}
}