Class Model

java.lang.Object
ece.ing3.java.projet.database.sql.Model
Direct Known Subclasses:
Chambre, Employe, Hospitalisation, Malade, Service, Soigne

public abstract class Model extends Object
Abstract database model helper.

Models should inherit from this class to be able to use other helpers.

A model is an object that represent a database entry.
Table name is the model's lowercase class name.
Each field is automatically mapped to a column, if it is not excluded with the ExcludedField annotation.
A field's corresponding column name is either described by a Column annotation or by using directly the lowercase field name.
A field can be described as being used as primary key in database using the Id annotation.

Each model must have a default constructor, getters and setters for each field. Failing in following this rule will lead to undefined behavior.


One example of model can be :

 public class ExampleModel extends Model {
     @Id
     @Column( name = 'id' )
     private Long exampleId; // Field being a database ID, named exampleId and mapped to column 'id'

     private String exampleString; // Field name exampleString, mapped to column 'examplestring'

     @ExcludedField
     private String excludedString; // Not used as a database column

     public ExampleModel() {}

     // ... getters and setters for exampleId and exampleString ...
 }
 
Author:
Virgile, Nicolas, Louis-Félix
  • Constructor Details

    • Model

      public Model()
  • Method Details

    • getTableName

      public static String getTableName(Class<?> modelClass)
      Builds a table name from a model class.
      Parameters:
      modelClass - Model class
      Returns:
      Model's table name
    • getColumnName

      public static String getColumnName(Field modelField)
      Builds a column name from a model field.
      Parameters:
      modelField - Model field
      Returns:
      Field's column name or null if field should be excluded
    • getFieldNameFromColumnName

      public static String getFieldNameFromColumnName(Class<? extends Model> modelClass, String columnName)
      Get the corresponding field name of a column for a defined model class.
      Parameters:
      modelClass - Model class
      columnName - Column name
      Returns:
      Column's name field name or null if field should be excluded
    • getColumnNameFromFieldName

      public static String getColumnNameFromFieldName(Class<? extends Model> modelClass, String fieldName)
      Get the corresponding field name of a column for a defined model class.
      Parameters:
      modelClass - Model class
      fieldName - Field name
      Returns:
      Field's column name or null if field should be excluded
    • getColumnFieldNames

      public static Map<String,String> getColumnFieldNames(Class<? extends Model> modelClass)
      Get all the column/field name pairs for a defined model class.
      Parameters:
      modelClass - Model class
      Returns:
      Column/field name pairs
    • getFieldNames

      public static String[] getFieldNames(Class<? extends Model> modelClass)
      Get all the field names for a defined model class.
      Parameters:
      modelClass - Model class
      Returns:
      Column/field name pairs
    • getPropertyDescriptor

      public static PropertyDescriptor getPropertyDescriptor(Class<? extends Model> modelClass, String fieldName)
      Get a field's property descriptor for a defined model class.
      Parameters:
      modelClass - Model class
      fieldName - Field to use
      Returns:
      Field's property descriptor
    • orderByIDs

      public static OrderBy orderByIDs(Class<? extends Model> modelClass)
      Get an Order By clause to order by a model's IDs in ascending order.
      Parameters:
      modelClass - Model class
      Returns:
      Order By clause
    • getIdFieldNames

      public static String[] getIdFieldNames(Class<? extends Model> modelClass)
      Returns a list of all the ID fields's names for a defined model class.
      Parameters:
      modelClass - Model class
      Returns:
      List of ID fields's names
    • whereByIds

      public boolean whereByIds(Where whereClause) throws NullPointerException
      Updates a Where clause to select the current model.
      Parameters:
      whereClause - Where clause to update
      Returns:
      true Where clause updated with success
      Throws:
      NullPointerException - Where clause is null
    • save

      public int save() throws DatabaseException
      Saves a model instance to database, using the model's attribute values.
      Returns:
      Either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
      Throws:
      DatabaseException - Error occurred while saving to database
    • delete

      public int delete() throws DatabaseException
      Removes the model's instance from database.
      Returns:
      Either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
      Throws:
      DatabaseException - Error occurred while deleting from database
    • exists

      public boolean exists() throws DatabaseException
      Tells if the model instance already exists in the database. Returns true if a superclass instance exists.
      Returns:
      true Model instance exist in database.
      Throws:
      DatabaseException