Search in sources :

Example 26 with ResourceReservation

use of org.olat.resource.accesscontrol.ResourceReservation in project OpenOLAT by OpenOLAT.

the class BusinessGroupServiceImpl method removeMembers.

@Override
public void removeMembers(Identity ureqIdentity, List<Identity> identities, OLATResource resource, MailPackage mailing) {
    // nothing to do
    if (identities == null || identities.isEmpty() || resource == null)
        return;
    List<BusinessGroup> groups = null;
    if ("BusinessGroup".equals(resource.getResourceableTypeName())) {
        // it's a group resource
        BusinessGroup group = loadBusinessGroup(resource);
        if (group != null) {
            groups = Collections.singletonList(group);
        }
    } else {
        RepositoryEntryRef re = repositoryManager.lookupRepositoryEntry(resource, false);
        groups = findBusinessGroups(null, re, 0, -1);
    }
    if (groups == null || groups.isEmpty()) {
        // nothing to do
        return;
    }
    // remove managed groups
    for (Iterator<BusinessGroup> groupIt = groups.iterator(); groupIt.hasNext(); ) {
        boolean managed = BusinessGroupManagedFlag.isManaged(groupIt.next(), BusinessGroupManagedFlag.membersmanagement);
        if (managed) {
            groupIt.remove();
        }
    }
    if (groups.isEmpty()) {
        // nothing to do
        return;
    }
    List<OLATResource> groupResources = new ArrayList<OLATResource>();
    Map<Long, BusinessGroup> idToGroup = new HashMap<>();
    for (BusinessGroup group : groups) {
        groupResources.add(group.getResource());
        idToGroup.put(group.getKey(), group);
    }
    final Map<Long, Identity> keyToIdentityMap = new HashMap<Long, Identity>();
    for (Identity identity : identities) {
        keyToIdentityMap.put(identity.getKey(), identity);
    }
    List<BusinessGroupModifiedEvent.Deferred> events = new ArrayList<BusinessGroupModifiedEvent.Deferred>();
    List<BusinessGroupMembershipViewImpl> memberships = businessGroupDAO.getMembershipInfoInBusinessGroups(groups, identities);
    Collections.sort(memberships, new BusinessGroupMembershipViewComparator());
    BusinessGroupMembershipViewImpl nextGroupMembership = null;
    for (final Iterator<BusinessGroupMembershipViewImpl> itMembership = memberships.iterator(); nextGroupMembership != null || itMembership.hasNext(); ) {
        final BusinessGroupMembershipViewImpl currentMembership;
        if (nextGroupMembership == null) {
            currentMembership = itMembership.next();
        } else {
            currentMembership = nextGroupMembership;
            nextGroupMembership = null;
        }
        Long groupKey = currentMembership.getGroupKey();
        BusinessGroup nextGroup = businessGroupDAO.loadForUpdate(idToGroup.get(groupKey));
        nextGroupMembership = removeGroupMembers(ureqIdentity, currentMembership, nextGroup, keyToIdentityMap, itMembership, mailing, events);
        // release the lock
        dbInstance.commit();
    }
    List<ResourceReservation> reservations = reservationDao.loadReservations(groupResources);
    for (ResourceReservation reservation : reservations) {
        if (identities.contains(reservation.getIdentity())) {
            reservationDao.deleteReservation(reservation);
        }
    }
    dbInstance.commit();
    BusinessGroupModifiedEvent.fireDeferredEvents(events);
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) HashMap(java.util.HashMap) ResourceReservation(org.olat.resource.accesscontrol.ResourceReservation) ArrayList(java.util.ArrayList) OLATResource(org.olat.resource.OLATResource) BusinessGroupModifiedEvent(org.olat.group.ui.edit.BusinessGroupModifiedEvent) RepositoryEntryRef(org.olat.repository.RepositoryEntryRef) Identity(org.olat.core.id.Identity) BusinessGroupMembershipViewImpl(org.olat.group.model.BusinessGroupMembershipViewImpl)

Example 27 with ResourceReservation

use of org.olat.resource.accesscontrol.ResourceReservation in project openolat by klemens.

the class ACFrontendManager method reserveAccessToResource.

@Override
public boolean reserveAccessToResource(final Identity identity, final OfferAccess offer) {
    final OLATResource resource = offer.getOffer().getResource();
    String resourceType = resource.getResourceableTypeName();
    if ("BusinessGroup".equals(resourceType)) {
        boolean reserved = false;
        final BusinessGroup group = businessGroupDao.loadForUpdate(resource.getResourceableId());
        if (group.getMaxParticipants() == null && group.getMaxParticipants() <= 0) {
            // don't need reservation
            reserved = true;
        } else {
            BusinessGroup reloadedGroup = businessGroupService.loadBusinessGroup(resource);
            ResourceReservation reservation = reservationDao.loadReservation(identity, resource);
            if (reservation != null) {
                reserved = true;
            }
            int currentCount = businessGroupService.countMembers(reloadedGroup, GroupRoles.participant.name());
            int reservations = reservationDao.countReservations(resource);
            if (currentCount + reservations < reloadedGroup.getMaxParticipants().intValue()) {
                reservationDao.createReservation(identity, offer.getMethod().getType(), null, resource);
                reserved = true;
            }
        }
        return reserved;
    }
    return true;
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) ResourceReservation(org.olat.resource.accesscontrol.ResourceReservation) OLATResource(org.olat.resource.OLATResource)

Example 28 with ResourceReservation

use of org.olat.resource.accesscontrol.ResourceReservation in project openolat by klemens.

the class ACFrontendManager method cleanupReservations.

@Override
public void cleanupReservations() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR_OF_DAY, -1);
    Date oneHourTimeout = cal.getTime();
    List<ResourceReservation> oldReservations = reservationDao.loadExpiredReservation(oneHourTimeout);
    for (ResourceReservation reservation : oldReservations) {
        log.audit("Remove reservation:" + reservation);
        reservationDao.deleteReservation(reservation);
    }
}
Also used : ResourceReservation(org.olat.resource.accesscontrol.ResourceReservation) Calendar(java.util.Calendar) Date(java.util.Date)

Example 29 with ResourceReservation

use of org.olat.resource.accesscontrol.ResourceReservation in project openolat by klemens.

the class BusinessGroupServiceTest method testCancelPendingParticipation_deletedGroup.

@Test
public void testCancelPendingParticipation_deletedGroup() {
    // create a group
    Identity id = JunitTestHelper.createAndPersistIdentityAsUser("Reserv-bg-" + UUID.randomUUID().toString());
    BusinessGroup group = businessGroupService.createBusinessGroup(null, "Free group", "But you must wait", new Integer(0), new Integer(2), false, false, null);
    // create a reservation
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR, 6);
    ResourceReservation reservation = reservationDao.createReservation(id, "group_participant", cal.getTime(), group.getResource());
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(reservation);
    // delete the group
    businessGroupService.deleteBusinessGroup(group);
    dbInstance.commitAndCloseSession();
    // accept reservation
    acService.removeReservation(id, id, reservation);
    dbInstance.commitAndCloseSession();
    // check that the user is not participant
    boolean participant2 = businessGroupService.hasRoles(id, group, GroupRoles.participant.name());
    Assert.assertFalse(participant2);
    // check that the reservations are deleted
    List<ResourceReservation> reservations = reservationDao.loadReservations(id);
    Assert.assertNotNull(reservations);
    Assert.assertTrue(reservations.isEmpty());
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) ResourceReservation(org.olat.resource.accesscontrol.ResourceReservation) Calendar(java.util.Calendar) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Example 30 with ResourceReservation

use of org.olat.resource.accesscontrol.ResourceReservation in project openolat by klemens.

the class BusinessGroupServiceTest method testAcceptPendingParticipation_coach.

@Test
public void testAcceptPendingParticipation_coach() {
    // create a group
    Identity id = JunitTestHelper.createAndPersistIdentityAsUser("Reserv-bg-" + UUID.randomUUID().toString());
    BusinessGroup group = businessGroupService.createBusinessGroup(null, "Free group", "But you must wait", new Integer(0), new Integer(2), false, false, null);
    // create a reservation
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR, 6);
    ResourceReservation reservation = reservationDao.createReservation(id, "group_coach", cal.getTime(), group.getResource());
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(reservation);
    // check that the user is not participant
    Assert.assertFalse(businessGroupService.hasRoles(id, group, GroupRoles.coach.name()));
    // accept reservation
    acService.acceptReservationToResource(id, reservation);
    dbInstance.commitAndCloseSession();
    // check that the user is participant
    Assert.assertTrue(businessGroupService.hasRoles(id, group, GroupRoles.coach.name()));
    // check that the reservations are deleted
    List<ResourceReservation> reservations = reservationDao.loadReservations(id);
    Assert.assertNotNull(reservations);
    Assert.assertTrue(reservations.isEmpty());
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) ResourceReservation(org.olat.resource.accesscontrol.ResourceReservation) Calendar(java.util.Calendar) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Aggregations

ResourceReservation (org.olat.resource.accesscontrol.ResourceReservation)36 Identity (org.olat.core.id.Identity)24 Calendar (java.util.Calendar)20 BusinessGroup (org.olat.group.BusinessGroup)20 ArrayList (java.util.ArrayList)14 Date (java.util.Date)12 Test (org.junit.Test)10 OLATResource (org.olat.resource.OLATResource)8 BusinessGroupModifiedEvent (org.olat.group.ui.edit.BusinessGroupModifiedEvent)6 HashMap (java.util.HashMap)4 Order (org.olat.resource.accesscontrol.Order)4 OrderLine (org.olat.resource.accesscontrol.OrderLine)4 OrderPart (org.olat.resource.accesscontrol.OrderPart)4 HashSet (java.util.HashSet)2 Locale (java.util.Locale)2 ActionType (org.olat.core.logging.activity.ActionType)2 BusinessGroupMembership (org.olat.group.BusinessGroupMembership)2 BusinessGroupShort (org.olat.group.BusinessGroupShort)2 BusinessGroupMembershipViewImpl (org.olat.group.model.BusinessGroupMembershipViewImpl)2 EnrollState (org.olat.group.model.EnrollState)2