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