/**
* @author ishaikh
*
*/
package com.transformuk.bdt.controller;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import com.transformuk.bdt.domain.Options;
import com.transformuk.bdt.domain.Page;
import com.transformuk.bdt.domain.Questions;
import com.transformuk.bdt.model.ProfileQuestions;
import com.transformuk.bdt.model.ReportModel;
import com.transformuk.bdt.util.StaticReferences;
public abstract class BaseWizardController {
protected static final String STEP_ZERO = "stepZero";
protected static final String STEP_NEXT = "stepNext";
protected static final String REPORT_PAGE = "report";
protected static final String QC_WORKFORCE = "WorkForce";
protected static final String QC_BUS_AGE = "BusAge";
protected static final String QC_LEG_STRUCT = "LegStruct";
protected static final String QC_FINANCE = "Finance";
@SuppressWarnings("unchecked")
protected void removePrevCategoryFromQuery(HttpSession session, List<String> childCatList) {
Map<String, String> queryCategoryMap = (Map<String,String>)session.getAttribute(StaticReferences.QUERY_CATEGORIES);
if (queryCategoryMap != null) {
for (String childPageTitle : childCatList) {
if (queryCategoryMap.containsKey(childPageTitle))
queryCategoryMap.remove(childPageTitle);
}
}
}
@SuppressWarnings("unchecked")
protected void setQueryAttributes(HttpSession session, Page currentPage, Questions q, ProfileQuestions profileQuestions) {
//sets query attribute to be used as dynamic substitution for ES
if (StringUtils.equalsIgnoreCase(QC_WORKFORCE, q.getQuestionCode())
|| StringUtils.equalsIgnoreCase(StaticReferences.QC_IND_DD, q.getQuestionCode())
|| StringUtils.equalsIgnoreCase(QC_FINANCE, q.getQuestionCode())) {
String query = buildQueryString(q, profileQuestions);
if (StringUtils.equalsIgnoreCase(QC_WORKFORCE, q.getQuestionCode())) {
session.setAttribute(StaticReferences.QUERY_SIZE, query);
} else if (StringUtils.equalsIgnoreCase(StaticReferences.QC_IND_DD, q.getQuestionCode())) {
session.setAttribute(StaticReferences.QUERY_SECTOR, query);
} else if (StringUtils.equalsIgnoreCase(QC_FINANCE, q.getQuestionCode())) {
session.setAttribute(StaticReferences.QUERY_FINANCE, query);
}
}
else if (currentPage.isFireQuery()) {
String query = q.getQuestionQuery() +" "+ buildQueryString(q, profileQuestions);
Map<String, String> queryCategoryMap = (Map<String,String>)session.getAttribute(StaticReferences.QUERY_CATEGORIES);
if (queryCategoryMap == null) {
queryCategoryMap = new LinkedHashMap<String,String>();
}
queryCategoryMap.put(currentPage.getPageTitle().trim(), query);
session.setAttribute(StaticReferences.QUERY_CATEGORIES, queryCategoryMap);
}
}
protected String buildQueryString(Questions q, ProfileQuestions profileQuestions) {
String userAnswerCode = profileQuestions.getValues().get(q.getQuestionCode());
String[] userAnswersList = StringUtils.split(userAnswerCode, ',');
String query = "";
for (String answerCode : userAnswersList) {
Options userOption = getOptionByAnswerCode(q, answerCode);
query += (userAnswersList.length <= 1) ? userOption.getResponseQuery()
: userOption.getResponseQuery() + StaticReferences.COMMA_SEPERATOR;
}
return query;
}
protected void buildReportModel(Questions q, String narrative, ReportModel reportModel) {
if (StringUtils.equalsIgnoreCase(QC_BUS_AGE, q.getQuestionCode())) {
reportModel.setBusinessAge(narrative);
}else if (StringUtils.equalsIgnoreCase(QC_LEG_STRUCT, q.getQuestionCode())) {
reportModel.setLegalStructure(narrative);
} else if (StringUtils.equalsIgnoreCase(QC_WORKFORCE, q.getQuestionCode())) {
reportModel.setWorkforce(narrative);
//} else if (StringUtils.equalsIgnoreCase(QC_POST_CODE, q.getQuestionCode())) {
// reportModel.setPostCode(narrative);
} else if (StringUtils.equalsIgnoreCase(StaticReferences.QC_IND_DD, q.getQuestionCode())) {
reportModel.setSector(narrative);
}
}
protected void setAsNotHighGrowth(HttpSession session) {
Boolean isHighGrowthBusiness = (Boolean)session.getAttribute(StaticReferences.HIGH_GROWTH);
if (isHighGrowthBusiness) {
//only set it to false as by default it is HG
session.setAttribute(StaticReferences.HIGH_GROWTH, false);
}
}
protected boolean answeredAsNotHG(Questions q, ProfileQuestions profileQuestions) {
String userAnswerCode = profileQuestions.getValues().get(q.getQuestionCode());
Options userOption = getOptionByAnswerCode(q, userAnswerCode);
return userOption.isHighGrowth();
}
//certain questions drive the report, checks if this question is one
protected boolean questionIsForReport(Questions q) {
return (StringUtils.equalsIgnoreCase(QC_BUS_AGE, q.getQuestionCode())
|| StringUtils.equalsIgnoreCase(QC_LEG_STRUCT, q.getQuestionCode())
|| StringUtils.equalsIgnoreCase(QC_WORKFORCE, q.getQuestionCode())
|| StringUtils.equalsIgnoreCase(StaticReferences.QC_POST_CODE, q.getQuestionCode())
|| StringUtils.equalsIgnoreCase(StaticReferences.QC_IND_DD, q.getQuestionCode()));
}
/*
* creates a list of user selection of next set of showPages
* based on the answeCode user selected
*/
protected List<String> createShowPageListByAnswerCode(Questions q, String[] answerCodeList) {
List<String> userSelectedCatPages = new ArrayList<String>();
for (String userAnswerCode: answerCodeList) {
Options option = getOptionByAnswerCode(q, userAnswerCode);
if (option != null) {
userSelectedCatPages.add(option.getShowPage());
}
}
return userSelectedCatPages;
}
//returns narrative
protected String getNarrativeForAnswer(ProfileQuestions profileQuestions, Questions q) {
String userAnswerCode = profileQuestions.getValues().get(q.getQuestionCode());
Options option = getOptionByAnswerCode(q, userAnswerCode);
if (option != null) {
return option.getNarrative();
}
else {
return userAnswerCode; //textfield not option
}
}
//returns Option pojo
protected Options getOptionByAnswerCode(Questions q, String userAnswerCode) {
if (q.getOptions() != null) {
for(Options option : q.getOptions()) {
if (StringUtils.equals(option.getAnswerCode(),userAnswerCode)) {
return option;
}
}
}
return null;
}
//returns reportmodel object
protected ReportModel getReportModel(HttpSession session, String narrative) {
ReportModel reportModel = (ReportModel)session.getAttribute(StaticReferences.REPORT_MODEL);
if (reportModel == null) {
reportModel = new ReportModel();
}
return reportModel;
}
protected boolean userClickedPrevious(int currentPage, int targetPage) {
// if targetPage is less than current page, user clicked 'prev'
return targetPage < currentPage;
}
//finish step clicked
protected boolean userClickedFinished(HttpServletRequest request) {
return request.getParameter("_finish") != null;
}
//navigate based on target page
protected String handlePrevNav(int targetPage) {
if (targetPage <=0)
return STEP_ZERO;
else
return STEP_NEXT;
}
//invalidate existing session (if any) and start fresh
protected void clearOldSession(HttpServletRequest request) {
HttpSession oldSession = request.getSession(false);
if (oldSession != null && !oldSession.isNew()) {
oldSession.invalidate();
}
}
//clean up any prev data that's invalidated due to user's action
protected void cleanUpPrevData(HttpSession session) {
session.removeAttribute(StaticReferences.QUERY_SIZE);
session.removeAttribute(StaticReferences.QUERY_SECTOR);
session.removeAttribute(StaticReferences.QUERY_FINANCE);
session.removeAttribute(StaticReferences.QUERY_CATEGORIES);
session.removeAttribute(StaticReferences.REPORT_MODEL);
}
}