Search in sources :

Example 6 with EmptyResultDataAccessException

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

the class AttributesManagerImpl method setAttributeInDB.

private boolean setAttributeInDB(final PerunSession sess, final Attribute attribute, final String tableName, final Map<String, Object> params) throws InternalErrorException {
    // get two sorted lists for parameter names and values
    List<String> columnNames = new ArrayList<>();
    List<Object> columnValues = new ArrayList<>();
    for (Entry entry : params.entrySet()) {
        columnNames.add((String) entry.getKey());
        columnValues.add(entry.getValue());
    }
    try {
        // deleting the attibute if the given attribute value is null
        if (attribute.getValue() == null) {
            int numAffected = jdbc.update("delete from " + tableName + " where " + buildParameters(columnNames, "=?", " and "), columnValues.toArray());
            if (numAffected > 1) {
                throw new ConsistencyErrorException(String.format("Too much rows to delete (" + numAffected + " rows). SQL: delete from " + tableName + " where " + buildParameters(columnNames, "=%s", " and "), columnValues.toArray()));
            }
            return numAffected == 1;
        }
        // set the column name according to the size of the attribute
        boolean largeAttribute = isLargeAttribute(sess, attribute);
        String valueColName = (largeAttribute ? "attr_value_text" : "attr_value");
        // if the DB value is the same as parameter, return
        if (!largeAttribute) {
            try {
                Object value = BeansUtils.stringToAttributeValue(jdbc.queryForObject("select attr_value from " + tableName + " where " + buildParameters(columnNames, "=?", " and "), String.class, columnValues.toArray()), attribute.getType());
                if (attribute.getValue().equals(value)) {
                    return false;
                }
            } catch (EmptyResultDataAccessException ex) {
            //This is ok. Attribute will be stored later.
            }
        }
        int repetatCounter = 0;
        while (true) {
            // number of values of this attribute value in db
            int numOfAttributesInDb = jdbc.queryForInt("select count(attr_id) from " + tableName + " where " + buildParameters(columnNames, "=?", " and "), columnValues.toArray());
            switch(numOfAttributesInDb) {
                case 0:
                    {
                        // value doesn't exist -> insert
                        try {
                            return self.insertAttribute(sess, valueColName, attribute, tableName, columnNames, columnValues);
                        } catch (DataAccessException ex) {
                            // unsuccessful insert, do it again in while loop
                            if (++repetatCounter > MERGE_TRY_CNT) {
                                throw new InternalErrorException("SQL merger (or other UPSERT command) failed more than " + MERGE_TRY_CNT + " times.");
                            }
                            try {
                                //randomized sleep
                                Thread.sleep(Math.round(MERGE_RAND_SLEEP_MAX * Math.random()));
                            } catch (InterruptedException IGNORE) {
                            }
                        }
                        break;
                    }
                case 1:
                    {
                        // value exists -> update
                        try {
                            return self.updateAttribute(sess, valueColName, attribute, tableName, columnNames, columnValues);
                        } catch (DataAccessException ex) {
                            // unsuccessful insert, do it again in while loop
                            if (++repetatCounter > MERGE_TRY_CNT) {
                                throw new InternalErrorException("SQL merger (or other UPSERT command) failed more than " + MERGE_TRY_CNT + " times.");
                            }
                            try {
                                //randomized sleep
                                Thread.sleep(Math.round(MERGE_RAND_SLEEP_MAX * Math.random()));
                            } catch (InterruptedException IGNORE) {
                            }
                        }
                        break;
                    }
                default:
                    throw new ConsistencyErrorException(String.format("Attribute id " + attribute.getId() + " for " + tableName + " with parameters: " + buildParameters(columnNames, "=%s", " and ") + " is more than once in DB.", columnValues.toArray()));
            }
        }
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Entry(java.util.Map.Entry) ConsistencyErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) DataAccessException(org.springframework.dao.DataAccessException)

Example 7 with EmptyResultDataAccessException

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

the class AttributesManagerImpl method getAttributes.

public List<Attribute> getAttributes(PerunSession sess, Group group, List<String> attrNames) throws InternalErrorException {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("gId", group.getId());
    parameters.addValue("nSC", AttributesManager.NS_GROUP_ATTR_CORE);
    parameters.addValue("nSO", AttributesManager.NS_GROUP_ATTR_OPT);
    parameters.addValue("nSD", AttributesManager.NS_GROUP_ATTR_DEF);
    parameters.addValue("nSV", AttributesManager.NS_GROUP_ATTR_VIRT);
    parameters.addValue("attrNames", attrNames);
    try {
        return namedParameterJdbcTemplate.query("select " + getAttributeMappingSelectQuery("groupattr") + " from attr_names " + "left join group_attr_values groupattr on id=groupattr.attr_id and group_id=:gId " + "where namespace in ( :nSC,:nSO,:nSD,:nSV ) and attr_names.attr_name in ( :attrNames )", parameters, new AttributeRowMapper(sess, this, group));
    } catch (EmptyResultDataAccessException ex) {
        return new ArrayList<Attribute>();
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ConsistencyErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) Attribute(cz.metacentrum.perun.core.api.Attribute) RichAttribute(cz.metacentrum.perun.core.api.RichAttribute) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 8 with EmptyResultDataAccessException

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

the class AttributesManagerImpl method getAttributes.

public List<Attribute> getAttributes(PerunSession sess, User user, List<String> attrNames) throws InternalErrorException {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("uId", user.getId());
    parameters.addValue("nSC", AttributesManager.NS_USER_ATTR_CORE);
    parameters.addValue("nSO", AttributesManager.NS_USER_ATTR_OPT);
    parameters.addValue("nSD", AttributesManager.NS_USER_ATTR_DEF);
    parameters.addValue("nSV", AttributesManager.NS_USER_ATTR_VIRT);
    parameters.addValue("attrNames", attrNames);
    try {
        return namedParameterJdbcTemplate.query("select " + getAttributeMappingSelectQuery("usr") + " from attr_names " + "left join user_attr_values usr on id=usr.attr_id and user_id=:uId " + "where namespace in ( :nSC,:nSO,:nSD,:nSV ) and attr_names.attr_name in ( :attrNames )", parameters, new AttributeRowMapper(sess, this, user));
    } catch (EmptyResultDataAccessException ex) {
        return new ArrayList<Attribute>();
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ConsistencyErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) Attribute(cz.metacentrum.perun.core.api.Attribute) RichAttribute(cz.metacentrum.perun.core.api.RichAttribute) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 9 with EmptyResultDataAccessException

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

the class AttributesManagerImpl method getAttributes.

public List<Attribute> getAttributes(PerunSession sess, Member member, List<String> attrNames) throws InternalErrorException {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("mId", member.getId());
    parameters.addValue("nSC", AttributesManager.NS_MEMBER_ATTR_CORE);
    parameters.addValue("nSO", AttributesManager.NS_MEMBER_ATTR_OPT);
    parameters.addValue("nSD", AttributesManager.NS_MEMBER_ATTR_DEF);
    parameters.addValue("nSV", AttributesManager.NS_MEMBER_ATTR_VIRT);
    parameters.addValue("attrNames", attrNames);
    try {
        return namedParameterJdbcTemplate.query("select " + getAttributeMappingSelectQuery("mem") + " from attr_names " + "left join member_attr_values mem on id=mem.attr_id and member_id=:mId " + "where namespace in ( :nSC,:nSO,:nSD,:nSV ) and attr_names.attr_name in ( :attrNames )", parameters, new AttributeRowMapper(sess, this, member));
    } catch (EmptyResultDataAccessException ex) {
        return new ArrayList<Attribute>();
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ConsistencyErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) Attribute(cz.metacentrum.perun.core.api.Attribute) RichAttribute(cz.metacentrum.perun.core.api.RichAttribute) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 10 with EmptyResultDataAccessException

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

the class UsersManagerImpl method getUsersByIds.

public List<User> getUsersByIds(PerunSession sess, List<Integer> usersIds) throws InternalErrorException {
    // If usersIds is empty, we can immediatelly return empty results
    if (usersIds.size() == 0) {
        return new ArrayList<User>();
    }
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("ids", usersIds);
    try {
        return namedParameterJdbcTemplate.query("select " + userMappingSelectQuery + "  from users where users.id in ( :ids )", parameters, USER_MAPPER);
    } catch (EmptyResultDataAccessException ex) {
        return new ArrayList<User>();
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Aggregations

EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)40 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)20 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)17 Attribute (cz.metacentrum.perun.core.api.Attribute)10 ConsistencyErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException)10 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)10 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)9 ArrayList (java.util.ArrayList)9 User (cz.metacentrum.perun.core.api.User)6 Group (cz.metacentrum.perun.core.api.Group)5 HashSet (java.util.HashSet)5 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 RowMapper (org.springframework.jdbc.core.RowMapper)4 Member (cz.metacentrum.perun.core.api.Member)3 User (com.mapia.domain.User)2 ContactGroup (cz.metacentrum.perun.core.api.ContactGroup)2 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)2 PerunNotifObject (cz.metacentrum.perun.notif.entities.PerunNotifObject)2 PerunNotifRegex (cz.metacentrum.perun.notif.entities.PerunNotifRegex)2