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