Package org.rydia.server

Source Code of org.rydia.server.SurveyServiceImpl

package org.rydia.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.jsurveylib.Survey;
import org.rydia.client.SurveyService;

import javax.servlet.http.HttpSession;

/**
* <u><b><font color="red">FOR INTERNAL USE ONLY.</font></b></u>
* Copyright (c)2007, Daniel Kaplan
*
* @author Daniel Kaplan
* @since 7.11.6
*/
public class SurveyServiceImpl extends RemoteServiceServlet implements SurveyService {
    private static final String PREVIOUS_BUTTON = "rydia_previousButton";
    private static final String NEXT_BUTTON = "rydia_nextButton";
    private static final String FINISH_BUTTON = "rydia_finishButton";

    public String[][] changeAnswer(String surveyProvider, String id, String answer) {
        Survey survey = getSurvey(surveyProvider);
        if (isAnswerToSet(id, answer)) {
            survey.setAnswer(id, answer);
        }
        return translatePage(survey);
    }

    public String[][] changePage(String surveyProvider, int toPage) {
        Survey survey = getSurvey(surveyProvider);
        survey.goToPage(toPage);
        return translatePage(survey);
    }

    private boolean isAnswerToSet(String id, String answer) {
        return id != null && answer != null;
    }

    public String[][] buttonClicked(String surveyProvider, String buttonName) {
        Survey survey = getSurvey(surveyProvider);

        if (PREVIOUS_BUTTON.equals(buttonName) && survey.isPreviousPageAvailable()) {
            survey.goToPreviousPage();
        } else if (NEXT_BUTTON.equals(buttonName) && survey.isNextPageAvailable()) {
            survey.goToNextPage();
        } else if (FINISH_BUTTON.equals(buttonName) && survey.isLastPageAndComplete()) {
            survey.finish();
        } else {
            System.err.println("Unknown button name: " + buttonName);
        }

        return translatePage(survey);
    }

    private Survey getSurvey(String serviceProviderClassName) {
        HttpSession session = this.getThreadLocalRequest().getSession();
        SurveyProvider surveyProvider = (SurveyProvider) session.getAttribute(serviceProviderClassName);
        if (surveyProvider != null) {
            return (Survey) surveyProvider.getSurvey();
        }

        surveyProvider = getSurveyProvider(serviceProviderClassName);
        session.setAttribute(serviceProviderClassName, surveyProvider);
        return (Survey) surveyProvider.getSurvey();
    }

    private String[][] translatePage(Survey survey) {
        return new PageTranslator(survey).translate();
    }

    private SurveyProvider getSurveyProvider(String className) {
        try {
            Class clazz = Class.forName(className);
            return (SurveyProvider) clazz.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
}
TOP

Related Classes of org.rydia.server.SurveyServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.