View Javadoc
1   package com.srv4pos.server.api.hairdresser;
2   
3   import com.srv4pos.server.api.infrastructure.HttpMethod;
4   import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
5   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
6   import com.srv4pos.server.api.infrastructure.http.HttpHelper;
7   import com.srv4pos.server.api.infrastructure.http.Transport;
8   import com.srv4pos.server.api.infrastructure.request.StringResponse;
9   
10  import java.net.URL;
11  
12  import static com.srv4pos.server.api.infrastructure.EmptyConnectionWriter.EMPTY_CONNECTION_WRITER;
13  import static com.srv4pos.server.api.infrastructure.http.HttpHelper.CONTENT_TYPE_APPLICATION_JSON;
14  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
15  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
16  import static java.lang.String.format;
17  
18  /**
19   * Exchange information for Hairdresser entity.
20   */
21  public class HairdresserService {
22      private URL url;
23      private Credentials credentials;
24      private Transport transport;
25  
26      /**
27       * Constructs the service.
28       *
29       * @param url         of the server
30       * @param credentials to work with the server
31       * @param transport   to know the method to reach the server
32       */
33      public HairdresserService(URL url, Credentials credentials, Transport transport) {
34          this.url = url;
35          this.credentials = credentials;
36          this.transport = transport;
37      }
38  
39      /**
40       * Get latest information about hairdresser from the server.
41       *
42       * @param sellerId of the seller
43       * @param version  put value from {@link com.srv4pos.server.api.seller.SellerService#getThisVersion()}. if you don't have any {@link
44       *                 com.srv4pos.server.api.seller.SellerInfo} instance, then pass null to return the data anyway
45       * @return null if not modified or fresh version of info
46       * @see com.srv4pos.server.api.seller.SellerServiceSample
47       */
48      public HairdresserInfo get(String sellerId, Integer version) {
49          StringResponse response = requestString(
50                  format("hairdressers/%s", sellerId),
51                  HttpMethod.GET,
52                  url,
53                  credentials.toAuthentication(),
54                  HttpHelper.integerToETag(version),
55                  null,
56                  EMPTY_CONNECTION_WRITER,
57                  CONTENT_TYPE_APPLICATION_JSON,
58                  transport
59          );
60          return response == null ? null : HairdresserInfo.fromJsonToHairdresserInfo(response.getContent());
61      }
62  
63      /**
64       * Send information about hairdresser to the server to store it.
65       *
66       * @param version of the seller. it must match to the version of seller on the server
67       * @param sellerId       seller-hairdresser id
68       * @param hairdresserInfo the data
69       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException if server has newer data sent from other client before
70       * @see com.srv4pos.server.api.seller.SellerServiceSample
71       */
72      public void put(int version, String sellerId, HairdresserInfo hairdresserInfo) {
73          requestVoid(
74                  format("hairdressers/%s", sellerId),
75                  HttpMethod.PUT,
76                  url,
77                  credentials.toAuthentication(),
78                  HttpHelper.integerToETag(version),
79                  null,
80                  new StringConnectionWriter(hairdresserInfo.toJson()),
81                  CONTENT_TYPE_APPLICATION_JSON,
82                  transport
83          );
84      }
85  
86      /**
87       * Remove hairdresser and all information that concerns hairdresser.
88       *
89       * @param sellerId identifier of seller
90       */
91      public void remove(String sellerId) {
92          requestVoid(
93                  format("hairdressers/%s", sellerId),
94                  HttpMethod.DELETE,
95                  url,
96                  credentials.toAuthentication(),
97                  null,
98                  null,
99                  EMPTY_CONNECTION_WRITER,
100                 CONTENT_TYPE_APPLICATION_JSON,
101                 transport);
102     }
103 }