View Javadoc
1   package com.srv4pos.server.api.keyboardentry;
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   * Service for reaching KeyboardEntry service.
22   * <p>User: Sergey, Date: 29.03.14 20:24</p>.
23   */
24  public class KeyboardEntryService {
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 KeyboardEntryService(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. 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 KeyboardEntryInfo get(Integer version, String identifier) {
54          StringResponse response = requestString(
55                  credentials.getSellerId() + "/keyboard-entries/" + 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 : KeyboardEntryInfo.fromJsonToKeyboardEntryInfo(response.getContent());
66      }
67  
68      /**
69       * Put keyboardEntry to the server.
70       *
71       * @param version   expected latest version of seller
72       * @param keyboardEntryInfo data to put
73       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
74       *          if version mismatch
75       * @throws com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException
76       *          if category in {@link KeyboardEntryInfo#keyboard}  is not found
77       *          if product in {@link KeyboardEntryInfo#product} is not found
78       */
79      public void put(int version, KeyboardEntryInfo keyboardEntryInfo) {
80          requestVoid(
81                  credentials.getSellerId() + "/keyboard-entries/" + keyboardEntryInfo.getIdentifier(),
82                  HttpMethod.PUT,
83                  url,
84                  credentials.toAuthentication(),
85                  HttpHelper.integerToETag(version),
86                  null,
87                  new StringConnectionWriter(keyboardEntryInfo.toJson()),
88                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
89                  transport
90          );
91      }
92  
93      /**
94       * Create keyboardEntry on the server.
95       *
96       * @param version   expected latest version of seller
97       * @param keyboardEntryInfo data to put
98       * @return identifier info
99       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
100      *          if version mismatch
101      * @throws com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException
102      *          if category in {@link KeyboardEntryInfo#keyboard}  is not found
103      *          if product in {@link KeyboardEntryInfo#product} is not found
104      */
105     public IdentifierInfo create(int version, KeyboardEntryInfo keyboardEntryInfo) {
106         return IdentifierInfo.fromJsonToIdentifierInfo(
107                 requestString(
108                         credentials.getSellerId() + "/keyboard-entries",
109                         HttpMethod.POST,
110                         url,
111                         credentials.toAuthentication(),
112                         HttpHelper.integerToETag(version),
113                         null,
114                         new StringConnectionWriter(keyboardEntryInfo.toJson()),
115                         HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
116                         transport
117                 ).getContent()
118         );
119     }
120 
121     /**
122      * Delete entity from the server.
123      *
124      * @param version    current seller version
125      * @param identifier identifies entity. must match to {@link com.srv4pos.server.api.infrastructure.Constraints#WINDOWS_FILENAME_REGEX}
126      * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
127      *          if version mismatch
128      */
129     public void delete(int version, String identifier) {
130         requestVoid(
131                 credentials.getSellerId() + "/keyboard-entries/" + identifier,
132                 HttpMethod.DELETE,
133                 url,
134                 credentials.toAuthentication(),
135                 HttpHelper.integerToETag(version),
136                 null,
137                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
138                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
139                 transport
140         );
141     }
142 
143     /**
144      * Returns list of item changes between two versions.
145      *
146      * @param versionFrom version from included
147      * @param versionTo   version to included
148      * @return list of items to be added, edited or deleted ({@link KeyboardEntryInfo#deleted} = true)
149      */
150     public List<KeyboardEntryInfo> listDiff(int versionFrom, int versionTo) {
151         StringResponse response = requestString(
152                 format("%s/keyboard-entries-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo),
153                 HttpMethod.GET,
154                 url,
155                 credentials.toAuthentication(),
156                 null,
157                 null,
158                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
159                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
160                 transport
161         );
162         return (List<KeyboardEntryInfo>) KeyboardEntryInfo.fromJsonArrayToKeyboardEntryInfoes(response.getContent());
163     }
164 
165     /**
166      * Returns list of entities.
167      *
168      * @param version version or null if fresh data required
169      * @return list of entities which exists on current seller or null if list of entities is not modified since version
170      */
171     public List<KeyboardEntryInfo> list(Integer version) {
172         return list(version, null, null, null, null, null);
173     }
174 
175     /**
176      * Returns list of entities.
177      *
178      * @param firstResult when pagination starts or null
179      * @param maxResults  amount of items per page or null
180      * @param like search string filter or null
181      * @param orderBy field to order by, possible values are "IDENTIFIER", "PRODUCT_NAME", "PRODUCT_BARCODE",
182      *                "PRODUCT_NETTO", "PRODUCT_SALES_UNIT", "PRODUCT_COUNTED_IN_PRICE", "KEYBOARD_NAME",
183      *                "SUB_KEYBOARD_NAME", "POSITION_X", "POSITION_Y", "WIDTH", "HEIGHT", "KEY_ACTION", "LABEL"
184      * @param orderDesc field to direction of ordering
185      * @param version version or null if fresh data required
186      * @return list of entities which exists on current seller or null if list of entities is not modified since version
187      */
188     public List<KeyboardEntryInfo> list(Integer version,
189                                         Integer firstResult,
190                                         Integer maxResults,
191                                         String like,
192                                         KeyboardEntryInfo.Fields orderBy,
193                                         Boolean orderDesc) {
194         HashMap<String, String> params = new HashMap<String, String>();
195         if (orderBy != null) {
196             params.put("orderBy", orderBy.name());
197         }
198         if (orderDesc != null) {
199             params.put("orderDesc", orderDesc.toString());
200         }
201         if (firstResult != null) {
202             params.put("firstResult", Integer.toString(firstResult));
203         }
204         if (maxResults != null) {
205             params.put("maxResults", Integer.toString(maxResults));
206         }
207         if (like != null) {
208             params.put("like", like);
209         }
210 
211         StringResponse response = requestString(
212                 credentials.getSellerId() + "/keyboard-entries",
213                 HttpMethod.GET,
214                 url,
215                 credentials.toAuthentication(),
216                 HttpHelper.integerToETag(version),
217                 params,
218                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
219                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
220                 transport
221 
222         );
223         return response == null ? null : (List<KeyboardEntryInfo>) KeyboardEntryInfo.fromJsonArrayToKeyboardEntryInfoes(response.getContent());
224     }
225 }