|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package org.olat.course.config.manager; |
|
|
|
import java.io.InputStream; |
|
import java.util.HashMap; |
|
import java.util.Hashtable; |
|
|
|
import org.apache.logging.log4j.Logger; |
|
import org.olat.core.CoreSpringFactory; |
|
import org.olat.core.commons.services.vfs.VFSRepositoryService; |
|
import org.olat.core.logging.Tracing; |
|
import org.olat.core.util.vfs.VFSConstants; |
|
import org.olat.core.util.vfs.VFSItem; |
|
import org.olat.core.util.vfs.VFSLeaf; |
|
import org.olat.core.util.xml.XStreamHelper; |
|
import org.olat.course.ICourse; |
|
import org.olat.course.config.CourseConfig; |
|
import org.olat.course.config.CourseConfigManager; |
|
import org.springframework.stereotype.Service; |
|
|
|
import com.thoughtworks.xstream.XStream; |
|
import com.thoughtworks.xstream.security.ExplicitTypePermission; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@Service |
|
public class CourseConfigManagerImpl implements CourseConfigManager { |
|
|
|
private static final Logger log = Tracing.createLoggerFor(CourseConfigManagerImpl.class); |
|
|
|
private static final XStream xstream = XStreamHelper.createXStreamInstance(); |
|
static { |
|
Class<?>[] types = new Class[] { |
|
CourseConfig.class, Hashtable.class, HashMap.class |
|
}; |
|
|
|
|
|
|
|
xstream.addPermission(new ExplicitTypePermission(types)); |
|
} |
|
|
|
@Override |
|
public CourseConfig copyConfigOf(ICourse course) { |
|
return course.getCourseEnvironment().getCourseConfig().clone(); |
|
} |
|
|
|
@Override |
|
public boolean deleteConfigOf(ICourse course) { |
|
VFSLeaf configFile = getConfigFile(course); |
|
if (configFile != null) { |
|
return configFile.delete() == VFSConstants.YES; |
|
} |
|
return false; |
|
} |
|
|
|
@Override |
|
public CourseConfig loadConfigFor(ICourse course) { |
|
CourseConfig retVal = null; |
|
VFSLeaf configFile = getConfigFile(course); |
|
if (configFile == null) { |
|
|
|
retVal = new CourseConfig(); |
|
retVal.initDefaults(); |
|
saveConfigTo(course, retVal); |
|
} else { |
|
|
|
Object tmp = XStreamHelper.readObject(xstream, configFile); |
|
if (tmp instanceof CourseConfig) { |
|
retVal = (CourseConfig) tmp; |
|
if (retVal.resolveVersionIssues()) { |
|
saveConfigTo(course, retVal); |
|
} |
|
} |
|
} |
|
return retVal; |
|
} |
|
|
|
@Override |
|
public void saveConfigTo(ICourse course, CourseConfig courseConfig) { |
|
VFSLeaf configFile = getConfigFile(course); |
|
if (configFile == null) { |
|
|
|
configFile = course.getCourseBaseContainer().createChildLeaf(COURSECONFIG_XML); |
|
} else if(configFile.exists() && configFile.canVersion() == VFSConstants.YES) { |
|
try(InputStream in = configFile.getInputStream()) { |
|
CoreSpringFactory.getImpl(VFSRepositoryService.class).addVersion(configFile, null, "", in); |
|
} catch (Exception e) { |
|
log.error("Cannot versioned CourseConfig.xml", e); |
|
} |
|
} |
|
XStreamHelper.writeObject(xstream, configFile, courseConfig); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static VFSLeaf getConfigFile(ICourse course) { |
|
VFSItem item = course.getCourseBaseContainer().resolve(COURSECONFIG_XML); |
|
return item instanceof VFSLeaf ? (VFSLeaf)item : null; |
|
} |
|
} |