View Javadoc
1   package com.srv4pos.server.api.tax;
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   * To reach Tax entity.
23   * <p>User: Kirill, Date: 05.08.13 18:00</p>.
24   */
25  public class TaxService {
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 TaxService(URL url, Credentials credentials, Transport transport) {
38          this.url = url;
39          this.credentials = credentials;
40          this.transport = transport;
41      }
42  
43      /**
44       * Returns an entity.
45       *
46       * @param version    latest version of seller, may be null (if latest data needed)
47       * @param identifier identifies entity
48       * @return null if not modified since version or entity. if version is null then return value can not be null
49       * @see TaxServiceSample
50       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
51       *          if version mismatch
52       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException
53       *          if entity is not found
54       */
55      public TaxInfo get(Integer version, String identifier) {
56          StringResponse response = requestString(
57                  format("%s/taxes/%s", credentials.getSellerId(), identifier),
58                  HttpMethod.GET,
59                  url,
60                  credentials.toAuthentication(),
61                  HttpHelper.integerToETag(version),
62                  null,
63                  EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
64                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
65                  transport
66          );
67          return response == null ? null : TaxInfo.fromJsonToTaxInfo(response.getContent());
68      }
69  
70      /**
71       * Put tax to the server.
72       *
73       * @param version   expected latest version of seller
74       * @param taxInfo data to put
75       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
76       *          if version mismatch
77       */
78      public void put(int version, TaxInfo taxInfo) {
79          requestVoid(
80                  format("%s/taxes/%s", credentials.getSellerId(), taxInfo.getIdentifier()),
81                  HttpMethod.PUT,
82                  url,
83                  credentials.toAuthentication(),
84                  HttpHelper.integerToETag(version),
85                  null,
86                  new StringConnectionWriter(taxInfo.toJson()),
87                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
88                  transport
89          );
90      }
91  
92      /**
93       * Create tax on the server.
94       *
95       * @param version   expected latest version of seller
96       * @param taxInfo data to put
97       * @return generated unique identifier
98       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
99       *          if version mismatch
100      */
101     public IdentifierInfo create(int version, TaxInfo taxInfo) {
102         return IdentifierInfo.fromJsonToIdentifierInfo(
103                 requestString(
104                         format("%s/taxes", credentials.getSellerId()),
105                         HttpMethod.POST,
106                         url,
107                         credentials.toAuthentication(),
108                         HttpHelper.integerToETag(version),
109                         null,
110                         new StringConnectionWriter(taxInfo.toJson()),
111                         HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
112                         transport
113                 ).getContent()
114         );
115     }
116 
117     /**
118      * Delete tax from the server.
119      *
120      * @param version    current version of the entity
121      * @param identifier identifies entity
122      * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
123      *          if version mismatch
124      */
125     public void delete(int version, String identifier) {
126         requestVoid(
127                 format("%s/taxes/%s", credentials.getSellerId(), identifier),
128                 HttpMethod.DELETE,
129                 url,
130                 credentials.toAuthentication(),
131                 HttpHelper.integerToETag(version),
132                 null,
133                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
134                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
135                 transport
136         );
137     }
138 
139     /**
140      * Returns list of item changes between two versions.
141      *
142      * @param versionFrom version from included
143      * @param versionTo   version to included
144      * @return list of items to be added, edited or deleted ({@link TaxInfo#deleted} = true)
145      */
146     public List<TaxInfo> listDiff(int versionFrom, int versionTo) {
147         StringResponse response = requestString(
148                 String.format("%s/taxes-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo),
149                 HttpMethod.GET,
150                 url,
151                 credentials.toAuthentication(),
152                 null,
153                 null,
154                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
155                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
156                 transport
157         );
158         return (List<TaxInfo>) TaxInfo.fromJsonArrayToTaxInfoes(response.getContent());
159     }
160 
161     /**
162      * Returns list of entities.
163      *
164      * @param version version or null if fresh data required
165      * @return list of entities which exists on current seller or null if list of entities is not modified since version
166      */
167     public List<TaxInfo> list(Integer version) {
168         return list(version, null, null, null, null, null);
169     }
170 
171     /**
172      * Returns list of entities.
173      *
174      * @param firstResult when pagination starts or null
175      * @param maxResults  amount of items per page or null
176      * @param like search string filter or null
177      * @param orderBy field to order by, possible values are "IDENTIFIER", "NAME", "VAT"
178      * @param orderDesc field to direction of ordering
179      * @param version version or null if fresh data required
180      * @return list of entities which exists on current seller or null if list of entities is not modified since version
181      */
182     public List<TaxInfo> list(Integer version,
183                               Integer firstResult,
184                               Integer maxResults,
185                               String like,
186                               TaxInfo.Fields orderBy,
187                               Boolean orderDesc) {
188         HashMap<String, String> params = new HashMap<String, String>();
189         if (orderBy != null) {
190             params.put("orderBy", orderBy.name());
191         }
192         if (orderDesc != null) {
193             params.put("orderDesc", orderDesc.toString());
194         }
195         if (firstResult != null) {
196             params.put("firstResult", Integer.toString(firstResult));
197         }
198         if (maxResults != null) {
199             params.put("maxResults", Integer.toString(maxResults));
200         }
201         if (like != null) {
202             params.put("like", like);
203         }
204         StringResponse response = requestString(
205                 format("%s/taxes", credentials.getSellerId()),
206                 HttpMethod.GET,
207                 url,
208                 credentials.toAuthentication(),
209                 HttpHelper.integerToETag(version),
210                 params,
211                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
212                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
213                 transport
214         );
215         return response == null ? null : (List<TaxInfo>) TaxInfo.fromJsonArrayToTaxInfoes(response.getContent());
216     }
217 
218     /**
219      * Returns stats by taxes.
220      * @param period filters by date period
221      * @param cashRegisterName filters by cash register name
222      * @return {@link TaxesStatsInfo} with stats.
223      * @throws com.srv4pos.server.api.exceptions.TypeMismatchJsonException if {@code period} {@link java.lang.String} isn't convertible to
224      *                                                                     {@link java.util.Date}.
225      */
226     public TaxesStatsInfo stats(String period, String cashRegisterName) {
227         Map<String, String> params = new HashMap<String, String>();
228         if (period != null) {
229             params.put("period", period);
230         }
231         if (cashRegisterName != null) {
232             params.put("cashRegisterName", cashRegisterName);
233         }
234 
235         StringResponse response = requestString(
236                 format("%s/taxes/stats", credentials.getSellerId()),
237                 HttpMethod.GET,
238                 url,
239                 credentials.toAuthentication(),
240                 null,
241                 params,
242                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
243                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
244                 transport
245         );
246         return response == null ? null : TaxesStatsInfo.fromJsonToTaxesStatsInfo(response.getContent());
247     }
248 }