My question is very long ... So be patient :)
I work with models in ExtJS 4, but I have some problems with associations, so I created a function to automatically create a model for me. Suppose I need to parse the following JSON:
{
"success": true,
"total": 28,
"itens": [{
"id":1,
"nome":"ACRE",
"sigla":"AC",
"pais":{
"id":31,
"nome":"BRASIL",
"sigla":"BR"
}
},{
"id":2,
"nome":"ALAGOAS",
"sigla":"AL",
"pais":{
"id":31,
"nome":"BRASIL",
"sigla":"BR"
}
}, ...]
}
Eden represent the provinces (Estados in Brazilian Portuguese) that have a country (PaΓs in Brazilian Portuguese). I tried to use ExtJS associations, but I thought it worked like a Java relationship, and I was wrong. Well, for this JSON, I have these Java classes and these Ext-models (models are created using the provided function).
Pais.java
@Entity
public class Pais implements Serializable {
@Id
@GeneratedValue
private Long id;
@NotNull
@NotEmpty
@Length( max = 100 )
private String nome;
@NotNull
@NotEmpty
@Column( unique = true )
@Length( min = 2, max = 4 )
private String sigla;
}
Estado.java
@Entity
public class Estado implements Serializable {
@Id
@GeneratedValue
private Long id;
@NotNull
@NotEmpty
@Length( max = 100 )
private String nome;
@NotNull
@NotEmpty
@Column( unique = true )
@Length( min = 2, max = 4 )
private String sigla;
@NotNull
@ManyToOne
private Pais pais;
}
Model Creation Function
Ext.ns( "Uteis" );
Uteis.createModel = function( modelData ) {
var fields = modelData.fields;
var processedFields = [];
var normalFields = [];
var relationFields = [];
for ( var i in fields ) {
if ( fields[i].type ) {
switch ( fields[i].type ) {
case "auto":
case "string":
case "int":
case "float":
case "boolean":
case "date":
normalFields.push( fields[i] );
break;
default:
var relationField = fields[i];
var prefix = relationField.name + ".";
var modelInstance = Ext.create( relationField.type );
modelInstance.fields.each( function( item, index, length ) {
var newField = {};
newField["name"] = prefix + item.name;
newField["type"] = item.type.type;
newField["convert"] = item.convert;
newField["dateFormat"] = item.dateFormat;
newField["defaultValue"] = item.defaultValue;
newField["mapping"] = item.mapping;
newField["persist"] = item.persist;
newField["sortDir"] = item.sortDir;
newField["sortType"] = item.sortType;
newField["useNull"] = item.useNull;
relationFields.push( newField );
});
break;
}
} else {
normalFields.push( fields[i] );
}
}
processedFields = normalFields.concat( relationFields );
Ext.define( modelData.name, {
extend: "Ext.data.Model",
fields: processedFields
});
};
Using the function to create models
Uteis.createModel({
name: "Modelos.Pais",
fields: [
{ name: "id", type: "int" },
{ name: "nome", type: "string" },
{ name: "sigla", type: "string" }
]
});
Uteis.createModel({
name: "Modelos.Estado",
fields: [
{ name: "id", type: "int" },
{ name: "nome", type: "string" },
{ name: "sigla", type: "string" },
{ name: "pais", type: "Modelos.Pais" } // <= references the model created above
]
});
. ( , ).
Ext.define( "Modelos.Pais", {
extend: "Ext.data.Model",
fields: [
{ name: "id", type: "int" },
{ name: "nome", type: "string" },
{ name: "sigla", type: "string" }
]
});
Ext.define( "Modelos.Estado", {
extend: "Ext.data.Model",
fields: [
{ name: "id", type: "int" },
{ name: "nome", type: "string" },
{ name: "sigla", type: "string" },
{ name: "pais.id", type: "int" },
{ name: "pais.nome", type: "string" },
{ name: "pais.sigla", type: "string" }
]
});
, ( createModel) JsonStores. Java , . , , . , , ( , , ). Gson JSON . , , undefined , , , ( ), Ext . Gson:
Gson gson = new GsonBuilder().serializeNulls().create();
, JSON , Ext . defaultValue . , Estados PaΓses (Privinces and Countries), Pais @NotNull. JSON pais :
{
"success": true,
"total": 28,
"itens": [{
"id":1,
"nome":"ACRE",
"sigla":"AC",
"pais":null // <= here
},{
"id":2,
"nome":"ALAGOAS",
"sigla":"AL",
"pais":{ // this is not null
"id":31,
"nome":"BRASIL",
"sigla":"BR"
}
}, ...]
}
pais.id, pais.nome pais.sigla , pais null. , : , undefined? ... !
. , 15 , ... " ", Gson , . , JSON . :
public static void traverseAndSetDefaultValue( Object target ) {
try {
for ( Field f : target.getClass().getDeclaredFields() ) {
f.setAccessible( true );
if ( f.get( target ) == null ) {
Object newInstance = null;
boolean okToTraverse = false;
switch ( f.getType().getSimpleName() ) {
case "Byte":
case "Short":
case "Integer":
newInstance = 0;
break;
case "Long":
newInstance = 0L;
break;
case "Float":
newInstance = 0F;
break;
case "Double":
newInstance = 0D;
break;
case "Character":
newInstance = '\0';
break;
case "Boolean":
newInstance = Boolean.FALSE;
break;
case "String":
newInstance = "";
break;
case "List":
newInstance = new ArrayList();
break;
case "Set":
newInstance = new HashSet();
break;
default:
newInstance = f.getType().newInstance();
okToTraverse = true;
break;
}
f.set( target, newInstance );
if ( okToTraverse ) {
traverseAndSetDefaultValue( newInstance );
}
}
}
} catch ( IllegalAccessException | InstantiationException exc ) {
exc.printStackTrace();
}
}
, ... !
2: . !:) , . , . , ( , ). , . , , . !