View Javadoc
1   package com.srv4pos.server.api.kdfacet;
2   
3   import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter;
4   import com.srv4pos.server.api.infrastructure.HttpMethod;
5   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
6   import com.srv4pos.server.api.infrastructure.exceptions.enums.InconsistentDataErrorType;
7   import com.srv4pos.server.api.infrastructure.http.HttpHelper;
8   import com.srv4pos.server.api.infrastructure.http.Transport;
9   import com.srv4pos.server.api.infrastructure.request.StringResponse;
10  
11  import java.net.URL;
12  import java.util.HashMap;
13  import java.util.List;
14  
15  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
16  
17  public class KdFacetService {
18      private URL url;
19      private Credentials credentials;
20      private Transport transport;
21  
22      public KdFacetService(URL url, Credentials credentials, Transport transport) {
23          this.url = url;
24          this.credentials = credentials;
25          this.transport = transport;
26      }
27  
28      public List<KdFacetInfo> list(
29              Integer firstResult,
30              Integer maxResults,
31              String serial,
32              String sellerId,
33              String cashRegisterName,
34              Long softwareVendorId) {
35          return list(firstResult, maxResults, serial, sellerId, cashRegisterName, softwareVendorId, null, null);
36      }
37  
38  
39      /**
40       * Returns list of kd facets. One of the filters must be used. Also, since cash register name is unique only through the particular seller it is forbidden
41       * to pass just a cash register name without seller.
42       *
43       * @param firstResult      when pagination starts or null
44       * @param maxResults       amount of items per page or null
45       * @param serial           filters kd facets by control unit
46       * @param sellerId         filters kd facets by seller
47       * @param cashRegisterName filters kd facets by cash register name (i.e. registration)
48       * @param softwareVendorId filters kd facets by software vendor
49       * @return list of entities
50       * @throws com.srv4pos.server.api.exceptions.InconsistentDataJsonException see:
51       *                                                                         {@link InconsistentDataErrorType#SELLER_SHOULD_BE_PROVIDED_FOR_CR}
52       *                                                                         {@link InconsistentDataErrorType#SELLER_HAS_NOT_REGISTRATION}
53       */
54      public List<KdFacetInfo> list(
55              Integer firstResult,
56              Integer maxResults,
57              String serial,
58              String sellerId,
59              String cashRegisterName,
60              Long softwareVendorId,
61              KdFacetInfo.Fields orderBy,
62              Boolean orderDesc) {
63          HashMap<String, String> params = new HashMap<String, String>();
64          if (firstResult != null) {
65              params.put("firstResult", Integer.toString(firstResult));
66          }
67          if (maxResults != null) {
68              params.put("maxResults", Integer.toString(maxResults));
69          }
70          if (serial != null) {
71              params.put("controlUnit", serial);
72          }
73          if (sellerId != null) {
74              params.put("seller", sellerId);
75          }
76          if (cashRegisterName != null) {
77              params.put("cashRegisterName", cashRegisterName);
78          }
79          if (softwareVendorId != null) {
80              params.put("softwareVendor", Long.toString(softwareVendorId));
81          }
82          if (orderBy != null) {
83              params.put("orderBy", orderBy.name());
84          }
85          if (orderDesc != null) {
86              params.put("orderDesc", orderDesc.toString());
87          }
88  
89          StringResponse response = requestString(
90                  "kd-facets",
91                  HttpMethod.GET,
92                  url,
93                  credentials.toAuthentication(),
94                  null,
95                  params,
96                  EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
97                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
98                  transport
99          );
100         return response == null ? null : (List<KdFacetInfo>) KdFacetInfo.fromJsonArrayToKdFacetInfoes(response.getContent());
101     }
102 }