View Javadoc
1   package com.srv4pos.server.api.treasurer;
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 work with Treasurer entity.
22   * @author Ruslan Kashapov.
23   */
24  public class TreasurerService {
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 TreasurerService(URL url, Credentials credentials, Transport transport) {
37          this.url = url;
38          this.credentials = credentials;
39          this.transport = transport;
40      }
41  
42      /**
43       * Returns an treasurer entity.
44       *
45       * @param version    latest version of seller, may be null (if latest data needed)
46       * @param identifier identifies entity. must match to {@link com.srv4pos.server.api.infrastructure.Constraints#WINDOWS_FILENAME_REGEX}
47       * @return null if not modified since version or entity. if version is null then return value can not be null
48       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
49       *          if version mismatch
50       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException
51       *          if entity is not found
52       */
53      public TreasurerInfo get(Integer version, String identifier) {
54          StringResponse response = requestString(
55                  format("%s/treasurers/%s", credentials.getSellerId(), identifier),
56                  HttpMethod.GET,
57                  url,
58                  credentials.toAuthentication(),
59                  HttpHelper.integerToETag(version),
60                  null,
61                  EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
62                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
63                  transport
64          );
65          return response == null ? null : TreasurerInfo.fromJsonToTreasurerInfo(response.getContent());
66      }
67  
68      /**
69       * Put treasurer to the server.
70       *
71       * @param version   expected latest version of seller
72       * @param treasurerInfo data to put
73       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
74       *          if version mismatch
75       */
76      public void put(int version, TreasurerInfo treasurerInfo) {
77          requestVoid(
78                  format("%s/treasurers/%s", credentials.getSellerId(), treasurerInfo.getIdentifier()),
79                  HttpMethod.PUT,
80                  url,
81                  credentials.toAuthentication(),
82                  HttpHelper.integerToETag(version),
83                  null,
84                  new StringConnectionWriter(treasurerInfo.toJson()),
85                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
86                  transport
87          );
88      }
89  
90      /**
91       * Create treasurer on the server.
92       *
93       * @param version   expected latest version of seller
94       * @param treasurerInfo data to put
95       * @return identifier info
96       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
97       *          if version mismatch
98       */
99      public IdentifierInfo create(int version, TreasurerInfo treasurerInfo) {
100         return IdentifierInfo.fromJsonToIdentifierInfo(
101                 requestString(
102                         format("%s/treasurers", credentials.getSellerId()),
103                         HttpMethod.POST,
104                         url,
105                         credentials.toAuthentication(),
106                         HttpHelper.integerToETag(version),
107                         null,
108                         new StringConnectionWriter(treasurerInfo.toJson()),
109                         HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
110                         transport
111                 ).getContent()
112         );
113     }
114 
115     /**
116      * Delete entity from the server.
117      *
118      * @param version    current seller version
119      * @param identifier identifies entity. must match to {@link com.srv4pos.server.api.infrastructure.Constraints#WINDOWS_FILENAME_REGEX}
120      * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
121      *          if version mismatch
122      * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException
123      *          if entity is not found
124      */
125     public void delete(int version, String identifier) {
126         requestVoid(
127                 format("%s/treasurers/%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 com.srv4pos.server.api.treasurer.TreasurerInfo#isDeleted()} = true)
145      */
146     public List<TreasurerInfo> listDiff(int versionFrom, int versionTo) {
147         StringResponse response = requestString(
148                 format("%s/treasurers-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<TreasurerInfo>) TreasurerInfo.fromJsonArrayToTreasurerInfoes(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<TreasurerInfo> 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 {@link com.srv4pos.server.api.treasurer.TreasurerInfo.Fields}
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<TreasurerInfo> list(Integer version,
183                                     Integer firstResult,
184                                     Integer maxResults,
185                                     String like,
186                                     TreasurerInfo.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 
205         StringResponse response = requestString(format("%s/treasurers",
206                         credentials.getSellerId()),
207                 HttpMethod.GET,
208                 url,
209                 credentials.toAuthentication(),
210                 HttpHelper.integerToETag(version),
211                 params,
212                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
213                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
214                 transport);
215 
216         return response == null ? null : (List<TreasurerInfo>) TreasurerInfo.fromJsonArrayToTreasurerInfoes(response.getContent());
217     }
218 }