net.shopxx.plugin.ossStorage.OssStorageController.java Source code

Java tutorial

Introduction

Here is the source code for net.shopxx.plugin.ossStorage.OssStorageController.java

Source

/*
 * Copyright 2005-2015 shopxx.net. All rights reserved.
 * Support: http://3936242.01p.com/
 * License: http://3936242.01p.com/license
 */
package net.shopxx.plugin.ossStorage;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import net.shopxx.Message;
import net.shopxx.controller.admin.BaseController;
import net.shopxx.entity.PluginConfig;
import net.shopxx.service.PluginConfigService;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller("adminOssStorageController")
@RequestMapping("/admin/storage_plugin/oss_storage")
public class OssStorageController extends BaseController {

    @Resource(name = "ossStoragePlugin")
    private OssStoragePlugin ossStoragePlugin;
    @Resource(name = "pluginConfigServiceImpl")
    private PluginConfigService pluginConfigService;

    @RequestMapping(value = "/install", method = RequestMethod.POST)
    public @ResponseBody Message install() {
        if (!ossStoragePlugin.getIsInstalled()) {
            PluginConfig pluginConfig = new PluginConfig();
            pluginConfig.setPluginId(ossStoragePlugin.getId());
            pluginConfig.setIsEnabled(false);
            pluginConfig.setAttributes(null);
            pluginConfigService.save(pluginConfig);
        }
        return SUCCESS_MESSAGE;
    }

    @RequestMapping(value = "/uninstall", method = RequestMethod.POST)
    public @ResponseBody Message uninstall() {
        if (ossStoragePlugin.getIsInstalled()) {
            pluginConfigService.deleteByPluginId(ossStoragePlugin.getId());
        }
        return SUCCESS_MESSAGE;
    }

    @RequestMapping(value = "/setting", method = RequestMethod.GET)
    public String setting(ModelMap model) {
        PluginConfig pluginConfig = ossStoragePlugin.getPluginConfig();
        model.addAttribute("pluginConfig", pluginConfig);
        return "/net/shopxx/plugin/ossStorage/setting";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(String endpoint, String accessId, String accessKey, String bucketName, String urlPrefix,
            @RequestParam(defaultValue = "false") Boolean isEnabled, Integer order,
            RedirectAttributes redirectAttributes) {
        PluginConfig pluginConfig = ossStoragePlugin.getPluginConfig();
        Map<String, String> attributes = new HashMap<String, String>();
        attributes.put("endpoint", endpoint);
        attributes.put("accessId", accessId);
        attributes.put("accessKey", accessKey);
        attributes.put("bucketName", bucketName);
        attributes.put("urlPrefix", StringUtils.removeEnd(urlPrefix, "/"));
        pluginConfig.setAttributes(attributes);
        pluginConfig.setIsEnabled(isEnabled);
        pluginConfig.setOrder(order);
        pluginConfigService.update(pluginConfig);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:/admin/storage_plugin/list.jhtml";
    }

}