1 package com.srv4pos.server.api.commodity;
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
22
23
24 public class CommodityService {
25 private URL url;
26 private Credentials credentials;
27 private Transport transport;
28
29
30
31
32
33
34
35
36 public CommodityService(URL url, Credentials credentials, Transport transport) {
37 this.url = url;
38 this.credentials = credentials;
39 this.transport = transport;
40 }
41
42
43
44
45
46
47
48
49
50
51
52
53 public CommodityInfo get(Integer version, String identifier) {
54 StringResponse response = requestString(
55 format("%s/commodities/%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 : CommodityInfo.fromJsonToCommodityInfo(response.getContent());
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79 public void put(int version, CommodityInfo commodityInfo) {
80 requestVoid(
81 format("%s/commodities/%s", credentials.getSellerId(), commodityInfo.getIdentifier()),
82 HttpMethod.PUT,
83 url,
84 credentials.toAuthentication(),
85 HttpHelper.integerToETag(version),
86 null,
87 new StringConnectionWriter(commodityInfo.toJson()),
88 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
89 transport
90 );
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public IdentifierInfo create(int version, CommodityInfo commodityInfo) {
106 return IdentifierInfo.fromJsonToIdentifierInfo(
107 requestString(
108 format("%s/commodities", credentials.getSellerId()),
109 HttpMethod.POST,
110 url,
111 credentials.toAuthentication(),
112 HttpHelper.integerToETag(version),
113 null,
114 new StringConnectionWriter(commodityInfo.toJson()),
115 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
116 transport
117 ).getContent()
118 );
119 }
120
121
122
123
124
125
126
127
128
129 public void delete(int version, String identifier) {
130 requestVoid(
131 format("%s/commodities/%s", credentials.getSellerId(), 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
145
146
147
148
149
150 public List<CommodityInfo> listDiff(int versionFrom, int versionTo) {
151 StringResponse response = requestString(
152 format("%s/commodities-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<CommodityInfo>) CommodityInfo.fromJsonArrayToCommodityInfoes(response.getContent());
163 }
164
165
166
167
168
169
170
171 public List<CommodityInfo> list(Integer version) {
172 return list(version, null, null, null, null, null);
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 public List<CommodityInfo> list(Integer version,
189 Integer firstResult,
190 Integer maxResults,
191 String like,
192 CommodityInfo.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 StringResponse response = requestString(
211 format("%s/commodities", credentials.getSellerId()),
212 HttpMethod.GET,
213 url,
214 credentials.toAuthentication(),
215 HttpHelper.integerToETag(version),
216 params,
217 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
218 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
219 transport
220 );
221 return response == null ? null : (List<CommodityInfo>) CommodityInfo.fromJsonArrayToCommodityInfoes(response.getContent());
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 public List<CommodityAdvancedInfo> listAdvanced(Integer version,
238 Integer firstResult,
239 Integer maxResults,
240 String like,
241 CommodityInfo.Fields orderBy,
242 Boolean orderDesc) {
243 HashMap<String, String> params = new HashMap<String, String>();
244 if (orderBy != null) {
245 params.put("orderBy", orderBy.name());
246 }
247 if (orderDesc != null) {
248 params.put("orderDesc", orderDesc.toString());
249 }
250 if (firstResult != null) {
251 params.put("firstResult", Integer.toString(firstResult));
252 }
253 if (maxResults != null) {
254 params.put("maxResults", Integer.toString(maxResults));
255 }
256 if (like != null) {
257 params.put("like", like);
258 }
259 StringResponse response = requestString(
260 format("%s/commodities-advanced", credentials.getSellerId()),
261 HttpMethod.GET,
262 url,
263 credentials.toAuthentication(),
264 HttpHelper.integerToETag(version),
265 params,
266 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
267 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
268 transport
269 );
270 return response == null ? null : (List<CommodityAdvancedInfo>) CommodityAdvancedInfo.fromJsonArrayToCommodityAdvancedInfoes(response.getContent());
271 }
272 }