org.hsweb.web.controller.config.ConfigController.java Source code

Java tutorial

Introduction

Here is the source code for org.hsweb.web.controller.config.ConfigController.java

Source

/*
 * Copyright 2015-2016 http://hsweb.me
 *
 * 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 org.hsweb.web.controller.config;

import com.alibaba.fastjson.JSON;
import org.hsweb.web.core.logger.annotation.AccessLogger;
import org.hsweb.web.core.authorize.annotation.Authorize;
import org.hsweb.web.bean.po.config.Config;
import org.hsweb.web.controller.GenericController;
import org.hsweb.web.core.message.ResponseMessage;
import org.hsweb.web.service.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

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

/**
 * ?.??
 *
 * @author zhouhao
 */
@RestController
@RequestMapping(value = "/config")
@AccessLogger("??")
@Authorize
public class ConfigController extends GenericController<Config, String> {
    private static final String CACHE_KEY = "config";

    //?
    @Autowired
    private ConfigService configService;

    @Override
    public ConfigService getService() {
        return this.configService;
    }

    /**
     * ??["core.system.version","upload.path"] ?coresystem.versionuploadpath
     * <br/>: {"core":{"system.version":"1.0"},"upload":{"path":"/files"}}
     *
     * @param ids ??
     * @return ?
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @Cacheable(value = CACHE_KEY, key = "'info_list'+#ids.hashCode()")
    @AccessLogger("???")
    public Object batch(@RequestParam(value = "ids", defaultValue = "[]") String ids, boolean map) {
        List<String> requestData = JSON.parseArray(ids, String.class);
        //??
        Map<String, Object> config = new LinkedHashMap<>();
        //: cfg.name,cfg.data,cfg.other?cfg?????
        Map<String, Map<String, String>> temp = new LinkedHashMap<>();
        for (String request : requestData) {
            Config conf = null;
            try {
                conf = configService.selectByPk(request);
            } catch (Exception e) {
            }
            //?[.]??: core.system.version,?core?system.version
            if (conf == null && request.contains(".")) {
                String[] res = request.split("[.]", 2);
                String name = res[0]; //: core
                String key = res[1]; //: system.version
                Map cache;
                //??
                if ((cache = temp.get(name)) == null) {
                    try {
                        conf = configService.selectByPk(name);
                        cache = conf.toMap();
                    } catch (Exception e) {
                    }
                    if (cache == null) {
                        config.put(request, new LinkedHashMap<>());
                        continue;
                    }
                    temp.put(name, cache);
                }
                Map<String, Object> tmp = (Map) config.get(name);
                if (tmp != null) {
                    tmp.put(key, cache.get(key));
                } else {
                    tmp = new LinkedHashMap<>();
                    tmp.put(key, cache.get(key));
                    config.put(name, tmp);
                }
            } else {
                //??
                if (conf != null) {
                    config.put(request, map ? conf.toMap() : conf.toList());
                }
            }
        }
        temp.clear();
        return config;
    }

    /**
     * ??,map?
     * ?:{key:value,key2:value2}
     *
     * @param name ???
     * @return ?
     */
    @RequestMapping(value = "/{name:.+}.map", method = RequestMethod.GET)
    @AccessLogger("??????map?")
    public Object configInfo(@PathVariable("name") String name) {
        return configService.get(name);
    }

    /**
     * ??,array?.
     * ?:[{key:key,value:value}....]
     *
     * @param name
     * @return ?
     */
    @RequestMapping(value = "/{name:.+}.array", method = RequestMethod.GET)
    @AccessLogger("??????(list?)")
    public Object listInfo(@PathVariable("name") String name) {
        String content = configService.getContent(name);
        if (content == null)
            content = "[]";
        return content;
    }

    /**
     * ???
     *
     * @param name ???
     * @param key  ??. system.version
     * @return ?
     */
    @RequestMapping(value = { "/{name:.+}/{key:.+}" }, method = RequestMethod.GET)
    @AccessLogger("??????")
    public Object configInfo(@PathVariable("name") String name, @PathVariable("key") String key) {
        return configService.get(name, key, "");
    }

    @Override
    @RequestMapping(value = "/{id:.+}", method = RequestMethod.GET)
    public ResponseMessage info(@PathVariable("id") String id) {
        return super.info(id);
    }

    @Override
    @Authorize(module = "config", action = "C")
    public ResponseMessage add(@RequestBody Config object) {
        return super.add(object);
    }

    @Override
    @Authorize(module = "config", action = "D")
    @RequestMapping(value = "/{id:.+}", method = RequestMethod.DELETE)
    public ResponseMessage delete(@PathVariable("id") String id) {
        return super.delete(id);
    }

    @Override
    @Authorize(module = "config", action = "U")
    @RequestMapping(value = "/{id:.+}", method = RequestMethod.PUT)
    public ResponseMessage update(@PathVariable("id") String id, @RequestBody Config object) {
        return super.update(id, object);
    }
}