Search in sources :

Example 86 with EmptyResultDataAccessException

use of org.springframework.dao.EmptyResultDataAccessException in project perun by CESNET.

the class PerunNotifRegexDaoImpl method getPerunNotifRegexById.

@Override
public PerunNotifRegex getPerunNotifRegexById(int id) {
    logger.debug("Loading regex from db by id: {}", id);
    PerunNotifRegex regex = null;
    try {
        regex = this.getJdbcTemplate().queryForObject("SELECT * from pn_regex where id = ?", new Object[] { id }, PerunNotifRegex.PERUN_NOTIF_REGEX);
    } catch (EmptyResultDataAccessException ex) {
        logger.debug("Regex with id: {}, not found.", id);
        return null;
    }
    logger.debug("Regex with id: {}, loaded from db. Loading objects.", id);
    List<PerunNotifObject> objects = this.getJdbcTemplate().query("SELECT * from pn_object object JOIN pn_regex_object bind ON object.id = bind.object_id WHERE bind.regex_id = ?", new Object[] { regex.getId() }, PerunNotifObject.PERUN_NOTIF_OBJECT);
    regex.addObjects(objects);
    logger.debug("Objects loaded result: {}", regex);
    return regex;
}
Also used : PerunNotifRegex(cz.metacentrum.perun.notif.entities.PerunNotifRegex) PerunNotifObject(cz.metacentrum.perun.notif.entities.PerunNotifObject) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) PerunNotifObject(cz.metacentrum.perun.notif.entities.PerunNotifObject)

Example 87 with EmptyResultDataAccessException

use of org.springframework.dao.EmptyResultDataAccessException in project perun by CESNET.

the class UsersManagerImpl method findUsersByName.

@Override
public List<User> findUsersByName(PerunSession sess, String searchString) {
    if (searchString == null || searchString.isEmpty()) {
        return new ArrayList<>();
    }
    // Convert to lowercase
    searchString = searchString.toLowerCase();
    log.trace("Search string '{}' converted into lower-cased", searchString);
    // remove spaces from the search string
    searchString = searchString.replaceAll(" ", "");
    log.debug("Searching users by name using searchString '{}'", searchString);
    // the searchString is already lower cased
    try {
        return jdbc.query("select " + userMappingSelectQuery + "  from users " + "where strpos(lower(" + Compatibility.convertToAscii("COALESCE(users.first_name,'') || COALESCE(users.middle_name,'') || COALESCE(users.last_name,'')") + ")," + Compatibility.convertToAscii("?") + ") > 0", USER_MAPPER, searchString);
    } catch (EmptyResultDataAccessException e) {
        return new ArrayList<>();
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 88 with EmptyResultDataAccessException

use of org.springframework.dao.EmptyResultDataAccessException in project perun by CESNET.

the class UsersManagerImpl method isLoginReserved.

@Override
public boolean isLoginReserved(PerunSession sess, String namespace, String login, boolean ignoreCase) {
    Utils.notNull(namespace, "loginNamespace");
    Utils.notNull(login, "userLogin");
    try {
        int numberOfExistences = 0;
        if (ignoreCase) {
            numberOfExistences = jdbc.queryForInt("select count(1) from application_reserved_logins where namespace=? and lower(login)=lower(?)", namespace, login);
        } else {
            numberOfExistences = jdbc.queryForInt("select count(1) from application_reserved_logins where namespace=? and login=?", namespace, login);
        }
        if (numberOfExistences == 1) {
            return true;
        } else if (numberOfExistences > 1) {
            throw new ConsistencyErrorException("Login " + login + " in namespace " + namespace + " is reserved more than once.");
        }
        return false;
    } catch (EmptyResultDataAccessException ex) {
        return false;
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 89 with EmptyResultDataAccessException

use of org.springframework.dao.EmptyResultDataAccessException in project perun by CESNET.

the class UsersManagerImpl method getUsersByAttributeValue.

@Override
public List<User> getUsersByAttributeValue(PerunSession sess, AttributeDefinition attributeDefinition, String attributeValue) {
    String value = "";
    String operator = "=";
    if (attributeDefinition.getType().equals(String.class.getName())) {
        value = attributeValue.trim();
        operator = "=";
    } else if (attributeDefinition.getType().equals(Integer.class.getName())) {
        value = attributeValue.trim();
        operator = "=";
    } else if (attributeDefinition.getType().equals(Boolean.class.getName())) {
        value = attributeValue.trim();
        operator = "=";
    } else if (attributeDefinition.getType().equals(ArrayList.class.getName())) {
        value = "%" + attributeValue.trim() + "%";
        operator = "like";
    } else if (attributeDefinition.getType().equals(LinkedHashMap.class.getName())) {
        value = "%" + attributeValue.trim() + "%";
        operator = "like";
    }
    String query = "select " + userMappingSelectQuery + " from users, user_attr_values where " + " user_attr_values.attr_value " + operator + " :value and users.id=user_attr_values.user_id and user_attr_values.attr_id=:attr_id";
    MapSqlParameterSource namedParams = new MapSqlParameterSource();
    namedParams.addValue("value", value);
    namedParams.addValue("attr_id", attributeDefinition.getId());
    try {
        return namedParameterJdbcTemplate.query(query, namedParams, USER_MAPPER);
    } catch (EmptyResultDataAccessException e) {
        return new ArrayList<>();
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) LinkedHashMap(java.util.LinkedHashMap)

Example 90 with EmptyResultDataAccessException

use of org.springframework.dao.EmptyResultDataAccessException in project perun by CESNET.

the class VosManagerImpl method getAdmins.

@Override
public List<User> getAdmins(PerunSession sess, Vo vo, String role) {
    try {
        // direct admins
        Set<User> setOfAdmins = new HashSet<>(jdbc.query("select " + UsersManagerImpl.userMappingSelectQuery + " from authz join users on authz.user_id=users.id " + "where authz.vo_id=? and authz.role_id=(select id from roles where name=?)", UsersManagerImpl.USER_MAPPER, vo.getId(), role.toLowerCase()));
        // admins through a group
        List<Group> listOfGroupAdmins = getAdminGroups(sess, vo, role);
        for (Group group : listOfGroupAdmins) {
            setOfAdmins.addAll(jdbc.query("select " + UsersManagerImpl.userMappingSelectQuery + " from users join members on users.id=members.user_id " + "join groups_members on groups_members.member_id=members.id where groups_members.group_id=?", UsersManagerImpl.USER_MAPPER, group.getId()));
        }
        return new ArrayList(setOfAdmins);
    } catch (EmptyResultDataAccessException ex) {
        return new ArrayList<>();
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) User(cz.metacentrum.perun.core.api.User) ArrayList(java.util.ArrayList) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) HashSet(java.util.HashSet)

Aggregations

EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)99 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)46 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)31 ArrayList (java.util.ArrayList)19 HashSet (java.util.HashSet)9 Transactional (org.springframework.transaction.annotation.Transactional)9 Group (cz.metacentrum.perun.core.api.Group)8 User (cz.metacentrum.perun.core.api.User)8 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)7 SQLException (java.sql.SQLException)7 Test (org.junit.Test)6 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)6 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Service (cz.metacentrum.perun.core.api.Service)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)3