com.seajas.search.attender.validator.TemplateValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.attender.validator.TemplateValidator.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.attender.validator;

import java.io.StringReader;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.seajas.search.attender.model.command.ProfileCommand;
import com.seajas.search.attender.model.command.TemplateCommand;
import com.seajas.search.attender.service.attender.AttenderService;

/**
 * Template validator.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@Component("templateValidator")
public class TemplateValidator implements Validator {
    /**
     * The attender service
     */
    @Autowired
    private AttenderService attenderService;

    /**
     * Determine whether this validator supports the command object.
     * 
     * @param klass
     * @return boolean
     */
    @Override
    @SuppressWarnings("rawtypes")
    public boolean supports(final Class klass) {
        return klass.equals(ProfileCommand.class);
    }

    /**
     * Validate the given command object.
     * 
     * @param command
     * @param errors
     */
    @Override
    public void validate(final Object command, final Errors errors) {
        TemplateCommand template = (TemplateCommand) command;

        if (template.getAction().equals("edit")) {
            if (StringUtils.isEmpty(template.getLanguage()))
                errors.rejectValue("language", "templates.error.language.empty");
            else if (!attenderService.getAvailableSearchLanguages().contains(template.getLanguage()))
                errors.rejectValue("language", "templates.error.language.invalid");

            // Now validate the content

            if (StringUtils.isEmpty(template.getResultsHtmlContent()))
                errors.rejectValue("resultsHtmlContent", "templates.error.results.html.content.empty");
            else {
                StreamSource source = new StreamSource(new StringReader(template.getResultsHtmlContent()));

                try {
                    TransformerFactory.newInstance().newTransformer(source);
                } catch (Exception e) {
                    errors.rejectValue("resultsHtmlContent", "templates.error.results.html.content.invalid");
                }
            }

            if (StringUtils.isEmpty(template.getResultsTextContent()))
                errors.rejectValue("resultsTextContent", "templates.error.results.text.content.empty");
            else {
                StreamSource source = new StreamSource(new StringReader(template.getResultsTextContent()));

                try {
                    TransformerFactory.newInstance().newTransformer(source);
                } catch (Exception e) {
                    errors.rejectValue("resultsTextContent", "templates.error.results.text.content.invalid");
                }
            }

            if (StringUtils.isEmpty(template.getConfirmationHtmlContent()))
                errors.rejectValue("confirmationHtmlContent", "templates.error.confirmation.html.content.empty");
            else {
                StreamSource source = new StreamSource(new StringReader(template.getConfirmationHtmlContent()));

                try {
                    TransformerFactory.newInstance().newTransformer(source);
                } catch (Exception e) {
                    errors.rejectValue("confirmationHtmlContent",
                            "templates.error.confirmation.html.content.invalid");
                }
            }

            if (StringUtils.isEmpty(template.getConfirmationTextContent()))
                errors.rejectValue("confirmationTextContent", "templates.error.confirmation.text.content.empty");
            else {
                StreamSource source = new StreamSource(new StringReader(template.getConfirmationTextContent()));

                try {
                    TransformerFactory.newInstance().newTransformer(source);
                } catch (Exception e) {
                    errors.rejectValue("confirmationTextContent",
                            "templates.error.confirmation.text.content.invalid");
                }
            }
        }
    }
}