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