View Javadoc
1   package com.srv4pos.server.api.booking;
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.exceptions.enums.ForbiddenErrorType;
9   import com.srv4pos.server.api.infrastructure.http.HttpHelper;
10  import com.srv4pos.server.api.infrastructure.http.Transport;
11  import com.srv4pos.server.api.infrastructure.request.StringResponse;
12  
13  import java.net.URL;
14  import java.util.HashMap;
15  import java.util.List;
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   * Created by eugene.shishkin on 05.09.14.
23   */
24  public class BookingService {
25  
26      private URL url;
27      private Credentials credentials;
28      private Transport transport;
29  
30      /**
31       * Constructs the service.
32       *
33       * @param url         of the server
34       * @param credentials to work with the server
35       * @param transport   to know the method to reach the server
36       */
37      public BookingService(URL url, Credentials credentials, Transport transport) {
38          this.url = url;
39          this.credentials = credentials;
40          this.transport = transport;
41      }
42  
43      /**
44       * Modifies a booking.
45       *
46       * @param version     expected latest version of seller
47       * @param bookingInfo data to put
48       * @param identifier  identifier of booking
49       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException if version mismatch
50       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException        if booking or seller wasn't found
51       * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException       see:
52       *                                                                        {@link ForbiddenErrorType#INSUFFICIENT_ACCESS_RIGHTS}
53       */
54      public void put(int version, BookingInfo bookingInfo, String identifier) {
55          requestVoid(
56                  format("%s/bookings/%s", credentials.getSellerId(), identifier),
57                  HttpMethod.PUT,
58                  url,
59                  credentials.toAuthentication(),
60                  HttpHelper.integerToETag(version),
61                  null,
62                  new StringConnectionWriter(bookingInfo.toJson()),
63                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
64                  transport
65          );
66      }
67  
68      /**
69       * Deletes a booking.
70       *
71       * @param version current version of the entity
72       * @param identifier  identifier of booking
73       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException if version mismatch
74       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException        if booking or seller wasn't found
75       * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException       see:
76       *                                                                        {@link ForbiddenErrorType#INSUFFICIENT_ACCESS_RIGHTS}
77       */
78      public void delete(int version, String identifier) {
79          requestVoid(
80                  format("%s/bookings/%s", credentials.getSellerId(), identifier),
81                  HttpMethod.DELETE,
82                  url,
83                  credentials.toAuthentication(),
84                  HttpHelper.integerToETag(version),
85                  null,
86                  EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
87                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
88                  transport
89          );
90      }
91  
92      /**
93       * Create booking on the server.
94       *
95       * @param bookingInfo data to put
96       * @param version current version of seller
97       * @return generated identifier
98       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException if seller wasn't found
99       */
100     public String create(BookingInfo bookingInfo, Integer version) {
101         return IdentifierInfo.fromJsonToIdentifierInfo(
102                 requestString(
103                         format("%s/bookings", credentials.getSellerId()),
104                         HttpMethod.POST,
105                         url,
106                         credentials.toAuthentication(),
107                         HttpHelper.integerToETag(version),
108                         null,
109                         new StringConnectionWriter(bookingInfo.toJson()),
110                         HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
111                         transport
112                 ).getContent()).getIdentifier();
113     }
114 
115     /**
116      * Returns an entity.
117      *
118      * @param identifier identifies entity
119      * @return the entity
120      * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException  if booking or seller wasn't found
121      * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException           see:
122      *                                                                            {@link ForbiddenErrorType#INSUFFICIENT_ACCESS_RIGHTS}
123      */
124     public BookingInfo get(final String identifier) {
125         return BookingInfo.fromJsonToBookingInfo(
126                 requestString(
127                         format("%s/bookings/%s", credentials.getSellerId(), identifier),
128                         HttpMethod.GET,
129                         url,
130                         credentials.toAuthentication(),
131                         null,
132                         null,
133                         EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
134                         HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
135                         transport).getContent());
136     }
137 
138     /**
139      * Returns list of bookings.
140      *
141      * @param firstResult when pagination starts or null
142      * @param maxResults  amount of items per page or null
143      * @param like        search string filter or null
144      * @param orderBy     field to order by, possible values are "IDENTIFIER"
145      * @param orderDesc   field to direction of ordering
146      * @return list of entities which exists on current seller or null if list of entities is not modified since version
147      */
148     public List<BookingInfo> list(Integer firstResult,
149                                   Integer maxResults,
150                                   String like,
151                                   BookingInfo.Fields orderBy,
152                                   Boolean orderDesc) {
153         HashMap<String, String> params = new HashMap<String, String>();
154         if (orderBy != null) {
155             params.put("orderBy", orderBy.name());
156         }
157         if (orderDesc != null) {
158             params.put("orderDesc", orderDesc.toString());
159         }
160         if (firstResult != null) {
161             params.put("firstResult", Integer.toString(firstResult));
162         }
163         if (maxResults != null) {
164             params.put("maxResults", Integer.toString(maxResults));
165         }
166         if (like != null) {
167             params.put("like", like);
168         }
169         StringResponse response = requestString(
170                 format("%s/bookings", credentials.getSellerId()),
171                 HttpMethod.GET,
172                 url,
173                 credentials.toAuthentication(),
174                 null,
175                 params,
176                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
177                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
178                 transport
179         );
180         return response == null ? null : (List<BookingInfo>) BookingInfo.fromJsonArrayToBookingInfoes(response.getContent());
181     }
182 
183     /**
184      * Returns list of item changes between two versions. Since we don't have delete and edit methods, it
185      * returns only newly created bookings
186      *
187      * @param versionFrom version from included
188      * @param versionTo   version to included
189      * @return list of items to be added, edited or deleted (actually, only created)
190      */
191     public List<BookingInfo> listDiff(int versionFrom, int versionTo) {
192         StringResponse response = requestString(String.format("%s/bookings-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo), HttpMethod.GET, url,
193                 credentials.toAuthentication(), null, null, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER, HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
194         return (List<BookingInfo>) BookingInfo.fromJsonArrayToBookingInfoes(response.getContent());
195     }
196 }