View Javadoc
1   package com.srv4pos.server.api.precept;
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  import java.util.Map;
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   * Sending precepts.
23   */
24  public class PreceptService {
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 PreceptService(URL url, Credentials credentials, Transport transport) {
37          this.url = url;
38          this.credentials = credentials;
39          this.transport = transport;
40      }
41  
42      /**
43       * Sends precept.
44       *
45       * @param country      where seller is located
46       * @param corporateId  of the seller
47       * @param preceptInfo  information about precept
48       * @return generated unique identifier
49       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException          if seller is not found
50       * @throws com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException if product for given product identifier is not found
51       * @throws com.srv4pos.server.api.exceptions.NonUniqueJsonException         if identifier is not unique within a seller
52       */
53      public String create(String country, String corporateId, PreceptInfo preceptInfo) {
54          return IdentifierInfo.fromJsonToIdentifierInfo(
55                  requestString(format("%s%s/precepts", country, corporateId), HttpMethod.POST, url,
56                          credentials == null ? null : credentials.toAuthentication(),
57                          null, null, new StringConnectionWriter(preceptInfo.toJson()),
58                          HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport).getContent()).getIdentifier();
59  
60      }
61  
62      /**
63       * Get list of precepts.
64       *
65       * @param firstResult when pagination starts or null
66       * @param maxResults amount of items per page or null
67       * @param like search string filter or null
68       * @return list with precepts
69       */
70      public List<PreceptInfo> list(Integer firstResult,
71                                  Integer maxResults,
72                                  String like) {
73          Map<String, String> params = new HashMap<String, String>();
74          if (firstResult != null) {
75              params.put("firstResult", Integer.toString(firstResult));
76          }
77          if (maxResults != null) {
78              params.put("maxResults", Integer.toString(maxResults));
79          }
80          if (like != null) {
81              params.put("like", like);
82          }
83  
84          return (List<PreceptInfo>) PreceptInfo.fromJsonArrayToPreceptInfoes(requestString(
85                  format("%s/precepts", credentials.getSellerId()),
86                  HttpMethod.GET,
87                  url,
88                  credentials.toAuthentication(),
89                  null,
90                  params,
91                  EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
92                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
93                  transport).getContent());
94      }
95  
96      /**
97       * Get precept with specified identifier.
98       *
99       * @param identifier unique identifier of precept
100      * @return precept
101      */
102     public PreceptInfo get(String identifier) {
103         return PreceptInfo.fromJsonToPreceptInfo(requestString(
104                 format("%s/precepts/%s", credentials.getSellerId(), identifier),
105                 HttpMethod.GET,
106                 url,
107                 credentials.toAuthentication(),
108                 null,
109                 null,
110                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
111                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
112                 transport).getContent());
113     }
114 
115     /**
116      * Put precept.
117      *
118      * @param identifier  unique identifier of precept
119      * @param preceptInfo precept info
120      */
121     public void put(String identifier, PreceptInfo preceptInfo) {
122         requestVoid(
123                 format("%s/precepts/%s", credentials.getSellerId(), identifier),
124                 HttpMethod.PUT,
125                 url,
126                 credentials.toAuthentication(),
127                 null,
128                 null,
129                 new StringConnectionWriter(preceptInfo.toJson()),
130                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
131                 transport
132         );
133     }
134 
135     /**
136      * Returns list of item changes between two versions.
137      *
138      * @param versionFrom version from included
139      * @param versionTo version to included
140      * @return list of items to be added, edited or deleted
141      * @see PreceptServiceSample
142      */
143     public List<PreceptInfo> listDiff(int versionFrom, int versionTo) {
144         StringResponse response = requestString(String.format("%s/precepts-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo), HttpMethod.GET, url,
145                 credentials.toAuthentication(), null, null, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER, HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
146         return (List<PreceptInfo>) PreceptInfo.fromJsonArrayToPreceptInfoes(response.getContent());
147     }
148 }