1 package com.srv4pos.server.api.activation;
2
3 import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter;
4 import com.srv4pos.server.api.infrastructure.HttpMethod;
5 import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
6 import com.srv4pos.server.api.infrastructure.credentials.Credentials;
7 import com.srv4pos.server.api.infrastructure.exceptions.enums.ForbiddenErrorType;
8 import com.srv4pos.server.api.infrastructure.exceptions.enums.InconsistentDataErrorType;
9 import com.srv4pos.server.api.infrastructure.exceptions.enums.UnprocessableEntityErrorType;
10 import com.srv4pos.server.api.infrastructure.http.HttpHelper;
11 import com.srv4pos.server.api.infrastructure.http.Transport;
12
13 import java.net.URL;
14 import java.util.Collection;
15 import java.util.Date;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Set;
19
20 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
21
22
23
24
25
26 public class ActivationService {
27
28 private URL url;
29 private Credentials credentials;
30 private Transport transport;
31
32
33
34
35
36
37
38
39 public ActivationService(URL url, Credentials credentials, Transport transport) {
40 this.url = url;
41 this.credentials = credentials;
42 this.transport = transport;
43 }
44
45
46
47
48
49
50
51 public boolean cashRegisterNameExists(CashRegisterNameExistsInfo cashRegisterNameExistsInfo) {
52 return CashRegisterNameExistsResultInfo.fromJsonToCashRegisterNameExistsResultInfo(
53 requestString(
54 "noauth/activations/cash-register-name/existance",
55 HttpMethod.POST,
56 url,
57 null,
58 null,
59 null,
60 new StringConnectionWriter(cashRegisterNameExistsInfo.toJson()),
61 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
62 transport).getContent()).isExists();
63 }
64
65
66
67
68
69
70
71
72
73
74
75 public ActivationCodeInfo getActivationCode(Credentials credentials) {
76 return ActivationCodeInfo.fromJsonToActivationCodeInfo(
77 requestString(
78 "activations/activation-code",
79 HttpMethod.POST,
80 url,
81 credentials.toAuthentication(),
82 null,
83 null,
84 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
85 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
86 transport).getContent());
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public ActivationSecurityInfo connect(ActivationConnectInfo activationConnectInfo) {
103 return ActivationSecurityInfo.fromJsonToActivationSecurityInfo(requestString(
104 "/activations/connection",
105 HttpMethod.POST,
106 url,
107 null,
108 null,
109 null,
110 new StringConnectionWriter(activationConnectInfo.toJson()),
111 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
112 transport).getContent());
113 }
114
115
116
117
118
119
120 public Collection<FullActivationAdvancedInfo> listFull() {
121 return FullActivationAdvancedInfo.fromJsonArrayToFullActivationAdvancedInfoes(requestString(
122 "/activations-full",
123 HttpMethod.GET,
124 url,
125 credentials.toAuthentication(),
126 null,
127 new HashMap<String, String>(),
128 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
129 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
130 transport).getContent());
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 public ActivationSecurityInfo create(ActivationCreationInfo activationCreationInfo) {
155 final String content = requestString(
156 "/activations-advanced",
157 HttpMethod.POST,
158 url,
159 credentials != null ? credentials.toAuthentication() : null,
160 null,
161 null,
162 new StringConnectionWriter(activationCreationInfo.toJson()),
163 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
164 transport).getContent();
165 return ActivationSecurityInfo.fromJsonToActivationSecurityInfo(content);
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 public ActivationSecurityInfo claim(String country, String corporateId, String cashRegisterName, String applicationPackage,
200 Set<Feature> features, Date validFrom, Date validTo, ContactInfo contactInfo, String comment,
201 String controlUnitSerial, ControlUnitLocation controlUnitLocation, GeolocationInfo controlUnitGeolocation,
202 String deviceId, Map<String, String> buildInfo, String gcmRegistrationId) {
203
204 ActivationCreationInfo activationCreationInfo =
205 newActivationCreationInfo(country, corporateId, cashRegisterName, applicationPackage, features, validFrom, validTo, contactInfo,
206 comment, controlUnitSerial, controlUnitLocation, controlUnitGeolocation, deviceId, buildInfo, gcmRegistrationId, controlUnitGeolocation);
207
208 return create(activationCreationInfo);
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 public ActivationSecurityInfo claim(String country, String corporateId, String cashRegisterName, String applicationPackage,
234 Set<Feature> features, Date validFrom, Date validTo, ContactInfo contactInfo, String comment,
235 String deviceId, Map<String, String> buildInfo) {
236 return claim(country, corporateId, cashRegisterName, applicationPackage, features, validFrom, validTo, contactInfo, comment, null, null,
237 null, deviceId, buildInfo, null);
238 }
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 public ActivationSecurityInfo up(String country, String corporateId, String cashRegisterName,
266 Set<Feature> features, Date validFrom, Date validTo, ContactInfo contactInfo, String comment,
267 String controlUnitSerial, ControlUnitLocation controlUnitLocation, GeolocationInfo controlUnitGeolocation) {
268 ActivationCreationInfo activationCreationInfo = new ActivationCreationInfo();
269
270 activationCreationInfo.setCorporateId(corporateId);
271 activationCreationInfo.setCountry(country);
272 activationCreationInfo.setCashRegisterName(cashRegisterName);
273 activationCreationInfo.setComment(comment);
274 activationCreationInfo.setContactInfo(contactInfo);
275 activationCreationInfo.setControlUnitGeolocation(controlUnitGeolocation);
276 activationCreationInfo.setControlUnitLocation(controlUnitLocation);
277 activationCreationInfo.setControlUnitSerial(controlUnitSerial);
278 activationCreationInfo.setFeatures(features);
279 activationCreationInfo.setValidFrom(validFrom);
280 activationCreationInfo.setValidTo(validTo);
281 activationCreationInfo.setRegistrationGeolocation(controlUnitGeolocation);
282
283 return create(activationCreationInfo);
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305 public ActivationSecurityInfo up(String country, String corporateId, String cashRegisterName,
306 Set<Feature> features, Date validFrom, Date validTo, ContactInfo contactInfo, String comment) {
307 return up(country, corporateId, cashRegisterName, features, validFrom, validTo, contactInfo, comment, null, null, null);
308 }
309
310 public static ActivationCreationInfo newActivationCreationInfo(String country, String corporateId, String cashRegisterName,
311 String applicationPackage, Set<Feature> features, Date validFrom, Date validTo,
312 ContactInfo contactInfo, String comment, String controlUnitSerial,
313 ControlUnitLocation controlUnitLocation, GeolocationInfo controlUnitGeolocation,
314 String deviceId,
315 Map<String, String> buildInfo, String gcmRegistrationId,
316 GeolocationInfo registrationLocation) {
317 return newActivationCreationInfo(country, corporateId, cashRegisterName,
318 applicationPackage, features, validFrom, validTo,
319 contactInfo, comment, controlUnitSerial, controlUnitLocation, controlUnitGeolocation,
320 deviceId,
321 buildInfo, gcmRegistrationId,
322 registrationLocation, null);
323 }
324
325
326 public static ActivationCreationInfo newActivationCreationInfo(String country, String corporateId, String cashRegisterName,
327 String applicationPackage, Set<Feature> features, Date validFrom, Date validTo,
328 ContactInfo contactInfo, String comment, String controlUnitSerial,
329 ControlUnitLocation controlUnitLocation, GeolocationInfo controlUnitGeolocation,
330 String deviceId,
331 Map<String, String> buildInfo, String gcmRegistrationId,
332 GeolocationInfo registrationLocation, String programVersion) {
333 ActivationCreationInfo activationCreationInfo = new ActivationCreationInfo();
334
335 if (deviceId != null) {
336 InstallationCreationInfo installationCreationInfo = new InstallationCreationInfo();
337 installationCreationInfo.setDeviceId(deviceId);
338 installationCreationInfo.setBuildInfo(buildInfo);
339 installationCreationInfo.setGcmRegistrationId(gcmRegistrationId);
340 installationCreationInfo.setProgramVersion(programVersion);
341
342 activationCreationInfo.setInstallationCreationInfo(installationCreationInfo);
343 }
344
345 activationCreationInfo.setCorporateId(corporateId);
346 activationCreationInfo.setCountry(country);
347 activationCreationInfo.setApplicationPackage(applicationPackage);
348 activationCreationInfo.setCashRegisterName(cashRegisterName);
349 activationCreationInfo.setComment(comment);
350 activationCreationInfo.setContactInfo(contactInfo);
351 activationCreationInfo.setControlUnitGeolocation(controlUnitGeolocation);
352 activationCreationInfo.setControlUnitLocation(controlUnitLocation);
353 activationCreationInfo.setControlUnitSerial(controlUnitSerial);
354 activationCreationInfo.setFeatures(features);
355 activationCreationInfo.setValidFrom(validFrom);
356 activationCreationInfo.setValidTo(validTo);
357 activationCreationInfo.setRegistrationGeolocation(registrationLocation);
358 return activationCreationInfo;
359 }
360 }