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);
}
}
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);
}
}
use of org.springframework.dao.EmptyResultDataAccessException in project perun by CESNET.
the class VosManagerImpl method getAdmins.
public List<User> getAdmins(PerunSession sess, Vo vo, Role role) throws InternalErrorException {
try {
Set<User> setOfAdmins = new HashSet<User>();
// direct admins
setOfAdmins.addAll(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.getRoleName()));
// 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<User>();
} catch (RuntimeException ex) {
throw new InternalErrorException(ex);
}
}
use of org.springframework.dao.EmptyResultDataAccessException in project perun by CESNET.
the class GroupsManagerImpl method getGroupsByIds.
public List<Group> getGroupsByIds(PerunSession sess, List<Integer> groupsIds) throws InternalErrorException {
// If groupsIds are empty, we can immediately return empty result
if (groupsIds.size() == 0) {
return new ArrayList<Group>();
}
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("ids", groupsIds);
try {
return this.namedParameterJdbcTemplate.query("select " + groupMappingSelectQuery + " from groups where groups.id in ( :ids )", parameters, GROUP_MAPPER);
} catch (EmptyResultDataAccessException ex) {
return new ArrayList<Group>();
} catch (RuntimeException ex) {
throw new InternalErrorException(ex);
}
}
use of org.springframework.dao.EmptyResultDataAccessException in project perun by CESNET.
the class PropagationStatsReaderImpl method getFacilityServicesState.
@Override
public List<ServiceState> getFacilityServicesState(PerunSession sess, Facility facility) throws ServiceNotExistsException, InternalErrorException, PrivilegeException {
if (!AuthzResolver.isAuthorized(sess, Role.FACILITYADMIN, facility)) {
throw new PrivilegeException("getFacilityServicesState");
}
Map<Service, ServiceState> serviceStates = new HashMap<Service, ServiceState>();
// fill states for all services which are currently on facility
for (Service service : perun.getServicesManagerBl().getAssignedServices(sess, facility)) {
serviceStates.put(service, new ServiceState(service, facility));
try {
// fill global allowance state based on existing exec services
List<ExecService> execs = getGeneralServiceManager().listExecServices(sess, service.getId());
for (ExecService ex : execs) {
if (!ex.isEnabled())
serviceStates.get(service).setBlockedGlobally(true);
// fill facility allowance state based on existing exec services
if (getGeneralServiceManager().isExecServiceDeniedOnFacility(ex, facility))
serviceStates.get(service).setBlockedOnFacility(true);
}
} catch (EmptyResultDataAccessException ex) {
// service has no exec services -> blocked globally
serviceStates.get(service).setBlockedGlobally(true);
}
// service has destination on facility
serviceStates.get(service).setHasDestinations(!perun.getServicesManagerBl().getDestinations(sess, service, facility).isEmpty());
}
// fill states for all tasks on facility
List<Task> tasks = taskDao.listAllTasksForFacility(facility.getId());
for (Task task : tasks) {
Service taskService = task.getExecService().getService();
ServiceState serviceState = serviceStates.get(taskService);
if (serviceState == null) {
serviceState = new ServiceState(taskService, facility);
serviceStates.put(taskService, serviceState);
// fill destinations if service was not assigned
serviceStates.get(taskService).setHasDestinations(!perun.getServicesManagerBl().getDestinations(sess, taskService, facility).isEmpty());
}
// fill service state
if (ExecService.ExecServiceType.GENERATE.equals(task.getExecService().getExecServiceType())) {
serviceState.setGenTask(task);
} else {
serviceState.setSendTask(task);
}
if (!task.getExecService().isEnabled()) {
serviceStates.get(taskService).setBlockedGlobally(true);
}
if (getGeneralServiceManager().isExecServiceDeniedOnFacility(task.getExecService(), facility))
serviceStates.get(taskService).setBlockedOnFacility(true);
}
return new ArrayList<ServiceState>(serviceStates.values());
}
Aggregations