Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2015 HaiYang Li
*
* 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
*
* 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 com.landawn.abacus.metadata.sql;
import static com.landawn.abacus.util.WD._PERIOD;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.landawn.abacus.core.NameUtil;
import com.landawn.abacus.idGenerator.AutoIncrementIdGenerator;
import com.landawn.abacus.idGenerator.IdGenerator;
import com.landawn.abacus.idGenerator.IdGeneratorFactory;
import com.landawn.abacus.metadata.Column;
import com.landawn.abacus.metadata.EntityDefXmlEle.EntityDefEle;
import com.landawn.abacus.metadata.EntityDefXmlEle.EntityDefEle.EntityEle;
import com.landawn.abacus.metadata.EntityDefXmlEle.EntityDefEle.EntityEle.PropertyEle;
import com.landawn.abacus.metadata.EntityDefinition;
import com.landawn.abacus.metadata.EntityDefinitionFactory;
import com.landawn.abacus.metadata.Property;
import com.landawn.abacus.metadata.Table;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.util.ClassUtil;
import com.landawn.abacus.util.Configuration;
import com.landawn.abacus.util.ImmutableList;
import com.landawn.abacus.util.ImmutableMap;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.XMLUtil;
// TODO: Auto-generated Javadoc
/**
*
* @author Haiyang Li
* @since 0.8
*/
public class SQLEntityDefinition implements EntityDefinition {
private final String javaType;
private final Class> typeClazz;
private final Class> arrayTypeClazz;
private final String name;
private final String tableName;
private final Map attrs;
private final Map propPool = new ConcurrentHashMap<>();
private final List propList;
private final List propNameList;
private final List idPropList;
private final List idPropNameList;
private final List uidPropList;
private final List uidPropNameList;
private final List entityPropList;
private final List defaultLoadPropList;
private final List defaultLoadPropNameList;
private final List defaultOnInsertPropList;
private final List defaultOnUpdatePropList;
private final Map> idGeneratorMap;
private final List> idGeneratorList;
private final List sliceEntityDefList;
private final boolean isIdAutoGenerated;
private final boolean isSliceEntity;
private EntityDefinition parentEntity;
private EntityDefinitionFactory factory;
public SQLEntityDefinition(String name, Table table, Method columnName2PropName) {
this(null, Object.class, name, table.getName(), false, parse(name, table, columnName2PropName));
}
protected SQLEntityDefinition(EntityDefinitionFactory factory, String pkgName, InputStream is) {
this(factory, pkgName, Configuration.parse(is).getDocumentElement());
}
protected SQLEntityDefinition(EntityDefinitionFactory factory, String pkgName, Element entityNode) {
this(factory, initClass(pkgName, entityNode), entityNode.getAttribute(EntityEle.NAME), entityNode.getAttribute(EntityEle.TABLE), false,
parse(factory, pkgName, entityNode));
}
SQLEntityDefinition(final EntityDefinitionFactory factory, final Class> cls, final String name, final String tableName, boolean isSliceEntity,
final Object[] propsAndSlices) {
if (N.isNullOrEmpty(name)) {
throw new RuntimeException("Entity name can't be null or empty.");
}
// use entity name if table name is null or empty
// if (N.isNullOrEmpty(tableName)) {
// throw new RuntimeException("Table name can't be null or empty.");
// }
final Map attrs = (Map) propsAndSlices[0];
final Map props = (Map) propsAndSlices[1];
final List sliceEntityDefList = (List) propsAndSlices[2];
this.factory = factory;
this.javaType = ClassUtil.getCanonicalClassName(cls);
this.typeClazz = cls;
this.arrayTypeClazz = Array.newInstance(typeClazz, 0).getClass();
this.name = NameUtil.getCachedName(name);
attrs.put(EntityEle.NAME, this.name);
this.tableName = N.isNullOrEmpty(tableName) ? this.name : NameUtil.getCachedName(tableName);
attrs.put(EntityEle.TABLE, this.tableName);
this.attrs = ImmutableMap.of(attrs);
final List propList = new ArrayList<>();
final List propNameList = new ArrayList<>();
final List idPropList = new ArrayList<>();
final List idPropNameList = new ArrayList<>();
final List uidPropList = new ArrayList<>();
final List uidPropNameList = new ArrayList<>();
final List entityPropList = new ArrayList<>();
final List defaultLoadPropList = new ArrayList<>();
final List defaultLoadPropNameList = new ArrayList<>();
final List defaultOnInsertPropList = new ArrayList<>();
final List defaultOnUpdatePropList = new ArrayList<>();
final Map> idGeneratorMap = new HashMap<>();
final List> idGeneratorList = new ArrayList<>();
for (SQLProperty prop : props.values()) {
prop.setEntityDefinition(this);
propPool.put(prop.getName(), prop);
propPool.put(prop.getCanonicalName(), prop);
propList.add(prop);
propNameList.add(prop.getName());
if (prop.isId()) {
idPropList.add(prop);
idPropNameList.add(prop.getName());
String idGeneratorAttr = prop.getAttribute(PropertyEle.ID_GENERATOR);
if (idGeneratorAttr != null) {
IdGenerator