All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.soento.core.lang.Bean Maven / Gradle / Ivy

package com.soento.core.lang;

import com.soento.core.consts.Constants;
import com.soento.core.enums.DeleteFlag;
import com.soento.core.util.JsonUtil;
import com.soento.core.util.StringUtil;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedHashMap;

/**
 * 实体对象
 *
 * @author: yantao.zeng
 */
public class Bean extends LinkedHashMap implements Serializable {

    public void initId(String id) {
        setField(Constants.ID, id);
    }

    public void initEntity(String userId) {
        setField(Constants.CREATE_BY, userId);
        setField(Constants.MODIFY_BY, userId);
        setField(Constants.CREATE_DATE, new Date());
        setField(Constants.MODIFY_DATE, new Date());
        setField(Constants.DELETE_FLAG, DeleteFlag.NOT_DELETED.getValue());
    }

    public void initEntity() {
        initEntity(Constants.ANONYMOUS);
    }

    public void initEntity(UserInfo user) {
        if (user != null && StringUtil.isNotBlank(user.getId())) {
            initEntity(user.getId());
        } else {
            initEntity();
        }
    }

    public void updateEntity(String userId) {
        setField(Constants.MODIFY_BY, userId);
        setField(Constants.MODIFY_DATE, new Date());
    }

    public void updateEntity() {
        updateEntity(Constants.ANONYMOUS);
    }

    public void updateEntity(UserInfo user) {
        if (user != null && StringUtil.isNotBlank(user.getId())) {
            updateEntity(user.getId());
        } else {
            updateEntity();
        }
    }

    public boolean hasField(String field) {
        return keySet().contains(field);
    }

    public void setField(String field, Object value) {
        this.put(field, value);
    }

    public String getString(String field) {
        Object obj = get(field);
        if (obj == null) {
            return null;
        }
        if (obj instanceof BigDecimal) {
            return ((BigDecimal) obj).toPlainString();
        } else {
            return obj.toString();
        }
    }

    public  T get(String field, Class clazz) {
        return (T) get(field);
    }

    public String toJson() {
        return JsonUtil.toJson(this);
    }

    public String toJsonp(String function) {
        return JsonUtil.toJsonp(function, this);
    }

    public  T toObject(Class clazz) {
        return JsonUtil.toObject(toJson(), clazz);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy