1 package com.srv4pos.server.api.day;
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 import java.util.Map;
16
17 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
18 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
19 import static java.lang.String.format;
20
21
22
23
24
25 public class DayService {
26 private URL url;
27 private Credentials credentials;
28 private Transport transport;
29
30
31
32
33
34
35
36
37 public DayService(URL url, Credentials credentials, Transport transport) {
38 this.url = url;
39 this.credentials = credentials;
40 this.transport = transport;
41 }
42
43
44
45
46
47
48
49
50
51 public List<DayInfo> listDiff(String cashRegisterName, int versionFrom, int versionTo) {
52 StringResponse response = requestString(
53 String.format("%s/registrations/%s/days-diff/%s/%s", credentials.getSellerId(), cashRegisterName, versionFrom, versionTo),
54 HttpMethod.GET,
55 url,
56 credentials.toAuthentication(),
57 null,
58 null,
59 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
60 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
61 transport
62 );
63 return (List<DayInfo>) DayInfo.fromJsonArrayToDayInfoes(response.getContent());
64 }
65
66
67
68
69
70
71
72
73
74 public IdentifierInfo create(int sellerVersion, DayInfo dayInfo) {
75 return IdentifierInfo.fromJsonToIdentifierInfo(
76 requestString(
77 format("%s/days", credentials.getSellerId()),
78 HttpMethod.POST,
79 url,
80 credentials.toAuthentication(),
81 HttpHelper.integerToETag(sellerVersion),
82 null,
83 new StringConnectionWriter(dayInfo.toJson()),
84 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
85 transport
86 ).getContent());
87 }
88
89
90
91
92
93
94
95
96 public void put(int sellerVersion, DayInfo dayInfo) {
97 requestVoid(
98 format("%s/days/%s", credentials.getSellerId(), dayInfo.getIdentifier()),
99 HttpMethod.PUT,
100 url,
101 credentials.toAuthentication(),
102 HttpHelper.integerToETag(sellerVersion),
103 null,
104 new StringConnectionWriter(dayInfo.toJson()),
105 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
106 transport
107 );
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122 public List<DayInfo> list(Integer version,
123 String cashRegisterName,
124 Integer firstResult,
125 Integer maxResults,
126 String like,
127 DayInfo.Fields orderBy,
128 Boolean orderDesc) {
129 HashMap<String, String> params = new HashMap<String, String>();
130
131 if (orderBy != null) {
132 params.put("orderBy", orderBy.name());
133 }
134 if (orderDesc != null) {
135 params.put("orderDesc", orderDesc.toString());
136 }
137 if (firstResult != null) {
138 params.put("firstResult", Integer.toString(firstResult));
139 }
140 if (maxResults != null) {
141 params.put("maxResults", Integer.toString(maxResults));
142 }
143 if (like != null) {
144 params.put("like", like);
145 }
146 StringResponse response = requestString(
147 format("%s/registrations/%s/days", credentials.getSellerId(), cashRegisterName),
148 HttpMethod.GET,
149 url,
150 credentials.toAuthentication(),
151 HttpHelper.integerToETag(version),
152 params,
153 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
154 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
155 transport
156 );
157 return response == null ? null : (List<DayInfo>) DayInfo.fromJsonArrayToDayInfoes(response.getContent());
158 }
159
160 public void close(String cashRegisterName, int sellerVersion, int registrationVersion) {
161 requestVoid(
162 format("%s/registrations/%s/days/current", credentials.getSellerId(), cashRegisterName),
163 HttpMethod.POST,
164 url,
165 credentials.toAuthentication(),
166 HttpHelper.toETag(sellerVersion, registrationVersion),
167 null,
168 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
169 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
170 transport
171 );
172 }
173
174 public DaysStatsInfo stats(String period, String cashRegisterName) {
175 Map<String, String> params = new HashMap<String, String>();
176 if (period != null) {
177 params.put("period", period);
178 }
179 if (cashRegisterName != null) {
180 params.put("cashRegisterName", cashRegisterName);
181 }
182
183 StringResponse response = requestString(
184 format("%s/days/stats", credentials.getSellerId()),
185 HttpMethod.GET,
186 url,
187 credentials.toAuthentication(),
188 null,
189 params,
190 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
191 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
192 transport
193 );
194 return response == null ? null : DaysStatsInfo.fromJsonToDaysStatsInfo(response.getContent());
195 }
196 }