View Javadoc
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   * Service to reach registrations.
30   *
31   * @author Ruslan Kashapov.
32   */
33  public class RegistrationService {
34  
35      private URL url;
36      private Credentials credentials;
37      private Transport transport;
38  
39      /**
40       * Constructs the service.
41       *
42       * @param url         of the server
43       * @param credentials to work with the server
44       * @param transport   to know the method to reach the server
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       * Get balance of registration.
54       *
55       * @param version          version of seller, may be null (if latest data needed)
56       * @param cashRegisterName described in swedish regulations as "cash register designation"
57       * @return balance of registration
58       * @throws ReferenceNotFoundJsonException                                     if cashRegisterName is not found
59       * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see:
60       *                                                                            {@link UnprocessableEntityErrorType#DAY_IS_NOT_CLOSED},
61       *                                                                            {@link UnprocessableEntityErrorType#REPORT_IS_NOT_PRINTED}
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       * Set balance of registration.
80       *
81       * @param version           expected latest version of seller
82       * @param cashRegisterName  described in swedish regulations as "cash register designation"
83       * @param changeBalanceInfo registration balance and action
84       * @throws ReferenceNotFoundJsonException                                     if cashRegisterName is not found
85       * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see:
86       *                                                                            {@link UnprocessableEntityErrorType#DAY_IS_CLOSED},
87       *                                                                            {@link UnprocessableEntityErrorType#DAY_IS_NOT_CLOSED},
88       *                                                                            {@link UnprocessableEntityErrorType#REPORT_IS_NOT_PRINTED}
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      * Returns list of all registrations.
105      *
106      * @param version version of seller, may be null (if latest data needed)
107      * @return list of all registrations
108      */
109     public List<RegistrationInfo> list(Integer version) {
110         return list(version, null, null, null, null, false);
111     }
112 
113     /**
114      * Returns list of registrations.
115      *
116      * @param version     version of seller, may be null (if latest data needed)
117      * @param firstResult when pagination starts or null
118      * @param maxResults  amount of items per page or null
119      * @param orderBy     field to order by, possible values are "IDENTIFIER", "NAME"
120      * @param orderDesc   field to direction of ordering
121      * @param activeOnly  True if need only active registrations, otherwise all registrations
122      * @return list of registrations
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      * Returns stats by registrations.
164      *
165      * @param period filters by date period
166      * @return
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      * Returns an registration info.
189      *
190      * @param seller           seller's identifier
191      * @param cashRegisterName cash register name
192      * @return the entity
193      * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException  if registration wasn't found
194      * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException see:
195      *                                                                  {@link ForbiddenErrorType#INSUFFICIENT_ACCESS_RIGHTS}
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      * Returns latest version of registration.
212      *
213      * @param cashRegisterName described in swedish regulations as "cash register designation"
214      * @return version number
215      * @see RegistrationServiceSample
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      * Returns list of item changes between two versions.
235      *
236      * @param versionFrom version from included
237      * @param versionTo   version to included
238      * @return list of items to be added, edited or deleted ({@link com.srv4pos.server.api.activation.RegistrationInfo#deleted} = true)
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      * Delete registration from the server.
257      *
258      * @param version    current version of the entity
259      * @param identifier identifies entity
260      * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException if version mismatch
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 }