View Javadoc
1   package com.srv4pos.server.api.ejournal;
2   
3   import com.srv4pos.commons.io.InputStreamConnectionWriter;
4   import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter;
5   import com.srv4pos.server.api.infrastructure.HttpMethod;
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   import com.srv4pos.server.api.infrastructure.request.StreamResponse;
10  
11  import java.io.InputStream;
12  import java.net.URL;
13  import java.util.HashMap;
14  
15  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestStream;
16  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
17  import static java.lang.String.format;
18  
19  public class EJournalService {
20      private URL url;
21      private Credentials credentials;
22      private Transport transport;
23  
24      /**
25       * Constructs the service.
26       *
27       * @param url         of the server
28       * @param credentials to work with the server
29       * @param transport   to know the method to reach the server
30       */
31      public EJournalService(URL url, Credentials credentials, Transport transport) {
32          this.url = url;
33          this.credentials = credentials;
34          this.transport = transport;
35      }
36  
37      /**
38       * Appends passed ejournal csv lines of cash register.
39       *
40       * @param cashRegisterName name of cash register. Its lines should be appended
41       * @param stream           {@link InputStream} with csv lines
42       */
43      public void append(String cashRegisterName, InputStream stream) {
44          requestVoid(
45                  format("%s/e-journals/%s", credentials.getSellerId(), cashRegisterName),
46                  HttpMethod.POST,
47                  url,
48                  credentials.toAuthentication(),
49                  null,
50                  null,
51                  new InputStreamConnectionWriter(stream),
52                  HttpHelper.CONTENT_TYPE_CSV,
53                  transport);
54      }
55  
56      /**
57       * Returns stream with csv lines belonging to passed {@code cashRegisterName}.
58       *
59       * @param cashRegisterName name of cash register. Its lines should be retrieved from server
60       * @param orderDesc        ordering, {@code desc} by default
61       * @return
62       */
63      public InputStream get(String cashRegisterName, Boolean orderDesc) {
64          HashMap<String, String> params = new HashMap<String, String>();
65          if (orderDesc != null) {
66              params.put("orderDesc", orderDesc.toString());
67          }
68          final StreamResponse streamResponse = requestStream(
69                  format("%s/e-journals/%s", credentials.getSellerId(), cashRegisterName),
70                  HttpMethod.GET,
71                  url,
72                  credentials.toAuthentication(),
73                  null,
74                  params,
75                  EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
76                  HttpHelper.CONTENT_TYPE_CSV,
77                  transport
78          );
79          return streamResponse != null ? streamResponse.getContent() : null;
80      }
81  }