Documentation for the "constants" JSON key.

As the name would suggest constants are designed to contain un-changing data that can be referenced from other parts of the codebase to ensure that there is a tight-coupling of the data and consistency throughout the code.

"tables": [
	{
		"name": "user_type",
		"cacheable": false,
		"cacheFindAll": false,
		"fields": [
			{ "name": "id_user_type", "type": "bigint", "nullable": false, "primary": true },
			{ "name": "nm_user_type", "type": "varchar", "length": "32", "nullable": false},
		],
		"constants": [
			{ "name": "NORMAL", "values": [ 1, "normal" ] },
			{ "name": "SPECIAL", "values": [ 2, "special" ] },
			{ "name": "ADMIN", "values": [ 3, "admin" ] },
			{ "name": "SUPER_ADMIN", "values": [ 4, "super admin" ] }
		]
	}
]

The generated code for the UserType is an example of a generated constant model. Constant models can not be modified in any way and as such the following interactions can not be generated:

  • Updaters
  • Deleters
  • Inserters

You may however genereate the following interactions:

  • Questions
  • Counters
  • Finders

The reasoning behind constant models is to allow fast lookup on data that doesn't change, so the utility of the interactions is questionable.

Models in General

Models come in two flavours, regular and constant. They both share some base features and functionality asdetailed below:

BINDER

// the binder is unused in code, but will generate compile problems if this 
// class is no longer referenced in the h2zero file. Just a nicety for
// removing dead code
@SuppressWarnings("unused")
private static final String BINDER = Constants.USER_TYPE_binder;

As mentioned in the comments above the field, the BINDER is only used as a compile time error so that dead code can be removed. Most of the generated code contains this code that references the constant. This will ensure that when model names are changed, or any of tyhe interaction names are changed (e.g. finders, questions, counters etc.)

PRIMARY_KEY_FIELD

public static final String PRIMARY_KEY_FIELD = "id_user_type";

This is a reference to the primary key field in the database. Every table (and therefore every model) must have a primary key on it.

Member variable declarations

Each field that is attached to the table will generate a declaration.

private Long idUserType = null;
private String nmUserType = null;

Constructor

Every model has a constructor with a signature that contains all of the fields. Despite the fact that you cannot create new constants, the constant models still include a constructor for use with finders, counters and questions.

public UserType(Long idUserType, String nmUserType) {
	this.idUserType = idUserType;
	this.nmUserType = nmUserType;
}

Getters

/*
 * Boring ol' getters and setters 
 */

public Long getIdUserType() {return(this.idUserType);}
public String getNmUserType() {return(this.nmUserType);}

Getters are generated (in condensed format) for each field on the table. No setters are generated for constant models as you may not change constants.

String methods

public String toString() {
	StringBuilder stringBuilder = new StringBuilder();
	stringBuilder.append("Model[UserType]\n");
	stringBuilder.append("  Field[idUserType:" + this.idUserType + "]\n");
	stringBuilder.append("  Field[nmUserType:" + this.nmUserType + "]\n");
	return(stringBuilder.toString());
}

public String toJsonString() {
	// !!! work in progress !!!
	StringBuilder stringBuilder = new StringBuilder();
	stringBuilder.append("{\n");
	stringBuilder.append("  \"model\": {\n");
	stringBuilder.append("    \"name\": \"UserType\",\n");
	stringBuilder.append("    \"fields\": [\n");
	stringBuilder.append("     { \"name\": \"idUserType\", \"value\": " + this.idUserType + " }, \n");
	stringBuilder.append("     { \"name\": \"nmUserType\", \"value\": \"" + this.nmUserType + "\" }\n");
	stringBuilder.append("    ]\n");
	stringBuilder.append("  }\n");
	stringBuilder.append("}\n");
	return(stringBuilder.toString());
}

public String getJsonString() {
	return(toJsonString());
}

The string methods are automatically generated for each model. As the comment notes above, it is a work in progress. Care must be taken when utilising this method on sensitive information contained within the model, e.g. passwords, credit card details etc.

The getJsonString() method signature above is for JSTL or templar referencing for output.

Additional methods and member variables

In addition to the general generation mentioned above, for each of the constant models, the following field and method signatures are generated:

Static Final Member Variable References

public static final UserType NORMAL = new UserType(new Long(1), "normal");
public static final UserType SPECIAL = new UserType(new Long(2), "special");
public static final UserType ADMIN = new UserType(new Long(3), "admin");
public static final UserType SUPER_ADMIN = new UserType(new Long(4), "super admin");

For each of the constant definitions a public static final member variable is generated which can then be referenced from other parts of the code. Should any of the constants be renamed, deleted or removed, compiler errors are generated.

Static lookup member variables

public static UserType[] ALL = {
	UserType.NORMAL, UserType.SPECIAL, UserType.ADMIN, UserType.SUPER_ADMIN
};

public static HashMap>Long, UserType< ALL_LOOKUP = new HashMap>Long, UserType<();
static{
	ALL_LOOKUP.put(new Long(1), UserType.NORMAL);
	ALL_LOOKUP.put(new Long(2), UserType.SPECIAL);
	ALL_LOOKUP.put(new Long(3), UserType.ADMIN);
	ALL_LOOKUP.put(new Long(4), UserType.SUPER_ADMIN);
};

The static lookup member variables allow quick lookups from other parts of the code and ensure with tight-binding that should they change at any point in time that any referenced code will fail with compile time errors