<<

. 52
( 87 .)



>>


// Dispatch to the current View class for the current mode.
// The View class name was set in the Action phase or is the
// initial view.
// View for the current portlet mode.
View nextView = ViewClassManager.getView(request);
if (log.isDebugEnabled())
log.debug(Dispatching to view "+nextView.getClass().getName());
nextView.performView(request, response,
getPortletConfig().getContext());

}

The ViewClassManager simply retrieves the view class name from the
session object using the current portlet mode as the attribute name and
returns an instance of that class. If the attribute is not found on session then
the initial view class for the current mode is returned.


Implementing the Action Classes
You have seen how the Action classes get invoked from the actionPer-
formed method in the Poll portlet class. You have also seen how a speci¬c
Action class is identi¬ed by name through the PortletURI tag in the
P1: FCH/SPH P2: FCH/SPH QC: FCH/SPH T1: FCH
WY009-16 WY009-BenNatan-v1.cls May 11, 2004 14:50




310 Chapter 16


JSP and you have seen the implementation for the ConfigVerifyData-
sourceAction class.


Con¬guration Mode
The con¬guration mode of this portlet has most of the action event process-
ing. The view pages really have just one action and that is to register the
user™s vote. The edit mode we will see shortly also has only one action, to
save the user™s color preference for the results bar graph.
The con¬guration mode has the following actions to perform:

Cancel the con¬guration (CancelAction)
Add an answer choice (ConfigAddChoiceAction)
Delete an answer choice (ConfigDeleteChoiceAction)
Get user input and show next page (ConfigPg2Action)
Get user input and show next page (ConfigPg3Action)
Verify the datasource (ConfigVerifyDatasourceAction)
Save the con¬guration updates (ConfigSaveAction)

The ConfigAddChoiceAction class and the ConfigDeleteChoice-
Action class are very similar from a controller perspective. They simply
get the current form data object from session, get the index of the select
choice to be deleted or the choice string to be added, and call the appropri-
ate method on the PollBean object. Here is the code for the action to add
a choice.

/**
* Action - Handle the request to add a poll choice
*/
public class ConfigAddChoiceAction extends Action {
public void actionPerformed(PortletRequest request,
PortletContext portletContext) throws PollException {

// Get the form bean from session
SessionData sessionData = SessionData.getSessionData(request);
ConfigureFormData formData = sessionData.getConfigureFormData();

// Get the updated choice and question values and
// save them to the form data bean
String pollQuestion = request.getParameter(POLL_QUESTION);
formData.getPollBean().setQuestion(pollQuestion);
String newChoice = request.getParameter(POLL_CHOICE);
formData.getPollBean().addChoice(newChoice);
}
P1: FCH/SPH P2: FCH/SPH QC: FCH/SPH T1: FCH
WY009-16 WY009-BenNatan-v1.cls May 11, 2004 14:50




Portlet Development 311

public void setView(PortletRequest request)
throws PollException {
setView(request, CONFIG_P3_STATE);
}
}

The ConfigPg2Action class and the ConfigPg3Action class are also
very similar. They get the current form data object from session, update the
form data with the user settings from the request parameters, and update
the form data object for the next page and invoke the JSP.
The following listing shows the ConfigPg2Action class. The datastore
options from the ¬rst con¬guration page are retrieved and held in the form
data and a list of existing polls (by name) is prepared for use by the JSP for
the next con¬guration page. The ConfigPg3Action class is very similar
to ConfigPg2Action, so we will not show it here.
/**
* Action - Handle the request to move to the next
* configuration page.
*/

public class ConfigPg2Action extends Action {

public void actionPerformed(PortletRequest request,
PortletContext portletContext) throws PollException {

// Get configuration form data bean from session
SessionData sessionData = SessionData.getSessionData(request);
ConfigureFormData formData = sessionData.getConfigureFormData();

// Update the persistence type and the datasource from request
// parameters, validate the input.
formData.updateDbParms(request);

// Get the list of current polls (by name) from the data store
// Get a broker instance using the form data parameters
Broker broker = Broker.getInstance(formData.getPersistType(),
formData.getDatasource());
List pollNames = broker.getPollNames();

// Put it in form data object
formData.setPollNames(pollNames);

}

public void setView(PortletRequest request)
throws PollException {
setView(request, CONFIG_P2_STATE);
}
}
P1: FCH/SPH P2: FCH/SPH QC: FCH/SPH T1: FCH
WY009-16 WY009-BenNatan-v1.cls May 11, 2004 14:50




312 Chapter 16


The ¬nal action event is to save the con¬guration mode modi¬cations.
This is invoked from the last page and implemented in the ConfigSaveAc-
tion class. In the following listing you see that, like the other con¬guration
action classes, the form data is retrieved from session and updated with the
con¬guration values from the last page.
The poll name and the persistence options are saved to the portlet set-
tings. Keeping those values there allows concrete portlet instances each to
represent a different poll with potentially different persistent stores. The
poll information itself is saved to the persistent store.

/**
* Action - Handle the request to save the configuration
* changes
*/
public class ConfigSaveAction extends Action {

public void actionPerformed(PortletRequest request,
PortletContext portletContext) throws PollException {

try {

// Get form data and poll bean from session
SessionData sessionData = SessionData.getSessionData(request);
ConfigureFormData formData = sessionData.getConfigureFormData();
PollBean pollBean = formData.getPollBean();

// Get the question and save it to the form data bean
String pollQuestion = request.getParameter(POLL_QUESTION);
pollBean.setQuestion(pollQuestion);

// Update the portlet settings
PortletSettings portletSettings = request.getPortletSettings();
portletSettings.setAttribute(POLL_NAME, pollBean.getName());
portletSettings.setAttribute(PERSIST_TYPE,
formData.getPersistType());
portletSettings.setAttribute(DATASOURCE,
formData.getDatasource());

// Save the portlet settings
portletSettings.store();

// Save the poll info
Broker.getInstance(portletSettings).updatePoll(pollBean);

// Clean up session data
SessionData.removeFrom(request.getPortletSession());

}
P1: FCH/SPH P2: FCH/SPH QC: FCH/SPH T1: FCH
WY009-16 WY009-BenNatan-v1.cls May 11, 2004 14:50




Portlet Development 313

catch (AccessDeniedException e) {
ResourceBundle bundle = ResourceBundle.getBundle("nls.polling",
request.getLocale());
String msg = bundle.getString("exception.accessdenied");
throw new PollWrapperException(msg, e);
}
catch (IOException e) {
ResourceBundle bundle = ResourceBundle.getBundle("nls.polling",
request.getLocale());
String msg = bundle.getString("exception.io");
throw new PollWrapperException(msg, e);
}
}

public void setView(PortletRequest request)
throws PollException {
setView(request, null);
}
}



View Mode
The view mode has only one action class and that is to register the user™s
vote. That class simply gets the userid from the User object for the logged-
in user and retrieves the vote selection from the request parameter. It then
calls the Broker class to register the vote. The following listing shows the
VoteAction implementation.
/**
* Action - Handle the request to cast a vote
*/
public class VoteAction extends Action {
public void actionPerformed(PortletRequest request,
PortletContext portletContext) throws PollException {

// Get the userid and selected choice
String userid = request.getUser().getUserID();
String indexStr = request.getParameter(SELECTED_CHOICE);
int index = new Integer(indexStr).intValue();
// Register the vote
PortletSettings portletSettings = request.getPortletSettings();
String pollName = portletSettings.getAttribute(POLL_NAME);
Broker.getInstance(portletSettings).vote(userid, pollName, index);
}
public void setView(PortletRequest request)
throws PollException {
setView(request, VIEW_STATE);
}
}
P1: FCH/SPH P2: FCH/SPH QC: FCH/SPH T1: FCH
WY009-16 WY009-BenNatan-v1.cls May 11, 2004 14:50




314 Chapter 16




Figure 16-3 Edit mode.


Edit Mode
The ¬nal action class is for edit mode and it is the code to save the edit
customization information (see Figure 16-3). The customization data is user-
speci¬c and so it is stored in portlet data. The following listing shows the
implementation for the EditSaveAction class:
/**
* Action - Handle the request to save the edit customizations
*/
public class EditSaveAction extends Action {

<<

. 52
( 87 .)



>>