Search in sources :

Example 1 with ProjectGroupManager

use of org.olat.course.nodes.projectbroker.service.ProjectGroupManager in project OpenOLAT by OpenOLAT.

the class ProjectBrokerCourseNode method postImport.

@Override
public void postImport(File importDirectory, ICourse course, CourseEnvironmentMapper envMapper, Processing processType) {
    // initialize managers
    if (processType == Processing.editor && importDirectory != null) {
        ProjectBrokerManager projectBrokerManager = CoreSpringFactory.getImpl(ProjectBrokerManager.class);
        CoursePropertyManager cpm = course.getCourseEnvironment().getCoursePropertyManager();
        // create a new projectBroker
        ProjectBroker projectBroker = projectBrokerManager.createAndSaveProjectBroker();
        projectBrokerManager.saveProjectBrokerId(projectBroker.getKey(), cpm, this);
        // get the node folder inside of the importDirectory
        File folderNodeData = new File(importDirectory, getIdent());
        // for the broker prefs
        File projectBrokerFile = new File(folderNodeData, "projectbroker.xml");
        if (projectBrokerFile.exists()) {
            XStream xstream = XStreamHelper.createXStreamInstance();
            ProjectGroupManager projectGroupManager = CoreSpringFactory.getImpl(ProjectGroupManager.class);
            ProjectBrokerConfig brokerConfig = (ProjectBrokerConfig) XStreamHelper.readObject(xstream, projectBrokerFile);
            if (brokerConfig != null && brokerConfig.getAccountGroupKey() != null) {
                Long accountGroupKey = envMapper.toGroupKeyFromOriginalKey(brokerConfig.getAccountGroupKey());
                if (accountGroupKey != null) {
                    projectGroupManager.saveAccountManagerGroupKey(accountGroupKey, cpm, this);
                }
            }
        }
        // loop through the project directories
        if (folderNodeData.exists()) {
            for (File projectDir : folderNodeData.listFiles(DirectoryFilter.DIRECTORY_FILTER)) {
                for (File projectFile : projectDir.listFiles(new FileNameSuffixFilter("xml"))) {
                    importProject(projectDir, projectFile, projectBroker, course, envMapper);
                }
            }
        }
    }
    super.postImport(importDirectory, course, envMapper, processType);
}
Also used : ProjectGroupManager(org.olat.course.nodes.projectbroker.service.ProjectGroupManager) FileNameSuffixFilter(org.olat.core.util.FileNameSuffixFilter) ProjectBroker(org.olat.course.nodes.projectbroker.datamodel.ProjectBroker) XStream(com.thoughtworks.xstream.XStream) ProjectBrokerManager(org.olat.course.nodes.projectbroker.service.ProjectBrokerManager) File(java.io.File) CoursePropertyManager(org.olat.course.properties.CoursePropertyManager) PersistingCoursePropertyManager(org.olat.course.properties.PersistingCoursePropertyManager)

Example 2 with ProjectGroupManager

use of org.olat.course.nodes.projectbroker.service.ProjectGroupManager in project OpenOLAT by OpenOLAT.

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 3 with ProjectGroupManager

use of org.olat.course.nodes.projectbroker.service.ProjectGroupManager in project openolat by klemens.

the class ProjectBrokerCourseNode method exportNode.

@Override
public void exportNode(File exportDirectory, ICourse course) {
    // initialize managers
    CoursePropertyManager cpm = course.getCourseEnvironment().getCoursePropertyManager();
    ProjectBrokerManager projectBrokerManager = CoreSpringFactory.getImpl(ProjectBrokerManager.class);
    ProjectBroker pb = projectBrokerManager.getProjectBroker(projectBrokerManager.getProjectBrokerId(cpm, this));
    ProjectGroupManager projectGroupManager = CoreSpringFactory.getImpl(ProjectGroupManager.class);
    XStream xstream = XStreamHelper.createXStreamInstance();
    // folder for the pb node
    File pbNodeFolder = new File(exportDirectory, getIdent());
    pbNodeFolder.mkdirs();
    // for the broker prefs
    ProjectBrokerConfig brokerConfig = new ProjectBrokerConfig();
    brokerConfig.setAccountGroupKey(projectGroupManager.getAccountManagerGroupKey(cpm, this));
    File projectBrokerFile = new File(pbNodeFolder, "projectbroker.xml");
    XStreamHelper.writeObject(xstream, projectBrokerFile, brokerConfig);
    // get all the projects available in the pb
    List<Project> projects = projectBrokerManager.getProjectListBy(pb.getKey());
    for (Project project : projects) {
        File projectFolder = new File(pbNodeFolder, project.getKey().toString());
        projectFolder.mkdirs();
        // create a hashmap with the project configuration and insert the
        // project data
        File projectFile = new File(projectFolder, project.getKey() + ".xml");
        HashMap<String, Object> projectData = new HashMap<String, Object>();
        projectData.put("title", project.getTitle());
        projectData.put("description", project.getDescription());
        projectData.put("customFieldSize", project.getCustomFieldSize());
        projectData.put("maxMembers", project.getMaxMembers());
        projectData.put("mailNotificationEnabled", project.isMailNotificationEnabled());
        projectData.put("attachmentFileName", project.getAttachmentFileName());
        projectData.put("allowDeselection", projectGroupManager.isDeselectionAllowed(project));
        projectData.put("customeFieldSize", project.getCustomFieldSize());
        projectData.put("businessGroupKey", project.getProjectGroup().getKey());
        // iterate through the customFields
        for (int i = 0; i < project.getCustomFieldSize(); i++) {
            projectData.put("customFieldValue" + i, project.getCustomFieldValue(i));
        }
        // writeout the project data
        XStreamHelper.writeObject(xstream, projectFile, projectData);
        // add attachment file
        OlatRootFolderImpl rootFolder = new OlatRootFolderImpl(projectBrokerManager.getAttamchmentRelativeRootPath(project, course.getCourseEnvironment(), this), null);
        VFSItem item = rootFolder.resolve(project.getAttachmentFileName());
        if (item instanceof VFSLeaf) {
            VFSLeaf itemLeaf = (VFSLeaf) item;
            File attachmentFolder = new File(projectFolder, "attachment");
            File attachment = new File(attachmentFolder, Base64.encodeBase64String(project.getAttachmentFileName().getBytes()));
            try {
                attachmentFolder.mkdirs();
                attachment.createNewFile();
                FileOutputStream attachmentOutputStream = new FileOutputStream(attachment);
                InputStream leafInputStream = itemLeaf.getInputStream();
                FileUtils.copy(leafInputStream, attachmentOutputStream);
                attachmentOutputStream.close();
                leafInputStream.close();
            } catch (IOException e) {
                log.error("Error while exporting attachments for projectbroker " + project.getTitle(), e);
            }
        }
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) HashMap(java.util.HashMap) XStream(com.thoughtworks.xstream.XStream) InputStream(java.io.InputStream) VFSItem(org.olat.core.util.vfs.VFSItem) IOException(java.io.IOException) ProjectBrokerManager(org.olat.course.nodes.projectbroker.service.ProjectBrokerManager) ProjectGroupManager(org.olat.course.nodes.projectbroker.service.ProjectGroupManager) Project(org.olat.course.nodes.projectbroker.datamodel.Project) ProjectBroker(org.olat.course.nodes.projectbroker.datamodel.ProjectBroker) OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) FileOutputStream(java.io.FileOutputStream) File(java.io.File) CoursePropertyManager(org.olat.course.properties.CoursePropertyManager) PersistingCoursePropertyManager(org.olat.course.properties.PersistingCoursePropertyManager)

Example 4 with ProjectGroupManager

use of org.olat.course.nodes.projectbroker.service.ProjectGroupManager in project openolat by klemens.

the class ProjectBrokerCourseNode method postCopy.

/**
 * Do re-arrange the projects in a new project broker after the copy happened
 */
@Override
public void postCopy(CourseEnvironmentMapper envMapper, Processing processType, ICourse course, ICourse sourceCourse) {
    super.postCopy(envMapper, processType, course, null);
    if (processType.equals(Processing.runstructure)) {
        // initialize the managers and services
        ProjectBrokerManager projectBrokerManager = CoreSpringFactory.getImpl(ProjectBrokerManager.class);
        ProjectGroupManager projectGroupManager = CoreSpringFactory.getImpl(ProjectGroupManager.class);
        CoursePropertyManager oldCpm = sourceCourse.getCourseEnvironment().getCoursePropertyManager();
        BusinessGroupService bgs = CoreSpringFactory.getImpl(BusinessGroupService.class);
        // create new Project broker and get the old one
        Long projectBrokerId = projectBrokerManager.createAndSaveProjectBroker().getKey();
        projectBrokerManager.saveProjectBrokerId(projectBrokerId, course.getCourseEnvironment().getCoursePropertyManager(), this);
        // find the group for account manager and remap the account group
        CourseNode sourceCourseNode = sourceCourse.getRunStructure().getNode(getIdent());
        Long sourceAccountGroupKey = projectGroupManager.getAccountManagerGroupKey(oldCpm, sourceCourseNode);
        if (sourceAccountGroupKey != null) {
            Long copiedGroupKey = envMapper.toGroupKeyFromOriginalKey(sourceAccountGroupKey);
            CoursePropertyManager cpm = course.getCourseEnvironment().getCoursePropertyManager();
            projectGroupManager.saveAccountManagerGroupKey(copiedGroupKey, cpm, this);
        }
        Long oldBrokerId = projectBrokerManager.getProjectBrokerId(oldCpm, this);
        List<Project> projectsFromGroup = projectBrokerManager.getProjectListBy(oldBrokerId);
        // loop create and configure the new Projects
        for (Project project : projectsFromGroup) {
            Long originalGroupKey = project.getProjectGroup().getKey();
            Long copiedGroupKey = envMapper.toGroupKeyFromOriginalKey(originalGroupKey);
            Identity author = envMapper.getAuthor();
            BusinessGroup projectGroup = bgs.loadBusinessGroup(copiedGroupKey);
            if (projectGroup == null) {
                projectGroup = projectGroupManager.createProjectGroupFor(projectBrokerId, author, project.getTitle(), project.getDescription(), course.getResourceableId());
            }
            if (author != null) {
                bgs.addOwners(author, null, Collections.singletonList(author), projectGroup, null);
            }
            Project newProject = projectBrokerManager.createAndSaveProjectFor(project.getTitle(), project.getDescription(), projectBrokerId, projectGroup);
            // copy all project configurations
            newProject.setMailNotificationEnabled(project.isMailNotificationEnabled());
            newProject.setMaxMembers(project.getMaxMembers());
            for (int i = 0; i < project.getCustomFieldSize(); i++) {
                newProject.setCustomFieldValue(i, project.getCustomFieldValue(i));
            }
            projectGroupManager.setDeselectionAllowed(newProject, project.getProjectGroup().isAllowToLeave());
            projectBrokerManager.updateProject(newProject);
            // attachment file
            OlatRootFolderImpl rootFolder = new OlatRootFolderImpl(projectBrokerManager.getAttamchmentRelativeRootPath(project, sourceCourse.getCourseEnvironment(), this), null);
            VFSItem item = rootFolder.resolve(project.getAttachmentFileName());
            if (item instanceof VFSLeaf) {
                projectBrokerManager.saveAttachedFile(newProject, project.getAttachmentFileName(), (VFSLeaf) item, course.getCourseEnvironment(), this);
                newProject.setAttachedFileName(project.getAttachmentFileName());
                projectBrokerManager.updateProject(newProject);
            }
        }
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) BusinessGroup(org.olat.group.BusinessGroup) VFSItem(org.olat.core.util.vfs.VFSItem) ProjectBrokerManager(org.olat.course.nodes.projectbroker.service.ProjectBrokerManager) ProjectGroupManager(org.olat.course.nodes.projectbroker.service.ProjectGroupManager) Project(org.olat.course.nodes.projectbroker.datamodel.Project) OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) BusinessGroupService(org.olat.group.BusinessGroupService) Identity(org.olat.core.id.Identity) CoursePropertyManager(org.olat.course.properties.CoursePropertyManager) PersistingCoursePropertyManager(org.olat.course.properties.PersistingCoursePropertyManager)

Example 5 with ProjectGroupManager

use of org.olat.course.nodes.projectbroker.service.ProjectGroupManager 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)

Aggregations

ProjectBrokerManager (org.olat.course.nodes.projectbroker.service.ProjectBrokerManager)10 ProjectGroupManager (org.olat.course.nodes.projectbroker.service.ProjectGroupManager)10 CoursePropertyManager (org.olat.course.properties.CoursePropertyManager)10 PersistingCoursePropertyManager (org.olat.course.properties.PersistingCoursePropertyManager)10 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)8 Project (org.olat.course.nodes.projectbroker.datamodel.Project)8 XStream (com.thoughtworks.xstream.XStream)6 File (java.io.File)6 OlatRootFolderImpl (org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl)6 VFSItem (org.olat.core.util.vfs.VFSItem)6 ProjectBroker (org.olat.course.nodes.projectbroker.datamodel.ProjectBroker)6 BusinessGroup (org.olat.group.BusinessGroup)6 BusinessGroupService (org.olat.group.BusinessGroupService)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Identity (org.olat.core.id.Identity)4 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 AssertException (org.olat.core.logging.AssertException)2 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)2