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
27
28
29
30
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
40
41
42
43
44
45
46
47
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
66
67
68
69
70
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
88
89
90
91
92
93
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
113
114
115
116
117
118
119
120
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
160
161
162
163
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 }