package org.huihoo.ofbiz.smart.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.huihoo.ofbiz.smart.C;
import org.huihoo.ofbiz.smart.base.util.CommUtil;
import org.huihoo.ofbiz.smart.entity.annotation.Id;
import org.huihoo.ofbiz.smart.entity.annotation.IdGenerator;
import org.huihoo.ofbiz.smart.entity.annotation.Table;
import org.huihoo.ofbiz.smart.entity.association.Association;


/**
 * <p>
 * {@link Model}的元数据据
 * </p>
 * 
 * @author huangbaihua
 * @version 1.0
 * @since 1.0
 */
public class ModelMeta implements Serializable {
  private static final long serialVersionUID = 1L;

  private List<Association> associations = new ArrayList<>();
  private final String idName;
  private final String tableName, dbType, dbName;
  private final Class<? extends Model> modelClass;
  private final String idGeneratorCode;
  private Set<String> attributeNamesNoId;

  public ModelMeta(String dbName, Class<? extends Model> modelClass, String dbType) {
    this.modelClass = modelClass;
    this.dbName = dbName;
    this.dbType = dbType;
    this.tableName = getTableName(modelClass);
    this.idName = getIdName(modelClass);
    this.idGeneratorCode = getIdGeneratorCode(modelClass);
  }

  private String getIdName(Class<? extends Model> modelClass) {
    Id idAnno = modelClass.getAnnotation(Id.class);
    return idAnno == null ? "id" : idAnno.value();
  }

  private String getTableName(Class<? extends Model> modelClass) {
    Table tableAnno = modelClass.getAnnotation(Table.class);
    return tableAnno == null ? CommUtil.underscore(modelClass.getSimpleName()) : tableAnno.value();
  }

  private String getIdGeneratorCode(Class<? extends Model> modelClass) {
    IdGenerator idGeneratorAnno = modelClass.getAnnotation(IdGenerator.class);
    return idGeneratorAnno == null ? null : idGeneratorAnno.value();
  }
  
  @Override
  public String toString(){
    StringBuilder sb = new StringBuilder();
    sb.append("Model["+modelClass+"] meta info:");
    sb.append("dbName="+dbName);
    sb.append(",dbType="+dbType);
    sb.append(",tableName="+tableName);
    sb.append(",idName="+idName);
    sb.append(",idGeneratorCode="+idGeneratorCode);
    sb.append(",attributeNamesNoId="+attributeNamesNoId);
    sb.append(",associations:{");
    for (Association assoc:associations) {
      sb.append(assoc.toString()).append(C.LINE_SEPARATOR);
    }
    sb.append("}");
    return sb.toString();
  }
}

@@ -499,17 +499,16 @@
 del%7D%E7%9A%84%E5%85%83%E6%95%B0%E6%8D%AE
-%E6%8D%AE
 %0D%0A * %3C/p
@@ -526,16 +526,17 @@
 @author 
+ 
 huangbai
@@ -567,16 +567,18 @@
  @since 
+  
 1.0%0D%0A */
@@ -681,16 +681,164 @@
  = 1L;%0D%0A
+  /** %E6%A8%A1%E5%9E%8BClass */%0D%0A  private final Class%3C? extends Model%3E modelClass;%0D%0A  /** %E6%9C%AA%E5%8C%85%E5%90%ABID%E7%9A%84%E5%B1%9E%E6%80%A7%E9%9B%86 */%0D%0A  private Set%3CString%3E attributeNamesNoId;%0D%0A  /** %E5%85%B3%E8%81%94%E5%85%B3%E7%B3%BB%E9%9B%86%E5%90%88 */
 %0D%0A  priv
@@ -894,16 +894,31 @@
 t%3C%3E();%0D%0A
+  /** %E4%B8%BB%E9%94%AE%E5%90%8D%E7%A7%B0 */%0D%0A
   privat
@@ -937,24 +937,38 @@
 ng idName;%0D%0A
+  /** %E8%A1%A8%E5%90%8D%E7%A7%B0 */%0D%0A
   private fi
@@ -991,25 +991,25 @@
 Name
-, dbType, dbName;
+;%0D%0A  /** %E6%95%B0%E6%8D%AE%E5%BA%93%E7%B1%BB%E5%9E%8B */
 %0D%0A  
@@ -1026,126 +1026,133 @@
 nal 
-Class%3C? extends Model%3E modelClass;%0D%0A  private final String idGeneratorCode;%0D%0A  private Set%3CString%3E attributeNamesNoId;
+String dbType;%0D%0A  /** %E6%95%B0%E6%8D%AE%E5%BA%93%E5%90%8D%E7%A7%B0 */%0D%0A  private final String dbName;%0D%0A  /** ID%E7%94%9F%E6%88%90%E5%99%A8%E4%BB%A3%E7%A0%81 */%0D%0A  private final String idGeneratorCode;%0D%0A  
 %0D%0A%0D%0A

Update ModelMeta