Search in sources :

Example 1 with UserInfo

use of org.folio.rest.jaxrs.model.UserInfo in project mod-source-record-manager by folio-org.

the class JobExecutionServiceImpl method lookupUser.

/**
 * Finds user by user id and returns UserInfo
 *
 * @param userId user id
 * @param params Okapi connection params
 * @return Future with found UserInfo
 */
private Future<UserInfo> lookupUser(String userId, OkapiConnectionParams params) {
    Promise<UserInfo> promise = Promise.promise();
    RestUtil.doRequest(params, GET_USER_URL + userId, HttpMethod.GET, null).onComplete(getUserResult -> {
        if (RestUtil.validateAsyncResult(getUserResult, promise)) {
            JsonObject response = getUserResult.result().getJson();
            if (!response.containsKey("totalRecords") || !response.containsKey("users")) {
                promise.fail("Error, missing field(s) 'totalRecords' and/or 'users' in user response object");
            } else {
                int recordCount = response.getInteger("totalRecords");
                if (recordCount > 1) {
                    String errorMessage = "There are more then one user by requested user id : " + userId;
                    LOGGER.error(errorMessage);
                    promise.fail(errorMessage);
                } else if (recordCount == 0) {
                    String errorMessage = "No user found by user id :" + userId;
                    LOGGER.error(errorMessage);
                    promise.fail(errorMessage);
                } else {
                    JsonObject jsonUser = response.getJsonArray("users").getJsonObject(0);
                    JsonObject userPersonalInfo = jsonUser.getJsonObject("personal");
                    String userName = jsonUser.getString("username");
                    UserInfo userInfo = new UserInfo().withFirstName(Objects.isNull(userPersonalInfo) ? userName : userPersonalInfo.getString("firstName")).withLastName(Objects.isNull(userPersonalInfo) ? DEFAULT_LASTNAME : userPersonalInfo.getString("lastName")).withUserName(userName);
                    promise.complete(userInfo);
                }
            }
        }
    });
    return promise.future();
}
Also used : JsonObject(io.vertx.core.json.JsonObject) UserInfo(org.folio.rest.jaxrs.model.UserInfo)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)1 UserInfo (org.folio.rest.jaxrs.model.UserInfo)1