Search in sources :

Example 26 with RepositoryEntryToGroupRelation

use of org.olat.repository.model.RepositoryEntryToGroupRelation in project openolat by klemens.

the class RepositoryServiceImpl method create.

private RepositoryEntry create(String initialAuthorName, Identity initialAuthor, String resourceName, String displayname, String description, OLATResource resource, int access) {
    Date now = new Date();
    RepositoryEntry re = new RepositoryEntry();
    if (StringHelper.containsNonWhitespace(initialAuthorName)) {
        re.setInitialAuthor(initialAuthorName);
    } else if (initialAuthor != null) {
        re.setInitialAuthor(initialAuthor.getName());
    } else {
        re.setInitialAuthor("-");
    }
    re.setCreationDate(now);
    re.setLastModified(now);
    re.setAccess(access);
    re.setCanDownload(false);
    re.setCanCopy(false);
    re.setCanReference(false);
    re.setCanLaunch(true);
    re.setDisplayname(displayname);
    re.setResourcename(StringHelper.containsNonWhitespace(resourceName) ? resourceName : "-");
    re.setDescription(description == null ? "" : description);
    re.setAllowToLeaveOption(repositoryModule.getAllowToLeaveDefaultOption());
    if (resource == null) {
        OLATResourceable ores = OresHelper.createOLATResourceableInstance("RepositoryEntry", CodeHelper.getForeverUniqueID());
        resource = resourceManager.createAndPersistOLATResourceInstance(ores);
    } else if (resource != null && resource.getKey() == null) {
        dbInstance.getCurrentEntityManager().persist(resource);
    }
    re.setOlatResource(resource);
    RepositoryEntryStatistics statistics = new RepositoryEntryStatistics();
    statistics.setLastUsage(now);
    statistics.setCreationDate(now);
    statistics.setLastModified(now);
    statistics.setDownloadCounter(0l);
    statistics.setLaunchCounter(0l);
    statistics.setNumOfRatings(0l);
    statistics.setNumOfComments(0l);
    dbInstance.getCurrentEntityManager().persist(statistics);
    re.setStatistics(statistics);
    Group group = groupDao.createGroup();
    RepositoryEntryToGroupRelation rel = new RepositoryEntryToGroupRelation();
    rel.setCreationDate(new Date());
    rel.setDefaultGroup(true);
    rel.setGroup(group);
    rel.setEntry(re);
    Set<RepositoryEntryToGroupRelation> rels = new HashSet<>(2);
    rels.add(rel);
    re.setGroups(rels);
    if (initialAuthor != null) {
        groupDao.addMembershipTwoWay(group, initialAuthor, GroupRoles.owner.name());
    }
    dbInstance.getCurrentEntityManager().persist(re);
    autoAccessManager.grantAccess(re);
    return re;
}
Also used : RepositoryEntryStatistics(org.olat.repository.model.RepositoryEntryStatistics) Group(org.olat.basesecurity.Group) OLATResourceable(org.olat.core.id.OLATResourceable) RepositoryEntry(org.olat.repository.RepositoryEntry) Date(java.util.Date) RepositoryEntryToGroupRelation(org.olat.repository.model.RepositoryEntryToGroupRelation) HashSet(java.util.HashSet)

Example 27 with RepositoryEntryToGroupRelation

use of org.olat.repository.model.RepositoryEntryToGroupRelation in project openolat by klemens.

the class GTAReminderRuleTest method addEnrollmentDate.

private void addEnrollmentDate(RepositoryEntry entry, Identity id, GroupRoles role, int amount, int field) {
    RepositoryEntryToGroupRelation rel = entry.getGroups().iterator().next();
    rel.getGroup();
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.add(field, amount);
    GroupMembershipImpl membership = new GroupMembershipImpl();
    membership.setCreationDate(cal.getTime());
    membership.setLastModified(cal.getTime());
    membership.setGroup(rel.getGroup());
    membership.setIdentity(id);
    membership.setRole(role.name());
    dbInstance.getCurrentEntityManager().persist(membership);
    dbInstance.commit();
}
Also used : Calendar(java.util.Calendar) Date(java.util.Date) GroupMembershipImpl(org.olat.basesecurity.model.GroupMembershipImpl) RepositoryEntryToGroupRelation(org.olat.repository.model.RepositoryEntryToGroupRelation)

Example 28 with RepositoryEntryToGroupRelation

use of org.olat.repository.model.RepositoryEntryToGroupRelation in project openolat by klemens.

the class BusinessGroupServiceImpl method addResourcesTo.

@Override
public void addResourcesTo(List<BusinessGroup> groups, List<RepositoryEntry> resources) {
    if (groups == null || groups.isEmpty())
        return;
    if (resources == null || resources.isEmpty())
        return;
    List<Group> baseGroupKeys = new ArrayList<Group>();
    for (BusinessGroup group : groups) {
        baseGroupKeys.add(group.getBaseGroup());
    }
    // check for duplicate entries
    List<RepositoryEntryToGroupRelation> relations = repositoryEntryRelationDao.getRelations(baseGroupKeys);
    for (BusinessGroup group : groups) {
        // reload the base group to prevent lazy loading exception
        Group baseGroup = businessGroupRelationDAO.getGroup(group);
        if (baseGroup == null) {
            continue;
        }
        for (RepositoryEntry re : resources) {
            boolean found = false;
            for (RepositoryEntryToGroupRelation relation : relations) {
                if (relation.getGroup().equals(baseGroup) && relation.getEntry().equals(re)) {
                    found = true;
                }
            }
            if (!found) {
                repositoryEntryRelationDao.createRelation(baseGroup, re);
            }
        }
    }
}
Also used : Group(org.olat.basesecurity.Group) SecurityGroup(org.olat.basesecurity.SecurityGroup) BusinessGroup(org.olat.group.BusinessGroup) BusinessGroup(org.olat.group.BusinessGroup) ArrayList(java.util.ArrayList) RepositoryEntry(org.olat.repository.RepositoryEntry) RepositoryEntryToGroupRelation(org.olat.repository.model.RepositoryEntryToGroupRelation)

Aggregations

RepositoryEntryToGroupRelation (org.olat.repository.model.RepositoryEntryToGroupRelation)28 RepositoryEntry (org.olat.repository.RepositoryEntry)16 Group (org.olat.basesecurity.Group)12 Date (java.util.Date)10 BusinessGroup (org.olat.group.BusinessGroup)10 Test (org.junit.Test)8 Identity (org.olat.core.id.Identity)8 EntityManager (javax.persistence.EntityManager)6 Calendar (java.util.Calendar)4 GroupMembershipImpl (org.olat.basesecurity.model.GroupMembershipImpl)4 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 SecurityGroup (org.olat.basesecurity.SecurityGroup)2 OLATResourceable (org.olat.core.id.OLATResourceable)2 RepositoryEntryStatistics (org.olat.repository.model.RepositoryEntryStatistics)2