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