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