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
33
34
35
36
37
38
39
40
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
59
60
61
62
63
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
81
82
83
84
85
86
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
106
107
108
109
110 public List<CustomizationItemInfo> list(Integer version) {
111 return list(version, null, null, null, null, null);
112 }
113
114
115
116
117
118
119
120
121
122
123
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
163
164
165
166
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 }