Search in sources :

Example 11 with BusinessGroupReference

use of org.olat.group.model.BusinessGroupReference in project OpenOLAT by OpenOLAT.

the class BusinessGroupImportExport method importGroups.

public BusinessGroupEnvironment importGroups(RepositoryEntry re, File fGroupExportXML) {
    if (!fGroupExportXML.exists())
        return new BusinessGroupEnvironment();
    // start with a new connection
    dbInstance.commitAndCloseSession();
    OLATGroupExport groupConfig = null;
    try {
        groupConfig = xstream.fromXML(fGroupExportXML);
    } catch (Exception ce) {
        throw new OLATRuntimeException("Error importing group config.", ce);
    }
    if (groupConfig == null) {
        throw new AssertException("Invalid group export file. Root does not match.");
    }
    BusinessGroupEnvironment env = new BusinessGroupEnvironment();
    Set<BGArea> areaSet = new HashSet<BGArea>();
    // get areas
    int dbCount = 0;
    if (groupConfig.getAreas() != null && groupConfig.getAreas().getGroups() != null) {
        for (Area area : groupConfig.getAreas().getGroups()) {
            String areaName = area.name;
            String areaDesc = (area.description != null && !area.description.isEmpty()) ? area.description.get(0) : "";
            BGArea newArea = areaManager.createAndPersistBGArea(areaName, areaDesc, re.getOlatResource());
            if (areaSet.add(newArea)) {
                env.getAreas().add(new BGAreaReference(newArea, area.key, area.name));
            }
            if (dbCount++ % 25 == 0) {
                dbInstance.commitAndCloseSession();
            }
        }
    }
    // get groups
    if (groupConfig.getGroups() != null && groupConfig.getGroups().getGroups() != null) {
        for (Group group : groupConfig.getGroups().getGroups()) {
            // create group
            String groupName = group.name;
            String groupDesc = (group.description != null && !group.description.isEmpty()) ? group.description.get(0) : "";
            // get min/max participants
            int groupMinParticipants = group.minParticipants == null ? -1 : group.minParticipants.intValue();
            int groupMaxParticipants = group.maxParticipants == null ? -1 : group.maxParticipants.intValue();
            // waiting list configuration
            boolean waitingList = false;
            if (group.waitingList != null) {
                waitingList = group.waitingList.booleanValue();
            }
            boolean enableAutoCloseRanks = false;
            if (group.autoCloseRanks != null) {
                enableAutoCloseRanks = group.autoCloseRanks.booleanValue();
            }
            // get properties
            boolean showOwners = true;
            boolean showParticipants = true;
            boolean showWaitingList = true;
            if (group.showOwners != null) {
                showOwners = group.showOwners;
            }
            if (group.showParticipants != null) {
                showParticipants = group.showParticipants;
            }
            if (group.showWaitingList != null) {
                showWaitingList = group.showWaitingList;
            }
            BusinessGroup newGroup = businessGroupService.createBusinessGroup(null, groupName, groupDesc, groupMinParticipants, groupMaxParticipants, waitingList, enableAutoCloseRanks, re);
            // map the group
            env.getGroups().add(new BusinessGroupReference(newGroup, group.key, group.name));
            // get tools config
            String[] availableTools = CollaborationToolsFactory.getInstance().getAvailableTools().clone();
            CollabTools toolsConfig = group.tools;
            CollaborationTools ct = CollaborationToolsFactory.getInstance().getOrCreateCollaborationTools(newGroup);
            for (int i = 0; i < availableTools.length; i++) {
                try {
                    Field field = toolsConfig.getClass().getField(availableTools[i]);
                    Boolean val = field.getBoolean(toolsConfig);
                    if (val != null) {
                        ct.setToolEnabled(availableTools[i], val);
                    }
                } catch (NoSuchFieldException e) {
                // hasOpenMeetings compatibility
                } catch (Exception e) {
                    log.error("", e);
                }
            }
            if (group.calendarAccess != null) {
                Long calendarAccess = group.calendarAccess;
                ct.saveCalendarAccess(calendarAccess);
            }
            if (group.folderAccess != null) {
                ct.saveFolderAccess(group.folderAccess);
            }
            if (group.info != null) {
                ct.saveNews(group.info);
            }
            // get memberships
            List<String> memberships = group.areaRelations;
            if (memberships != null && memberships.size() > 0) {
                Set<String> uniqueMemberships = new HashSet<>(memberships);
                for (String membership : uniqueMemberships) {
                    BGArea area = areaManager.findBGArea(membership, re.getOlatResource());
                    if (area != null) {
                        areaManager.addBGToBGArea(newGroup, area);
                    } else {
                        log.error("Area not found", null);
                    }
                }
            }
            boolean download = groupModule.isUserListDownloadDefaultAllowed();
            newGroup = businessGroupService.updateDisplayMembers(newGroup, showOwners, showParticipants, showWaitingList, false, false, false, download);
            if (dbCount++ % 3 == 0) {
                dbInstance.commitAndCloseSession();
            }
        }
    }
    dbInstance.commitAndCloseSession();
    return env;
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) Field(java.lang.reflect.Field) BGArea(org.olat.group.area.BGArea) BusinessGroupReference(org.olat.group.model.BusinessGroupReference) HashSet(java.util.HashSet) BusinessGroupEnvironment(org.olat.group.model.BusinessGroupEnvironment) AssertException(org.olat.core.logging.AssertException) BusinessGroup(org.olat.group.BusinessGroup) BGAreaReference(org.olat.group.model.BGAreaReference) AssertException(org.olat.core.logging.AssertException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) IOException(java.io.IOException) BGArea(org.olat.group.area.BGArea) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) CollaborationTools(org.olat.collaboration.CollaborationTools)

Example 12 with BusinessGroupReference

use of org.olat.group.model.BusinessGroupReference in project openolat by klemens.

the class BusinessGroupRoleRuleSPI method clone.

@Override
public ReminderRule clone(ReminderRule rule, CourseEnvironmentMapper envMapper) {
    ReminderRuleImpl clone = (ReminderRuleImpl) rule.clone();
    String groupKey = clone.getRightOperand();
    boolean found = false;
    if (StringHelper.isLong(groupKey)) {
        Long key = Long.parseLong(groupKey);
        for (BusinessGroupReference ref : envMapper.getGroups()) {
            if (key.equals(ref.getOriginalKey()) && ref.getKey() != null) {
                clone.setRightOperand(ref.getKey().toString());
                found = true;
            }
        }
    }
    return found ? clone : null;
}
Also used : ReminderRuleImpl(org.olat.modules.reminder.model.ReminderRuleImpl) BusinessGroupReference(org.olat.group.model.BusinessGroupReference)

Example 13 with BusinessGroupReference

use of org.olat.group.model.BusinessGroupReference in project openolat by klemens.

the class BusinessGroupImportExport method importGroups.

public BusinessGroupEnvironment importGroups(RepositoryEntry re, File fGroupExportXML) {
    if (!fGroupExportXML.exists())
        return new BusinessGroupEnvironment();
    // start with a new connection
    dbInstance.commitAndCloseSession();
    OLATGroupExport groupConfig = null;
    try {
        groupConfig = xstream.fromXML(fGroupExportXML);
    } catch (Exception ce) {
        throw new OLATRuntimeException("Error importing group config.", ce);
    }
    if (groupConfig == null) {
        throw new AssertException("Invalid group export file. Root does not match.");
    }
    BusinessGroupEnvironment env = new BusinessGroupEnvironment();
    Set<BGArea> areaSet = new HashSet<BGArea>();
    // get areas
    int dbCount = 0;
    if (groupConfig.getAreas() != null && groupConfig.getAreas().getGroups() != null) {
        for (Area area : groupConfig.getAreas().getGroups()) {
            String areaName = area.name;
            String areaDesc = (area.description != null && !area.description.isEmpty()) ? area.description.get(0) : "";
            BGArea newArea = areaManager.createAndPersistBGArea(areaName, areaDesc, re.getOlatResource());
            if (areaSet.add(newArea)) {
                env.getAreas().add(new BGAreaReference(newArea, area.key, area.name));
            }
            if (dbCount++ % 25 == 0) {
                dbInstance.commitAndCloseSession();
            }
        }
    }
    // get groups
    if (groupConfig.getGroups() != null && groupConfig.getGroups().getGroups() != null) {
        for (Group group : groupConfig.getGroups().getGroups()) {
            // create group
            String groupName = group.name;
            String groupDesc = (group.description != null && !group.description.isEmpty()) ? group.description.get(0) : "";
            // get min/max participants
            int groupMinParticipants = group.minParticipants == null ? -1 : group.minParticipants.intValue();
            int groupMaxParticipants = group.maxParticipants == null ? -1 : group.maxParticipants.intValue();
            // waiting list configuration
            boolean waitingList = false;
            if (group.waitingList != null) {
                waitingList = group.waitingList.booleanValue();
            }
            boolean enableAutoCloseRanks = false;
            if (group.autoCloseRanks != null) {
                enableAutoCloseRanks = group.autoCloseRanks.booleanValue();
            }
            // get properties
            boolean showOwners = true;
            boolean showParticipants = true;
            boolean showWaitingList = true;
            if (group.showOwners != null) {
                showOwners = group.showOwners;
            }
            if (group.showParticipants != null) {
                showParticipants = group.showParticipants;
            }
            if (group.showWaitingList != null) {
                showWaitingList = group.showWaitingList;
            }
            BusinessGroup newGroup = businessGroupService.createBusinessGroup(null, groupName, groupDesc, groupMinParticipants, groupMaxParticipants, waitingList, enableAutoCloseRanks, re);
            // map the group
            env.getGroups().add(new BusinessGroupReference(newGroup, group.key, group.name));
            // get tools config
            String[] availableTools = CollaborationToolsFactory.getInstance().getAvailableTools().clone();
            CollabTools toolsConfig = group.tools;
            CollaborationTools ct = CollaborationToolsFactory.getInstance().getOrCreateCollaborationTools(newGroup);
            for (int i = 0; i < availableTools.length; i++) {
                try {
                    Field field = toolsConfig.getClass().getField(availableTools[i]);
                    Boolean val = field.getBoolean(toolsConfig);
                    if (val != null) {
                        ct.setToolEnabled(availableTools[i], val);
                    }
                } catch (NoSuchFieldException e) {
                // hasOpenMeetings compatibility
                } catch (Exception e) {
                    log.error("", e);
                }
            }
            if (group.calendarAccess != null) {
                Long calendarAccess = group.calendarAccess;
                ct.saveCalendarAccess(calendarAccess);
            }
            if (group.folderAccess != null) {
                ct.saveFolderAccess(group.folderAccess);
            }
            if (group.info != null) {
                ct.saveNews(group.info);
            }
            // get memberships
            List<String> memberships = group.areaRelations;
            if (memberships != null && memberships.size() > 0) {
                Set<String> uniqueMemberships = new HashSet<>(memberships);
                for (String membership : uniqueMemberships) {
                    BGArea area = areaManager.findBGArea(membership, re.getOlatResource());
                    if (area != null) {
                        areaManager.addBGToBGArea(newGroup, area);
                    } else {
                        log.error("Area not found", null);
                    }
                }
            }
            boolean download = groupModule.isUserListDownloadDefaultAllowed();
            newGroup = businessGroupService.updateDisplayMembers(newGroup, showOwners, showParticipants, showWaitingList, false, false, false, download);
            if (dbCount++ % 3 == 0) {
                dbInstance.commitAndCloseSession();
            }
        }
    }
    dbInstance.commitAndCloseSession();
    return env;
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) Field(java.lang.reflect.Field) BGArea(org.olat.group.area.BGArea) BusinessGroupReference(org.olat.group.model.BusinessGroupReference) HashSet(java.util.HashSet) BusinessGroupEnvironment(org.olat.group.model.BusinessGroupEnvironment) AssertException(org.olat.core.logging.AssertException) BusinessGroup(org.olat.group.BusinessGroup) BGAreaReference(org.olat.group.model.BGAreaReference) AssertException(org.olat.core.logging.AssertException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) IOException(java.io.IOException) BGArea(org.olat.group.area.BGArea) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) CollaborationTools(org.olat.collaboration.CollaborationTools)

Example 14 with BusinessGroupReference

use of org.olat.group.model.BusinessGroupReference in project openolat by klemens.

the class ProjectBrokerCourseNode method importProject.

private void importProject(File projectDir, File projectFile, ProjectBroker projectBroker, ICourse course, CourseEnvironmentMapper envMapper) {
    XStream xstream = XStreamHelper.createXStreamInstance();
    BusinessGroupService bgs = CoreSpringFactory.getImpl(BusinessGroupService.class);
    CoursePropertyManager cpm = course.getCourseEnvironment().getCoursePropertyManager();
    ProjectGroupManager projectGroupManager = CoreSpringFactory.getImpl(ProjectGroupManager.class);
    ProjectBrokerManager projectBrokerManager = CoreSpringFactory.getImpl(ProjectBrokerManager.class);
    // read the projectConfiguration from the importDirectory
    try {
        @SuppressWarnings("unchecked") Map<String, Object> projectConfig = (HashMap<String, Object>) XStreamHelper.readObject(xstream, projectFile);
        String projectTitle = (String) projectConfig.get("title");
        Long originalGroupKey = null;
        if (projectConfig.containsKey("businessGroupKey")) {
            originalGroupKey = (Long) projectConfig.get("businessGroupKey");
        } else {
            for (BusinessGroupReference ref : envMapper.getGroups()) {
                if (ref.getName().endsWith(projectTitle)) {
                    originalGroupKey = ref.getOriginalKey();
                }
            }
        }
        BusinessGroup projectGroup = null;
        if (originalGroupKey != null) {
            Long groupKey = envMapper.toGroupKeyFromOriginalKey(originalGroupKey);
            projectGroup = bgs.loadBusinessGroup(groupKey);
        }
        if (projectGroup == null) {
            projectGroup = projectGroupManager.createProjectGroupFor(projectBroker.getKey(), envMapper.getAuthor(), projectTitle, (String) projectConfig.get("description"), course.getResourceableId());
        }
        if (envMapper.getAuthor() != null) {
            Identity author = envMapper.getAuthor();
            bgs.addOwners(author, null, Collections.singletonList(author), projectGroup, null);
        }
        Project project = projectBrokerManager.createAndSaveProjectFor(projectTitle, (String) projectConfig.get("description"), projectBrokerManager.getProjectBrokerId(cpm, this), projectGroup);
        projectGroupManager.setDeselectionAllowed(project, (boolean) projectConfig.get("allowDeselection"));
        project.setMailNotificationEnabled((boolean) projectConfig.get("mailNotificationEnabled"));
        project.setMaxMembers((int) projectConfig.get("maxMembers"));
        project.setAttachedFileName(projectConfig.get("attachmentFileName").toString());
        for (int i = 0; i < (int) projectConfig.get("customeFieldSize"); i++) {
            project.setCustomFieldValue(i, projectConfig.get("customFieldValue" + i).toString());
        }
        projectBrokerManager.updateProject(project);
        // get the attachment directory within the project
        // directory
        // .getParentFile().listFiles(attachmentFilter);
        File attachmentDir = new File(projectDir, "attachment");
        if (attachmentDir.exists()) {
            File[] attachment = attachmentDir.listFiles();
            if (attachment.length > 0) {
                VFSLeaf attachmentLeaf = new LocalFileImpl(attachment[0]);
                projectBrokerManager.saveAttachedFile(project, projectConfig.get("attachmentFileName").toString(), attachmentLeaf, course.getCourseEnvironment(), this);
            }
        }
    } catch (Exception e) {
        // handle/log error in case of FileIO exception or cast
        // exception if import input is not correct
        log.error("Error while importing a project into projectbroker", e);
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) HashMap(java.util.HashMap) BusinessGroup(org.olat.group.BusinessGroup) XStream(com.thoughtworks.xstream.XStream) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) ProjectBrokerManager(org.olat.course.nodes.projectbroker.service.ProjectBrokerManager) AssertException(org.olat.core.logging.AssertException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) IOException(java.io.IOException) ProjectGroupManager(org.olat.course.nodes.projectbroker.service.ProjectGroupManager) Project(org.olat.course.nodes.projectbroker.datamodel.Project) BusinessGroupService(org.olat.group.BusinessGroupService) BusinessGroupReference(org.olat.group.model.BusinessGroupReference) Identity(org.olat.core.id.Identity) File(java.io.File) CoursePropertyManager(org.olat.course.properties.CoursePropertyManager) PersistingCoursePropertyManager(org.olat.course.properties.PersistingCoursePropertyManager)

Example 15 with BusinessGroupReference

use of org.olat.group.model.BusinessGroupReference in project openolat by klemens.

the class KeyAndNameConverter method convertExpressionKeyToName.

/**
 * isLearningGroupFull, inLearningGroup, inRightGroup, inLearningArea, isLearningGroupFull
 */
public static String convertExpressionKeyToName(String expression, CourseEnvironmentMapper envMapper) {
    for (String groupMethod : groupMethods) {
        for (BusinessGroupReference group : envMapper.getGroups()) {
            String strToMatch = groupMethod + "(\"" + group.getKey() + "\")";
            String replacement = groupMethod + "(\"" + group.getName() + "\")";
            expression = StringHelper.replaceAllCaseInsensitive(expression, strToMatch, replacement);
        }
    }
    for (BGAreaReference area : envMapper.getAreas()) {
        String strToMatch = areaMethod + "(\"" + area.getKey() + "\")";
        String replacement = areaMethod + "(\"" + area.getName() + "\")";
        expression = StringHelper.replaceAllCaseInsensitive(expression, strToMatch, replacement);
    }
    for (BusinessGroupReference group : envMapper.getGroups()) {
        String strToMatch = "\"" + group.getKey() + "\"";
        String replacement = "\"" + group.getName() + "\"";
        expression = StringHelper.replaceAllCaseInsensitive(expression, strToMatch, replacement);
    }
    for (BGAreaReference area : envMapper.getAreas()) {
        String strToMatch = "\"" + area.getKey() + "\"";
        String replacement = "\"" + area.getName() + "\"";
        expression = StringHelper.replaceAllCaseInsensitive(expression, strToMatch, replacement);
    }
    return expression;
}
Also used : BusinessGroupReference(org.olat.group.model.BusinessGroupReference) BGAreaReference(org.olat.group.model.BGAreaReference)

Aggregations

BusinessGroupReference (org.olat.group.model.BusinessGroupReference)32 BGAreaReference (org.olat.group.model.BGAreaReference)20 CourseEnvironmentMapper (org.olat.course.export.CourseEnvironmentMapper)16 Test (org.junit.Test)12 BGArea (org.olat.group.area.BGArea)12 BusinessGroup (org.olat.group.BusinessGroup)8 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 AssertException (org.olat.core.logging.AssertException)4 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)4 XStream (com.thoughtworks.xstream.XStream)2 File (java.io.File)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CollaborationTools (org.olat.collaboration.CollaborationTools)2 Identity (org.olat.core.id.Identity)2 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)2 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)2 Project (org.olat.course.nodes.projectbroker.datamodel.Project)2