View Javadoc
1   package com.srv4pos.server.api.sale;
2   
3   import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter;
4   import com.srv4pos.server.api.infrastructure.HttpMethod;
5   import com.srv4pos.server.api.infrastructure.IdInfo;
6   import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
7   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
8   import com.srv4pos.server.api.infrastructure.exceptions.enums.UnprocessableEntityErrorType;
9   import com.srv4pos.server.api.infrastructure.http.HttpHelper;
10  import com.srv4pos.server.api.infrastructure.http.Transport;
11  import com.srv4pos.server.api.infrastructure.request.StringResponse;
12  
13  import java.net.URL;
14  import java.util.HashMap;
15  import java.util.List;
16  import java.util.Map;
17  
18  import static com.srv4pos.server.api.infrastructure.http.HttpHelper.CONTENT_TYPE_APPLICATION_JSON;
19  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
20  import static java.lang.String.format;
21  
22  /**
23   * Service for reaching Sale entity.
24   * <p>User: Pavel Abizyaev, Date: 11.09.2014 18:52</p>
25   */
26  public class SaleService {
27      private URL url;
28      private Credentials credentials;
29      private Transport transport;
30  
31      /**
32       * Constructs the service.
33       *
34       * @param url         of the server
35       * @param credentials to work with the server
36       * @param transport   to know the method to reach the server
37       */
38      public SaleService(URL url, Credentials credentials, Transport transport) {
39          this.url = url;
40          this.credentials = credentials;
41          this.transport = transport;
42      }
43  
44      /**
45       * Create sale on the server.
46       *
47       * @param sellerVersion       expected latest version of seller
48       * @param registrationVersion expected latest version of registration
49       * @param saleInfo            data to put
50       * @return generated unique identifier
51       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException     if version mismatch
52       * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see:
53       *                                                                            {@link UnprocessableEntityErrorType#DAY_IS_CLOSED},
54       *                                                                            {@link UnprocessableEntityErrorType#DAY_IS_NOT_CLOSED},
55       *                                                                            {@link UnprocessableEntityErrorType#RECEIPT_IS_PRINTED_ALREADY},
56       *                                                                            {@link UnprocessableEntityErrorType#REPORT_IS_NOT_PRINTED}
57       */
58      public long create(int sellerVersion, int registrationVersion, SaleInfo saleInfo) {
59          return IdInfo.fromJsonToIdInfo(requestString(
60                  format("%s/sales", saleInfo.getSellerIdentifier()),
61                  HttpMethod.POST,
62                  url,
63                  credentials.toAuthentication(),
64                  HttpHelper.toETag(sellerVersion, registrationVersion),
65                  null,
66                  new StringConnectionWriter(saleInfo.toJson()),
67                  CONTENT_TYPE_APPLICATION_JSON,
68                  transport
69          ).getContent()).getId();
70      }
71  
72      /**
73       * Returns list of entities.
74       *
75       * @param sellerId    filters by seller
76       * @param period      filters by date period
77       * @param firstResult when pagination starts or null
78       * @param maxResults  amount of items per page or null
79       * @param like        search string filter or null
80       * @param orderBy     field to order by, possible values are "IDENTIFIER", "NAME", "VAT"
81       * @param orderDesc   field to direction of ordering
82       * @return list of entities which exists on current seller or null if list of entities is not modified since version
83       */
84      public List<SaleShortInfo> list(String sellerId, String period, Integer firstResult,
85                                      Integer maxResults, String like,
86                                      SaleInfo.Fields orderBy,
87                                      Boolean orderDesc) {
88  
89          Map<String, String> params = new HashMap<String, String>();
90  
91          if (orderBy != null) {
92              params.put("orderBy", orderBy.name());
93          }
94          if (orderDesc != null) {
95              params.put("orderDesc", orderDesc.toString());
96          }
97          if (firstResult != null) {
98              params.put("firstResult", Integer.toString(firstResult));
99          }
100         if (maxResults != null) {
101             params.put("maxResults", Integer.toString(maxResults));
102         }
103         if (like != null) {
104             params.put("like", like);
105         }
106         if (period != null) {
107             params.put("period", period);
108         }
109 
110         StringResponse response = requestString(
111                 format("%s/sales-minified", sellerId),
112                 HttpMethod.GET,
113                 url,
114                 credentials.toAuthentication(),
115                 null,
116                 params,
117                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
118                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
119                 transport
120         );
121         return response == null ? null : (List<SaleShortInfo>) SaleShortInfo.fromJsonArrayToSaleShortInfoes(response.getContent());
122     }
123 
124     /**
125      * Returns sales stats filtered by seller, cash register name and period.
126      *
127      * @param sellerId         filters by seller
128      * @param period           filters by date period
129      * @param cashRegisterName filters by cash register name
130      * @return {@link com.srv4pos.server.api.sale.SaleStatsInfo} with stats.
131      * @throws com.srv4pos.server.api.exceptions.TypeMismatchJsonException if {@code period} {@link java.lang.String} isn't convertible to
132      *                                                                     {@link java.util.Date}.
133      */
134     public SaleStatsInfo
135     stats(String sellerId, String period, String cashRegisterName) {
136         Map<String, String> params = new HashMap<String, String>();
137         if (cashRegisterName != null) {
138             params.put("cashRegisterName", cashRegisterName);
139         }
140         if (period != null) {
141             params.put("period", period);
142         }
143 
144         StringResponse response = requestString(
145                 format("%s/sales/stats", sellerId),
146                 HttpMethod.GET,
147                 url,
148                 credentials.toAuthentication(),
149                 null,
150                 params,
151                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
152                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
153                 transport
154         );
155         return response == null ? null : SaleStatsInfo.fromJsonToSaleStatsInfo(response.getContent());
156     }
157 
158     /**
159      * Returns list of item changes between two versions.
160      *
161      * @param versionFrom version from included
162      * @param versionTo   version to included
163      * @return list of items to be added
164      */
165     public List<SaleInfo> listDiff(int versionFrom, int versionTo) {
166         StringResponse response = requestString(
167                 String.format("%s/sales-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo),
168                 HttpMethod.GET,
169                 url,
170                 credentials.toAuthentication(),
171                 null,
172                 null,
173                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
174                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
175                 transport
176         );
177         return (List<SaleInfo>) SaleInfo.fromJsonArrayToSaleInfoes(response.getContent());
178     }
179 }