View Javadoc
1   package com.srv4pos.server.api.posplus;
2   
3   import com.srv4pos.server.api.infrastructure.HttpMethod;
4   import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
5   import com.srv4pos.server.api.infrastructure.exceptions.enums.InconsistentDataErrorType;
6   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
7   import com.srv4pos.server.api.infrastructure.http.HttpHelper;
8   import com.srv4pos.server.api.infrastructure.http.Transport;
9   
10  import java.net.URL;
11  
12  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
13  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
14  import static java.lang.String.format;
15  
16  /**
17   * For sending data to remote control unit.
18   * <p>User: Kirill, Date: 08.03.14 17:12</p>
19   */
20  public class PosPlusService {
21      private URL url;
22      private Credentials credentials;
23      private Transport transport;
24  
25      /**
26       * Constructs the service.
27       *
28       * @param url         of the server
29       * @param credentials to work with the server
30       * @param transport   to know the method to reach the server
31       */
32      public PosPlusService(URL url, Credentials credentials, Transport transport) {
33          this.url = url;
34          this.credentials = credentials;
35          this.transport = transport;
36      }
37  
38  /*
39      public Collection<PosPlusInfo> posPluses() {
40          return PosPlusInfo.fromJsonArrayToPosPlusInfoes(
41                  request(
42                          "pospluses",
43                          HttpMethod.GET,
44                          url,
45                          credentials,
46                          null,
47                          null,
48                          EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
49                          ServiceHelper.CONTENT_TYPE_APPLICATION_JSON,
50                          transport).toText());
51      }
52  
53      public PosPlusInfo updateAndGetPosPlusInfo(String id) {
54          return PosPlusInfo.fromJsonToPosPlusInfo(
55                  request(
56                          String.format("posplus/%s", id),
57                          HttpMethod.GET,
58                          url,
59                          credentials,
60                          null,
61                          null,
62                          EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
63                          ServiceHelper.CONTENT_TYPE_APPLICATION_JSON,
64                          transport).toText());
65      }
66  */
67  
68      /**
69       * Sends data to control unit.
70       *
71       * @param kdInfo data to send
72       * @param sellerId seller identifier
73       * @param cashRegisterName cash register name
74       *
75       * @return result of sending
76       * @throws com.srv4pos.server.api.exceptions.ControlUnitReceiptJsonException       when Control Unit decides something is
77       *                                                                                 wrong according to it's internal business logic
78       * @throws com.srv4pos.server.api.exceptions.InconsistentDataJsonException         see:
79       *                                                                                 {@link InconsistentDataErrorType#WORKING_WITH_CU_IS_NOT_SUPPORTED_BY_APP}
80       * @throws com.srv4pos.server.api.exceptions.ControlUnitUnavailableJsonException   when server is unable to reach the control
81       *                                                                                 unit device (for instance it's ejected from the server)
82       * @throws com.srv4pos.server.api.exceptions.ControlUnitCommunicationJsonException when error has happened during transmission data to the control unit
83       *                                                                                 (for instance, it was ejected from the server during sending the data)
84       *                                                                                 workable yet
85       */
86      public KdReplyInfo kd(KdInfo kdInfo, String sellerId, String cashRegisterName) {
87          return KdReplyInfo.fromJsonToKdReplyInfo(
88                  requestString(
89                          format("%s/registration/%s/kd", sellerId, cashRegisterName),
90                          HttpMethod.POST,
91                          url,
92                          credentials.toAuthentication(),
93                          null,
94                          null,
95                          new StringConnectionWriter(kdInfo.toJson()),
96                          HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
97                          transport).getContent());
98      }
99  
100     /**
101      * Sends data to control unit.
102      *
103      * @param kdInfo data to send
104      * @param sellerId seller identifier
105      * @param cashRegisterName cash register name
106      *
107      * @return result of sending
108      * @throws com.srv4pos.server.api.exceptions.ControlUnitReceiptJsonException       when Control Unit decides something is
109      *                                                                                 wrong according to it's internal business logic
110      * @throws com.srv4pos.server.api.exceptions.InconsistentDataJsonException         see:
111      *                                                                                 {@link InconsistentDataErrorType#WORKING_WITH_CU_IS_NOT_SUPPORTED_BY_APP}
112      * @throws com.srv4pos.server.api.exceptions.ControlUnitUnavailableJsonException   when server is unable to reach the control
113      *                                                                                 unit device (for instance it's ejected from the server)
114      * @throws com.srv4pos.server.api.exceptions.ControlUnitCommunicationJsonException when error has happened during transmission data to the control unit
115      *                                                                                 (for instance, it was ejected from the server during sending the data)
116      *                                                                                 workable yet
117      */
118     public KdReplyInfo kdForSV(SVKdInfo kdInfo, String sellerId, String cashRegisterName) {
119         return KdReplyInfo.fromJsonToKdReplyInfo(
120                 requestString(
121                         format("%s/registration/%s/kd", sellerId, cashRegisterName),
122                         HttpMethod.POST,
123                         url,
124                         credentials.toAuthentication(),
125                         null,
126                         null,
127                         new StringConnectionWriter(kdInfo.toJson()),
128                         HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
129                         transport).getContent());
130     }
131 
132     /**
133      * The same as {@link #kd(KdInfo, String, String)}, but also has param logId for load testing.
134      * For logging details, see com.srv4pos.server.core.web.infrastructure.MethodProfiler
135      * and com.srv4pos.server.core.web.infrastructure.ServletTimeMeasurementFilter.
136      *
137      * @param kdInfo data to send
138      * @param logId  the identifier of log
139      * @return result of sending
140      */
141     public KdReplyInfo kd(KdInfo kdInfo, String sellerId, String cashRegisterName, String logId) {
142         return KdReplyInfo.fromJsonToKdReplyInfo(
143                 requestString(
144                         format("%s/registration/%s/kd", sellerId, cashRegisterName),
145                         HttpMethod.POST,
146                         url,
147                         credentials.toAuthentication(),
148                         null,
149                         null,
150                         new StringConnectionWriter(kdInfo.toJson()),
151                         HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
152                         transport).getContent());
153     }
154 
155     /**
156      * Creates new entity.
157      *
158      * @param controlUnitEndpointInfo contains information about new control unit endpoint
159      */
160     public void create(ControlUnitEndpointInfo controlUnitEndpointInfo) {
161         requestVoid("control-unit-endpoints",
162                 HttpMethod.POST,
163                 url,
164                 credentials.toAuthentication(),
165                 null,
166                 null,
167                 new StringConnectionWriter(controlUnitEndpointInfo.toJson()),
168                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
169                 transport);
170     }
171 }