From 0a1a9a317f6ecefd5f246e9861e3ecee9d1958f7 Mon Sep 17 00:00:00 2001
From: joinee0208 <364595444@qq.com>
Date: Fri, 24 Jun 2016 14:37:47 +0800
Subject: [PATCH] add default value and pk autoincrement

---
 DaoGenerator/src-template/dao.ftl             |  9 +++++
 DaoGenerator/src-template/entity.ftl          | 38 +++++++++++++++++--
 .../de/greenrobot/daogenerator/Property.java  | 20 +++++++++-
 3 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/DaoGenerator/src-template/dao.ftl b/DaoGenerator/src-template/dao.ftl
index 60f7045b1..2d2184918 100644
--- a/DaoGenerator/src-template/dao.ftl
+++ b/DaoGenerator/src-template/dao.ftl
@@ -135,10 +135,19 @@ as property>\"${property.columnName}\"<#if property_has_next>,</#if></#list>);")
         }
 </#if>
 <#else> <#-- nullable, non-protobuff -->
+        <#if property.pkAutoincrement>
+        //PrimaryKey Autoincrement,so we do not care this property.
+        /** ${property.javaTypeInEntity} ${property.propertyName} = entity.get${property.propertyName?cap_first}();
+                  if (${property.propertyName} != null) {
+                      stmt.bind${toBindType[property.propertyType]}(${property_index + 1}, ${property.databaseValueExpression});
+                  }
+        */
+        <#else>
         ${property.javaTypeInEntity} ${property.propertyName} = entity.get${property.propertyName?cap_first}();
         if (${property.propertyName} != null) {
             stmt.bind${toBindType[property.propertyType]}(${property_index + 1}, ${property.databaseValueExpression});
         }
+        </#if>
 </#if>
 </#list>
 <#list entity.toOneRelations as toOne>
diff --git a/DaoGenerator/src-template/entity.ftl b/DaoGenerator/src-template/entity.ftl
index 0e383d847..273ea3293 100644
--- a/DaoGenerator/src-template/entity.ftl
+++ b/DaoGenerator/src-template/entity.ftl
@@ -101,21 +101,48 @@ ${keepFields!}    // KEEP FIELDS END
 </#if>
 <#if entity.constructors>
     public ${entity.className}() {
+      	<#list entity.properties as property>
+      	<#if property.defValType>
+    		this.${property.propertyName} = ${property.defValue};
+    	</#if>
+    	</#list>
     }
 <#if entity.propertiesPk?has_content && entity.propertiesPk?size != entity.properties?size>
 
+    <#if entity.propertiesPk?has_content && entity.propertiesPk?size == 1 && entity.propertiesPk[0].pkAutoincrement>
+    //pass one aleady constructor.
+    <#else>
     public ${entity.className}(<#list entity.propertiesPk as
-property>${property.javaType} ${property.propertyName}<#if property_has_next>, </#if></#list>) {
-<#list entity.propertiesPk as property>
-        this.${property.propertyName} = ${property.propertyName};
-</#list>
+property><#if !property.pkAutoincrement>${property.javaType} ${property.propertyName}<#if property_has_next>, </#if></#if></#list>) {
+        this();
+        <#list entity.propertiesPk as property>
+                <#if !property.pkAutoincrement>this.${property.propertyName} = ${property.propertyName};</#if>
+                <#if property.defValType>
+                if(this.${property.propertyName}==null)this.${property.propertyName} = ${property.defValue};
+                </#if>
+        </#list>
     }
+    </#if>
 </#if>
 
+<#if entity.propertiesPk?has_content && (entity.propertiesPk?size > 0) && entity.propertiesPk[0].pkAutoincrement>
+    public ${entity.className}(<#list entity.properties as
+property><#if !property.pkAutoincrement>${property.javaTypeInEntity} ${property.propertyName}<#if property_has_next>, </#if></#if></#list>) {
+<#list entity.properties as property>
+        <#if !property.pkAutoincrement>this.${property.propertyName} = ${property.propertyName};</#if>
+        <#if property.defValType>
+        if(this.${property.propertyName}==null)this.${property.propertyName} = ${property.defValue};
+        </#if>
+</#list>
+    }
+</#if>
     public ${entity.className}(<#list entity.properties as
 property>${property.javaTypeInEntity} ${property.propertyName}<#if property_has_next>, </#if></#list>) {
 <#list entity.properties as property>
         this.${property.propertyName} = ${property.propertyName};
+        <#if property.defValType>
+        if(this.${property.propertyName}==null)this.${property.propertyName} = ${property.defValue};
+        </#if>
 </#list>
     }
 </#if>
@@ -153,6 +180,9 @@ ${property.javaDocSetter}
 </#if>
     public void set${property.propertyName?cap_first}(${property.javaTypeInEntity} ${property.propertyName}) {
         this.${property.propertyName} = ${property.propertyName};
+        <#if property.defValType>
+        if(this.${property.propertyName}==null) this.${property.propertyName} = ${property.defValue};
+        </#if>
     }
 
 </#list>
diff --git a/DaoGenerator/src/de/greenrobot/daogenerator/Property.java b/DaoGenerator/src/de/greenrobot/daogenerator/Property.java
index 14534df55..f2ae8e40c 100644
--- a/DaoGenerator/src/de/greenrobot/daogenerator/Property.java
+++ b/DaoGenerator/src/de/greenrobot/daogenerator/Property.java
@@ -26,7 +26,12 @@ public static class PropertyBuilder {
         public PropertyBuilder(Schema schema, Entity entity, PropertyType propertyType, String propertyName) {
             property = new Property(schema, entity, propertyType, propertyName);
         }
-
+        public PropertyBuilder defValue(String pVal)
+        {
+            property.defValue = pVal;
+            property.defValType = true;
+            return this;
+        }
         public PropertyBuilder columnName(String columnName) {
             property.columnName = columnName;
             return this;
@@ -166,6 +171,8 @@ public Property getProperty() {
     private final Entity entity;
     private PropertyType propertyType;
     private final String propertyName;
+    private boolean defValType;
+    private Object defValue;
 
     private String columnName;
     private String columnType;
@@ -199,6 +206,7 @@ public Property getProperty() {
     private String javaType;
 
     public Property(Schema schema, Entity entity, PropertyType propertyType, String propertyName) {
+        defValType = false;
         this.schema = schema;
         this.entity = entity;
         this.propertyName = propertyName;
@@ -229,7 +237,7 @@ public boolean isPrimaryKey() {
         return primaryKey;
     }
 
-    public boolean isAutoincrement() {
+    public boolean isPkAutoincrement() {
         return pkAutoincrement;
     }
 
@@ -248,6 +256,14 @@ public boolean isNotNull() {
     public String getJavaType() {
         return javaType;
     }
+    public String getDefValue()
+    {
+        return (String)defValue;
+    }
+    public boolean isDefValType()
+    {
+        return defValType;
+    }
 
     public String getJavaTypeInEntity() {
         if (customTypeClassName != null) {