View Javadoc
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   * Service for reaching Day entity.
22   * <p>User: Pavel Abizyaev, Date: 11.09.2014 18:52</p>
23   */
24  public class DayService {
25      private URL url;
26      private Credentials credentials;
27      private Transport transport;
28  
29      /**
30       * Constructs the service.
31       *
32       * @param url         of the server
33       * @param credentials to work with the server
34       * @param transport   to know the method to reach the server
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       * Returns list of item changes between two versions.
44       *
45       * @param cashRegisterName cash register name
46       * @param versionFrom version from included
47       * @param versionTo   version to included
48       * @return list of items to be added or updated
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       * Create day on the server.
67       *
68       * @param sellerVersion       expected latest version of seller
69       * @param registrationVersion expected latest version of registration
70       * @param dayInfo data to put
71       * @return generated unique identifier
72       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
73       *          if version mismatch
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       * Put day to the server.
92       *
93       * @param sellerVersion       expected latest version of seller
94       * @param registrationVersion expected latest version of registration
95       * @param dayInfo data to put
96       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
97       *          if version mismatch
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      * Returns list of entities.
115      *
116      * @param cashRegisterName cash register name or null
117      * @param firstResult when pagination starts or null
118      * @param maxResults  amount of items per page or null
119      * @param like search string filter or null
120      * @param orderBy field to order by, possible values are "IDENTIFIER", "NAME", "VAT"
121      * @param orderDesc field to direction of ordering
122      * @param version version or null if fresh data required
123      * @return list of entities which exists on current seller or null if list of entities is not modified since version
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 }