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