<<

. 53
( 87 .)



>>


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

try {
// Save the selected color to portlet data
String color = request.getParameter(COLOR);
if (color != null) {
PortletData portletData = request.getData();
portletData.setAttribute(COLOR, color);
portletData.store();
}
}
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",
P1: FCH/SPH P2: FCH/SPH QC: FCH/SPH T1: FCH
WY009-16 WY009-BenNatan-v1.cls May 11, 2004 14:50




Portlet Development 315


request.getLocale());
String msg = bundle.getString("exception.io");
throw new PollWrapperException(msg, e);
}
}
public void setView(PortletRequest request)
throws PollException {
setView(request, null);
}
}




Implementing the View Classes
The View classes are invoked during the portlet rendering phase. Typically
the view classes simply invoke the JSP. Beans needed to render the JSP are
most often created during the action phase processing and put in session
data for use by the JSP. You might be inclined to instead put these objects in
the request object during action processing; however, they would then not
be available on subsequent portal page refreshes when the portlet is asked
to render itself without explicit user interaction with the portlet and so no
action processing occurs.
The following view classes are used in this portlet:

Initial con¬gure view (ConfigureView)
Con¬gure view”page 2 (ConfigurePg2View)
Con¬gure view”page 3 (ConfigurePg3View)
Initial edit view (InitialEditView)
Initial view (MainView)

Only in the initial views do we need to execute processing logic. The Con-
figurePg2View and ConfigurePg3View simply invoke their respective
JSPs. Similarly, the initial edit view and the initial con¬gure view do not
require any processing prior to invoking their JSPs. The edit page simply
shows a static set of color choices from which the user can make a selection.
The initial con¬guration page shows static persistent store choices.
The main view is different however. This view requires that we get the
portlet settings to determine the poll name and the persistent store infor-
mation. Then we ask the Broker for the poll details and check if the user
has already voted. If so, we need to call the Broker again for the current
voting results. Since the voting totals are dynamic we want to refresh this
information when the portlet is asked to render itself not just when the user
is interacting with this portlet.
P1: FCH/SPH P2: FCH/SPH QC: FCH/SPH T1: FCH
WY009-16 WY009-BenNatan-v1.cls May 11, 2004 14:50




316 Chapter 16


We have looked at the code to generate the main view earlier. Here it is
again in complete form in the MainView class:
/**
* View - Poll view
*/

public class MainView implements View {

public void performView(PortletRequest request,
PortletResponse response, PortletContext portletContext)
throws IOException, PortletException, PollException {

// Is the portlet configured
validateConfiguration(request);

// Get the poll bean
PortletSettings portletSettings = request.getPortletSettings();
String pollName = portletSettings.getAttribute(POLL_NAME);
Broker broker = Broker.getInstance(portletSettings);
PollBean pollBean = broker.getPoll(pollName);

// If the poll was not found generate an error message
if (pollBean == null) {
ResourceBundle bundle = ResourceBundle.getBundle("nls.polling",
request.getLocale());
String msg = bundle.getString("exception.notfound");
throw new PollMessageException(msg);
}

// If the user has not already voted show poll question
// otherwise show poll results.
String user = request.getUser().getUserID();
boolean hasUserVoted = broker.hasUserVoted(user, pollName);

if (hasUserVoted) {

// Get customization color value from portlet data
PortletData portletData = request.getData();
String color = (String)portletData.getAttribute(COLOR);
request.setAttribute(COLOR, color);

pollBean.initializeResults(broker);
request.setAttribute("pollBean", pollBean);
portletContext.include(VIEW_RESULTS_JSP, request, response);
}
else {
request.setAttribute("pollBean", pollBean);
portletContext.include(VIEW_QUESTION_JSP, request, response);
}
}
}
P1: FCH/SPH P2: FCH/SPH QC: FCH/SPH T1: FCH
WY009-16 WY009-BenNatan-v1.cls May 11, 2004 14:50




Portlet Development 317


Implementing the Model Classes
Let™s start with the implementation of the model classes for the polling
portlet. There are two model objects that we should consider for this portlet.
The most obvious is the object to represent a poll (PollBean). In addition,
we need access to user information. For this, the User object is provided to
the portlet developer through the portlet API. If we ensure that our portlet
executes only on authenticated portal pages then we have access to the
User object that represents the user that is logged in to portal.
The PollBean object needs to implement the behavior that we have seen
so far. It needs to know its poll name, the poll question, and the collection of
answer choices. The Poll object should also know the total number of votes
cast, the number of votes cast for each answer choice, and the percentage
value of votes for each choice. We will also implement an initializ-
eResults method to explicitly initialize the votes cast information for the
poll.
The PollBean implementation is pretty straightforward. Create a pack-
age in the Poll Project under the JavaSource directory. In the package
create a new class called PollBean. The following listing shows most of
the class implementation. Some of the accessor methods have been deleted
in the interest of space.

public class PollBean implements Serializable, PollConstants {

// Poll attributes
protected String name = "";
protected String question = "";
protected String choices = "";

// Choice list
protected int numberOfChoices = 0;
protected List choiceList = null;

// Votes cast on this poll
protected int votesCast = 0;
protected List voteCountList = new ArrayList();
protected List votePercentageList = new ArrayList();

/**
* Set the current vote counts
*/
public void initializeResults(Broker broker) throws PollException {

votesCast = 0;
for (int i = 0; i < numberOfChoices; i++) {
int voteCount = broker.voteCount(name, i);
voteCountList.add(i, new Integer(voteCount));
P1: FCH/SPH P2: FCH/SPH QC: FCH/SPH T1: FCH
WY009-16 WY009-BenNatan-v1.cls May 11, 2004 14:50




318 Chapter 16

votesCast = votesCast + voteCount;
}
calcVotePercentages();
}

/**
* Calculate the voting percentages for each choice
*/
protected void calcVotePercentages() throws PollException {

int voteCount = 0;
int voteLargestPercent = 0;
int voteLargestIndex = 0;
int votePercent = 0;
int voteCumPercent = 0;

for (int i = 0; i < numberOfChoices; i++) {
voteCount = ((Integer) voteCountList.get(i)).intValue();
votePercent = (voteCount * 100) / votesCast;
votePercentageList.add(new Integer(votePercent));
voteCumPercent = voteCumPercent + votePercent;
if (votePercent > voteLargestPercent) {
voteLargestIndex = i;
voteLargestPercent = votePercent;
}
}

voteLargestPercent = voteLargestPercent + (100 - voteCumPercent);
votePercentageList.remove(voteLargestIndex);
votePercentageList.add(
voteLargestIndex,
new Integer(voteLargestPercent));
}

/**
* Sets the choices and parses the string
*
* @param choices The choices to set
*/
public void setChoices(String choices) {
this.choices = choices;
parseChoices();
}

/**
* Parse the choices string
*/
public void parseChoices() {

List choiceList = new ArrayList(3);
P1: FCH/SPH P2: FCH/SPH QC: FCH/SPH T1: FCH
WY009-16 WY009-BenNatan-v1.cls May 11, 2004 14:50




Portlet Development 319

int lastIndex = 0;
int index = choices.indexOf(DELIMITER, lastIndex);
while (index > 0) {
choiceList.add(choices.substring(lastIndex, index));
lastIndex = index + 1;
index = choices.indexOf(DELIMITER, lastIndex);
}

// Set the number of poll choices
this.numberOfChoices = choiceList.size();
this.choiceList = choiceList;
}

/**
* Add a poll choice

<<

. 53
( 87 .)



>>