1 package com.srv4pos.server.api.registration;
2
3 import com.srv4pos.server.api.activation.GeolocationInfo;
4 import com.srv4pos.server.api.activation.RegistrationDetailedOverviewInfo;
5 import com.srv4pos.server.api.activation.RegistrationInfo;
6 import com.srv4pos.server.api.day.RegistrationVersion;
7 import com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException;
8 import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter;
9 import com.srv4pos.server.api.infrastructure.HttpMethod;
10 import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
11 import com.srv4pos.server.api.infrastructure.credentials.Credentials;
12 import com.srv4pos.server.api.infrastructure.exceptions.enums.ForbiddenErrorType;
13 import com.srv4pos.server.api.infrastructure.exceptions.enums.UnprocessableEntityErrorType;
14 import com.srv4pos.server.api.infrastructure.http.HttpHelper;
15 import com.srv4pos.server.api.infrastructure.http.Transport;
16 import com.srv4pos.server.api.infrastructure.request.StringResponse;
17
18 import java.net.URL;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
24 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
25 import static java.lang.String.format;
26
27
28
29
30
31
32
33 public class RegistrationService {
34
35 private URL url;
36 private Credentials credentials;
37 private Transport transport;
38
39
40
41
42
43
44
45
46 public RegistrationService(URL url, Credentials credentials, Transport transport) {
47 this.url = url;
48 this.credentials = credentials;
49 this.transport = transport;
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63 public BalanceInfo getBalance(Integer version, String cashRegisterName) {
64 StringResponse response = requestString(
65 format("%s/registrations/%s/balance", credentials.getSellerId(), cashRegisterName),
66 HttpMethod.GET,
67 url,
68 credentials.toAuthentication(),
69 HttpHelper.integerToETag(version),
70 null,
71 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
72 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
73 transport);
74
75 return response == null ? null : BalanceInfo.fromJsonToBalanceInfo(response.getContent());
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public void setBalance(int version, String cashRegisterName, ChangeBalanceInfo changeBalanceInfo) {
91 requestVoid(
92 format("%s/registrations/%s/balance", credentials.getSellerId(), cashRegisterName),
93 HttpMethod.POST,
94 url,
95 credentials.toAuthentication(),
96 HttpHelper.integerToETag(version),
97 null,
98 new StringConnectionWriter(changeBalanceInfo.toJson()),
99 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
100 transport);
101 }
102
103
104
105
106
107
108
109 public List<RegistrationInfo> list(Integer version) {
110 return list(version, null, null, null, null, false);
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124 public List<RegistrationInfo> list(Integer version,
125 Integer firstResult,
126 Integer maxResults,
127 RegistrationInfo.Fields orderBy,
128 Boolean orderDesc,
129 Boolean activeOnly) {
130 Map<String, String> params = new HashMap<String, String>();
131
132 if (activeOnly != null) {
133 params.put("activeOnly", String.valueOf(activeOnly));
134 }
135 if (firstResult != null) {
136 params.put("firstResult", Integer.toString(firstResult));
137 }
138 if (maxResults != null) {
139 params.put("maxResults", Integer.toString(maxResults));
140 }
141 if (orderBy != null) {
142 params.put("orderBy", orderBy.name());
143 }
144 if (orderDesc != null) {
145 params.put("orderDesc", orderDesc.toString());
146 }
147
148 StringResponse response = requestString(
149 format("%s/registrations", credentials.getSellerId()),
150 HttpMethod.GET,
151 url,
152 credentials.toAuthentication(),
153 null,
154 params,
155 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
156 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
157 transport);
158
159 return response == null ? null : (List<RegistrationInfo>) RegistrationInfo.fromJsonArrayToRegistrationInfoes(response.getContent());
160 }
161
162
163
164
165
166
167
168 public List<RegistrationStatsInfo> stats(String period) {
169 Map<String, String> params = new HashMap<String, String>();
170 if (period != null) {
171 params.put("period", period);
172 }
173 StringResponse response = requestString(
174 format("%s/registrations/stats", credentials.getSellerId()),
175 HttpMethod.GET,
176 url,
177 credentials.toAuthentication(),
178 null,
179 params,
180 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
181 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
182 transport
183 );
184 return response == null ? null : (List<RegistrationStatsInfo>) RegistrationStatsInfo.fromJsonArrayToRegistrationStatsInfoes(response.getContent());
185 }
186
187
188
189
190
191
192
193
194
195
196
197 public RegistrationDetailedOverviewInfo getOverviewInfo(String seller, String cashRegisterName) {
198 return RegistrationDetailedOverviewInfo.fromJsonToRegistrationDetailedOverviewInfo(requestString(
199 format("%s/registrations/%s", seller, cashRegisterName),
200 HttpMethod.GET,
201 url,
202 credentials.toAuthentication(),
203 null,
204 null,
205 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
206 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
207 transport).getContent());
208 }
209
210
211
212
213
214
215
216
217 public RegistrationVersion getRegistrationVersion(String cashRegisterName) {
218 return RegistrationVersion.fromJsonToRegistrationVersion(
219 requestString(
220 format("%s/registrations/%s/version", credentials.getSellerId(), cashRegisterName),
221 HttpMethod.GET,
222 url,
223 credentials.toAuthentication(),
224 null,
225 null,
226 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
227 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
228 transport
229 ).getContent()
230 );
231 }
232
233
234
235
236
237
238
239
240 public List<RegistrationInfo> listDiff(int versionFrom, int versionTo) {
241 StringResponse response = requestString(
242 String.format("%s/registrations-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo),
243 HttpMethod.GET,
244 url,
245 credentials.toAuthentication(),
246 null,
247 null,
248 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
249 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
250 transport
251 );
252 return (List<RegistrationInfo>) RegistrationInfo.fromJsonArrayToRegistrationInfoes(response.getContent());
253 }
254
255
256
257
258
259
260
261
262 public void delete(int version, String identifier) {
263 requestVoid(
264 format("%s/registrations/%s", credentials.getSellerId(), identifier),
265 HttpMethod.DELETE,
266 url,
267 credentials.toAuthentication(),
268 HttpHelper.integerToETag(version),
269 null,
270 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
271 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
272 transport
273 );
274 }
275
276 public List<GeolocationInfo> listGeolocations(String sellerId) {
277 StringResponse response = requestString(
278 format("%s/registrations-locations", sellerId),
279 HttpMethod.GET,
280 url,
281 credentials.toAuthentication(),
282 null,
283 null,
284 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
285 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
286 transport
287 );
288 return (List<GeolocationInfo>) GeolocationInfo.fromJsonArrayToGeolocationInfoes(response.getContent());
289 }
290 }