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

io.xream.x7.common.bean.Parsed Maven / Gradle / Ivy

There is a newer version: 2.3.13.RELEASE
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 io.xream.x7.common.bean;

import io.xream.x7.common.repository.X;
import io.xream.x7.common.util.BeanUtilX;
import io.xream.x7.common.util.StringUtil;

import java.lang.reflect.Field;
import java.util.*;


public class Parsed {


	private boolean isChecked = false;

	private Class clz;
	private String tableName;
	private String originTable;
	private boolean isNoSpec = true;

	private final Map keyMap = new HashMap();
	private final Map keyFieldMap = new HashMap();
	
	private List beanElementList;
	
	private Map elementMap = new HashMap();
	private Map propertyMapperMap = new HashMap();
	private Map mapperPropertyMap = new HashMap();
	private Map mapperPropertyMapLower = new HashMap();
	
	private boolean isNoCache;


	public Class getClz() {
		return clz;
	}

	public void setClz(Class clz) {
		this.clz = clz;
	}
	
	public Parsed(Class clz){
		this.clz = clz;
	}

	public String getId(){
		return String.valueOf(keyMap.get(X.KEY_ONE));
	}
	
	public BeanElement getElement(String property){
		return elementMap.get(property);
	}

	public BeanElement getElementExisted(String property) {

		BeanElement be = elementMap.get(property);
		if (be == null)
			throw new RuntimeException(
					"Not exist: "
							+ property);
		return be;
	}

	public Map getElementMap() {
		return elementMap;
	}

	public Map getKeyMap() {
		return keyMap;
	}
	
	public boolean contains(String property) {
		return this.elementMap.containsKey(property);
	}

	public Map getKeyFieldMap() {
		return keyFieldMap;
	}
	
	public Field getKeyField(int index){
		Field field = keyFieldMap.get(index);
		if (Objects.isNull(field))
			throw new RuntimeException("No setting of PrimaryKey by @X.Key");
		return field;
	}

	public boolean isAutoIncreaseId(Long keyOneValue){
		Field keyOneField = getKeyField(X.KEY_ONE);
		Class keyOneType = keyOneField.getType();

		return keyOneType != String.class && (keyOneValue == null || keyOneValue == 0);
	}

	public String getKey(int index){
		String key = keyMap.get(index);
		if (Objects.isNull(key))
			throw new RuntimeException("No setting of PrimaryKey by @X.Key");
		return key;
	}

	public Long tryToGetLongKey(Object obj){
		Long keyOneValue = 0L;
		Field keyOneField = getKeyField(X.KEY_ONE);
		if (Objects.isNull(keyOneField))
			throw new RuntimeException("No setting of PrimaryKey by @X.Key");
		Class keyOneType = keyOneField.getType();
		if (keyOneType != String.class) {
			try {
				Object keyValue = keyOneField.get(obj);
				if (keyValue != null) {
					keyOneValue = Long.valueOf(keyValue.toString());
				}
			}catch (Exception e){
				throw new RuntimeException(e.getMessage());
			}
		}
		return keyOneValue;
	}

	public List getBeanElementList() {
		return beanElementList;
	}

	public void setBeanElementList(List beanElementList) {
		this.beanElementList = beanElementList;
	}

	public String getOriginTable() {
		return originTable;
	}

	public void setOriginTable(String originTable) {
		this.originTable = originTable;
	}

	public void reset(List beanElementList){
		this.beanElementList = beanElementList;
		this.propertyMapperMap.clear();
		this.mapperPropertyMap.clear();
		this.elementMap.clear();
		this.mapperPropertyMapLower.clear();
		for (BeanElement e : this.beanElementList){
			String property = e.getProperty();
			String mapper = e.getMapper();
			this.elementMap.put(property, e);
			this.propertyMapperMap.put(property, mapper);
			this.mapperPropertyMap.put(mapper, property);
			this.mapperPropertyMapLower.put(mapper.toLowerCase(),property);
		}
	}


	public String getTableName(String alia) {
		if (StringUtil.isNullOrEmpty(alia))
			return tableName;
		if (! alia.toLowerCase().equals(getClzName().toLowerCase()))
			return alia;
		return tableName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = BeanUtilX.filterSQLKeyword(tableName);
	}
	
	public String getClzName() {
		return this.clz.getSimpleName();
	}

	public boolean isNoCache() {
		return isNoCache;
	}

	public void setNoCache(boolean isNoCache) {
		this.isNoCache = isNoCache;
	}


	public String getMapper(String property) {
		return propertyMapperMap.get(property);
	}

	public String getPropertyByLower(String mapper){
		return mapperPropertyMapLower.get(mapper.toLowerCase());
	}
	
	public String getProperty(String mapper){
		return mapperPropertyMap.get(mapper);
	}

	public Map getPropertyMapperMap() {
		return propertyMapperMap;
	}

	public Map getMapperPropertyMap() {
		return mapperPropertyMap;
	}
	
	public boolean isNoSpec() {
		return isNoSpec;
	}

	public void setNoSpec(boolean isNoSpec2) {
		this.isNoSpec = isNoSpec2;
	}

	@Override
	public String toString() {
		return "Parsed{" +
				", isChecked=" + isChecked +
				", clz=" + clz +
				", tableName='" + tableName + '\'' +
				", originTable='" + originTable + '\'' +
				", isNoSpec=" + isNoSpec +
				", keyMap=" + keyMap +
				", keyFieldMap=" + keyFieldMap +
				", beanElementList=" + beanElementList +
				", elementMap=" + elementMap +
				", propertyMapperMap=" + propertyMapperMap +
				", mapperPropertyMap=" + mapperPropertyMap +
				", isNoCache=" + isNoCache +
				'}';
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy