com.handu.open.dubbo.monitor.support.CommonResponse.java Source code

Java tutorial

Introduction

Here is the source code for com.handu.open.dubbo.monitor.support.CommonResponse.java

Source

/**
 * Copyright 2006-2015 handu.com
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 com.handu.open.dubbo.monitor.support;

import org.springframework.validation.FieldError;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * 
 *
 * @author Zhiguo.Chen on 30/6/15.
 */
public class CommonResponse extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    /**
     * 
     */
    public CommonResponse() {
        super();
        this.put("success", false);
    }

    /**
     * ?
     */
    public void success() {
        this.put("success", true);
    }

    /**
     * ??
     * @param message ????
     */
    public void success(String message) {
        this.put("success", true);
        this.put("message", message);
    }

    /**
     * ?
     * @param message ???
     */
    public void fail(String message) {
        this.put("success", false);
        this.put("message", message);
    }

    /**
     * ???
     * @param message
     * @param errors
     */
    public void fail(String message, List<FieldError> errors) {
        this.put("success", false);
        StringBuilder messageBuilder = new StringBuilder(message);
        messageBuilder.append("<ul>");
        for (FieldError fieldError : errors) {
            messageBuilder.append("<li>").append(fieldError.getDefaultMessage()).append("</li>");
        }
        messageBuilder.append("</ul>");
        this.put("message", messageBuilder.toString());
    }

    /**
     * ???
     * @param url ???URL
     */
    public void redirect(String url) {
        this.put("redirect", url);
    }

    /**
     * ??
     * @param data
     */
    public void setData(Object data) {
        Collection collection;
        if (!containsKey("data") || get("data") == null) {
            collection = new ArrayList();
            put("data", collection);
        } else {
            collection = (Collection) get("data");
        }
        collection.add(data);
    }

    /**
     * ??
     * @param collection
     */
    public void setData(Collection collection) {
        this.put("data", collection);
    }

    /**
     * ?
     * @return
     */
    public static CommonResponse createCommonResponse() {
        CommonResponse commonResponse = new CommonResponse();
        commonResponse.success();
        return commonResponse;
    }

    /**
     * , ??, success
     * @param data
     * @return
     */
    public static CommonResponse createCommonResponse(Object data) {
        CommonResponse commonResponse = new CommonResponse();
        commonResponse.success();
        commonResponse.setData(data);
        return commonResponse;
    }
}