diff --git a/Java/Class.java.twig b/Java/Class.java.twig index c3ab5bf..3723f45 100644 --- a/Java/Class.java.twig +++ b/Java/Class.java.twig @@ -3,6 +3,20 @@ */ package {{ packageName }}.api; +{%- if (storageAttributes is not null) %} + +import android.arch.persistence.room.Delete; +import android.arch.persistence.room.Entity; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.OnConflictStrategy; +import android.arch.persistence.room.Query; +import android.arch.persistence.room.Transaction; +import android.arch.persistence.room.TypeConverter; +import android.arch.persistence.room.TypeConverters; + +import io.reactivex.Flowable; +import io.reactivex.Single; +{%- endif %} import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.util.ObjectsCompat; @@ -29,14 +43,6 @@ import ru.touchin.templates.ApiModel; import ru.touchin.roboswag.core.utils.ObjectUtils; import ru.touchin.templates.logansquare.LoganSquareJsonModel; -{%- if (storageAttributes is not null) %} - -import android.arch.persistence.room.Entity; -import android.arch.persistence.room.PrimaryKey; -import android.arch.persistence.room.TypeConverters; -import android.arch.persistence.room.TypeConverter; -{%- endif %} - {%- if (description is not empty) %} /** @@ -50,20 +56,18 @@ import android.arch.persistence.room.TypeConverter; {%- set attributeWas = true -%} tableName = "{{ storageAttributes.tableName }}" {%- endif -%} - {%- if attributeWas -%}, {% endif %} + {%- if attributeWas and storageAttributes.primaryKeys is not empty -%}, {% endif %} {%- if (storageAttributes.primaryKeys is not empty) -%} {%- set attributeWas = true -%} - primaryKeys = { {%- for key in storageAttributes.primaryKeys -%} {%- if not (loop.last) %}, {% endif -%} "{{- key.name -}}" {%- endfor -%} } + primaryKeys = { {%- for key in storageAttributes.primaryKeys -%} {%- if not (loop.last) %}, {% endif -%} "{{- key -}}" {%- endfor -%} } {%- endif -%} ) @TypeConverters({{ type.baseTypeName }}.class) {%- endif %} -@JsonObject(serializeNullObjects = true) +@JsonObject(serializeNullObjects = {{ serializeNulls }}) public class {% include 'blocks/class/classtype.twig' with { type: type } %} extends {% include 'blocks/class/supertype.twig' with { type: type, parent: parent } %} { -{%- if (storageAttributes is not null) -%} -{%- include 'blocks/class/converters.twig' -%} -{%- endif -%} +{%- include 'blocks/class/fields-converters.twig' -%} {% include 'blocks/class/fields.twig' with { fields: fields } %} @@ -133,4 +137,6 @@ public class {% include 'blocks/class/classtype.twig' with { type: type } %} ext {%- endif %} +{%- include 'blocks/class/dao.twig' %} + } diff --git a/Java/Enum.java.twig b/Java/Enum.java.twig index ac3ef1f..ddf115b 100644 --- a/Java/Enum.java.twig +++ b/Java/Enum.java.twig @@ -2,9 +2,7 @@ * This code is autogenerated by Touch Instinct tools */ package {{ packageName }}.api; - -{%- if storageAttributes.store %} - +{% if storable %} import android.arch.persistence.room.TypeConverter; {%- endif %} import android.support.annotation.NonNull; @@ -20,17 +18,14 @@ import com.bluelinelabs.logansquare.annotation.JsonNumberValue; import ru.touchin.templates.logansquare.LoganSquareEnum; import ru.touchin.templates.logansquare.LoganSquareEnumConverter; -{%- if storable %} - -import android.arch.persistence.room.TypeConverter; -{%- endif %} - /** * {{ description }} */ @JsonEnum public enum {{ name }} { - {%- include 'blocks/enum/cases.twig' with { values: values } %} + + {%- include 'blocks/enum/cases.twig' with { values: values } %} {%- include 'blocks/enum/converters.twig' %} + } diff --git a/Java/blocks/class/dao.twig b/Java/blocks/class/dao.twig new file mode 100644 index 0000000..48678ff --- /dev/null +++ b/Java/blocks/class/dao.twig @@ -0,0 +1,38 @@ +{%- import '../../utils.twig' as utils -%} + +{%- if (storageAttributes is not null) %} + + @android.arch.persistence.room.Dao + public interface Dao { + + @NonNull + @Query("SELECT * FROM {{ default(storageAttributes.tableName, type.baseTypeName) }}") + List<{{ type.baseTypeName }}> get(); + + @NonNull + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(@NonNull final Iterable<{{ type.baseTypeName }}> list); + + @NonNull + @Delete + void delete(@NonNull final Iterable<{{ type.baseTypeName }}> list); + + @Query("DELETE FROM {{ default(storageAttributes.tableName, type.baseTypeName) }}") + void clear(); + + } + + @android.arch.persistence.room.Dao + public interface ReactiveDao { + + @NonNull + @Query("SELECT * FROM {{ default(storageAttributes.tableName, type.baseTypeName) }}") + Flowable> observe(); + + @NonNull + @Query("SELECT * FROM {{ default(storageAttributes.tableName, type.baseTypeName) }}") + Single> get(); + + } + +{%- endif -%} diff --git a/Java/blocks/class/converters.twig b/Java/blocks/class/fields-converters.twig similarity index 67% rename from Java/blocks/class/converters.twig rename to Java/blocks/class/fields-converters.twig index 87fb1e8..8395936 100644 --- a/Java/blocks/class/converters.twig +++ b/Java/blocks/class/fields-converters.twig @@ -1,11 +1,17 @@ {%- import '../../utils.twig' as utils -%} +{%- if (storageAttributes is not null) -%} +{% set converters = [] %} + {%- for field in fields -%} + +{%- if not (field.type.type.baseTypeName in converters) -%} + {%- if field.type.type.baseTypeName == "Array" %} @TypeConverter @Nullable - public static String serialize{{ capitalize(field.name) }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { + public static String serialize{{ field.type.type.itemsType.baseTypeName }}List(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { if (value == null) { return null; } @@ -18,7 +24,7 @@ @TypeConverter @Nullable - public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ capitalize(field.name) }}(@Nullable final String value) { + public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ field.type.type.itemsType.baseTypeName }}List(@Nullable final String value) { if (value == null) { return null; } @@ -28,11 +34,13 @@ return null; } } + + {%- set converters = merge(converters, field.type.type.itemsType.baseTypeName) -%} {%- elseif field.type.type.baseTypeName == "Map" %} @TypeConverter @Nullable - public static String serialize{{ capitalize(field.name) }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { + public static String serialize{{ field.type.type.valuesType.baseTypeName }}Map(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { if (value == null) { return null; } @@ -45,7 +53,7 @@ @TypeConverter @Nullable - public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ capitalize(field.name) }}(@Nullable final String value) { + public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ field.type.type.valuesType.baseTypeName }}Map(@Nullable final String value) { if (value == null) { return null; } @@ -55,6 +63,8 @@ return null; } } + + {%- set converters = merge(converters, field.type.type.valuesType.baseTypeName) -%} {%- elseif field.type.type.baseTypeName != "Bool" and field.type.type.baseTypeName != "Int" and field.type.type.baseTypeName != "Long" @@ -64,7 +74,7 @@ @TypeConverter @Nullable - public static String serialize{{ capitalize(field.name) }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { + public static String serialize{{ field.type.type.baseTypeName }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { if (value == null) { return null; } @@ -77,7 +87,7 @@ @TypeConverter @Nullable - public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ capitalize(field.name) }}(@Nullable final String value) { + public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ field.type.type.baseTypeName }}(@Nullable final String value) { if (value == null) { return null; } @@ -87,5 +97,12 @@ return null; } } + + {%- set converters = merge(converters, field.type.type.baseTypeName) -%} {%- endif -%} + +{%- endif -%} + {%- endfor -%} + +{%- endif -%} diff --git a/Java/blocks/class/fields-getters-setters.twig b/Java/blocks/class/fields-getters-setters.twig index a88c58d..4d9bb28 100644 --- a/Java/blocks/class/fields-getters-setters.twig +++ b/Java/blocks/class/fields-getters-setters.twig @@ -9,7 +9,7 @@ {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }} public {{ utils.formatValueType(field.type.type, field.nullable) }} {% if (field.type.type.baseTypeName == "Bool") and (field.name matches "^is[A-Z,0-9].*") -%}{{ field.name }}{%- else -%}get{{ capitalize(field.name) }}{%- endif -%}() { {%- if field.nullable %} - if ({{ fieldName }} == null) { + if (this.{{ field.name }} == null) { return null; } {%- endif %} diff --git a/Java/blocks/class/fields-validate.twig b/Java/blocks/class/fields-validate.twig index bdd245e..3f00681 100644 --- a/Java/blocks/class/fields-validate.twig +++ b/Java/blocks/class/fields-validate.twig @@ -31,12 +31,14 @@ and field.type.type.baseTypeName != "Decimal" and field.type.type.baseTypeName != "DateTime" and (field.type.values is empty) %} - {%- if field.nullable %} - if (this.{{ field.name }} != null) { - this.{{ field.name }}.validate(); + if (this.{{ field.name }} instanceof ApiModel) { + {%- if field.nullable %} + if (this.{{ field.name }} != null) { + ((ApiModel) this.{{ field.name }}).validate(); + } + {%- else %} + ((ApiModel) this.{{ field.name }}).validate(); + {%- endif %} } - {%- else %} - this.{{ field.name }}.validate(); - {%- endif -%} {%- endif -%} {%- endfor -%} diff --git a/Java/blocks/class/fields.twig b/Java/blocks/class/fields.twig index efdd7f6..7a79cd1 100644 --- a/Java/blocks/class/fields.twig +++ b/Java/blocks/class/fields.twig @@ -8,9 +8,14 @@ */ {%- endif %} {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }} - {%- if (storageAttributes is not null) and field.type.storable %} + {%- if (storageAttributes is not null) %} + {%- if (field.type.storable) %} @TypeConverters({{ field.type.type.baseTypeName }}.class) {%- endif %} + {%- if (field.autoGenerate) %} + @PrimaryKey(autoGenerate = true) + {%- endif %} + {%- endif %} @JsonField(name = "{{ field.jsonName }}") private {{ utils.formatValueType(field.type.type, field.nullable) }} {{ field.name }}; {%- endfor -%} diff --git a/Java/blocks/enum/cases.twig b/Java/blocks/enum/cases.twig index fb627d0..e591965 100644 --- a/Java/blocks/enum/cases.twig +++ b/Java/blocks/enum/cases.twig @@ -1,10 +1,11 @@ {%- import '../../utils.twig' as utils -%} {%- for value in values %} - {% if (value.description is not empty) %} + {%- if (value.description is not empty) %} + /** - * {{ value.description }} - */ - {% endif -%} + * {{ value.description }} + */ + {%- endif %} {%- if valuesTypes == "STRING" %} @JsonStringValue("{{ value.value }}") {%- elseif valuesTypes == "INT" %} diff --git a/Java/blocks/enum/converters.twig b/Java/blocks/enum/converters.twig index 60d8b5e..7ffc9f2 100644 --- a/Java/blocks/enum/converters.twig +++ b/Java/blocks/enum/converters.twig @@ -19,4 +19,4 @@ return {{ name }}.valueOf(value); } -{%- endif %} +{%- endif -%}