View Javadoc
1   package com.srv4pos.server.api.seller;
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.exceptions.enums.UnprocessableEntityErrorType;
7   import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
8   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
9   import com.srv4pos.server.api.infrastructure.http.HttpHelper;
10  import com.srv4pos.server.api.infrastructure.http.Transport;
11  import com.srv4pos.server.api.user.UserInfo;
12  import com.srv4pos.server.api.user.UserRegistrationInfo;
13  import com.srv4pos.server.api.user.UserShortInfo;
14  import com.srv4pos.server.api.infrastructure.exceptions.enums.ForbiddenErrorType;
15  
16  import java.net.URL;
17  import java.util.Collection;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
22  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
23  import static java.lang.String.format;
24  
25  public class UserService {
26      private URL url;
27      private Credentials credentials;
28      private Transport transport;
29  
30      /**
31       * Constructs the service.
32       *
33       * @param url         of the server
34       * @param credentials to work with the server
35       * @param transport   to know the method to reach the server
36       */
37      public UserService(URL url, Credentials credentials, Transport transport) {
38          this.url = url;
39          this.credentials = credentials;
40          this.transport = transport;
41      }
42  
43      /**
44       * Modifies an user.
45       *
46       * @param id       identifies entity
47       * @param userInfo the entity content to put
48       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException            if user wasn't found
49       * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see:
50       *                                                                            {@link UnprocessableEntityErrorType#SOFTWARE_VENDOR_IS_MISSING}
51       * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException           see:
52       *                                                                            {@link ForbiddenErrorType#INSUFFICIENT_ACCESS_RIGHTS}
53       */
54      public void put(String id, UserInfo userInfo) {
55          requestVoid(format("users/%s", id),
56                  HttpMethod.PUT,
57                  url,
58                  credentials.toAuthentication(),
59                  null,
60                  null,
61                  new StringConnectionWriter(userInfo.toJson()),
62                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
63                  transport);
64      }
65  
66      /**
67       * Returns an user.
68       *
69       * @param id identifies entity
70       * @return the entity
71       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException  if user wasn't found
72       * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException see:
73       *                                                                  {@link ForbiddenErrorType#INSUFFICIENT_ACCESS_RIGHTS}
74       */
75      public UserInfo get(String id) {
76          return UserInfo.fromJsonToUserInfo(requestString(
77                  format("users/%s", id),
78                  HttpMethod.GET,
79                  url,
80                  credentials.toAuthentication(),
81                  null,
82                  null,
83                  EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
84                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
85                  transport).getContent());
86      }
87  
88      /**
89       * Creates an user.
90       *
91       * @param userRegistrationInfo the entity
92       * @return generated id of the entity
93       * * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException  if version mismatch
94       * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see:
95       *                                                                            {@link UnprocessableEntityErrorType#SOFTWARE_VENDOR_IS_MISSING}
96       * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException           see:
97       *                                                                            {@link ForbiddenErrorType#INSUFFICIENT_ACCESS_RIGHTS}
98       */
99      public String create(UserRegistrationInfo userRegistrationInfo) {
100         return IdentifierInfo.fromJsonToIdentifierInfo(requestString(
101                 "users",
102                 HttpMethod.POST,
103                 url,
104                 credentials.toAuthentication(),
105                 null,
106                 null,
107                 new StringConnectionWriter(userRegistrationInfo.toJson()),
108                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
109                 transport).getContent()).getIdentifier();
110     }
111 
112     /**
113      * Returns list of users.
114      *
115      * @param firstResult when pagination starts or null
116      * @param maxResults  amount of items per page or null
117      * @param like        search string filter or null
118      * @return list of entities
119      */
120     public Collection<UserShortInfo> listMinified(Integer firstResult,
121                                                   Integer maxResults,
122                                                   String like) {
123         Map<String, String> params = new HashMap<String, String>();
124         if (firstResult != null) {
125             params.put("firstResult", Integer.toString(firstResult));
126         }
127         if (maxResults != null) {
128             params.put("maxResults", Integer.toString(maxResults));
129         }
130         if (like != null) {
131             params.put("like", like);
132         }
133 
134         return UserShortInfo.fromJsonArrayToUserShortInfoes(requestString(
135                 "users-minified",
136                 HttpMethod.GET,
137                 url,
138                 credentials.toAuthentication(),
139                 null,
140                 params,
141                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
142                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
143                 transport).getContent());
144     }
145 
146     /**
147      * Returns list of users.
148      *
149      * @param firstResult when pagination starts or null
150      * @param maxResults  amount of items per page or null
151      * @param like        search string filter or null
152      * @return list of entities
153      */
154     public Collection<UserShortInfo> list(Integer firstResult,
155                                                   Integer maxResults,
156                                                   String like) {
157         Map<String, String> params = new HashMap<String, String>();
158         if (firstResult != null) {
159             params.put("firstResult", Integer.toString(firstResult));
160         }
161         if (maxResults != null) {
162             params.put("maxResults", Integer.toString(maxResults));
163         }
164         if (like != null) {
165             params.put("like", like);
166         }
167 
168         return UserShortInfo.fromJsonArrayToUserShortInfoes(requestString(
169                 "users",
170                 HttpMethod.GET,
171                 url,
172                 credentials.toAuthentication(),
173                 null,
174                 params,
175                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
176                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
177                 transport).getContent());
178     }
179 }