no.dusken.barweb.admin.PluginStoreController.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.barweb.admin.PluginStoreController.java

Source

/*
 Copyright 2009 - 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.barweb.admin;

import no.dusken.barweb.plugin.BarwebPlugin;
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.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

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

    private PluginStoreProvider pluginStoreProvider;

    private PluginManager<BarwebPlugin> pluginManager;

    private String view;

    protected 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 (BarwebPlugin 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<BarwebPlugin, Map<String, String>> plugins = new HashMap<BarwebPlugin, Map<String, String>>();
        for (BarwebPlugin 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 setPluginManager(PluginManager<BarwebPlugin> pluginManager) {
        this.pluginManager = pluginManager;
    }

    public void setView(String view) {
        this.view = view;
    }
}