use of cz.metacentrum.perun.core.api.ContactGroup in project perun by CESNET.
the class FacilitiesManagerEntry method checkFacilityContactEntitiesExists.
/**
* Check existence of every entity in contactGroup
*
* @param sess
* @param contactGroup
* @throws FacilityNotExistsException
* @throws UserNotExistsException
* @throws OwnerNotExistsException
* @throws GroupNotExistsException
* @throws InternalErrorException
*/
private void checkFacilityContactEntitiesExists(PerunSession sess, ContactGroup contactGroup) throws FacilityNotExistsException, UserNotExistsException, OwnerNotExistsException, GroupNotExistsException, InternalErrorException {
Utils.notNull(contactGroup, "contactGroup");
Utils.notNull(contactGroup.getFacility(), "facility");
Utils.notNull(contactGroup.getName(), "name");
this.getFacilitiesManagerBl().checkFacilityExists(sess, contactGroup.getFacility());
if (contactGroup.getUsers() != null) {
for (RichUser user : contactGroup.getUsers()) {
getPerunBl().getUsersManagerBl().checkUserExists(sess, user);
}
}
if (contactGroup.getGroups() != null) {
for (Group group : contactGroup.getGroups()) {
getPerunBl().getGroupsManagerBl().checkGroupExists(sess, group);
}
}
if (contactGroup.getOwners() != null) {
for (Owner owner : contactGroup.getOwners()) {
getPerunBl().getOwnersManagerBl().checkOwnerExists(sess, owner);
}
}
}
use of cz.metacentrum.perun.core.api.ContactGroup in project perun by CESNET.
the class FacilitiesManagerEntry method addFacilityContacts.
@Override
public void addFacilityContacts(PerunSession sess, List<ContactGroup> contactGroupsToAdd) throws InternalErrorException, PrivilegeException, FacilityNotExistsException, UserNotExistsException, OwnerNotExistsException, GroupNotExistsException {
Utils.checkPerunSession(sess);
this.checkFacilityContactsEntitiesExist(sess, contactGroupsToAdd);
Iterator<ContactGroup> iter = contactGroupsToAdd.iterator();
while (iter.hasNext()) {
ContactGroup contactGroupToAdd = iter.next();
if (!AuthzResolver.isAuthorized(sess, Role.FACILITYADMIN, contactGroupToAdd.getFacility())) {
iter.remove();
continue;
}
}
if (!contactGroupsToAdd.isEmpty()) {
this.facilitiesManagerBl.addFacilityContacts(sess, contactGroupsToAdd);
}
}
use of cz.metacentrum.perun.core.api.ContactGroup in project perun by CESNET.
the class FacilitiesManagerImpl method getFacilityContactGroup.
@Override
public ContactGroup getFacilityContactGroup(PerunSession sess, Facility facility, String name) throws InternalErrorException, FacilityContactNotExistsException {
try {
List<ContactGroup> contactGroups = jdbc.query("select " + facilityContactsMappingSelectQueryWithAllEntities + " from facility_contacts " + "left join facilities on facilities.id=facility_contacts.facility_id " + "left join owners on owners.id=facility_contacts.owner_id " + "left join users on users.id=facility_contacts.user_id " + "left join groups on groups.id=facility_contacts.group_id " + "where facility_contacts.facility_id=? and facility_contacts.name=?", FACILITY_CONTACT_MAPPER, facility.getId(), name);
contactGroups = mergeContactGroups(contactGroups);
if (contactGroups.size() == 1) {
return contactGroups.get(0);
} else {
throw new InternalErrorException("Merging group contacts for facility " + facility + " and contact name " + name + " failed, more than 1 object returned " + name);
}
} catch (EmptyResultDataAccessException ex) {
throw new FacilityContactNotExistsException(facility, name);
} catch (RuntimeException ex) {
throw new InternalErrorException(ex);
}
}
use of cz.metacentrum.perun.core.api.ContactGroup in project perun by CESNET.
the class FacilitiesManagerImpl method addFacilityContact.
// FACILITY CONTACTS METHODS
@Override
public ContactGroup addFacilityContact(PerunSession sess, Facility facility, String name, User user) throws InternalErrorException {
Utils.notNull(facility, "facility");
Utils.notNull(user, "user");
if (name == null || name.isEmpty()) {
throw new InternalErrorException("ContactGroupName can't be null or empty.");
}
ContactGroup contactGroup;
try {
jdbc.update("insert into facility_contacts(facility_id, name, user_id) " + "values (?,?,?)", facility.getId(), name, user.getId());
RichUser ru = new RichUser(user, null);
List<RichUser> rulist = new ArrayList<>();
rulist.add(ru);
contactGroup = new ContactGroup(name, facility, new ArrayList<Group>(), new ArrayList<Owner>(), rulist);
log.info("Facility contact {} created", contactGroup);
} catch (RuntimeException ex) {
throw new InternalErrorException(ex);
}
return contactGroup;
}
use of cz.metacentrum.perun.core.api.ContactGroup in project perun by CESNET.
the class FacilitiesManagerImpl method mergeContactGroups.
/**
* Take list of contact groups and merged them.
*
* Merge means:
* If two groups are from the same facility with the same contactName join
* them to the one with groups, owners and users from both.
*
* @param notMergedContactGroups list of groups to merge
* @return list of merged contact groups
*/
private List<ContactGroup> mergeContactGroups(List<ContactGroup> notMergedContactGroups) {
List<ContactGroup> mergedContactGroups = new ArrayList<>();
while (!notMergedContactGroups.isEmpty()) {
ContactGroup contactGroup = new ContactGroup();
Iterator<ContactGroup> contactGroupIter = notMergedContactGroups.iterator();
boolean first = true;
while (contactGroupIter.hasNext()) {
if (first) {
contactGroup = contactGroupIter.next();
if (contactGroup.getGroups() == null)
contactGroup.setGroups(new ArrayList<Group>());
if (contactGroup.getOwners() == null)
contactGroup.setOwners(new ArrayList<Owner>());
if (contactGroup.getUsers() == null)
contactGroup.setUsers(new ArrayList<RichUser>());
first = false;
} else {
ContactGroup cp = contactGroupIter.next();
//if same facility and same name merge them
if (contactGroup.equalsGroup(cp)) {
List<Group> groups = new ArrayList<>();
groups.addAll(contactGroup.getGroups());
groups.addAll(cp.getGroups());
contactGroup.setGroups(groups);
List<Owner> owners = new ArrayList<>();
owners.addAll(contactGroup.getOwners());
owners.addAll(cp.getOwners());
contactGroup.setOwners(owners);
List<RichUser> users = new ArrayList<>();
users.addAll(contactGroup.getUsers());
users.addAll(cp.getUsers());
contactGroup.setUsers(users);
// if not, skip this one for another round
} else
continue;
}
//remove used groups of contacts
contactGroupIter.remove();
}
mergedContactGroups.add(contactGroup);
}
return mergedContactGroups;
}
Aggregations