no.dusken.common.plugin.control.admin.PluginStoreController.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.common.plugin.control.admin.PluginStoreController.java

Source

/*
 Copyright 2006 - 2010 Under Dusken
    
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package no.dusken.common.plugin.control.admin;

import no.dusken.common.plugin.DuskenPlugin;
import org.kantega.jexmec.PluginManager;
import org.kantega.jexmec.store.PluginStore;
import org.kantega.jexmec.store.PluginStoreProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author Marvin B. Lillehaug <lillehau@underdusken.no>
 */
@Controller
public class PluginStoreController {

    private PluginStoreProvider pluginStoreProvider;

    private List<PluginManager<DuskenPlugin>> pluginManagers;

    private String view = "no/dusken/common/plugin/admin/pluginStore/edit";

    @RequestMapping("/pluginStore.do")
    public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (isFormSubmission(request)) {
            // A map containing messages in the form pluginUid.setting -> value for each setting that is changed.
            Map<String, String> updateMsgMap = new HashMap<String, String>();
            //Check if settings has changed and store the ones that has.
            for (PluginManager<DuskenPlugin> pluginManager : pluginManagers) {
                for (DuskenPlugin plugin : pluginManager.getPlugins()) {
                    PluginStore store = pluginStoreProvider.getStore(plugin);
                    Set<String> settings = store.getStrings();
                    for (String setting : settings) {
                        String settingKey = plugin.getPluginUid().concat("-").concat(setting);
                        String currentValue = ServletRequestUtils.getStringParameter(request, settingKey, null);
                        String oldValue = store.getString(setting, null);
                        if (currentValue != null && oldValue != null && !currentValue.equals(oldValue)) {
                            store.setString(setting, currentValue);
                            updateMsgMap.put(settingKey, oldValue.concat("->").concat(currentValue));
                        }
                    }
                }
            }
            map.put("updatemap", updateMsgMap);
        }

        Map<DuskenPlugin, Map<String, String>> plugins = new HashMap<DuskenPlugin, Map<String, String>>();
        for (PluginManager<DuskenPlugin> pluginManager : pluginManagers) {
            for (DuskenPlugin plugin : pluginManager.getPlugins()) {
                PluginStore store = pluginStoreProvider.getStore(plugin);
                plugins.put(plugin, getKeyValueMap(store));
            }
        }
        map.put("plugins", plugins);
        map.put("locale", Locale.getDefault());
        return new ModelAndView(view, map);
    }

    private Map<String, String> getKeyValueMap(PluginStore store) {
        Map<String, String> map = new HashMap<String, String>();
        for (String key : store.getStrings()) {
            map.put(key, store.getString(key, null));
        }
        return map;
    }

    protected boolean isFormSubmission(HttpServletRequest request) {
        return "POST".equals(request.getMethod());
    }

    @Autowired
    public void setPluginStoreProvider(PluginStoreProvider pluginStoreProvider) {
        this.pluginStoreProvider = pluginStoreProvider;
    }

    @Autowired
    public void setPluginManagers(List<PluginManager<DuskenPlugin>> pluginManagers) {
        this.pluginManagers = pluginManagers;
    }
}