Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto generate Parcelable & Json serialize code #280

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 83 additions & 18 deletions DaoGenerator/src-template/entity.ftl
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

-->
Expand All @@ -30,6 +30,15 @@ import ${schema.defaultJavaPackageDao}.DaoSession;
import de.greenrobot.dao.DaoException;

</#if>
<#if entity.implParcelable>
import android.os.Parcel;
import android.os.Parcelable;
</#if>
<#if entity.implJsonSerializable>
import org.json.JSONObject;
import org.json.JSONException;
</#if>

<#if entity.additionalImportsEntity?has_content>
<#list entity.additionalImportsEntity as additionalImport>
import ${additionalImport};
Expand All @@ -42,7 +51,7 @@ import ${additionalImport};
// KEEP INCLUDES - put your custom includes here
<#if keepIncludes?has_content>${keepIncludes!}</#if>// 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>
<#if entity.javaDoc ??>

Expand Down Expand Up @@ -207,7 +216,7 @@ ${property.javaDocSetter}
</#if>
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 : </#if>${toOne.name}.get${toOne.targetEntity.pkProperty.propertyName?cap_first}();
${toOne.name}__resolvedKey = ${toOne.fkProperties[0].propertyName};
<#else>
Expand Down Expand Up @@ -258,23 +267,23 @@ ${property.javaDocSetter}
public void delete() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
}
myDao.delete(this);
}

/** Convenient call for {@link AbstractDao#update(Object)}. Entity must attached to an entity context. */
public void update() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
}
myDao.update(this);
}

/** Convenient call for {@link AbstractDao#refresh(Object)}. Entity must attached to an entity context. */
public void refresh() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
}
myDao.refresh(this);
}

Expand All @@ -284,4 +293,60 @@ ${property.javaDocSetter}
${keepMethods!} // KEEP METHODS END

</#if>

<#if entity.implParcelable>
//Parcelable
protected ${entity.className} (Parcel in) {
<#list entity.properties as property>
${property.propertyName} = in.read${property.parcelableTypeInEntity}();
</#list>
}

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});
</#list>
}
//Parcelable END
</#if>
<#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}");
</#list>

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});
</#list>
return jsonObject.toString();
}
//JsonSerializable END
</#if>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
21 changes: 21 additions & 0 deletions DaoGenerator/src/de/greenrobot/daogenerator/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -437,6 +455,9 @@ public void setHasKeepSections(Boolean hasKeepSections) {
}

public List<String> getInterfacesToImplement() {
if(implParcelable && !interfacesToImplement.contains("Parcelable")){
interfacesToImplement.add("Parcelable");
}
return interfacesToImplement;
}

Expand Down
16 changes: 16 additions & 0 deletions DaoGenerator/src/de/greenrobot/daogenerator/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down