com.seajas.search.profiler.controller.ModifierController.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.profiler.controller.ModifierController.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.profiler.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

import com.seajas.search.bridge.profiler.model.feed.Feed;
import com.seajas.search.bridge.profiler.model.modifier.Modifier;
import com.seajas.search.bridge.profiler.model.modifier.Modifier.UrlType;
import com.seajas.search.profiler.model.command.ModifierCommand;
import com.seajas.search.profiler.service.profiler.ProfilerService;
import com.seajas.search.utilities.spring.security.model.User;

/**
 * Modifier controller.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@RequestMapping("/modifiers.html")
@Controller
public class ModifierController {
    /**
     * Profiler service.
     */
    @Autowired
    private ProfilerService profilerService;

    /**
     * The validator.
     */
    @Qualifier("modifierValidator")
    @Autowired
    private Validator validator;

    /**
     * User attribute.
     * 
     * @return User
     */
    @ModelAttribute("user")
    public User populateUser() {
        return profilerService.getUser();
    }

    /**
     * Data attribute.
     * 
     * @return List<Modifier>
     */
    @ModelAttribute("data")
    public List<Modifier> populateData() {
        return profilerService.getModifiers();
    }

    /**
     * Feeds attribute.
     * 
     * @return Map<Integer, String>
     */
    @ModelAttribute("feeds")
    public Map<Integer, String> populateFeeds() {
        List<Feed> feeds = profilerService.getFeeds();

        Map<Integer, String> result = new LinkedHashMap<Integer, String>();

        for (Feed feed : feeds)
            result.put(feed.getId(), feed.getName());

        return result;
    }

    /**
     * Available character sets.
     * 
     * @return Map<String, String>
     */
    @ModelAttribute("urlTypes")
    public Map<String, String> populateUrlTypes() {
        Map<String, String> urlTypes = new LinkedHashMap<String, String>();

        urlTypes.put(UrlType.Result.name(), "modifiers.collection.result");
        urlTypes.put(UrlType.Feed.name(), "modifiers.collection.feed");

        return urlTypes;
    }

    /**
     * Available script languages.
     * 
     * @return Map<String, String>
     */
    @ModelAttribute("scriptLanguages")
    public Map<String, String> populateScriptLanguages() {
        Map<String, String> scriptLanguages = new LinkedHashMap<String, String>();

        for (ScriptEngineFactory factory : new ScriptEngineManager().getEngineFactories())
            if (factory.getLanguageName().equalsIgnoreCase("ECMAScript"))
                scriptLanguages.put(factory.getLanguageName(), "JavaScript");
            else if (factory.getLanguageName().equalsIgnoreCase("Groovy")
                    || factory.getLanguageName().equalsIgnoreCase("Scala")
                    || factory.getLanguageName().equalsIgnoreCase("Ruby"))
                scriptLanguages.put(factory.getLanguageName(), StringUtils.capitalize(factory.getLanguageName()));

        scriptLanguages.put("xslt", "XSLT");

        return scriptLanguages;
    }

    /**
     * Handle the request.
     * 
     * @param model
     * @return String
     */
    @RequestMapping(method = RequestMethod.GET)
    public String handleRequest(final ModelMap model) {
        model.put("modifierCommand", new ModifierCommand());

        return "modifiers";
    }

    /**
     * Render the submit action in the same way as a regular page view is rendered.
     * 
     * @param command
     * @param result
     * @param model
     * @param request
     * @return String
     */
    @RequestMapping(method = RequestMethod.POST)
    public String processSubmit(@ModelAttribute("modifierCommand") final ModifierCommand command,
            final BindingResult result, final ModelMap model, final WebRequest request) {
        if (request.getParameter("isEnabled") == null)
            command.setIsEnabled(false);
        if (request.getParameter("filterAsExpression") == null)
            command.setFilterAsExpression(false);

        validator.validate(command, result);

        if (!result.hasErrors()) {
            // Modifiers

            if (command.getAction() != null) {
                if (command.getAction().equals("add"))
                    profilerService.addModifier(command.getUrlExpression(), command.getUrlType(),
                            command.getTestFeed(), command.getIsEnabled());
                else if (command.getAction().equals("edit"))
                    profilerService.modifyModifier(command.getId(), command.getUrlExpression(),
                            command.getUrlType(), command.getTestFeed(), command.getIsEnabled());
                else if (command.getAction().equals("delete"))
                    profilerService.deleteModifier(command.getId());
            }

            // Filters

            if (command.getFilterAction() != null) {
                if (command.getFilterAction().equals("add-filter"))
                    profilerService.addModifierFilter(command.getFilterModifierId(), command.getFilterStart(),
                            command.getFilterEnd(), command.getFilterAsExpression());
                else if (command.getFilterAction().equals("edit-filter"))
                    profilerService.modifyModifierFilter(command.getFilterId(), command.getFilterModifierId(),
                            command.getFilterStart(), command.getFilterEnd(), command.getFilterAsExpression());
                else if (command.getFilterAction().equals("delete-filter"))
                    profilerService.deleteModifierFilter(command.getFilterId());
            }

            // Scripts

            if (command.getScriptAction() != null) {
                if (command.getScriptAction().equals("add-script"))
                    profilerService.addModifierScript(command.getScriptModifierId(), command.getScriptLanguage(),
                            command.getScriptContent());
                else if (command.getScriptAction().equals("edit-script"))
                    profilerService.modifyModifierScript(command.getScriptId(), command.getScriptModifierId(),
                            command.getScriptLanguage(), command.getScriptContent());
                else if (command.getScriptAction().equals("delete-script"))
                    profilerService.deleteModifierScript(command.getScriptId());
            }

            // Re-populate the data

            model.put("data", populateData());

            command.clear();
        }

        return "modifiers";
    }
}