View Javadoc
1   package com.srv4pos.server.api.keyboard;
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  
19  /**
20   * Service for reaching Keyboard entity. <p>User: Sergey, Date: 18.03.14 18:14</p>.
21   */
22  public class KeyboardService {
23      private URL url;
24      private Credentials credentials;
25      private Transport transport;
26  
27      /**
28       * Constructs the service.
29       *
30       * @param url         of the server
31       * @param credentials to work with the server
32       * @param transport   to know the method to reach the server
33       */
34      public KeyboardService(URL url, Credentials credentials, Transport transport) {
35          this.url = url;
36          this.credentials = credentials;
37          this.transport = transport;
38      }
39  
40      /**
41       * Returns an entity.
42       *
43       * @param version    latest version of seller, may be null (if latest data needed)
44       * @param identifier identifies entity
45       * @return null if not modified since version or entity. if version is null then return value can not be null
46       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException if version mismatch
47       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException        if entity is not found
48       * @see KeyboardServiceSample
49       */
50      public KeyboardInfo get(Integer version, String identifier) {
51          StringResponse response = requestString(credentials.getSellerId() + "/keyboards/" + identifier, HttpMethod.GET, url, credentials.toAuthentication(),
52                  HttpHelper.integerToETag(version), null, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
53                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
54          return response == null ? null : KeyboardInfo.fromJsonToKeyboardInfo(response.getContent());
55      }
56  
57      /**
58       * Put keyboard to the server.
59       *
60       * @param version      expected latest version of seller
61       * @param keyboardInfo data to put
62       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException if version mismatch
63       */
64      public void put(int version, KeyboardInfo keyboardInfo) {
65          requestVoid(credentials.getSellerId() + "/keyboards/" + keyboardInfo.getIdentifier(), HttpMethod.PUT, url, credentials.toAuthentication(),
66                  HttpHelper.integerToETag(version), null, new StringConnectionWriter(keyboardInfo.toJson()),
67                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
68      }
69  
70      /**
71       * Create keyboard on the server.
72       *
73       * @param version      expected latest version of seller
74       * @param keyboardInfo data to put
75       * @return generated unique identifier
76       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException if version mismatch
77       */
78      public IdentifierInfo create(int version, KeyboardInfo keyboardInfo) {
79          return IdentifierInfo
80                  .fromJsonToIdentifierInfo(requestString(credentials.getSellerId() + "/keyboards", HttpMethod.POST, url, credentials.toAuthentication(),
81                          HttpHelper.integerToETag(version), null, new StringConnectionWriter(keyboardInfo.toJson()),
82                          HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport).getContent());
83      }
84  
85      /**
86       * Delete keyboard from the server.
87       *
88       * @param version    current version of the entity
89       * @param identifier identifies entity
90       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException if version mismatch
91       */
92      public void delete(int version, String identifier) {
93          requestVoid(credentials.getSellerId() + "/keyboards/" + identifier, HttpMethod.DELETE, url, credentials.toAuthentication(),
94                  HttpHelper.integerToETag(version), null, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
95                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
96      }
97  
98      /**
99       * Returns list of item changes between two versions.
100      *
101      * @param versionFrom version from included
102      * @param versionTo   version to included
103      * @return list of items to be added, edited or deleted ({@link KeyboardInfo#deleted} = true)
104      */
105     public List<KeyboardInfo> listDiff(int versionFrom, int versionTo) {
106         StringResponse response = requestString(String.format("%s/keyboards-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo),
107                 HttpMethod.GET, url, credentials.toAuthentication(), null, null, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
108                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
109         return (List<KeyboardInfo>) KeyboardInfo.fromJsonArrayToKeyboardInfoes(response.getContent());
110     }
111 
112     /**
113      * Returns list of entities.
114      *
115      * @param version version or null if fresh data required
116      * @return list of entities which exists on current seller or null if list of entities is not modified since version
117      */
118     public List<KeyboardInfo> list(Integer version) {
119         return list(version, null, null, null, null, null);
120     }
121 
122     /**
123      * Returns list of entities.
124      *
125      * @param firstResult when pagination starts or null
126      * @param maxResults  amount of items per page or null
127      * @param like        search string filter or null
128      * @param orderBy     field to order by, possible values are "IDENTIFIER", "NAME", "WIDTH", "HEIGHT", "DEVICE_FORM_FACTOR"
129      * @param orderDesc   field to direction of ordering
130      * @param version     version or null if fresh data required
131      * @return list of entities which exists on current seller or null if list of entities is not modified since version
132      */
133     public List<KeyboardInfo> list(Integer version,
134                                    Integer firstResult,
135                                    Integer maxResults,
136                                    String like,
137                                    KeyboardInfo.Fields orderBy,
138                                    Boolean orderDesc) {
139         HashMap<String, String> params = new HashMap<String, String>();
140         if (orderBy != null) {
141             params.put("orderBy", orderBy.name());
142         }
143         if (orderDesc != null) {
144             params.put("orderDesc", orderDesc.toString());
145         }
146         if (firstResult != null) {
147             params.put("firstResult", Integer.toString(firstResult));
148         }
149         if (maxResults != null) {
150             params.put("maxResults", Integer.toString(maxResults));
151         }
152         if (like != null) {
153             params.put("like", like);
154         }
155         StringResponse response = requestString(credentials.getSellerId() + "/keyboards", HttpMethod.GET, url, credentials.toAuthentication(),
156                 HttpHelper.integerToETag(version), params, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
157                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
158         return response == null ? null : (List<KeyboardInfo>) KeyboardInfo.fromJsonArrayToKeyboardInfoes(response.getContent());
159     }
160 }