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