Friday, 31 October 2014

Exploring Dyanamic user interfaces in websphere portal8.0

Dynamic user interfaces :- Dynamic ui's are pages or portlets that are created from the definition of existing page or portlet. Dynamic uis can be created only from DynamicUiManager Api. The scope of this dynamic ui is to that session only these data is not persisted in database. This Dynamic ui can be destroyed before the session ends also.

Developing a dynamic UI configuration :-  Let us have a use-case of manager he has to approve leave. Process for approving the leave needs some clicks in the browser. If 10 people are applying for leave. Manager should go through everyone in webpage and then approve it. To make managers work as easy as possible we can get 10 virtual pages with each member request in corresponding page.

Step1 :-  Creating the extension node for dynamic pages.
    1. Create the page to be used for containing dynamic UIs.
    2. Assign a unique name{DynamicUiInterface} to the node. To do this, use the Unique names portlet or do it in edit page.
    3. Change to the directory was_profile_root/ConfigEngine.
    4. Run the following config task: 
      • Windows: ConfigEngine.bat action-enable-page-as-extension-node-wp.dynamicui.config -DPageUniqueName=DynamicUiInterface -DPortalAdminPwd=passw0rd_123

Step 2 :- Now you have to create a portlet and then add to DynamicUiInterface portlet page and then set below portlet preferences in it.

<portlet-preferences>
<preference>
<name>com.ibm.portal.context.enable</name>
<value>true</value>
</preference>
</portlet-preferences>

Step 3 :- Final portlet code should like below

package com.ibm.dynamicuiportlet;

import java.io.*;
import java.util.*;

import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.portlet.*;

import com.ibm.portal.ObjectID;
import com.ibm.portal.dynamicui.DynamicUICtrl;
import com.ibm.portal.dynamicui.DynamicUIManagementException;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.dynamicui.DynamicUIManagementFactoryService;
import com.ibm.portal.portlet.service.state.RedirectURLGeneratorFactoryService;
import com.ibm.portal.propertybroker.property.Direction;
import com.ibm.portal.propertybroker.property.PropertyController;
import com.ibm.portal.propertybroker.property.PropertyValue;
import com.ibm.portal.propertybroker.service.PropertyFactory;
import com.ibm.portal.state.EngineURL;
import com.ibm.portal.state.RedirectURLGenerator;
import com.ibm.portal.state.exceptions.StateException;
import java.util.logging.Logger;
import com.ibm.wps.engine.localized.LocalizedContextImpl;

/**
 * A sample portlet based on GenericPortlet
 */
public class DynamicUiPortlet extends GenericPortlet {

public static final String JSP_FOLDER    = "/_DynamicUiPortlet/jsp/";    // JSP folder name

public static final String VIEW_JSP      = "DynamicUiPortletView";         // JSP file name to be rendered on the view mode

public static final Logger logger = Logger.getLogger(DynamicUiPortlet.class.getName());

DynamicUIManagementFactoryService dynamicUIManagerFactoryService = null;
RedirectURLGeneratorFactoryService redirectService = null;
PropertyFactory propertyFactory = null;


/**
* @see javax.portlet.Portlet#init()
*/
public void init() throws PortletException{
super.init();
Context ctx;
try{
ctx = new InitialContext();
// Obtain a reference to the Dynamic UI Management Factory service
PortletServiceHome dynamicUIManagerFactoryServiceHome = (PortletServiceHome) 
ctx.lookup("portletservice/com.ibm.portal.portlet.service.dynamicui.DynamicUIManagementFactoryService"); 
dynamicUIManagerFactoryService = (DynamicUIManagementFactoryService) 
dynamicUIManagerFactoryServiceHome.getPortletService(DynamicUIManagementFactoryService.class);

// Obtain a reference to the property factory
PortletServiceHome serviceHome = (PortletServiceHome) 
ctx.lookup("portletservice/com.ibm.portal.propertybroker.service.PropertyFactory"); 
propertyFactory
(PropertyFactory)serviceHome.getPortletService(com.ibm.portal.propertybroker.service.PropertyFactory.class);

// If the dynamic UI should be displayed immediately upon launch,
// obtain a reference to the RedirectURLGeneratorFactory service 
PortletServiceHome redirectServiceHome = (PortletServiceHome) 
ctx.lookup("portletservice/com.ibm.portal.portlet.service.state.RedirectURLGeneratorFactoryService");
redirectService = (RedirectURLGeneratorFactoryService) 
redirectServiceHome.getPortletService(RedirectURLGeneratorFactoryService.class);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 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 {
String specialAction = request.getParameter("com.ibm.portal.action");
PortletSession portletSession = request.getPortletSession();

if (specialAction != null &&
specialAction.equals("com.ibm.portal.context.receive")) 
{
//this indicates context was passed to the launched page
Map contextMap = (Map)
request.getAttribute("com.ibm.portal.context");

logger.info("Map object :-"+ contextMap.toString());

Object propertyValue =  (Object) contextMap.get("");

portletSession.setAttribute("", propertyValue);

}


try {
//Retrieving portlet object id
Context ctx= new InitialContext();
Name portletName = 
new CompositeName("portal:config/portletdefinition");
portletName.add("com.ibm.dynamicuiportlet.DynamicUiPortlet.a176350594");
portletName.add("DynamicUiPortlet");  
ObjectID portletDefOID = (ObjectID) ctx.lookup(portletName);

//Obtain the dynamic control
DynamicUICtrl DynamicUICtrl = 
dynamicUIManagerFactoryService.getDynamicUICtrl(request, response, "DynamicUiPortlet");

//get the page Id
Name uniqueName = new CompositeName("portal:uniquename");
uniqueName.add("DynamicUiInterface");//Current page uniquename
ObjectID oidForUniqueName = (ObjectID) ctx.lookup(uniqueName);

//launch page or portlet using addPage or addPortlet
ObjectID newObjectId = DynamicUICtrl.addPage(oidForUniqueName, null, null);

//Navigating user to newly created page
RedirectURLGenerator redirector = 
redirectService.getURLGenerator(request, response);
EngineURL redirectURL = redirector.createPortletURL(portletDefOID);
response.sendRedirect(redirectURL.toString());
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DynamicUIManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StateException 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";
}


}

Comment :- This code is not dev ready it just implementation stage. even there are open PMRs

No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...