<%! final int MAX_NUM_ANSWERS = 4; final String FORECASTED_YEAR = "2015"; final int MAX_ALLOWED_TEXT_LENGTH = 255; void renderDriverOptions(Company company, JspWriter out, String selectedIdentifier) throws IOException { for(Identifier division : TrefisModelDAO.getDivisions(company)) { renderDivisionOptionGroup(division, out, selectedIdentifier); } } void renderDivisionOptionGroup(Identifier division, JspWriter out, String selectedIdentifier) throws IOException { out.println(""); List drivers = TrefisModelDAO.getDriversForDivision(division); Collections.sort(drivers, new Comparator() { public int compare(Identifier o1, Identifier o2) { return o1.getShortName().compareTo(o2.getShortName()); } }); for(Identifier driver : drivers) { renderDriverOption(driver, out, selectedIdentifier); } out.println(""); } void renderDriverOption(Identifier driver, JspWriter out, String selectedIdentifier) throws IOException { out.println(""); } void renderDriverData(Company company, JspWriter out) throws JSONException, IOException { JSONObject data = new JSONObject(); for(Identifier driver : TrefisModelDAO.getDrivers(company.getSymbol())) { Identifier yearDriver = IdentifierDAO.get(driver.getName() + "." + FORECASTED_YEAR); if(yearDriver != null) { Value value = yearDriver.getSingleValue(); if(value != null) { data.put( driver.getName(), new JSONObject() .put("unit", value.getUnit()) .put("value", UnitUtil.getDisplayString(value.getValue(), value.getUnit()))); } } } out.println(""); } %><% final Encoder encoder = ESAPI.encoder(); String action = request.getParameter("action"); if(action == null) action = "show"; if("submitwizard".equals(action)) { Integer id = ServletUtil.getIntegerParameter(request, "wid"); ScenarioWizard wizard; if(id != null) { wizard = ScenarioWizardDAO.get(id); } else { wizard = new ScenarioWizard(); } wizard.setCompany(CompanyDAO.loadCompany(request.getParameter("symbol"))); wizard.setVisible(ServletUtil.getBooleanParameter(request, "visible")); wizard.setTitle(TextUtil.truncateToLen(request.getParameter("title"), MAX_ALLOWED_TEXT_LENGTH)); if(id == null) ScenarioWizardDAO.saveNew(wizard); response.sendRedirect(LinkUtil.createLink("/editwizard.jsp?action=showwizard&wid=" + wizard.getId())); return; } else if("submitquestion".equals(action)) { Integer qid = ServletUtil.getIntegerParameter(request, "qid"); ScenarioWizardQuestion question; if(qid != null) { question = ScenarioWizardDAO.getQuestion(qid); } else { question = new ScenarioWizardQuestion(); question.setWizard(ScenarioWizardDAO.get(ServletUtil.getIntegerParameter(request, "wid"))); question.setQuestionIndex(question.getWizard().getQuestions().size() + 1); } Identifier driver = IdentifierDAO.get(request.getParameter("driver")); Identifier yearDriver = IdentifierDAO.get(request.getParameter("driver") + "." + FORECASTED_YEAR); Value driverYearValue = yearDriver.getSingleValue(); // get the scale factor that we use to convert back from the display form to the DB form // (note that we use the Trefis value for calculating the scale factor, not the user-inputted value, because // using the Trefis value gives us exactly the scale factor that is displayed next to the input box. If we use // the user-inputted value instead, the scale factor we use here might differ from what the user saw). double scaleFactor = UnitUtil.calculateScaleFactorForDisplay(driverYearValue.getUnit(), driverYearValue.getValue()); question.setDriver(driver); question.setQuestionText(TextUtil.truncateToLen(request.getParameter("text"), MAX_ALLOWED_TEXT_LENGTH)); List answers = new ArrayList(4); for(int i = 0; i < MAX_NUM_ANSWERS; i++) { String answerText = request.getParameter("answertext" + i); String answerValueAsString = request.getParameter("answervalue" + i); if(!TextUtil.NullOrEmpty(answerText) && !TextUtil.NullOrEmpty(answerValueAsString)) { double answerValue = Double.parseDouble(answerValueAsString); ScenarioWizardAnswer answer = new ScenarioWizardAnswer(); answer.setQuestion(question); answer.setAnswerIndex(answers.size() + 1); answer.setAnswerText(TextUtil.truncateToLen(answerText, MAX_ALLOWED_TEXT_LENGTH)); answer.setForecastedValue(answerValue / scaleFactor); answers.add(answer); } } question.getAnswers().clear(); question.getAnswers().addAll(answers); if(qid == null) ScenarioWizardDAO.saveNew(question); response.sendRedirect(LinkUtil.createLink("/editwizard.jsp?action=showwizard&wid=" + question.getWizard().getId())); return; } else if("deletewizard".equals(action)) { Integer id = ServletUtil.getIntegerParameter(request, "wid"); ScenarioWizardDAO.delete(id); response.sendRedirect(LinkUtil.createLink("/editwizard.jsp?action=show")); return; } else if("deletequestion".equals(action)) { Integer id = ServletUtil.getIntegerParameter(request, "qid"); ScenarioWizardQuestion question = ScenarioWizardDAO.getQuestion(id); ScenarioWizard wizard = question.getWizard(); ScenarioWizardDAO.deleteQuestion(id); response.sendRedirect(LinkUtil.createLink("/editwizard.jsp?action=showwizard&wid=" + wizard.getId())); return; } else if("movequestion".equals(action)) { Integer id = ServletUtil.getIntegerParameter(request, "qid"); ScenarioWizardQuestion question = ScenarioWizardDAO.getQuestion(id); ScenarioWizard wizard = question.getWizard(); int indexDelta = "up".equals(request.getParameter("dir")) ? -1 : ("down".equals(request.getParameter("dir")) ? 1 : 0); if(indexDelta != 0) { int newIndex = question.getQuestionIndex() + indexDelta; if(newIndex > 0 && newIndex <= wizard.getQuestions().size()) { ScenarioWizardQuestion swapWith = wizard.getQuestions().get(newIndex - 1); swapWith.setQuestionIndex(question.getQuestionIndex()); question.setQuestionIndex(newIndex); } } response.sendRedirect(LinkUtil.createLink("/editwizard.jsp?action=showwizard&wid=" + wizard.getId())); return; } %> <%@page import="org.json.JSONObject"%> <%@page import="org.json.JSONException"%> <%@page import="java.io.IOException"%> <%@page import="java.util.List"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.Comparator"%> <%@page import="java.util.Collections"%> <%@page import="org.owasp.esapi.ESAPI"%> <%@page import="org.owasp.esapi.Encoder"%> <%@page import="com.insightguru.giraffe.common.constant.UserProfileBitFlags"%> <%@page import="com.insightguru.giraffe.server.utils.TextUtil"%> <%@page import="com.insightguru.giraffe.server.utils.UnitUtil"%> <%@page import="com.insightguru.giraffe.server.dao.IdentifierDAO"%> <%@page import="com.insightguru.giraffe.server.dao.CompanyDAO"%> <%@page import="com.insightguru.giraffe.server.dao.ScenarioWizardDAO"%> <%@page import="com.insightguru.giraffe.server.dao.TrefisModelDAO"%> <%@page import="com.insightguru.giraffe.server.domain.legacy.SankeyCompany"%> <%@page import="com.insightguru.giraffe.server.domain.legacy.SankeyIdentifierValue"%> <%@page import="com.insightguru.giraffe.server.domain.Value"%> <%@page import="com.insightguru.giraffe.server.domain.ScenarioWizard"%> <%@page import="com.insightguru.giraffe.server.domain.ScenarioWizardQuestion"%> <%@page import="com.insightguru.giraffe.server.domain.ScenarioWizardAnswer"%> <%@page import="com.insightguru.giraffe.server.controller.LoginController"%> <%@page import="com.insightguru.giraffe.server.utils.LinkUtil"%> <%@ include file="components/contentType.jsp" %> Scenario Wizard Management <%@ include file="components/commonHead.jsp" %>
<% User loggedInUser = UserDAO.getCurrentUser(); if(loggedInUser == null || !UserProfileBitFlags.isAdmin(loggedInUser)) { %>

Admin Access Only

Logout and try again <% } else { %>

Scenario Wizard Management

Home

<% if("show".equals(action)) { List wizards; String title; if(request.getParameter("ticker") != null) { wizards = ScenarioWizardDAO.getWizardsForCompany(request.getParameter("ticker").toUpperCase(), true); title = "All Wizards for " + request.getParameter("ticker").toUpperCase(); } else { wizards = ScenarioWizardDAO.getAllWizards(true); title = "All Wizards"; } %>

<%= title %>

Filter by ticker: "/>

New Wizard

<% for(ScenarioWizard wizard : wizards) { %><% } %>
Company Visible? Title
<%= encoder.encodeForHTML(wizard.getCompany().getCompanyName()) %> <%= wizard.isVisible() ? "Yes" : "No" %> <%= encoder.encodeForHTML(TextUtil.truncateToLen(wizard.getTitle(), 100)) %> View/Edit Delete
<% } else if("showwizard".equals(action)) { Integer id = ServletUtil.getIntegerParameter(request, "wid"); ScenarioWizard wizard = ScenarioWizardDAO.get(id); %>

Wizard #<%= wizard.getId() %>

Edit | Delete
Company: <%= encoder.encodeForHTML(wizard.getCompany().getCompanyName()) %>
Visible: <%= wizard.isVisible() ? "Yes" : "No" %>
Title: <%= encoder.encodeForHTML(wizard.getTitle()) %>

Questions:

Add Question

<% for(int questionIndex = 0; questionIndex < wizard.getQuestions().size(); questionIndex++) { ScenarioWizardQuestion question = wizard.getQuestions().get(questionIndex); int rowspan = Math.max(1, question.getAnswers().size()); Identifier driver = question.getDriver(); Value value = driver.getSingleValue(); %> <% for(ScenarioWizardAnswer answer : question.getAnswers()) { %> <% } %> <% } %>
Driver Question Answer Value in <%= FORECASTED_YEAR %>
<%= encoder.encodeForHTML(question.getDriver().getShortName()) %> (<%= question.getDriver().getName() %>) <%= TextUtil.bb2HTML(question.getQuestionText()) %> <% if(questionIndex > 0) { %> Move up
<% } %> <% if(questionIndex < wizard.getQuestions().size() - 1) { %> Move down <% } %>
Edit Delete <%= TextUtil.bb2HTML(answer.getAnswerText()) %> <%= UnitUtil.getDisplayString(answer.getForecastedValue(), value.getUnit()) %>
<% } else if("editwizard".equals(action)) { Integer id = ServletUtil.getIntegerParameter(request, "wid"); ScenarioWizard wizard = id == null ? null : ScenarioWizardDAO.get(id); %>

<%= wizard == null ? "Creating New Wizard" : "Editing Wizard #" + wizard.getId() %>

Company Ticker: "/>
Title:
Visible: />
/>

Cancel
<% } else if("editquestion".equals(action)) { Integer qid = ServletUtil.getIntegerParameter(request, "qid"); ScenarioWizardQuestion question = qid == null ? null : ScenarioWizardDAO.getQuestion(qid); ScenarioWizard wizard = question != null ? question.getWizard() : ScenarioWizardDAO.get(ServletUtil.getIntegerParameter(request, "wid")); renderDriverData(wizard.getCompany(), out); %>

Editing Question on Wizard #<%= wizard.getId() %>

Driver:
Question Text:

<% for(int answerIndex = 0; answerIndex < MAX_NUM_ANSWERS; answerIndex++) { ScenarioWizardAnswer answer; Double scaleFactor; if(question == null) { answer = null; scaleFactor = null; } else { answer = question.getAnswers().size() > answerIndex ? question.getAnswers().get(answerIndex) : null; Value value = question.getDriver().getSingleValue(); scaleFactor = UnitUtil.calculateScaleFactorForDisplay(value.getUnit(), value.getValue()); } %> <% } %>
Answer # Answer Text Forecasted Value in <%= FORECASTED_YEAR %> Trefis Value in <%= FORECASTED_YEAR %>
<%= answerIndex+1 %>: " class="numeric"/> ()

Cancel
<% } %>
Some of the text entries are too long. The maximum length is <%= MAX_ALLOWED_TEXT_LENGTH %> characters.
<% } // end if admin %>
<%@ include file="components/commonJS.jsp" %>