[Building Sakai] Programmatic content-tool/resource manipulation question

Steve Swinsburg steve.swinsburg at gmail.com
Thu Jun 6 16:09:08 PDT 2013


Hi,

Contenthostingservice.addresource is what you are after. You then need to commitresource. I have pasted a code snippet below, ignore the local methods and look at the contenthosting parts.
Do you have the java docs?

ContentResourceEdit resource = null;
		
		String currentSiteCollectionId = contentHostingService.getSiteCollection(getCurrentSiteId());
		String baseName = FilenameUtils.getBaseName(lo.getFilename());
		String extension = FilenameUtils.getExtension(lo.getFilename());
		
		byte[] bytes = retrieveStashedFile(lo.getStashedFilePath());
		if(bytes == null) {
			log.error("Stashed file could not be retrieved, aborting");
			return false;
		}
		
		SecurityAdvisor advisor = enableSecurityAdvisor();
		
		try {
				
			resource = contentHostingService.addResource(currentSiteCollectionId, baseName, extension, 100);
			resource.setContent(bytes);
			resource.setContentType(lo.getMimetype());
			
			//add standard properties
			ResourceProperties props = resource.getPropertiesEdit();
			props.addProperty(ResourceProperties.PROP_CONTENT_TYPE, lo.getMimetype());
			
			//set displayName as filename, or fall back to actual filename
			if(StringUtils.isNotBlank(lo.getDisplayName())){
				props.addProperty(ResourceProperties.PROP_DISPLAY_NAME, lo.getDisplayName());
			} else {
				props.addProperty(ResourceProperties.PROP_DISPLAY_NAME, lo.getFilename());
			}
			props.addProperty(ResourceProperties.PROP_CREATOR, getCurrentUserDisplayName());
			props.addProperty(ResourceProperties.PROP_CREATION_DATE, getCurrentDateFormatted());
			
			//set version to 1 for new files
			lo.setVersion(1);
			
			//add general LO props
			addLearningObjectProperties(props, lo);
						
			contentHostingService.commitResource(resource, NotificationService.NOTI_NONE);
			return true;

		} catch (Exception e) {
			
			contentHostingService.cancelResource(resource);
			log.error("addNewLearningObject: failed: " + e.getClass() + " : " + e.getMessage());
			e.printStackTrace();
		} finally {
			disableSecurityAdvisor(advisor);
		}
		


Sent from my iPad

On 07/06/2013, at 8:13, Lydia Rice <lydia at sipx.com> wrote:

> Hi,
> I'm hunting for a means by which a Sakai tool can 
> programmatically inject items into the resources of a 
> course, but have so far failed to find a means to do so.  
> Any pointers would be very much appreciated.
> 
> To clarify what I'm looking to do a little, I have an 
> existing Sakai tool (which is not the built-in 
> content-tool), and I want the code in my tool to be able to 
> manipulate the resources associated with a course in a 
> manner analogous to what the user would achieve by futzing 
> with the resources of a course using the UI, (i.e. by 
> clicking on a Site tab, then Resources).  I had hoped to 
> find a piece of API lurking that would be functionally 
> equivalent to (say):
> 
>    ContentTool.upsertResource(<<courseId>>, 
> <<resourceId>>,  <<name>>, <<description>>, <<url>>);
> 
> (or perhaps findResource, createResource, updateResource, 
> deleteResource, ....)
> 
> Obviously, the content-tool's UI has a lot more features, so 
> any real API would take a bunch more parameters, and so 
> would be expected to be a little more complicated, but I 
> hope you get the picture.
> 
> I'm probably being dumb about this, but I've searched high 
> and low and haven't been able to find anything even remotely 
> approximating an API for resource manipulation.  I'm a Sakai 
> neophyte, so perhaps if I were more immersed in Sakai 
> culture the answer would be obvious, but I've come up with a 
> blank so far in all my poking around.
> 
> I'd be very grateful for any pointers,
> 
> 
> 
> 
> Lydia.
> _______________________________________________
> sakai-dev mailing list
> sakai-dev at collab.sakaiproject.org
> http://collab.sakaiproject.org/mailman/listinfo/sakai-dev
> 
> TO UNSUBSCRIBE: send email to sakai-dev-unsubscribe at collab.sakaiproject.org with a subject of "unsubscribe"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://collab.sakaiproject.org/pipermail/sakai-dev/attachments/20130607/0fdf84dc/attachment.html 


More information about the sakai-dev mailing list