From f14905905c81ad5ec1e0f2ba3b7c6f66129f7ee1 Mon Sep 17 00:00:00 2001 From: wuxiangpeng <97046335@qq.com> Date: Thu, 31 Dec 2015 18:26:55 +0800 Subject: [PATCH] Add auto generate Parcelable & Json serialize code . --- DaoGenerator/src-template/entity.ftl | 101 ++++++++++++++---- .../test/SimpleDaoGeneratorTest.java | 2 + .../de/greenrobot/daogenerator/Entity.java | 21 ++++ .../de/greenrobot/daogenerator/Property.java | 16 +++ 4 files changed, 122 insertions(+), 18 deletions(-) diff --git a/DaoGenerator/src-template/entity.ftl b/DaoGenerator/src-template/entity.ftl index 0e383d847..28872e9c0 100644 --- a/DaoGenerator/src-template/entity.ftl +++ b/DaoGenerator/src-template/entity.ftl @@ -1,19 +1,19 @@ <#-- Copyright (C) 2011-2015 Markus Junginger, greenrobot (http://greenrobot.de) - -This file is part of greenDAO Generator. - -greenDAO Generator is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. -greenDAO Generator is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License + +This file is part of greenDAO Generator. + +greenDAO Generator is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. +greenDAO Generator is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with greenDAO Generator. If not, see . --> @@ -30,6 +30,15 @@ import ${schema.defaultJavaPackageDao}.DaoSession; import de.greenrobot.dao.DaoException; +<#if entity.implParcelable> +import android.os.Parcel; +import android.os.Parcelable; + +<#if entity.implJsonSerializable> +import org.json.JSONObject; +import org.json.JSONException; + + <#if entity.additionalImportsEntity?has_content> <#list entity.additionalImportsEntity as additionalImport> import ${additionalImport}; @@ -42,7 +51,7 @@ import ${additionalImport}; // KEEP INCLUDES - put your custom includes here <#if keepIncludes?has_content>${keepIncludes!}// KEEP INCLUDES END <#else> -// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit. +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit. <#if entity.javaDoc ??> @@ -207,7 +216,7 @@ ${property.javaDocSetter} synchronized (this) { this.${toOne.name} = ${toOne.name}; -<#if toOne.useFkProperty> +<#if toOne.useFkProperty> ${toOne.fkProperties[0].propertyName} = <#if !toOne.fkProperties[0].notNull>${toOne.name} == null ? null : ${toOne.name}.get${toOne.targetEntity.pkProperty.propertyName?cap_first}(); ${toOne.name}__resolvedKey = ${toOne.fkProperties[0].propertyName}; <#else> @@ -258,7 +267,7 @@ ${property.javaDocSetter} public void delete() { if (myDao == null) { throw new DaoException("Entity is detached from DAO context"); - } + } myDao.delete(this); } @@ -266,7 +275,7 @@ ${property.javaDocSetter} public void update() { if (myDao == null) { throw new DaoException("Entity is detached from DAO context"); - } + } myDao.update(this); } @@ -274,7 +283,7 @@ ${property.javaDocSetter} public void refresh() { if (myDao == null) { throw new DaoException("Entity is detached from DAO context"); - } + } myDao.refresh(this); } @@ -284,4 +293,60 @@ ${property.javaDocSetter} ${keepMethods!} // KEEP METHODS END + +<#if entity.implParcelable> + //Parcelable + protected ${entity.className} (Parcel in) { + <#list entity.properties as property> + ${property.propertyName} = in.read${property.parcelableTypeInEntity}(); + + } + + public static final Creator<${entity.className}> CREATOR = new Creator<${entity.className}>() { + @Override + public ${entity.className} createFromParcel(Parcel in) { + return new ${entity.className}(in); + } + + @Override + public ${entity.className}[] newArray(int size) { + return new ${entity.className}[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + <#list entity.properties as property> + dest.write${property.parcelableTypeInEntity}(${property.propertyName}); + + } + //Parcelable END + +<#if entity.implJsonSerializable> + + //JsonSerializable + public static ${entity.className} fromJson(JSONObject jsonObject) throws JSONException { + ${entity.className} domain = new ${entity.className}(); + + <#list entity.properties as property> + domain.${property.propertyName} = jsonObject.get${property.jsonTypeInEntity}("${property.propertyName}"); + + + return domain; + } + + public static String toJson(${entity.className} obj) throws JSONException { + JSONObject jsonObject = new JSONObject(); + <#list entity.properties as property> + jsonObject.put("${property.propertyName}",obj.${property.propertyName}); + + return jsonObject.toString(); + } + //JsonSerializable END + } diff --git a/DaoGenerator/src-test/de/greenrobot/daogenerator/test/SimpleDaoGeneratorTest.java b/DaoGenerator/src-test/de/greenrobot/daogenerator/test/SimpleDaoGeneratorTest.java index b1a9f583a..c0a09e51b 100644 --- a/DaoGenerator/src-test/de/greenrobot/daogenerator/test/SimpleDaoGeneratorTest.java +++ b/DaoGenerator/src-test/de/greenrobot/daogenerator/test/SimpleDaoGeneratorTest.java @@ -37,6 +37,8 @@ public void testMinimalSchema() throws Exception { Property idProperty = addressEntity.addIdProperty().getProperty(); addressEntity.addIntProperty("count").index(); addressEntity.addIntProperty("dummy").notNull(); + addressEntity.setImplJsonSerializable(true); + addressEntity.setImplParcelable(true); assertEquals(1, schema.getEntities().size()); assertEquals(3, addressEntity.getProperties().size()); diff --git a/DaoGenerator/src/de/greenrobot/daogenerator/Entity.java b/DaoGenerator/src/de/greenrobot/daogenerator/Entity.java index da865a423..2db63c000 100644 --- a/DaoGenerator/src/de/greenrobot/daogenerator/Entity.java +++ b/DaoGenerator/src/de/greenrobot/daogenerator/Entity.java @@ -75,6 +75,9 @@ public class Entity { private Boolean active; private Boolean hasKeepSections; + private boolean implParcelable; + private boolean implJsonSerializable; + Entity(Schema schema, String className) { this.schema = schema; this.className = className; @@ -93,6 +96,21 @@ public class Entity { constructors = true; } + public void setImplParcelable(boolean impl){ + implParcelable = impl; + } + + public boolean getImplParcelable(){ + return implParcelable; + } + public void setImplJsonSerializable(boolean impl){ + implParcelable = impl; + } + + public boolean getImplJsonSerializable(){ + return implParcelable; + } + public PropertyBuilder addBooleanProperty(String propertyName) { return addProperty(PropertyType.Boolean, propertyName); } @@ -437,6 +455,9 @@ public void setHasKeepSections(Boolean hasKeepSections) { } public List getInterfacesToImplement() { + if(implParcelable && !interfacesToImplement.contains("Parcelable")){ + interfacesToImplement.add("Parcelable"); + } return interfacesToImplement; } diff --git a/DaoGenerator/src/de/greenrobot/daogenerator/Property.java b/DaoGenerator/src/de/greenrobot/daogenerator/Property.java index 14534df55..1bb892237 100644 --- a/DaoGenerator/src/de/greenrobot/daogenerator/Property.java +++ b/DaoGenerator/src/de/greenrobot/daogenerator/Property.java @@ -257,6 +257,22 @@ public String getJavaTypeInEntity() { } } + public String getJsonTypeInEntity(){ + String javaType = getJavaTypeInEntity(); + if("Integer".equals(javaType)){ + javaType = "Int"; + } + if(Character.isLowerCase(javaType.charAt(0))){ + javaType = new StringBuilder().append(Character.toUpperCase(javaType.charAt(0))).append(javaType.substring(1)).toString(); + } + + return javaType; + } + + public String getParcelableTypeInEntity(){ + return getJsonTypeInEntity(); + } + public int getOrdinal() { return ordinal; }