NewSurvey.java :  » Portal » Open-Portal » com » sun » portal » app » collab » survey » faces » Java Open Source

Java Open Source » Portal » Open Portal 
Open Portal » com » sun » portal » app » collab » survey » faces » NewSurvey.java
package com.sun.portal.app.collab.survey.faces;

import com.sun.portal.app.collab.survey.model.SurveyException;
import java.util.Date;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Calendar;

import javax.faces.context.FacesContext;

import com.sun.web.ui.model.Option;
import com.sun.data.provider.impl.ObjectArrayDataProvider;

import com.sun.portal.app.collab.survey.model.SurveyDB;
import com.sun.portal.app.collab.survey.model.SurveyModel;
import com.sun.portal.app.collab.survey.model.QuestionModel;
import com.sun.portal.app.collab.survey.model.AnswerModel;
import com.sun.portal.app.collab.survey.util.SurveyUtil;
import com.sun.portal.log.common.PortalLogger;
import java.util.logging.Logger;

public class NewSurvey {
    
    private static Logger logger = PortalLogger.getLogger(NewSurvey.class);
    private String surveyName = null;
    private String surveyDescription = null;
    private int startDay, startMonth, startYear;
    private int endDay, endMonth, endYear;
    private Date startDate, endDate;
    
    /**
     * Current question order.
     */
    private int questionOrder = 0;
    private String question = null;
    private String answer = null;
    private boolean isMandatory = false;
    private boolean isSingleChoice = true;
    private boolean isOpen = false;
    
    private ArrayList questionAndAnswers = new ArrayList();
    
    private boolean showQuestionsPage = false;
    
    public NewSurvey() {
        initDateFields();
    }
    
    private void initDateFields() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        startDay = cal.get(Calendar.DATE);
        startMonth = cal.get(Calendar.MONTH);
        startYear = cal.get(Calendar.YEAR);

        // default 1 month later
        cal.add(Calendar.MONTH,1);
        endDay = cal.get(Calendar.DATE);
        endMonth = cal.get(Calendar.MONTH);
        endYear = cal.get(Calendar.YEAR);
    }
    
    public String getSurveyName() {
        return surveyName;
    }
    
    public void setSurveyName(String val) {
        surveyName = val;
    }
    
    public String getSurveyDescription() {
        return surveyDescription;
    }
    
    public void setSurveyDescription(String val) {
        surveyDescription = val;
    }
    
    public void setStartDate() {
        Calendar cal = Calendar.getInstance();
        cal.set(startYear, startMonth, startDay,0,0,0);
        startDate = cal.getTime();
        
        /**
         * Correct the date fields.
         * Eg., 02/31/2005 = 03/03/2005
         */
        startDay = cal.get(Calendar.DATE);
        startMonth = cal.get(Calendar.MONTH);
        startYear = cal.get(Calendar.YEAR);
    }
    
    public void setEndDate() {
        Calendar cal = Calendar.getInstance();
        cal.set(endYear, endMonth, endDay,0,0,0);
        // endDate is 00:00:00 of day following selection
        endDate = cal.getTime();
        
        /**
         * Correct the date fields.
         * Eg., 02/31/2005 = 03/03/2005
         */
        endDay = cal.get(Calendar.DATE);
        endMonth = cal.get(Calendar.MONTH);
        endYear = cal.get(Calendar.YEAR);
        
    }
    
    public String getQuestionOrder() {
        if (new SurveyUtil().isSurvey()) {
            return String.valueOf(questionOrder);
        }
        return "";
    }
    
    public void setQuestionOrder(String val) {
        questionOrder = Integer.parseInt(val.trim());
    }
    
    public String getQuestion() {
        return question;
    }
    
    public void setQuestion(String val) {
        question = val;
    }
    
    public String getAnswer() {
        return answer;
    }
    
    public void setAnswer(String val) {
        answer = val;
    }
    
    public boolean getIsMandatory() {
        return isMandatory;
    }
    
    public void setIsMandatory(boolean val) {
        isMandatory = val;
    }
    
    public boolean getIsMultiSelect() {
        return !isSingleChoice;
    }
    
    public void setIsMultiSelect(boolean val) {
        isSingleChoice = !val;
    }
    
    public boolean getIsOpen() {
        return isOpen;
    }
    
    public void setIsOpen(boolean val) {
        isOpen = val;
    }
    
    public boolean getShowQuestionsPage() {
        return showQuestionsPage;
    }
    
    public boolean getShowDetailsPage() {
        return !showQuestionsPage;
    }
    
    public boolean getNextButtonDisabled() {
        return (new SurveyUtil().isPoll() && showQuestionsPage);
    }
    
    public ObjectArrayDataProvider getQuestionAndAnswers() {
        
        ObjectArrayDataProvider QnAndAnsProvider = null;
        
        if (questionAndAnswers != null) {
            QuestionAndAnswers qnAndAnswers[] = new QuestionAndAnswers[questionAndAnswers.size()];
            for(int i=0; i<questionAndAnswers.size(); i++) {
                qnAndAnswers[i] = (QuestionAndAnswers)questionAndAnswers.get(i);
            }
            QnAndAnsProvider = new ObjectArrayDataProvider(qnAndAnswers);
        }
        
        return QnAndAnsProvider;
        
    }
    
    public boolean processForm() throws SurveyException {

        /**
         * Get start and end dates.
         */
        setStartDate();
        setEndDate();
        
        /**
         * Case : create a new question.
         */
        boolean createNewQuestion = true;
        
        if (questionAndAnswers != null) {
            if(questionOrder <= questionAndAnswers.size()) {
                createNewQuestion = false;
            }
        }
        
        if(createNewQuestion) {
            addNewQuestion();
        } else {
            updateQuestion(questionOrder);
        }
        
        return true;
    }
    
    
    public void handleNextQuestion() throws SurveyException {
        if (showQuestionsPage) {
            // if nothing in the question and not the last one
            // then complain for missing input
            if(!processForm() && questionOrder < questionAndAnswers.size()) {
                //throw an exception as the questions is mandatory
                throw new SurveyException("incomplete-question-error",null);
            }
        }
        
        showQuestionsPage = true;
        ++questionOrder;
        setQuestionOrder(String.valueOf(questionOrder));
        
        /**
         * Case : new question.
         */
        if (questionOrder > questionAndAnswers.size()) {
            clearData();
        }
        /**
         * Case : Update question.
         */
        else  {
            populateData(questionOrder-1);
        }
    }
    
    public void handlePreviousQuestion() throws SurveyException {
        // if nothing in the question and not the last one
        // then complain for missing input
        if(!processForm() && questionOrder < questionAndAnswers.size()) {
            //throw an exception as the questions is mandatory
            throw new SurveyException("incomplete-question-error",null);
        }
        
        --questionOrder;
        
        if (questionOrder == 0) {
            showQuestionsPage = false;
            return;
        }
        
        
        
        setQuestionOrder(String.valueOf(questionOrder));
        
        populateData(questionOrder-1);
        
    }
    
    public void handlePreview() throws SurveyException {
        
        if(showQuestionsPage) {
            processForm();
        }
        
        if (surveyName == null || surveyName.trim().length() == 0 ||
                questionAndAnswers == null || questionAndAnswers.size() == 0) {
            throw new SurveyException("nothingtopreview",null);
        }
        
    }
    
    public void handleDone() throws SurveyException {
        if(showQuestionsPage) {
            processForm();
        }
        
        createSurvey();
        
    }
    
    public void createSurvey() throws SurveyException {
        
        if (surveyName == null || surveyName.trim().length() == 0 ||
                questionAndAnswers == null || questionAndAnswers.size() == 0) {
            throw new SurveyException("create-error-details",null);
        }
        SurveyUtil util = new SurveyUtil();
        String userId = util.getUserId();
        if (userId == null) {
            throw new SurveyException("auth-failed", null);
        }
        
        SurveyModel survey = new SurveyModel();
        survey.setSurveyName(surveyName);
        survey.setSurveyDescription(surveyDescription);
        survey.setPoll(util.isPoll());
        survey.setCommunityId(util.getCommunityId());
        survey.setSurveyOwner(userId);
        survey.setStartDate(startDate);
        survey.setEndDate(endDate);
        
        
        QuestionModel q[] = new QuestionModel[questionAndAnswers.size()];
        
        for (int i=0; i<questionAndAnswers.size(); i++) {
            QuestionAndAnswers qa = (QuestionAndAnswers)questionAndAnswers.get(i);
            q[i] = new QuestionModel();
            q[i].setQuestionOrder(Integer.parseInt(qa.getQuestionOrder()));
            q[i].setQuestionText(qa.getQuestion());
            boolean mandatory = util.isSurvey() ? qa.getIsMandatory() : true;
            q[i].setMandatory(mandatory);
            q[i].setMultiselect(qa.getMultiChoice());
            q[i].setOpen(qa.getIsOpen());
            
            String answers[] = qa.getAnswersAsStringArray();
            
            if (answers != null) {
                AnswerModel a[] = new AnswerModel[answers.length];
                for (int j=0; j < answers.length; j++) {
                    a[j] = new AnswerModel(j+1, answers[j]);
                }
                q[i].setAnswers(a);
            }
        }
        
        survey.setQuestions(q);
        
        SurveyDB db = new SurveyDB();
        db.create(survey);
        
    }
    
    private QuestionAndAnswers createQuestionAndAnswers() throws SurveyException {
        if (question == null || question.trim().length() == 0) {
            throw new SurveyException("incomplete-question-error",null);
        }
        QuestionAndAnswers qa = new QuestionAndAnswers();
        
        qa.setQuestionOrder(String.valueOf(questionOrder));
        
        qa.setQuestion(question);
        
        ArrayList answers = new ArrayList();
        
        boolean hasAnswers = true;
        
        if(answer == null || answer.trim().length() == 0) {
            isOpen = true;
            hasAnswers = false;
        } else {
            String[] result = answer.split("(\\s*\n+)+");
            for (int ndx=0;ndx < result.length; ndx++) {
                if (result[ndx] != null) {
                    String qe = result[ndx].trim();
                    if (qe.length() > 0) {
                        // adding only non empty lines.
                        if (!answers.contains(qe)) {
                            // skipping duplicates
                            answers.add(qe);
                        }
                    }
                }
            }
        }
        
        qa.setAnswers(answers,isOpen & hasAnswers);
        
        qa.setIsMandatory(isMandatory);
        
        qa.setSingleChoice(isSingleChoice);
        
        qa.setIsOpen(isOpen);
        
        return qa;
        
    }
    
    private void clearData() {
        setQuestion("");
        setAnswer("");
        setIsMandatory(false);
        setIsMultiSelect(false);
        setIsOpen(false);
    }
    
    private void populateData(int index) {
        QuestionAndAnswers qa = (QuestionAndAnswers) questionAndAnswers.get(index);
        setQuestion(qa.getQuestion());
        setAnswer(qa.getAnswersAsString());
        setIsMandatory(qa.getIsMandatory());
        setIsMultiSelect(qa.getMultiChoice());
        setIsOpen(qa.getIsOpen());
    }
    
    private void addNewQuestion() throws SurveyException {
        if (questionAndAnswers == null) {
            questionAndAnswers = new ArrayList();
        }
        questionAndAnswers.add(createQuestionAndAnswers());
    }
    
    private void updateQuestion(int qOrder) throws SurveyException {
        if (questionAndAnswers != null) {
            questionAndAnswers.set(qOrder-1,  createQuestionAndAnswers());
        }
    }
    
    public Option[] getYears() {
        Option opts[] = new Option[10];
        int curYear = Calendar.getInstance().get(Calendar.YEAR);
        for(int i=0;i<10;i++) {
            opts[i] = new Option(String.valueOf(curYear+i),String.valueOf(curYear+i));
        }
        return opts;
    }
    
    public Option[] getMonths() {
        Option opts[] = new Option[12];
        String months[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        for(int i=0;i<months.length;i++) {
            opts[i] = new Option(String.valueOf(i),months[i]);
        }
        return opts;
    }
    
    public Option[] getDays() {
        Option opts[] = new Option[31];
        for(int i=0;i<31;i++) {
            opts[i] = new Option(String.valueOf(i+1),String.valueOf(i+1));
        }
        return opts;
    }
    
    public String getStartYear() {
        return String.valueOf(startYear);
    }
    
    public void setStartYear(String val) {
        startYear = Integer.parseInt(val);
    }
    
    public String getStartMonth() {
        return String.valueOf(startMonth);
    }
    
    public void setStartMonth(String val) {
        startMonth = Integer.parseInt(val);
    }
    
    public String getStartDay() {
        return String.valueOf(startDay);
    }
    
    public void setStartDay(String val) {
        startDay = Integer.parseInt(val);
    }
    
    public String getEndYear() {
        return String.valueOf(endYear);
    }
    
    public void setEndYear(String val) {
        endYear = Integer.parseInt(val);
    }
    
    public String getEndMonth() {
        return String.valueOf(endMonth);
    }
    
    public void setEndMonth(String val) {
        endMonth = Integer.parseInt(val);
    }
    
    public String getEndDay() {
        return String.valueOf(endDay);
    }
    
    public void setEndDay(String val) {
        endDay = Integer.parseInt(val);
    }
    
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.