View Javadoc
1   package com.srv4pos.server.api.order;
2   
3   import com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException;
4   import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter;
5   import com.srv4pos.server.api.infrastructure.HttpMethod;
6   import com.srv4pos.server.api.infrastructure.IdentifierInfo;
7   import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
8   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
9   import com.srv4pos.server.api.infrastructure.exceptions.enums.UnprocessableEntityErrorType;
10  import com.srv4pos.server.api.infrastructure.http.HttpHelper;
11  import com.srv4pos.server.api.infrastructure.http.Transport;
12  import com.srv4pos.server.api.infrastructure.request.StringResponse;
13  
14  import java.net.URL;
15  import java.util.HashMap;
16  import java.util.List;
17  import java.util.Map;
18  
19  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
20  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
21  import static java.lang.String.format;
22  
23  /**
24   * Sending order.
25   * User: Kirill
26   * Date: 25.07.13 11:33
27   */
28  public class OrderService {
29      private URL url;
30      private Credentials credentials;
31      private Transport transport;
32  
33      /**
34       * Constructs the service.
35       *
36       * @param url         of the server
37       * @param credentials to work with the server
38       * @param transport   to know the method to reach the server
39       */
40      public OrderService(URL url, Credentials credentials, Transport transport) {
41          this.url = url;
42          this.credentials = credentials;
43          this.transport = transport;
44      }
45  
46      /**
47       * Returns list of item changes between two versions. Since we don't have delete and edit methods, it
48       * returns only newly created orders
49       *
50       * @param versionFrom version from included
51       * @param versionTo   version to included
52       * @return list of items to be added, edited or deleted (actually, only created)
53       * @see OrderServiceSample
54       */
55      public List<OrderInfo> listDiff(int versionFrom, int versionTo) {
56          StringResponse response = requestString(String.format("%s/orders-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo), HttpMethod.GET, url,
57                  credentials.toAuthentication(), null, null, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER, HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
58          return (List<OrderInfo>) OrderInfo.fromJsonArrayToOrderInfoes(response.getContent());
59      }
60  
61      /**
62       * Sends an order.
63       *
64       * @param version   expected latest version of seller
65       * @param orderInfo information about order
66       * @return generated identifier
67       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException          if seller is not found
68       * @throws com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException if product for given product identifier is not found
69       * @see OrderServiceSample
70       */
71      public IdentifierInfo create(Integer version, OrderInfo orderInfo) {
72          return IdentifierInfo.fromJsonToIdentifierInfo(
73                  requestString(format("%s/orders", credentials.getSellerId()),
74                          HttpMethod.POST,
75                          url,
76                          credentials.toAuthentication(),
77                          HttpHelper.integerToETag(version),
78                          null,
79                          new StringConnectionWriter(orderInfo.toJson()),
80                          HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
81                          transport).getContent());
82      }
83  
84      public List<OrderAdvancedInfo> listAdvanced(Integer firstResult,
85                                                  Integer maxResults,
86                                                  OrderInfo.Fields orderBy,
87                                                  Boolean orderDesc,
88                                                  String like,
89                                                  OrderPaymentStatus status,
90                                                  Integer version) {
91          Map<String, String> params = new HashMap<String, String>();
92          if (firstResult != null) {
93              params.put("firstResult", Integer.toString(firstResult));
94          }
95          if (maxResults != null) {
96              params.put("maxResults", Integer.toString(maxResults));
97          }
98          if (orderBy != null) {
99              params.put("orderBy", orderBy.name());
100         }
101         if (orderDesc != null) {
102             params.put("orderDesc", orderDesc.toString());
103         }
104         if (like != null) {
105             params.put("like", like);
106         }
107         if (status != null) {
108             params.put("status", status.name());
109         }
110 
111         final StringResponse response = requestString(
112                 format("%s/orders-advanced", credentials.getSellerId()),
113                 HttpMethod.GET,
114                 url,
115                 credentials.toAuthentication(),
116                 HttpHelper.integerToETag(version),
117                 params,
118                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
119                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
120                 transport);
121         return response == null ?
122                 null
123                 : (List<OrderAdvancedInfo>) OrderAdvancedInfo.fromJsonArrayToOrderAdvancedInfoes(response.getContent());
124     }
125 
126     public OrderAdvancedInfo getAdvanced(String identifier, Integer version) {
127         return OrderAdvancedInfo.fromJsonToOrderAdvancedInfo(requestString(
128                 format("%s/orders-advanced/%s", credentials.getSellerId(), identifier),
129                 HttpMethod.GET,
130                 url,
131                 credentials.toAuthentication(),
132                 HttpHelper.integerToETag(version),
133                 null,
134                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
135                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
136                 transport).getContent());
137     }
138 
139     public List<OrderInfo> list(Integer firstResult,
140                                 Integer maxResults,
141                                 OrderInfo.Fields orderBy,
142                                 Boolean orderDesc,
143                                 String like,
144                                 OrderPaymentStatus status,
145                                 Integer version) {
146         Map<String, String> params = new HashMap<String, String>();
147         if (firstResult != null) {
148             params.put("firstResult", Integer.toString(firstResult));
149         }
150         if (maxResults != null) {
151             params.put("maxResults", Integer.toString(maxResults));
152         }
153         if (orderBy != null) {
154             params.put("orderBy", orderBy.name());
155         }
156         if (orderDesc != null) {
157             params.put("orderDesc", orderDesc.toString());
158         }
159         if (like != null) {
160             params.put("like", like);
161         }
162         if (status != null) {
163             params.put("status", status.name());
164         }
165 
166         final StringResponse response = requestString(
167                 format("%s/orders", credentials.getSellerId()),
168                 HttpMethod.GET,
169                 url,
170                 credentials.toAuthentication(),
171                 HttpHelper.integerToETag(version),
172                 params,
173                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
174                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
175                 transport);
176         return response == null ?
177                 null
178                 : (List<OrderInfo>) OrderInfo.fromJsonArrayToOrderInfoes(response.getContent());
179     }
180 
181     public OrderInfo get(String identifier, Integer version) {
182         return OrderInfo.fromJsonToOrderInfo(requestString(
183                 format("%s/orders/%s", credentials.getSellerId(), identifier),
184                 HttpMethod.GET,
185                 url,
186                 credentials.toAuthentication(),
187                 HttpHelper.integerToETag(version),
188                 null,
189                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
190                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
191                 transport).getContent());
192     }
193 
194     /**
195      * Change an order. Provides changes of order payment status.
196      *
197      * @param identifier identifies entity
198      * @param orderInfo  data to put
199      * @param version    expected latest version of seller
200      * @throws UnprocessableEntityJsonException see: {@link UnprocessableEntityErrorType#PROHIBITED_ORDER_STATUS_CHANGE}
201      */
202     public void putOrder(String identifier, OrderInfo orderInfo, Integer version) throws UnprocessableEntityJsonException {
203         requestVoid(
204                 format("%s/orders/%s", credentials.getSellerId(), identifier),
205                 HttpMethod.PUT,
206                 url,
207                 credentials.toAuthentication(),
208                 HttpHelper.integerToETag(version),
209                 null,
210                 new StringConnectionWriter(orderInfo.toJson()),
211                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
212                 transport
213         );
214     }
215 
216     public void delete(Integer version, String identifier) {
217         requestVoid(
218                 format("%s/orders/%s", credentials.getSellerId(), identifier),
219                 HttpMethod.DELETE,
220                 url,
221                 credentials.toAuthentication(),
222                 HttpHelper.integerToETag(version),
223                 null,
224                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
225                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
226                 transport
227         );
228     }
229 }