Checkin Content from HashMap - Transactional


This is a very straightforward demonstration JSP that will transactionally checkin 10 pieces of "News" content using the 2.1 starter site content schema.  Each piece of content is constructed as a Hashmap that is used to create the new piece of content 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.  

Attachment: content-checkin-from-hashmap.jsp (3 KB)



// 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 ROOT / folder
Folder folder = APILocator.getFolderAPI().findSystemFolder();

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

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

	// begin transaction
	for(int i = 0;i<10;i++){
		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("story", "story " + i);
		m.put("title", "title "+i);
		m.put("byline", "byline" + i);
		m.put("sysPublishDate", new Date());
		m.put("lead", "My Lead " + i);
		m.put("caption", "Caption " + i);
		m.put("urlTitle", "urlTitle " + i);
		// binary field, pass the (local) file, which will be moved into the content repository
		m.put("image", new File("/tmp/test.jpg"));
		// 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: " + i);
		// publish the content
		APILocator.getContentletAPI().publish(content, user, respectFrontendRoles);
		out.println("    published: " + i);


	// commit transaction

catch(Exception e){

<%@page import="com.dotmarketing.db.HibernateUtil"%>
<%@page import=""%>
<%@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=""%>
<%@page import="com.dotmarketing.portlets.contentlet.model.Contentlet"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>