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
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 DayService {
25 private URL url;
26 private Credentials credentials;
27 private Transport transport;
28
29
30
31
32
33
34
35
36 public DayService(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 public List<DayInfo> listDiff(String cashRegisterName, int versionFrom, int versionTo) {
51 StringResponse response = requestString(
52 String.format("%s/registrations/%s/days-diff/%s/%s", credentials.getSellerId(), cashRegisterName, versionFrom, versionTo),
53 HttpMethod.GET,
54 url,
55 credentials.toAuthentication(),
56 null,
57 null,
58 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
59 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
60 transport
61 );
62 return (List<DayInfo>) DayInfo.fromJsonArrayToDayInfoes(response.getContent());
63 }
64
65
66
67
68
69
70
71
72
73
74
75 public IdentifierInfo create(int sellerVersion, int registrationVersion, DayInfo dayInfo) {
76 return IdentifierInfo.fromJsonToIdentifierInfo(
77 requestString(
78 format("%s/days", credentials.getSellerId()),
79 HttpMethod.POST,
80 url,
81 credentials.toAuthentication(),
82 HttpHelper.toETag(sellerVersion, registrationVersion),
83 null,
84 new StringConnectionWriter(dayInfo.toJson()),
85 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
86 transport
87 ).getContent());
88 }
89
90
91
92
93
94
95
96
97
98
99 public void put(int sellerVersion, int registrationVersion, DayInfo dayInfo) {
100 requestVoid(
101 format("%s/days/%s", credentials.getSellerId(), dayInfo.getIdentifier()),
102 HttpMethod.PUT,
103 url,
104 credentials.toAuthentication(),
105 HttpHelper.toETag(sellerVersion, registrationVersion),
106 null,
107 new StringConnectionWriter(dayInfo.toJson()),
108 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
109 transport
110 );
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public List<DayInfo> list(Integer version,
126 String cashRegisterName,
127 Integer firstResult,
128 Integer maxResults,
129 String like,
130 DayInfo.Fields orderBy,
131 Boolean orderDesc) {
132 HashMap<String, String> params = new HashMap<String, String>();
133
134 if (orderBy != null) {
135 params.put("orderBy", orderBy.name());
136 }
137 if (orderDesc != null) {
138 params.put("orderDesc", orderDesc.toString());
139 }
140 if (firstResult != null) {
141 params.put("firstResult", Integer.toString(firstResult));
142 }
143 if (maxResults != null) {
144 params.put("maxResults", Integer.toString(maxResults));
145 }
146 if (like != null) {
147 params.put("like", like);
148 }
149 StringResponse response = requestString(
150 format("%s/registrations/%s/days", credentials.getSellerId(), cashRegisterName),
151 HttpMethod.GET,
152 url,
153 credentials.toAuthentication(),
154 HttpHelper.integerToETag(version),
155 params,
156 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
157 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
158 transport
159 );
160 return response == null ? null : (List<DayInfo>) DayInfo.fromJsonArrayToDayInfoes(response.getContent());
161 }
162 }