001: package com.sun.portal.sample.j2ee.poll.provider;
002:
003: import java.util.List;
004: import com.sun.portal.providers.context.ProviderContext;
005: import com.sun.portal.providers.jsp.JSPProvider;
006: import com.sun.portal.providers.ProviderException;
007:
008: import com.sun.portal.sample.j2ee.poll.ejb.Poll;
009: import com.sun.portal.sample.j2ee.poll.ejb.PollHome;
010:
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013:
014: import javax.ejb.CreateException;
015: import javax.naming.Context;
016: import javax.naming.InitialContext;
017: import javax.naming.NamingException;
018: import javax.rmi.PortableRemoteObject;
019: import java.rmi.RemoteException;
020:
021: public class PollProvider extends JSPProvider {
022:
023: public StringBuffer getContent(HttpServletRequest req,
024: HttpServletResponse res) throws ProviderException {
025:
026: String errMsg = null;
027:
028: Poll poll = lookupPoll();
029: ProviderContext ctx = getProviderContext();
030:
031: if (poll != null) {
032: String pollPage = req.getParameter("pollPage");
033:
034: if (pollPage == null
035: || pollPage.equals("pollChannelContent")) {
036: setupPollPage(poll);
037: } else if (pollPage.equals("pollChannelResults")) {
038: setupResultsPage(req, poll);
039: }
040: } else {
041: //ctx.setSessionProperty("pollQuestion", errMsg);
042: }
043:
044: return super .getContent(req, res);
045:
046: }
047:
048: private Poll lookupPoll() {
049: Poll poll = null;
050: String errMsg = null;
051: try {
052: Context ic = new InitialContext();
053: Object objRef = ic.lookup("java:comp/env/ejb/poll");
054: PollHome home = (PollHome) PortableRemoteObject.narrow(
055: objRef, PollHome.class);
056: poll = home.create();
057: } catch (RemoteException ex) {
058: errMsg = "Couldn't create poll bean." + ex.getMessage();
059: } catch (CreateException ex) {
060: errMsg = "Couldn't create poll bean." + ex.getMessage();
061: } catch (NamingException ex) {
062: errMsg = "Unable to lookup home: " + "ejb/poll "
063: + ex.getMessage();
064: }
065: return poll;
066: }
067:
068: private void setupPollPage(Poll poll) throws ProviderException {
069: String action = null;
070: boolean error = false;
071: ProviderContext ctx = getProviderContext();
072: String pollName = getStringProperty("pollName");
073: String lastVoted = getStringProperty("lastVoted");
074: if (pollName.equals(lastVoted)) {
075: action = "CHANGE VOTE";
076: } else {
077: action = "VOTE";
078: }
079: try {
080: String question = poll.getQuestion(pollName);
081: List choices = poll.getChoices(pollName);
082:
083: if (question != null && !question.trim().equals("")) {
084: ctx.setSessionProperty("pollChannelQuestion", question);
085: } else {
086: error = true;
087: }
088:
089: if (choices != null && !choices.isEmpty()) {
090: ctx.setSessionProperty("pollChannelChoice", choices);
091: } else {
092: error = true;
093: }
094: ctx.setSessionProperty("pollChannelAction", action);
095:
096: if (error) {
097: ctx.setSessionProperty("pollChannelError", "true");
098: error = false;
099: }
100: } catch (java.rmi.RemoteException ex) {
101: //errMsg = "Failed to access poll bean method. "+ ex.getMessage();
102: //ctx.setSessionProperty("ejbcounter", errMsg);
103: }
104: }
105:
106: private void setupResultsPage(HttpServletRequest req, Poll poll)
107: throws ProviderException {
108: ProviderContext ctx = getProviderContext();
109: String pollName = getStringProperty("pollName");
110: try {
111: String action = req.getParameter("SUBMIT");
112: ctx.setSessionProperty("pollChannelQuestion", poll
113: .getQuestion(pollName));
114: if (action == null || action.equals("RESULTS")) {
115: ctx.setSessionProperty("pollChannelResults", poll
116: .getResults(pollName));
117: } else if (action.equals("VOTE")) {
118: String vote = req.getParameter("pollChannelVote");
119: ctx.setSessionProperty("pollChannelResults", poll.vote(
120: pollName, vote));
121: setStringProperty("vote", vote);
122: setStringProperty("lastVoted", pollName);
123: } else if (action.equals("CHANGE VOTE")) {
124: String vote = req.getParameter("pollChannelVote");
125: String oldVote = getStringProperty("vote");
126: ctx.setSessionProperty("pollChannelResults", poll
127: .changeVote(pollName, oldVote, vote));
128: setStringProperty("vote", vote);
129: setStringProperty("lastVoted", pollName);
130: } else {
131: }
132: } catch (java.rmi.RemoteException ex) {
133: //set errror page here
134: }
135: }
136: }
|