Search in sources :

Example 1 with ContactGroup

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);
        }
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ContactGroup(cz.metacentrum.perun.core.api.ContactGroup) Owner(cz.metacentrum.perun.core.api.Owner) RichUser(cz.metacentrum.perun.core.api.RichUser)

Example 2 with ContactGroup

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);
    }
}
Also used : ContactGroup(cz.metacentrum.perun.core.api.ContactGroup)

Example 3 with ContactGroup

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);
    }
}
Also used : EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ContactGroup(cz.metacentrum.perun.core.api.ContactGroup) FacilityContactNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityContactNotExistsException)

Example 4 with ContactGroup

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;
}
Also used : RichUser(cz.metacentrum.perun.core.api.RichUser) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ContactGroup(cz.metacentrum.perun.core.api.ContactGroup)

Example 5 with 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;
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ContactGroup(cz.metacentrum.perun.core.api.ContactGroup) Owner(cz.metacentrum.perun.core.api.Owner) RichUser(cz.metacentrum.perun.core.api.RichUser) ArrayList(java.util.ArrayList) ContactGroup(cz.metacentrum.perun.core.api.ContactGroup)

Aggregations

ContactGroup (cz.metacentrum.perun.core.api.ContactGroup)26 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)10 Test (org.junit.Test)10 Group (cz.metacentrum.perun.core.api.Group)9 RichUser (cz.metacentrum.perun.core.api.RichUser)9 Member (cz.metacentrum.perun.core.api.Member)8 ArrayList (java.util.ArrayList)8 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)6 Owner (cz.metacentrum.perun.core.api.Owner)5 User (cz.metacentrum.perun.core.api.User)4 RelationExistsException (cz.metacentrum.perun.core.api.exceptions.RelationExistsException)2 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)1 BanOnFacility (cz.metacentrum.perun.core.api.BanOnFacility)1 Facility (cz.metacentrum.perun.core.api.Facility)1 Host (cz.metacentrum.perun.core.api.Host)1 SecurityTeam (cz.metacentrum.perun.core.api.SecurityTeam)1 BanNotExistsException (cz.metacentrum.perun.core.api.exceptions.BanNotExistsException)1 FacilityContactNotExistsException (cz.metacentrum.perun.core.api.exceptions.FacilityContactNotExistsException)1 OwnerAlreadyRemovedException (cz.metacentrum.perun.core.api.exceptions.OwnerAlreadyRemovedException)1 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)1