View Javadoc
1   package com.srv4pos.server.api.restaurant;
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   import com.srv4pos.server.api.seller.PublicSellerInfo;
10  import com.srv4pos.server.api.infrastructure.exceptions.enums.ForbiddenErrorType;
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.EmptyConnectionWriter.EMPTY_CONNECTION_WRITER;
17  import static com.srv4pos.server.api.infrastructure.http.HttpHelper.CONTENT_TYPE_APPLICATION_JSON;
18  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
19  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
20  import static java.lang.String.format;
21  
22  /**
23   * Exchange information for Restaurant entity.
24   *
25   * @author pavel.tsarev
26   */
27  public class RestaurantService {
28      private URL url;
29      private Credentials credentials;
30      private Transport transport;
31  
32      /**
33       * Constructs the service.
34       *
35       * @param url         of the server
36       * @param credentials to work with the server
37       * @param transport   to know the method to reach the server
38       */
39      public RestaurantService(URL url, Credentials credentials, Transport transport) {
40          this.url = url;
41          this.credentials = credentials;
42          this.transport = transport;
43      }
44  
45      /**
46       * Get latest information about seller from the server.
47       *
48       * @param sellerId of the seller
49       * @param version  put value from {@link com.srv4pos.server.api.seller.SellerService#getThisVersion()}. if you don't have any {@link
50       *                 com.srv4pos.server.api.seller.SellerInfo} instance, then pass null to return the data anyway
51       * @return null if not modified or fresh version of info
52       * @see com.srv4pos.server.api.seller.SellerServiceSample
53       */
54      public RestaurantOverviewInfo get(String sellerId, Integer version) {
55          StringResponse response = requestString(
56                  format("/restaurants-overview/%s", sellerId),
57                  HttpMethod.GET,
58                  url,
59                  credentials.toAuthentication(),
60                  HttpHelper.integerToETag(version),
61                  null,
62                  EMPTY_CONNECTION_WRITER,
63                  CONTENT_TYPE_APPLICATION_JSON,
64                  transport
65          );
66          return response == null ? null : RestaurantOverviewInfo.fromJsonToRestaurantOverviewInfo(response.getContent());
67      }
68  
69      /**
70       * Send information about restaurant to the server to store it.
71       *
72       * @param restaurantInfo the data
73       * @param sellerId       seller-restaurant id
74       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException if server has newer data sent from other client before
75       * @peram version of the seller. it must match to the version of seller on the server
76       * @see com.srv4pos.server.api.seller.SellerServiceSample
77       */
78      public void put(int version, String sellerId, RestaurantInfo restaurantInfo) {
79          requestVoid(
80                  format("restaurants/%s", sellerId),
81                  HttpMethod.PUT,
82                  url,
83                  credentials.toAuthentication(),
84                  HttpHelper.integerToETag(version),
85                  null,
86                  new StringConnectionWriter(restaurantInfo.toJson()),
87                  CONTENT_TYPE_APPLICATION_JSON,
88                  transport
89          );
90      }
91  
92      /**
93       * Remove seller and all information that concerns seller.
94       *
95       * @param sellerId identifier of seller
96       */
97      public void remove(String sellerId) {
98          requestVoid(
99                  format("restaurants/%s", sellerId),
100                 HttpMethod.DELETE,
101                 url,
102                 credentials.toAuthentication(),
103                 null,
104                 null,
105                 EMPTY_CONNECTION_WRITER,
106                 CONTENT_TYPE_APPLICATION_JSON,
107                 transport);
108     }
109 
110     /**
111      * Returns list of entities. Only for admins.
112      *
113      * @return list of entities which exists on current seller or null if list of entities is not modified since version
114      * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException see:
115      *                                                                  {@link ForbiddenErrorType#INSUFFICIENT_ACCESS_RIGHTS}
116      */
117     public List<RestaurantListInfo> list() {
118         return list(null, null, null, null, null);
119     }
120 
121     /**
122      * Returns list of entities. Only for admins.
123      *
124      * @param firstResult when pagination starts or null
125      * @param maxResults  amount of items per page or null
126      * @param like        search string filter or null
127      * @param orderBy     field to order by, possible values are "identifier", "name", "vat"
128      * @param orderDesc   field to direction of ordering
129      * @return list of entities which exists on current seller or null if list of entities is not modified since version
130      * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException see:
131      *                                                                  {@link ForbiddenErrorType#INSUFFICIENT_ACCESS_RIGHTS}
132      */
133     public List<RestaurantListInfo> list(Integer firstResult,
134                                          Integer maxResults,
135                                          String like,
136                                          PublicSellerInfo.Fields orderBy,
137                                          Boolean orderDesc) {
138         HashMap<String, String> params = new HashMap<String, String>();
139         if (orderBy != null) {
140             params.put("orderBy", orderBy.name());
141         }
142         if (orderDesc != null) {
143             params.put("orderDesc", orderDesc.toString());
144         }
145         if (firstResult != null) {
146             params.put("firstResult", Integer.toString(firstResult));
147         }
148         if (maxResults != null) {
149             params.put("maxResults", Integer.toString(maxResults));
150         }
151         if (like != null) {
152             params.put("like", like);
153         }
154 
155         StringResponse response = requestString(
156                 "/restaurants",
157                 HttpMethod.GET,
158                 url,
159                 credentials.toAuthentication(),
160                 null,
161                 params,
162                 EMPTY_CONNECTION_WRITER,
163                 CONTENT_TYPE_APPLICATION_JSON,
164                 transport
165         );
166         return response == null ? null : (List<RestaurantListInfo>) RestaurantListInfo.fromJsonArrayToRestaurantListInfoes(response.getContent());
167     }
168 }