|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package org.olat.repository; |
|
|
|
import java.io.File; |
|
import java.io.FileInputStream; |
|
import java.io.FileOutputStream; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.io.OutputStream; |
|
import java.nio.file.FileSystem; |
|
import java.nio.file.FileSystems; |
|
import java.nio.file.Files; |
|
import java.nio.file.Path; |
|
import java.util.zip.ZipEntry; |
|
import java.util.zip.ZipOutputStream; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import org.apache.commons.io.IOUtils; |
|
import org.apache.logging.log4j.Logger; |
|
import org.olat.core.CoreSpringFactory; |
|
import org.olat.core.commons.services.license.LicenseService; |
|
import org.olat.core.commons.services.license.LicenseType; |
|
import org.olat.core.commons.services.license.ResourceLicense; |
|
import org.olat.core.commons.services.license.ui.LicenseUIFactory; |
|
import org.olat.core.gui.media.MediaResource; |
|
import org.olat.core.logging.OLATRuntimeException; |
|
import org.olat.core.logging.Tracing; |
|
import org.olat.core.util.FileUtils; |
|
import org.olat.core.util.StringHelper; |
|
import org.olat.core.util.ZipUtil; |
|
import org.olat.core.util.io.HttpServletResponseOutputStream; |
|
import org.olat.core.util.io.ShieldOutputStream; |
|
import org.olat.core.util.vfs.LocalFileImpl; |
|
import org.olat.core.util.vfs.VFSContainer; |
|
import org.olat.core.util.vfs.VFSLeaf; |
|
import org.olat.core.util.vfs.VFSManager; |
|
import org.olat.core.util.xml.XStreamHelper; |
|
import org.olat.repository.handlers.RepositoryHandler; |
|
import org.olat.repository.handlers.RepositoryHandlerFactory; |
|
|
|
import com.thoughtworks.xstream.XStream; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class RepositoryEntryImportExport { |
|
|
|
private static final Logger log = Tracing.createLoggerFor(RepositoryEntryImportExport.class); |
|
|
|
private static final String CONTENT_FILE = "repo.zip"; |
|
public static final String PROPERTIES_FILE = "repo.xml"; |
|
private static final String PROP_ROOT = "RepositoryEntryProperties"; |
|
private static final String PROP_SOFTKEY = "Softkey"; |
|
private static final String PROP_RESOURCENAME = "ResourceName"; |
|
private static final String PROP_DISPLAYNAME = "DisplayName"; |
|
private static final String PROP_DECRIPTION = "Description"; |
|
private static final String PROP_INITIALAUTHOR = "InitialAuthor"; |
|
|
|
private static final XStream xstream = XStreamHelper.createXStreamInstance(); |
|
static { |
|
XStreamHelper.allowDefaultPackage(xstream); |
|
xstream.alias(PROP_ROOT, RepositoryEntryImport.class); |
|
xstream.aliasField(PROP_SOFTKEY, RepositoryEntryImport.class, "softkey"); |
|
xstream.aliasField(PROP_RESOURCENAME, RepositoryEntryImport.class, "resourcename"); |
|
xstream.aliasField(PROP_DISPLAYNAME, RepositoryEntryImport.class, "displayname"); |
|
xstream.aliasField(PROP_DECRIPTION, RepositoryEntryImport.class, "description"); |
|
xstream.aliasField(PROP_INITIALAUTHOR, RepositoryEntryImport.class, "initialAuthor"); |
|
xstream.omitField(RepositoryEntryImport.class, "outer-class"); |
|
xstream.ignoreUnknownElements(); |
|
} |
|
|
|
|
|
private boolean propertiesLoaded = false; |
|
|
|
private RepositoryEntry re; |
|
private File baseDirectory; |
|
private RepositoryEntryImport repositoryProperties; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public RepositoryEntryImportExport(RepositoryEntry re, File baseDirecotry) { |
|
this.re = re; |
|
this.baseDirectory = baseDirecotry; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public RepositoryEntryImportExport(File baseDirectory) { |
|
this.baseDirectory = baseDirectory; |
|
} |
|
|
|
public RepositoryEntryImportExport(File baseDirectory, String subDir) { |
|
this.baseDirectory = new File(baseDirectory, subDir); |
|
} |
|
|
|
public boolean anyExportedPropertiesAvailable() { |
|
return new File(baseDirectory, PROPERTIES_FILE).exists(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean exportDoExport() { |
|
exportDoExportProperties(); |
|
return exportDoExportContent(); |
|
} |
|
|
|
public boolean exportDoExport(String zipPath, ZipOutputStream zout) { |
|
exportDoExportProperties(zipPath, zout); |
|
return exportDoExportContent(zipPath, zout); |
|
} |
|
|
|
public void exportDoExportProperties(String zipPath, ZipOutputStream zout) { |
|
|
|
RepositoryEntryImport imp = new RepositoryEntryImport(re); |
|
RepositoryManager rm = RepositoryManager.getInstance(); |
|
VFSLeaf image = rm.getImage(re); |
|
if(image instanceof LocalFileImpl) { |
|
imp.setImageName(image.getName()); |
|
ZipUtil.addFileToZip(ZipUtil.concat(zipPath, image.getName()) , ((LocalFileImpl)image).getBasefile(), zout); |
|
} |
|
|
|
RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class); |
|
VFSLeaf movie = repositoryService.getIntroductionMovie(re); |
|
if(movie instanceof LocalFileImpl) { |
|
imp.setMovieName(movie.getName()); |
|
ZipUtil.addFileToZip(ZipUtil.concat(zipPath, movie.getName()), ((LocalFileImpl)movie).getBasefile(), zout); |
|
} |
|
|
|
try(ShieldOutputStream fOut = new ShieldOutputStream(zout)) { |
|
addLicenseInformations(imp, re); |
|
zout.putNextEntry(new ZipEntry(ZipUtil.concat(zipPath, PROPERTIES_FILE))); |
|
xstream.toXML(imp, fOut); |
|
zout.closeEntry(); |
|
} catch (IOException ioe) { |
|
throw new OLATRuntimeException("Error writing repo properties.", ioe); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public void exportDoExportProperties() { |
|
|
|
try(FileOutputStream fOut = new FileOutputStream(new File(baseDirectory, PROPERTIES_FILE))) { |
|
RepositoryEntryImport imp = new RepositoryEntryImport(re); |
|
RepositoryManager rm = RepositoryManager.getInstance(); |
|
VFSLeaf image = rm.getImage(re); |
|
if(image instanceof LocalFileImpl) { |
|
imp.setImageName(image.getName()); |
|
FileUtils.copyFileToDir(((LocalFileImpl)image).getBasefile(), baseDirectory, ""); |
|
|
|
} |
|
|
|
RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class); |
|
VFSLeaf movie = repositoryService.getIntroductionMovie(re); |
|
if(movie instanceof LocalFileImpl) { |
|
imp.setMovieName(movie.getName()); |
|
FileUtils.copyFileToDir(((LocalFileImpl)movie).getBasefile(), baseDirectory, ""); |
|
} |
|
|
|
addLicenseInformations(imp, re); |
|
|
|
xstream.toXML(imp, fOut); |
|
} catch (IOException ioe) { |
|
throw new OLATRuntimeException("Error writing repo properties.", ioe); |
|
} |
|
} |
|
|
|
private void addLicenseInformations(RepositoryEntryImport imp, RepositoryEntry entry) { |
|
LicenseService licenseService = CoreSpringFactory.getImpl(LicenseService.class); |
|
ResourceLicense license = licenseService.loadLicense(entry.getOlatResource()); |
|
if (license != null) { |
|
imp.setLicenseTypeKey(String.valueOf(license.getLicenseType().getKey())); |
|
imp.setLicenseTypeName(license.getLicenseType().getName()); |
|
imp.setLicensor(license.getLicensor()); |
|
imp.setLicenseText(LicenseUIFactory.getLicenseText(license)); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean exportDoExportContent() { |
|
|
|
RepositoryHandler rh = RepositoryHandlerFactory.getInstance().getRepositoryHandler(re); |
|
MediaResource mr = rh.getAsMediaResource(re.getOlatResource()); |
|
try(FileOutputStream fOut = new FileOutputStream(new File(baseDirectory, CONTENT_FILE)); |
|
InputStream in = mr.getInputStream()) { |
|
if(in == null) { |
|
HttpServletResponse hres = new HttpServletResponseOutputStream(fOut); |
|
mr.prepare(hres); |
|
} else { |
|
IOUtils.copy(in, fOut); |
|
} |
|
fOut.flush(); |
|
} catch (IOException fnfe) { |
|
return false; |
|
} finally { |
|
mr.release(); |
|
} |
|
return true; |
|
} |
|
|
|
public boolean exportDoExportContent(String zipPath, ZipOutputStream zout) { |
|
|
|
RepositoryHandler rh = RepositoryHandlerFactory.getInstance().getRepositoryHandler(re); |
|
MediaResource mr = rh.getAsMediaResource(re.getOlatResource()); |
|
try(OutputStream fOut = new ShieldOutputStream(zout); |
|
InputStream in = mr.getInputStream()) { |
|
zout.putNextEntry(new ZipEntry(ZipUtil.concat(zipPath, CONTENT_FILE))); |
|
if(in == null) { |
|
HttpServletResponse hres = new HttpServletResponseOutputStream(fOut); |
|
mr.prepare(hres); |
|
} else { |
|
IOUtils.copy(in, fOut); |
|
} |
|
fOut.flush(); |
|
zout.closeEntry(); |
|
} catch (IOException fnfe) { |
|
return false; |
|
} finally { |
|
mr.release(); |
|
} |
|
return true; |
|
} |
|
|
|
public RepositoryEntry importContent(RepositoryEntry newEntry, VFSContainer mediaContainer) { |
|
if(!anyExportedPropertiesAvailable()) return newEntry; |
|
|
|
RepositoryManager repositoryManager = CoreSpringFactory.getImpl(RepositoryManager.class); |
|
if(StringHelper.containsNonWhitespace(getImageName())) { |
|
File newFile = new File(baseDirectory, getImageName()); |
|
VFSLeaf newImage = new LocalFileImpl(newFile); |
|
repositoryManager.setImage(newImage, newEntry); |
|
} |
|
if(StringHelper.containsNonWhitespace(getMovieName())) { |
|
String movieName = getMovieName(); |
|
String extension = FileUtils.getFileSuffix(movieName); |
|
File newFile = new File(baseDirectory, movieName); |
|
try(InputStream inStream = new FileInputStream(newFile)) { |
|
VFSLeaf movieLeaf = mediaContainer.createChildLeaf(newEntry.getKey() + "." + extension); |
|
VFSManager.copyContent(inStream, movieLeaf); |
|
} catch(IOException e) { |
|
log.error("", e); |
|
} |
|
} |
|
|
|
return setRepoEntryPropertiesFromImport(newEntry); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public RepositoryEntry setRepoEntryPropertiesFromImport(RepositoryEntry newEntry) { |
|
if(!propertiesLoaded) { |
|
loadConfiguration(); |
|
} |
|
|
|
importLicense(newEntry); |
|
|
|
RepositoryManager repositoryManager = CoreSpringFactory.getImpl(RepositoryManager.class); |
|
return repositoryManager.setDescriptionAndName(newEntry, newEntry.getDisplayname(), null, |
|
repositoryProperties.getAuthors(), repositoryProperties.getDescription(), |
|
repositoryProperties.getObjectives(), repositoryProperties.getRequirements(), |
|
repositoryProperties.getCredits(), repositoryProperties.getMainLanguage(), |
|
repositoryProperties.getLocation(), repositoryProperties.getExpenditureOfWork(), null, null, null); |
|
} |
|
|
|
private void importLicense(RepositoryEntry newEntry) { |
|
if(!propertiesLoaded) { |
|
loadConfiguration(); |
|
} |
|
LicenseService licenseService = CoreSpringFactory.getImpl(LicenseService.class); |
|
boolean hasLicense = StringHelper.containsNonWhitespace(repositoryProperties.getLicenseTypeName()); |
|
if (hasLicense) { |
|
String licenseTypeName = repositoryProperties.getLicenseTypeName(); |
|
LicenseType licenseType = licenseService.loadLicenseTypeByName(licenseTypeName); |
|
if (licenseType == null) { |
|
licenseType = licenseService.createLicenseType(licenseTypeName); |
|
licenseType.setText(repositoryProperties.getLicenseText()); |
|
licenseService.saveLicenseType(licenseType); |
|
} |
|
ResourceLicense license = licenseService.loadOrCreateLicense(newEntry.getOlatResource()); |
|
license.setLicenseType(licenseType); |
|
license.setLicensor(repositoryProperties.getLicensor()); |
|
if (licenseService.isFreetext(licenseType)) { |
|
license.setFreetext(repositoryProperties.getLicenseText()); |
|
} |
|
licenseService.update(license); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public File importGetExportedFile() { |
|
return new File(baseDirectory, CONTENT_FILE); |
|
} |
|
|
|
|
|
|
|
|
|
private void loadConfiguration() { |
|
if(baseDirectory != null && baseDirectory.exists()) { |
|
if(baseDirectory.getName().endsWith(".zip")) { |
|
try(FileSystem fs = FileSystems.newFileSystem(baseDirectory.toPath(), null)) { |
|
Path fPath = fs.getPath("/"); |
|
Path manifestPath = fPath.resolve("export").resolve(PROPERTIES_FILE); |
|
repositoryProperties = (RepositoryEntryImport)XStreamHelper.readObject(xstream, manifestPath); |
|
} catch(Exception e) { |
|
log.error("", e); |
|
} |
|
} else { |
|
File inputFile = new File(baseDirectory, PROPERTIES_FILE); |
|
if(inputFile.exists()) { |
|
repositoryProperties = (RepositoryEntryImport)XStreamHelper.readObject(xstream, inputFile); |
|
} else { |
|
repositoryProperties = new RepositoryEntryImport(); |
|
} |
|
} |
|
} else { |
|
repositoryProperties = new RepositoryEntryImport(); |
|
} |
|
propertiesLoaded = true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static RepositoryEntryImport getConfiguration(Path repoXmlPath) { |
|
try (InputStream in=Files.newInputStream(repoXmlPath)) { |
|
return (RepositoryEntryImport)xstream.fromXML(in); |
|
} catch(IOException e) { |
|
log.error("", e); |
|
return null; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static RepositoryEntryImport getConfiguration(InputStream repoMetaFileInputStream) { |
|
return (RepositoryEntryImport)xstream.fromXML(repoMetaFileInputStream); |
|
} |
|
|
|
|
|
|
|
|
|
public String getSoftkey() { |
|
if(!propertiesLoaded) { |
|
loadConfiguration(); |
|
} |
|
return repositoryProperties.getSoftkey(); |
|
} |
|
|
|
|
|
|
|
|
|
public String getDisplayName() { |
|
if(!propertiesLoaded) { |
|
loadConfiguration(); |
|
} |
|
return repositoryProperties.getDisplayname(); |
|
} |
|
|
|
|
|
|
|
|
|
public String getResourceName() { |
|
if(!propertiesLoaded) { |
|
loadConfiguration(); |
|
} |
|
return repositoryProperties.getResourcename(); |
|
} |
|
|
|
|
|
|
|
|
|
public String getDescription() { |
|
if(!propertiesLoaded) { |
|
loadConfiguration(); |
|
} |
|
return repositoryProperties.getDescription(); |
|
} |
|
|
|
|
|
|
|
|
|
public String getInitialAuthor() { |
|
if(!propertiesLoaded) { |
|
loadConfiguration(); |
|
} |
|
return repositoryProperties.getInitialAuthor(); |
|
} |
|
|
|
public String getMovieName() { |
|
if(!propertiesLoaded) { |
|
loadConfiguration(); |
|
} |
|
return repositoryProperties.getMovieName(); |
|
} |
|
|
|
public String getImageName() { |
|
if(!propertiesLoaded) { |
|
loadConfiguration(); |
|
} |
|
return repositoryProperties.getImageName(); |
|
} |
|
|
|
public class RepositoryEntryImport { |
|
|
|
private Long key; |
|
private String softkey; |
|
private String resourcename; |
|
private String displayname; |
|
private String description; |
|
private String initialAuthor; |
|
|
|
private String authors; |
|
private String mainLanguage; |
|
private String objectives; |
|
private String requirements; |
|
private String credits; |
|
private String expenditureOfWork; |
|
private String location; |
|
|
|
private String movieName; |
|
private String imageName; |
|
|
|
private String licenseTypeKey; |
|
private String licenseTypeName; |
|
private String licensor; |
|
private String licenseText; |
|
|
|
public RepositoryEntryImport() { |
|
|
|
} |
|
|
|
public RepositoryEntryImport(RepositoryEntry re) { |
|
key = re.getKey(); |
|
softkey = re.getSoftkey(); |
|
resourcename = re.getResourcename(); |
|
displayname = re.getDisplayname(); |
|
description = re.getDescription(); |
|
initialAuthor = re.getInitialAuthor(); |
|
|
|
authors = re.getAuthors(); |
|
mainLanguage = re.getMainLanguage(); |
|
objectives = re.getObjectives(); |
|
requirements = re.getRequirements(); |
|
credits = re.getCredits(); |
|
expenditureOfWork = re.getExpenditureOfWork(); |
|
} |
|
|
|
public Long getKey() { |
|
return key; |
|
} |
|
|
|
public void setKey(Long key) { |
|
this.key = key; |
|
} |
|
|
|
public String getMovieName() { |
|
return movieName; |
|
} |
|
|
|
public void setMovieName(String movieName) { |
|
this.movieName = movieName; |
|
} |
|
|
|
public String getImageName() { |
|
return imageName; |
|
} |
|
|
|
public void setImageName(String imageName) { |
|
this.imageName = imageName; |
|
} |
|
|
|
public String getSoftkey() { |
|
return softkey; |
|
} |
|
|
|
public void setSoftkey(String softkey) { |
|
this.softkey = softkey; |
|
} |
|
|
|
public String getResourcename() { |
|
return resourcename; |
|
} |
|
|
|
public void setResourcename(String resourcename) { |
|
this.resourcename = resourcename; |
|
} |
|
|
|
public String getDisplayname() { |
|
return displayname; |
|
} |
|
|
|
public void setDisplayname(String displayname) { |
|
this.displayname = displayname; |
|
} |
|
|
|
public String getDescription() { |
|
return description; |
|
} |
|
|
|
public void setDescription(String description) { |
|
this.description = description; |
|
} |
|
|
|
public String getInitialAuthor() { |
|
return initialAuthor; |
|
} |
|
|
|
public void setInitialAuthor(String initialAuthor) { |
|
this.initialAuthor = initialAuthor; |
|
} |
|
|
|
public String getAuthors() { |
|
return authors; |
|
} |
|
|
|
public void setAuthors(String authors) { |
|
this.authors = authors; |
|
} |
|
|
|
public String getMainLanguage() { |
|
return mainLanguage; |
|
} |
|
|
|
public void setMainLanguage(String mainLanguage) { |
|
this.mainLanguage = mainLanguage; |
|
} |
|
|
|
public String getLocation() { |
|
return location; |
|
} |
|
|
|
public void setLocation(String location) { |
|
this.location = location; |
|
} |
|
|
|
public String getObjectives() { |
|
return objectives; |
|
} |
|
|
|
public void setObjectives(String objectives) { |
|
this.objectives = objectives; |
|
} |
|
|
|
public String getRequirements() { |
|
return requirements; |
|
} |
|
|
|
public void setRequirements(String requirements) { |
|
this.requirements = requirements; |
|
} |
|
|
|
public String getCredits() { |
|
return credits; |
|
} |
|
|
|
public void setCredits(String credits) { |
|
this.credits = credits; |
|
} |
|
|
|
public String getExpenditureOfWork() { |
|
return expenditureOfWork; |
|
} |
|
|
|
public void setExpenditureOfWork(String expenditureOfWork) { |
|
this.expenditureOfWork = expenditureOfWork; |
|
} |
|
|
|
public String getLicenseTypeKey() { |
|
return licenseTypeKey; |
|
} |
|
|
|
public void setLicenseTypeKey(String licenseTypeKey) { |
|
this.licenseTypeKey = licenseTypeKey; |
|
} |
|
|
|
public String getLicenseTypeName() { |
|
return licenseTypeName; |
|
} |
|
|
|
public void setLicenseTypeName(String licenseTypeName) { |
|
this.licenseTypeName = licenseTypeName; |
|
} |
|
|
|
public String getLicensor() { |
|
return licensor; |
|
} |
|
|
|
public void setLicensor(String licensor) { |
|
this.licensor = licensor; |
|
} |
|
|
|
public String getLicenseText() { |
|
return licenseText; |
|
} |
|
|
|
public void setLicenseText(String licenseText) { |
|
this.licenseText = licenseText; |
|
} |
|
} |
|
} |