Skip to content
Snippets Groups Projects
Commit e2bdb79a authored by srosse's avatar srosse
Browse files

OO-990: refactor import of resources in REST API to match the new import...

OO-990: refactor import of resources in REST API to match the new import mechanism in authoring environment
parent 41b7a44b
No related branches found
No related tags found
No related merge requests found
...@@ -34,7 +34,6 @@ import org.olat.core.gui.control.Controller; ...@@ -34,7 +34,6 @@ import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.WindowControl; import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.media.MediaResource; import org.olat.core.gui.media.MediaResource;
import org.olat.core.id.OLATResourceable; import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.AssertException;
import org.olat.core.logging.OLATRuntimeException; import org.olat.core.logging.OLATRuntimeException;
import org.olat.core.manager.BasicManager; import org.olat.core.manager.BasicManager;
import org.olat.core.util.FileUtils; import org.olat.core.util.FileUtils;
...@@ -43,27 +42,7 @@ import org.olat.core.util.coordinate.CoordinatorManager; ...@@ -43,27 +42,7 @@ import org.olat.core.util.coordinate.CoordinatorManager;
import org.olat.core.util.coordinate.SyncerCallback; import org.olat.core.util.coordinate.SyncerCallback;
import org.olat.core.util.vfs.VFSContainer; import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSItem; import org.olat.core.util.vfs.VFSItem;
import org.olat.fileresource.types.AddingResourceException;
import org.olat.fileresource.types.AnimationFileResource;
import org.olat.fileresource.types.BlogFileResource;
import org.olat.fileresource.types.DocFileResource;
import org.olat.fileresource.types.FileResource; import org.olat.fileresource.types.FileResource;
import org.olat.fileresource.types.GlossaryResource;
import org.olat.fileresource.types.ImageFileResource;
import org.olat.fileresource.types.ImsCPFileResource;
import org.olat.fileresource.types.MovieFileResource;
import org.olat.fileresource.types.PdfFileResource;
import org.olat.fileresource.types.PodcastFileResource;
import org.olat.fileresource.types.PowerpointFileResource;
import org.olat.fileresource.types.ScormCPFileResource;
import org.olat.fileresource.types.SharedFolderFileResource;
import org.olat.fileresource.types.SoundFileResource;
import org.olat.fileresource.types.WikiResource;
import org.olat.fileresource.types.XlsFileResource;
import org.olat.ims.qti.fileresource.SurveyFileResource;
import org.olat.ims.qti.fileresource.TestFileResource;
import org.olat.modules.cp.CPOfflineReadableManager;
import org.olat.portfolio.EPTemplateMapResource;
import org.olat.resource.OLATResource; import org.olat.resource.OLATResource;
import org.olat.resource.OLATResourceManager; import org.olat.resource.OLATResourceManager;
...@@ -91,116 +70,6 @@ public class FileResourceManager extends BasicManager { ...@@ -91,116 +70,6 @@ public class FileResourceManager extends BasicManager {
return INSTANCE; return INSTANCE;
} }
/**
* Add a file resource if the resource type is already known.
*
* @param fResource
* @param newName
* @param knownResource maybe null, FileResource type will be calculated
* @return True upon success, false otherwise.
*/
private FileResource addFileResource(File fResource, String newName, FileResource knownResource) {
// ZIPDIR is a reserved name... check
if (fResource.getName().equals(ZIPDIR)) throw new AssertException("Trying to add FileResource with reserved name '" + ZIPDIR + "'.");
FileResource tempFr = new FileResource();
if (knownResource != null) tempFr = knownResource;
// move file to its new place
File fResourceFileroot = getFileResourceRoot(tempFr);
if (!FileUtils.copyFileToDir(fResource, fResourceFileroot, "add file resource")) return null;
if (!fResource.getName().equals(newName)) { // need to rename file to new
// name
File fNewName = new File(fResourceFileroot, newName);
if (!new File(fResourceFileroot, fResource.getName()).renameTo(fNewName)) {
FileUtils.deleteDirsAndFiles(fResourceFileroot, true, true);
return null;
}
fResource = fNewName;
}
if (knownResource == null) {
// save resourceableID
Long resourceableId = tempFr.getResourceableId();
// extract type
if (DocFileResource.validate(fResource)) tempFr = new DocFileResource();
else if (XlsFileResource.validate(fResource)) tempFr = new XlsFileResource();
else if (PowerpointFileResource.validate(fResource)) tempFr = new PowerpointFileResource();
else if (PdfFileResource.validate(fResource)) tempFr = new PdfFileResource();
else if (ImageFileResource.validate(fResource)) tempFr = new ImageFileResource();
else if (MovieFileResource.validate(fResource)) tempFr = new MovieFileResource();
else if (SoundFileResource.validate(fResource)) tempFr = new SoundFileResource();
else if (AnimationFileResource.validate(fResource)) tempFr = new AnimationFileResource();
else if (SharedFolderFileResource.validate(fResource)) tempFr = new SharedFolderFileResource();
// add a wiki copy
else if (WikiResource.validate(fResource, null).isValid()) tempFr = new WikiResource();
// add a podcast copy
else if (PodcastFileResource.validate(fResource)) tempFr = new PodcastFileResource(fResourceFileroot, fResource);
// add a blog copy
else if (BlogFileResource.validate(fResource)) tempFr = new BlogFileResource(fResourceFileroot, fResource);
// add a glossary copy
else if (GlossaryResource.validate(fResource)) tempFr = new GlossaryResource();
//add portfolio copy
else if (EPTemplateMapResource.validate(fResource)) tempFr = new EPTemplateMapResource();
// the following types need unzippedDir
else if (fResource.getName().toLowerCase().endsWith(".zip")) {
File fUnzippedDir = unzipFileResource(tempFr);
if (fUnzippedDir == null) {
// in case of failure we forward the error message
return null;
}
if (TestFileResource.validate(fUnzippedDir)) tempFr = new TestFileResource();
else if (WikiResource.validate(fUnzippedDir, null).isValid()) tempFr = new WikiResource();
else if (PodcastFileResource.validate(fUnzippedDir)) tempFr = new PodcastFileResource(fResourceFileroot, fUnzippedDir);
else if (BlogFileResource.validate(fUnzippedDir)) tempFr = new BlogFileResource(fResourceFileroot, fUnzippedDir);
else if (SurveyFileResource.validate(fUnzippedDir)) tempFr = new SurveyFileResource();
// CP must be later entry... Test- and SurveyFileResource may contain
// imsmanifest.xml as well
else if (ImsCPFileResource.validate(fUnzippedDir)) tempFr = new ImsCPFileResource();
// scorm and cp now can throw an exception which helps to show a
// better error message in case
// of a failure in adding a new resource
else if (ScormCPFileResource.validate(fUnzippedDir)) tempFr = new ScormCPFileResource();
// glossary resources are packaged within zip for import/export
else if (GlossaryResource.validate(fUnzippedDir)) tempFr = new GlossaryResource();
// portfolio template are packaged within zip for import/export
else if (EPTemplateMapResource.validate(fUnzippedDir)) {
tempFr = new EPTemplateMapResource();
}
else {
// just a generic ZIP file... we can delete the temporary unziped
// dir...
return null;
}
}
tempFr.overrideResourceableId(resourceableId);
}
// add olat resource
OLATResourceManager rm = OLATResourceManager.getInstance();
OLATResource ores = rm.findOrPersistResourceable(tempFr);
// make IMS-Content-Packaging offline readable adding a html-frameset
if (tempFr instanceof ImsCPFileResource) {
CPOfflineReadableManager cporm = CPOfflineReadableManager.getInstance();
cporm.makeCPOfflineReadable(ores, newName);
}
return tempFr;
}
/**
* @param fResource
* @param newName
* @return Newly created file resource
*/
public FileResource addFileResource(File fResource, String newName) throws AddingResourceException {
return addFileResource(fResource, newName, null);
}
/** /**
* @param res * @param res
*/ */
......
...@@ -34,6 +34,7 @@ import java.net.URI; ...@@ -34,6 +34,7 @@ import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
...@@ -56,19 +57,18 @@ import org.olat.basesecurity.BaseSecurity; ...@@ -56,19 +57,18 @@ import org.olat.basesecurity.BaseSecurity;
import org.olat.basesecurity.BaseSecurityManager; import org.olat.basesecurity.BaseSecurityManager;
import org.olat.core.CoreSpringFactory; import org.olat.core.CoreSpringFactory;
import org.olat.core.id.Identity; import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.core.id.Roles; import org.olat.core.id.Roles;
import org.olat.core.logging.OLog; import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing; import org.olat.core.logging.Tracing;
import org.olat.core.util.StringHelper; import org.olat.core.util.StringHelper;
import org.olat.fileresource.FileResourceManager; import org.olat.core.util.i18n.I18nModule;
import org.olat.fileresource.types.FileResource; import org.olat.fileresource.types.ResourceEvaluation;
import org.olat.repository.RepositoryEntry; import org.olat.repository.RepositoryEntry;
import org.olat.repository.RepositoryManager; import org.olat.repository.RepositoryManager;
import org.olat.repository.RepositoryService; import org.olat.repository.RepositoryService;
import org.olat.repository.handlers.RepositoryHandler;
import org.olat.repository.handlers.RepositoryHandlerFactory;
import org.olat.repository.model.SearchRepositoryEntryParameters; import org.olat.repository.model.SearchRepositoryEntryParameters;
import org.olat.resource.OLATResource;
import org.olat.resource.OLATResourceManager;
import org.olat.restapi.security.RestSecurityHelper; import org.olat.restapi.security.RestSecurityHelper;
import org.olat.restapi.support.MediaTypeVariants; import org.olat.restapi.support.MediaTypeVariants;
import org.olat.restapi.support.MultipartReader; import org.olat.restapi.support.MultipartReader;
...@@ -324,39 +324,48 @@ public class RepositoryEntriesResource { ...@@ -324,39 +324,48 @@ public class RepositoryEntriesResource {
return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
} }
private RepositoryEntry importFileResource(Identity identity, File fResource, String resourcename, String displayname, private RepositoryEntry importFileResource(Identity identity, File fResource, String resourcename,
String softkey, int access) { String displayname, String softkey, int access) {
RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class);
RepositoryHandlerFactory handlerFactory = CoreSpringFactory.getImpl(RepositoryHandlerFactory.class);
try { try {
FileResourceManager frm = FileResourceManager.getInstance(); RepositoryHandler handler = null;
FileResource newResource = frm.addFileResource(fResource, fResource.getName()); for(String type:handlerFactory.getSupportedTypes()) {
return importResource(identity, newResource, resourcename, displayname, softkey, access); RepositoryHandler h = handlerFactory.getRepositoryHandler(type);
ResourceEvaluation eval = h.acceptImport(fResource, fResource.getName());
if(eval != null && eval.isValid()) {
handler = h;
break;
}
}
RepositoryEntry addedEntry = null;
if(handler != null) {
Locale locale = I18nModule.getDefaultLocale();
addedEntry = handler.importResource(identity, null, displayname,
"", true, locale, fResource, fResource.getName());
if(StringHelper.containsNonWhitespace(resourcename)) {
addedEntry.setResourcename(resourcename);
}
if(StringHelper.containsNonWhitespace(softkey)) {
addedEntry.setSoftkey(softkey);
}
if(access < RepositoryEntry.ACC_OWNERS || access > RepositoryEntry.ACC_USERS_GUESTS) {
addedEntry.setAccess(RepositoryEntry.ACC_OWNERS);
} else {
addedEntry.setAccess(access);
}
addedEntry = repositoryService.update(addedEntry);
}
return addedEntry;
} catch(Exception e) { } catch(Exception e) {
log.error("Fail to import a resource", e); log.error("Fail to import a resource", e);
throw new WebApplicationException(e); throw new WebApplicationException(e);
} }
} }
public static RepositoryEntry importResource(Identity identity, OLATResourceable newResource, String resourcename, String displayname,
String softkey, int access) {
RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class);
OLATResource ores = OLATResourceManager.getInstance().findOrPersistResourceable(newResource);
RepositoryEntry addedEntry = repositoryService.create(identity, null, resourcename, displayname, null, ores, 0);
if(StringHelper.containsNonWhitespace(softkey)) {
addedEntry.setSoftkey(softkey);
}
//TODO repository
// Do set access for owner at the end, because unfinished course should be invisible
if(access < RepositoryEntry.ACC_OWNERS || access > RepositoryEntry.ACC_USERS_GUESTS) {
addedEntry.setAccess(RepositoryEntry.ACC_OWNERS);
} else {
addedEntry.setAccess(access);
}
repositoryService.update(addedEntry);
return addedEntry;
}
@Path("{repoEntryKey}") @Path("{repoEntryKey}")
public RepositoryEntryResource getRepositoryEntryResource() { public RepositoryEntryResource getRepositoryEntryResource() {
......
...@@ -137,7 +137,7 @@ public class RepositoryEntriesTest extends OlatJerseyTestCase { ...@@ -137,7 +137,7 @@ public class RepositoryEntriesTest extends OlatJerseyTestCase {
@Test @Test
public void testGetEntry() throws IOException, URISyntaxException { public void testGetEntry() throws IOException, URISyntaxException {
RepositoryEntry re = createRepository("Rei Ayanami", "Test GET repo entry"); RepositoryEntry re = createRepository("Test GET repo entry");
RestConnection conn = new RestConnection(); RestConnection conn = new RestConnection();
assertTrue(conn.login("administrator", "openolat")); assertTrue(conn.login("administrator", "openolat"));
...@@ -154,7 +154,7 @@ public class RepositoryEntriesTest extends OlatJerseyTestCase { ...@@ -154,7 +154,7 @@ public class RepositoryEntriesTest extends OlatJerseyTestCase {
@Test @Test
public void testGetEntry_managed() throws IOException, URISyntaxException { public void testGetEntry_managed() throws IOException, URISyntaxException {
RepositoryEntry re = createRepository("Rei Ayanami", "Test GET repo entry"); RepositoryEntry re = createRepository("Test GET repo entry");
re.setManagedFlagsString("all"); re.setManagedFlagsString("all");
re = dbInstance.getCurrentEntityManager().merge(re); re = dbInstance.getCurrentEntityManager().merge(re);
dbInstance.commitAndCloseSession(); dbInstance.commitAndCloseSession();
...@@ -737,7 +737,7 @@ public class RepositoryEntriesTest extends OlatJerseyTestCase { ...@@ -737,7 +737,7 @@ public class RepositoryEntriesTest extends OlatJerseyTestCase {
} }
} }
private RepositoryEntry createRepository(String owner, String name) { private RepositoryEntry createRepository(String name) {
OLATResourceManager rm = OLATResourceManager.getInstance(); OLATResourceManager rm = OLATResourceManager.getInstance();
// create course and persist as OLATResourceImpl // create course and persist as OLATResourceImpl
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment