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

shz.core.orm.MetaObject Maven / Gradle / Ivy

There is a newer version: 2024.0.2
Show newest version
package shz.core.orm;

import shz.core.*;
import shz.core.constant.NullConstant;
import shz.core.orm.enums.ValueStrategy;

import java.util.*;

public final class MetaObject {
    final Object entity;
    public final Map cp;
    public final ClassInfo classInfo;
    public final boolean existId;

    MetaObject(Object entity, Map cp, ClassInfo classInfo) {
        this.entity = entity;
        this.cp = cp;
        this.classInfo = classInfo;
        Object idValue = this.cp.get(this.classInfo.idName);
        if (NullHelp.isBlank(idValue)) idValue = AccessibleHelp.getField(this.classInfo.idField, this.entity);
        existId = NullHelp.nonBlank(idValue);
        if (existId) this.cp.put(this.classInfo.idName, idValue);
    }

    private static MetaObject of(Object entity, ClassInfo classInfo, List fieldNames, boolean force) {
        Map cp = ToMap.get(fieldNames.size()).build();
        for (String fieldName : fieldNames) {
            Object val = AccessibleHelp.getField(classInfo.fieldNameFieldMap.get(fieldName), entity);
            if (!force) {
                ValueStrategy strategy = classInfo.fieldNameValueStrategyMap.get(fieldName);
                if (strategy == null || strategy == ValueStrategy.NOT_NULL) {
                    if (val == null) continue;
                } else if (strategy == ValueStrategy.NOT_EMPTY) {
                    if (NullHelp.isEmpty(val)) continue;
                } else if (strategy == ValueStrategy.NOT_BLANK) {
                    if (NullHelp.isBlank(val)) continue;
                }
            }
            cp.put(classInfo.fieldNameColumnMap.get(fieldName), val);
        }
        return new MetaObject(entity, cp, classInfo);
    }

    static MetaObject of(Object entity, ClassInfo classInfo, List fieldNames) {
        boolean force = NullHelp.nonEmpty(fieldNames);
        return of(entity, classInfo, force ? fieldNames : classInfo.fieldNames, force);
    }

    static List of(List entities, ClassInfo classInfo, List fieldNames) {
        boolean force = NullHelp.nonEmpty(fieldNames);
        List temp = force ? fieldNames : classInfo.fieldNames;
        return ToList.explicitCollect(entities.stream().map(entity -> of(entity, classInfo, temp, force)), entities.size());
    }

    Object getId() {
        return cp.get(classInfo.idName);
    }

    void setId(Object id) {
        FieldSetter.simpleSet(classInfo.idField, entity, id);
    }

    Object getVersion() {
        return cp.get(classInfo.versionName);
    }

    void setVersion(Object version) {
        FieldSetter.simpleSet(classInfo.versionField, entity, version);
    }

    public Object getFieldValByName(String fieldName) {
        String column = classInfo.fieldNameColumnMap.get(fieldName);
        return column == null ? NullConstant.OBJECT : cp.get(column);
    }

    public void setFieldValByName(String fieldName, Object val) {
        String column = classInfo.fieldNameColumnMap.get(fieldName);
        if (column != null) cp.put(column, val);
    }

    public void removeFieldByName(String fieldName) {
        String column = classInfo.fieldNameColumnMap.get(fieldName);
        if (column != null) cp.remove(column);
    }

    List columnsForInsert() {
        return new ArrayList<>(cp.keySet());
    }

    List columnsForUpdate() {
        ArrayList columns = new ArrayList<>(cp.keySet());
        columns.remove(classInfo.idName);
        if (classInfo.versionField != null) columns.remove(classInfo.versionName);
        return columns.isEmpty() ? Collections.emptyList() : columns;
    }

    List values(List columns, List extra) {
        if (NullHelp.isEmpty(extra)) return ToList.explicitCollect(columns.stream().map(cp::get), columns.size());
        List values = ToList.explicitCollect(columns.stream().map(cp::get), columns.size() + extra.size());
        values.addAll(extra);
        return values;
    }

    List values(List columns) {
        return values(columns, null);
    }
}