The Controller SPI provides controllers for portal resources. You can use these controllers to modify portal resources that are exposed by particular models of the Model SPI. Controllers offer methods to modify the topology and properties of the model and of its nodes. They expose the same interfaces as the corresponding read-only model, and they instantly reflect modifications that you apply to the controller.
Content Model Controller
Content Model Controller
This allows you to modify the content topology and the properties of content nodes such as pages, labels, and content URLs.
Note: If you modify the content topology, this might also change the navigation of your portal for your users.
Note: If you modify the content topology, this might also change the navigation of your portal for your users.
Layout Model Controller
This allows you to modify the layout of a page, such as the topology of layout elements of a page, and the properties of layout elements such as layout containers and layout controls.
Portlet Model Controller
This allows you to create, update and delete portlets.
Note: At this time there is no controller for the following models:
- NavigationModel. This is by implication of the structure of the content model.
- NavigationSelectionModel. This is computed from the navigational state per request.
- LanguageList, MarkupList, SkinList, and ThemeList.
I create a page called Custom Page under "LOT 915" page and also i read the portlet setting of LOT 915 page.
package com.ibm.controllermodel;
import java.io.*;
import java.util.Iterator;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.*;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import com.ibm.portal.ListModel;
import com.ibm.portal.Locator;
import com.ibm.portal.ModelException;
import com.ibm.portal.content.ContentModel;
import com.ibm.portal.model.ContentModelHome;
import com.ibm.portal.model.ContentModelProvider;
import com.ibm.portal.model.PortletModelProvider;
import com.ibm.portal.content.ContentModelController;
import com.ibm.portal.model.controller.ContentModelControllerHome;
import com.ibm.portal.model.controller.CreationContextBuilderFactory;
import com.ibm.portal.model.controller.PortletModelControllerHome;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portletmodel.PortletDefinition;
import com.ibm.portal.portletmodel.PortletEntity;
import com.ibm.portal.portletmodel.PortletModel;
import com.ibm.portal.portletmodel.PortletWindow;
import com.ibm.portal.portletmodel.WebApplication;
import com.ibm.portal.content.ContentPageCreationContext;
import com.ibm.portal.content.ControlLocator;
import com.ibm.portal.content.LayoutControl;
import com.ibm.portal.content.LayoutModel;
import com.ibm.portal.content.LayoutModelController;
import com.ibm.portal.content.ModifiableContentPage;
import com.ibm.portal.content.ContentNode;
import com.ibm.portal.content.ContentPage;
import com.ibm.portal.cp.tagging.TagModelControllerProvider;
import com.ibm.wps.cp.tagging.TagModelControllerImpl;
/**
* A sample portlet based on GenericPortlet
*/
public class ControllerModelPortlet extends GenericPortlet {
public static final String JSP_FOLDER = "/_ControllerModel/jsp/"; // JSP folder name
public static final String VIEW_JSP = "ControllerModelPortletView"; // JSP file name to be rendered on the view mode
/**
* @see javax.portlet.Portlet#init()
*/
public void init() throws PortletException{
super.init();
}
/**
* Serve up the <code>view</code> mode.
*
* @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
public void doView(RenderRequest request, RenderResponse response) throws PortletException,IOException {
// Set the MIME type for the render response
response.setContentType(request.getResponseContentType());
// Invoke the JSP to render
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
rd.include(request,response);
}
/**
* Process an action request.
*
* @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
*/
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
createPrivatePage( request, response);
getLayout(request,response);
try {
getPortlet(request,response);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Returns JSP file path.
*
* @param request Render request
* @param jspFile JSP file name
* @return JSP file path
*/
private static String getJspFilePath(RenderRequest request, String jspFile) {
String markup = request.getProperty("wps.markup");
if( markup == null )
markup = getMarkup(request.getResponseContentType());
return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);
}
/**
* Convert MIME type to markup name.
*
* @param contentType MIME type
* @return Markup name
*/
private static String getMarkup(String contentType) {
if( "text/vnd.wap.wml".equals(contentType) )
return "wml";
else
return "html";
}
/**
* Returns the file extension for the JSP file
*
* @param markupName Markup name
* @return JSP extension
*/
private static String getJspExtension(String markupName) {
return "jsp";
}
private void createPrivatePage(ActionRequest request,ActionResponse response)
{
ContentModelController ctrl = null;
CreationContextBuilderFactory factory = null;
ContentPageCreationContext ctx=null;;
ModifiableContentPage newPage=null;
ContentNode parent=null;
try {
ctrl = getController(request, response);
factory=CreationContextBuilderFactory.getInstance();
// Use the context to create the private page
ctx = factory.newContentPageCreationContext(true);
newPage= (ModifiableContentPage) ctrl.create(ContentPage.class, ctx);
newPage.setTitle(request.getLocale(),"Custom Page");
// Insert the new page under the node with unique name “lot915”
parent = (ContentNode)ctrl.getLocator().findByUniqueName("lot915");
if (parent != null) {
ctrl.insert(newPage, parent, null);
// Commit, i. e. persist the changes.
ctrl.commit();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
if(null!=ctrl)
// Dispose the Controller.
ctrl.dispose();
}
}
private ContentModelController getController(PortletRequest request,PortletResponse response)
{
ContentModel aContentmodel = null;
ContentModelController ctrl = null;
ContentModelControllerHome home=null;
try {
Context ictx = new InitialContext();
ContentModelHome homea = (ContentModelHome)ictx.lookup("portal:service/model/ContentModel");
if (homea != null)
{
ContentModelProvider provider = homea.getContentModelProvider();
aContentmodel =provider.getContentModel((ServletRequest) request, (ServletResponse) response);
}
home= (ContentModelControllerHome)
ictx.lookup(ContentModelControllerHome.CONTENT_MODEL_CONTROLLER_JNDI_NAME);
if (home != null)
{
ctrl = home.getContentModelControllerProvider().createContentModelController(aContentmodel);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return ctrl;
}
private void getLayout(PortletRequest request,PortletResponse response)
{
ContentModelController ctrl = null;
ContentPage Page=null;
LayoutModelController lmController =null;
try{
ctrl = getController(request, response);
Page = (ContentPage)ctrl.getLocator().findByUniqueName("lot915");
lmController=ctrl.getLayoutModelController(Page);
for (java.util.Iterator it = lmController.getAllAllowedPortlets().iterator(); it.hasNext(); ) {
System.out.println("Element: " + it.next());
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
if(null!=ctrl)
// Dispose the Controller.
ctrl.dispose();
}
}
private void getPortlet(PortletRequest request,PortletResponse response) throws NamingException, ModelException
{
System.out.println("Entering into getPortlet");
Context ctx = new InitialContext();
ContentModelHome psh = (ContentModelHome) (ContentModelHome)ctx.lookup("portal:service/model/ContentModel");
PortletModelControllerHome home = (PortletModelControllerHome) ctx.lookup(PortletModelControllerHome.PORTLET_MODEL_CONTROLLER_JNDI_NAME);
ContentModelProvider contentModelProvider = (ContentModelProvider) psh.getContentModelProvider();
ContentModel contentModel = contentModelProvider.getContentModel((ServletRequest) request, (ServletResponse) response);
Locator locator = contentModel.getLocator();
ContentPage cPage = (ContentPage)locator.findByUniqueName("lot915");
// System.out.println("got the locator from model");
// System.out.println("got the modifable portlet definiton");
LayoutModel lModel = contentModel.getLayoutModel(cPage);
ControlLocator cLocator = (ControlLocator) lModel.getLocator();
ListModel allPortlets = cLocator.findAllControls();
LayoutControl lControl = null;
Iterator controlIter = allPortlets.iterator();
while ( controlIter.hasNext()) {
lControl = (LayoutControl ) controlIter.next();
System.out.println(lControl.getTemplateName());
}
/*PortletServiceHome psHomeModel = null;
PortletModelProvider modelProvider = null;
if (psHomeModel == null ){
//Context ctx2= new InitialContext();
psHomeModel = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.portlet.service.model.PortletModelProvider");
}
modelProvider = (PortletModelProvider) psHomeModel.getPortletService(PortletModelProvider.class);
Iterator controlIter = allPortlets.iterator();
LayoutControl lControl = null;
System.out.println("just befor while loop");
while ( controlIter.hasNext()) {
System.out.println("inside while loop");
lControl = (LayoutControl ) controlIter.next();
PortletModel pModel = (PortletModel) modelProvider.getAdminPortletModel((ServletRequest) request, (ServletResponse) response);
if (home != null) {
result = home.getPortletModelControllerProvider().createPortletModelController(pModel);
}
PortletWindow pWindow2 = pModel.getPortletWindow(lControl);
PortletDefinition pDefinition = pModel.getPortletDefinition(pWindow2);
Modifiable modifiable = result.getModifiableNode(pDefinition);
ModifiablePortletPreferences preferences = ((ModifiablePortletDefinition)modifiable).getModif iablePortletPreferencesLayer();
System.out.println("THI IS FROM MODIFIABLE REFERENCE"+preferences.getStringValue("view.page") );
if(preferences.getStringValue("view.page")!=null)
{
System.out.println("VIEW.PAGE NOT NULL %%%%%%");
System.out.println("THIS PREFERENCE CAN BE REMOVED??"+preferences.confirmRemoveName("view.pag e"));
System.out.println("THIS VALUE CAN BE SET?? "+preferences.confirmSetStringValue("view.page","n ewvalue" ));
preferences.setStringValue("view.page", "newvalue");
//System.out.println("REMOVE"+preferences.removeName s());
System.out.println("NEW VALUE AFTER SETTING"+preferences.getStringValue("view.page"));//getting the updated value
}
PortletPreferences preferenceLayer=(PortletPreferences) pDefinition.getPortletPreferencesLayer();
//PortletPreferences preferenceLayer2=pDefinition.getPortletPreferences Layer();
if(preferenceLayer!=null)
{
System.out.println("NOT NULL NORMAL" +preferenceLayer.getStringValue("view.page"));//I'm getting the previous value here
}*/
}
}
After Click of Create Private Page
Result of Portlet Code:
[9/4/11 3:04:14:622 PDT] 0000003d SystemOut O Entering into getPortlet
[9/4/11 3:04:14:638 PDT] 0000003d SystemOut O Control
[9/4/11 3:04:14:638 PDT] 0000003d SystemOut O Control
[9/4/11 3:04:14:638 PDT] 0000003d SystemOut O Control
[9/4/11 3:04:14:638 PDT] 0000003d SystemOut O Control
[9/4/11 3:04:14:638 PDT] 0000003d SystemOut O Control
Click here to download the code
No comments:
Post a Comment