com.surveypanel.web.admin.ScriptAction.java Source code

Java tutorial

Introduction

Here is the source code for com.surveypanel.web.admin.ScriptAction.java

Source

/*
* SurveyPanel
* Copyright (C) 2009 Serge Tan Panza
* All rights reserved.
* License: GNU/GPL License v3 , see LICENSE.txt
* SurveyPanel is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/
package com.surveypanel.web.admin;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.katsuo.dao.QueryFilter;
import org.katsuo.domain.Field;

import com.opensymphony.xwork2.Preparable;
import com.surveypanel.domain.Client;
import com.surveypanel.domain.Script;
import com.surveypanel.domain.Survey;

@Namespace("/admin/script")
public class ScriptAction extends BaseCrudAction<Script> implements Preparable {

    private static final long serialVersionUID = -8096153688118202180L;
    protected String name, source;
    protected Long id, surveyId, clientId;
    protected String clientName, surveyName;

    public void prepare() throws Exception {
        if (surveyId != null && clientId != null) {
            Client client = entityManager.load(Client.class, new Field("id", clientId));
            if (client != null) {
                clientName = client.getOrganisation();
            } else {
                addActionError(getText("error.client.notfound"));
            }
            Survey survey = entityManager.load(Survey.class, new Field("id", surveyId));
            if (survey != null) {
                surveyName = survey.getName();
            } else {
                addActionError(getText("error.survey.notfound"));
            }
        }
    }

    @Override
    protected Script bind() {
        Script script = new Script();
        script.setSource(StringUtils.trim(source));
        script.setName(StringUtils.trim(name));
        script.setId(id == null ? 0 : id);
        script.setSurveyId(surveyId);
        return script;
    }

    @Override
    protected void bind(Script script) {
        source = script.getSource();
        name = script.getName();
        surveyId = script.getSurveyId();
        id = script.getId();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public Long getSurveyId() {
        return surveyId;
    }

    public void setSurveyId(Long surveyId) {
        this.surveyId = surveyId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getClientName() {
        return clientName;
    }

    public String getSurveyName() {
        return surveyName;
    }

    public String getModelName() {
        return "script";
    }

    public Long getClientId() {
        return clientId;
    }

    public void setClientId(Long clientId) {
        this.clientId = clientId;
    }

    @Override
    protected Field[] getFieldsToUpdate() {
        return new Field[] { new Field("name", name), new Field("source", source) };
    }

    @Override
    protected Field[] getPks() {
        return new Field[] { new Field("id", id) };
    }

    @Override
    protected QueryFilter getSearchFilter() {
        QueryFilter paginationFilter = new QueryFilter();
        paginationFilter.set("surveyId", surveyId);
        return paginationFilter;
    }

}