FileAsset Creation using the ContentletAPI

Description

This is a very straightforward demonstration JSP that will transactionally checking a FileAsset content using the 2.2 starter site content schema.  The content constructed as a Hashmap that is used to create the new File which is then checked in using standard ContentletAPI methods.

The example also demonstrates how to checkin a piece of content with a Binary Field as well.  

Code

<%
response.setContentType("text/plain");


// User to check in as
User user = APILocator.getUserAPI().getSystemUser();

// respect CMS Anonymous permissions
boolean respectFrontendRoles = false;

// Host on which to place the content
Host host = APILocator.getHostAPI().findDefaultHost(user, respectFrontendRoles);

// Folder on which to place the content, we will use the images / folder
Folder folder = APILocator.getFolderAPI().findFolderByPath("/images/", host, user, respectFrontendRoles);

// Language of the content
Language lang = APILocator.getLanguageAPI().getDefaultLanguage();

//Structure to checkin
Structure struc = StructureFactory.getStructureByVelocityVarName("FileAsset");

try{
	
	
	// begin transaction
	HibernateUtil.startTransaction();
	

	
	File f = new File("/tmp/tv.png");
	
	
	Map m = new HashMap();
	m.put("stInode", struc.getInode());
	m.put("host", host.getIdentifier());
	m.put("folder", folder.getInode()); // You need the inode of the folder (folders are not versioned)
	m.put("languageId", lang.getId());
	m.put("sortOrder", new Long(0));
	m.put("title", f.getName());
	m.put("fileName", f.getName());


	// binary field, pass the (local) file, which will be copied into the content repository
	m.put("fileAsset", f);
	
	
	
	
	
	
	// create a new piece of content backed by the map created above
	Contentlet content = new Contentlet(m);
	
	// check in the content
	content= APILocator.getContentletAPI().checkin(content,user, respectFrontendRoles);
	
	out.print("checkin contentlet: " + f.getName());
	
	// publish the content
	APILocator.getContentletAPI().publish(content, user, respectFrontendRoles);
	
	out.println("    published: "  + f.getName());
	


	

	
	// commit transaction
	HibernateUtil.commitTransaction();

}
catch(Exception e){
	out.println(e);
	HibernateUtil.rollbackTransaction();
	
}


%>
<%@page import="com.dotmarketing.db.HibernateUtil"%>
<%@page import="java.io.File"%>
<%@page import="com.dotmarketing.portlets.languagesmanager.model.Language"%>
<%@page import="com.dotmarketing.portlets.structure.factories.StructureFactory"%>
<%@page import="com.dotmarketing.portlets.structure.model.Structure"%>
<%@page import="com.dotmarketing.portlets.folders.model.Folder"%>
<%@page import="com.dotmarketing.beans.Host"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="com.dotmarketing.business.APILocator"%>
<%@page import="com.dotmarketing.portlets.contentlet.model.Contentlet"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>