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

panda.dao.entity.EntityHelper Maven / Gradle / Ivy

Go to download

Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.

There is a newer version: 1.8.0
Show newest version
package panda.dao.entity;

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

import panda.cast.Castors;
import panda.dao.Dao;
import panda.dao.query.DataQuery;
import panda.lang.Collections;
import panda.lang.Objects;

public abstract class EntityHelper {
	/**
	 * clear primary key value of data
	 * @param entity the Entity
	 * @param data the data
	 */
	public static  void clearPrimaryKeyValues(Entity entity, T data) {
		if (data == null) {
			return;
		}
		
		List pks = entity.getPrimaryKeys();
		for (EntityField pk : pks) {
			Object value = Castors.scast(null, pk.getType());
			pk.setValue(data, value);
		}
	}

	/**
	 * clear identity value of data
	 * @param entity the Entity
	 * @param data the data
	 */
	public static  void clearIdentityValue(Entity entity, T data) {
		if (data == null) {
			return;
		}
		
		EntityField eid = entity.getIdentity();
		if (eid != null) {
			Object value = Castors.scast(null, eid.getType());
			eid.setValue(data, value);
		}
	}

	/**
	 * copy identity value from source data to destination data
	 * @param entity entity
	 * @param src source data
	 * @param des destination data
	 */
	public static  void copyIdentityValue(Entity entity, T src, T des) {
		if (src == null || des == null || src == des) {
			return;
		}

		EntityField eid = entity.getIdentity();
		if (eid != null) {
			eid.setValue(des, eid.getValue(src));
		}
	}

	/**
	 * copy primary key values from source data to destination data
	 * @param entity entity
	 * @param src source data
	 * @param des destination data
	 */
	public static  void copyPrimaryKeyValues(Entity entity, T src, T des) {
		if (src == null || des == null || src == des) {
			return;
		}

		Collection efs = entity.getPrimaryKeys();
		for (EntityField ef : efs) {
			ef.setValue(des, ef.getValue(src));
		}
	}

	/**
	 * @param efs the EntityField list
	 * @param lhs the left object
	 * @param rhs the right object
	 * @return true if lhs is different with rhs
	 */
	public static boolean isDifferent(Collection efs, Object lhs, Object rhs) {
		for (EntityField ef : efs) {
			Object lv = ef.getValue(lhs);
			Object rv = ef.getValue(rhs);
			if (!Objects.equals(lv, rv)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * @param entity the Entity
	 * @param data the data
	 * @return true if check successfully
	 */
	public static  boolean hasPrimaryKeyValues(Entity entity, T data) {
		for (EntityField ef : entity.getPrimaryKeys()) {
			Object dv = ef.getValue(data);
			if (dv == null) {
				return false;
			}
		}

		return true;
	}

	/**
	 * find duplicate unique index
	 * @param dao dao
	 * @param entity entity
	 * @param data data
	 * @param sdat source data, check unique index data modified or not if sdat is supplied 
	 * @return duplicate unique index
	 */
	public static  EntityIndex findDuplicateUniqueIndex(Dao dao, Entity entity, T data, T sdat) {
		Collection eis = entity.getIndexes();
		if (Collections.isEmpty(eis)) {
			return null;
		}
		
		for (EntityIndex ei : eis) {
			if (!ei.isUnique()) {
				continue;
			}

			if (sdat != null) {
				if (EntityHelper.isDifferent(ei.getFields(), data, sdat)) {
					if (findDuplicatedUniqueIndex(dao, entity, ei, data)) {
						return ei;
					}
				}
			}
			else {
				if (findDuplicatedUniqueIndex(dao, entity, ei, data)) {
					return ei;
				}
			}
		}
		return null;
	}

	public static  boolean findDuplicatedUniqueIndex(Dao dao, Entity entity, EntityIndex ei, T data) {
		if (!ei.isUnique()) {
			return false;
		}

		DataQuery q = new DataQuery(entity);
		for (EntityField ef : ei.getFields()) {
			Object dv = ef.getValue(data);
			if (dv == null) {
				return false;
			}
			q.eq(ef.getName(), dv);
		}

		return dao.exists(q);
	}

	/**
	 * findIncorrectForeignKey
	 * @param dao dao
	 * @param entity entity
	 * @param data data
	 * @return true if check successfully
	 */
	public static  EntityFKey findIncorrectForeignKey(Dao dao, Entity entity, T data) {
		Collection efks = entity.getForeignKeys();
		if (Collections.isEmpty(efks)) {
			return null;
		}
		
		for (EntityFKey efk : efks) {
			if (!checkForeignKey(dao, entity, data, efk)) {
				return efk;
			}
		}
		return null;
	}
	
	/**
	 * findIncorrectForeignKeys
	 * 
	 * @param dao DAO object
	 * @param entity the Entity
	 * @param data the data to check
	 * @return true if check successfully
	 */
	public static  List findIncorrectForeignKeys(Dao dao, Entity entity, T data) {
		Collection efks = entity.getForeignKeys();
		if (Collections.isEmpty(efks)) {
			return null;
		}

		List eefks = new ArrayList();
		for (EntityFKey efk : efks) {
			if (!checkForeignKey(dao, entity, data, efk)) {
				eefks.add(efk);
			}
		}
		return eefks.isEmpty() ? null : eefks;
	}

	/**
	 * checkForeignKey
	 * 
	 * @param dao the DAO
	 * @param entity the Entity
	 * @param data the data
	 * @param efk the EntityFKey
	 * @return true if check successfully
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static  boolean checkForeignKey(Dao dao, Entity entity, T data, EntityFKey efk) {
		boolean allNull = true;
		
		DataQuery q = new DataQuery(efk.getReference());

		int i = 0;
		for (EntityField rf : efk.getReference().getPrimaryKeys()) {
			EntityField ef = efk.getFields().get(i);
			Object dv = ef.getValue(data);
			if (dv == null) {
				q.isNull(rf.getName());
			}
			else {
				allNull = false;
				q.eq(rf.getName(), dv);
			}
			i++;
		}

		if (!allNull) {
			q.setLimit(1);
			if (dao.count(q) < 1) {
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * check not null fields
	 * 
	 * @param entity the Entity
	 * @param data the data to check
	 * @return null fields
	 */
	public static  List checkNotNulls(Entity entity, T data) {
		List nulls = new ArrayList();
		for (EntityField ef : entity.getFields()) {
			if (ef.isNotNull() && !ef.isAutoGenerate()) {
				Object o = ef.getValue(data);
				if (o == null) {
					nulls.add(ef);
				}
			}
		}
		return nulls.isEmpty() ? null : nulls;
	}
	
	/**
	 * check not null fields
	 * 
	 * @param efs the EntityField list
	 * @param data the data to check
	 * @return null fields
	 */
	public static  List checkNotNulls(List efs, T data) {
		List nulls = new ArrayList();
		for (EntityField ef : efs) {
			Object o = ef.getValue(data);
			if (o == null) {
				nulls.add(ef);
			}
		}
		return nulls.isEmpty() ? null : nulls;
	}

	/**
	 * fetch data by the entity field key.
	 * if key is null, then null will be returned.
	 * @param dao dao
	 * @param entity entity
	 * @param ef entity field
	 * @param data the data contains the query key
	 * @return the fetched data (first 1)
	 */
	public static  T fetchDataByField(Dao dao, Entity entity, EntityField ef, T data) {
		Object dv = ef.getValue(data);
		if (dv == null) {
			return null;
		}
		
		DataQuery q = new DataQuery(entity);
		q.eq(ef.getName(), dv).limit(1);
		return dao.fetch(q);
	}
	
	/**
	 * fetch data by the keys of entity fields.
	 * if keys is null, then null will be returned.
	 * @param dao dao
	 * @param entity entity
	 * @param fs entity fields
	 * @param data the data contains the query keys
	 * @return the fetched data (first 1)
	 */
	public static  T fetchDataByFields(Dao dao, Entity entity, String[] fs, T data) {
		Collection efs = entity.getFields(fs);
		return fetchDataByFields(dao, entity, efs, data);
	}
	
	/**
	 * fetch data by the keys of entity fields.
	 * if keys is null, then null will be returned.
	 * @param dao dao
	 * @param entity entity
	 * @param efs entity fields
	 * @param data the data contains the query keys
	 * @return the fetched data (first 1)
	 */
	public static  T fetchDataByFields(Dao dao, Entity entity, Collection efs, T data) {
		boolean allNull = true;

		DataQuery q = new DataQuery(entity);
		for (Object o : efs) {
			EntityField ef = null;
			if (o instanceof EntityField) {
				ef = (EntityField)o;
			}
			else if (o instanceof String) {
				ef = entity.getField((String)o);
			}
			else {
				throw new IllegalArgumentException("Invalid entity field: " + (o == null ? null : o.getClass()));
			}

			Object dv = ef.getValue(data);
			if (dv == null) {
				q.isNull(ef.getName());
			}
			else {
				allNull = false;
				q.eq(ef.getName(), dv);
			}
		}

		if (allNull) {
			return null;
		}
		
		q.limit(1);
		return dao.fetch(q);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy