Class DefaultPersistence

java.lang.Object
com.creativityfactory.swiftserver.persistence.DefaultPersistence
All Implemented Interfaces:
Persistence<Object>

public class DefaultPersistence extends Object implements Persistence<Object>
Default implementation of the Persistence interface that provides default implementation for the CRUD operations using a DefaultDataSource instance. This implementation is based on the default data source specified by the DataSource annotation on the model class.

The class uses a file database to store and retrieve data. It assumes that each entity has an ID field annotated with the Id annotation. If the ID field is not present, the first field in the entity will be used as the ID.

  • Constructor Details

    • DefaultPersistence

      public DefaultPersistence(Class<?> model, DefaultDataSource api)
      Constructs a new DefaultPersistence instance with the given model class and API.
      Parameters:
      model - the class of the model to be persisted.
      api - the API to be used for persistence operations.
  • Method Details

    • getAll

      public List<Object> getAll()
      Returns a list of all objects of the specified model type.
      Specified by:
      getAll in interface Persistence<Object>
      Returns:
      a list of all objects of the specified model type, or null if an exception occurs.
    • getLimit

      public List<Object> getLimit(Integer limit)
      Returns a list of objects of the specified model type, up to the given limit.
      Specified by:
      getLimit in interface Persistence<Object>
      Parameters:
      limit - the maximum number of objects to return.
      Returns:
      a list of up to 'limit' objects of the specified model type, or null if an exception occurs.
    • getById

      public Object getById(Object id)
      Returns the object with the specified ID of the specified model type, or null if it does not exist.
      Specified by:
      getById in interface Persistence<Object>
      Parameters:
      id - the ID of the object to be returned.
      Returns:
      the object with the specified ID of the specified model type, or null if it does not exist.
    • save

      public Object save(Object o)
      Saves the specified object in the data store if it does not already exist.
      Specified by:
      save in interface Persistence<Object>
      Parameters:
      o - the object to be saved.
      Returns:
      the saved object if the operation is successful, or null if it already exists or an exception occurs.
    • update

      public Object update(Object o)
      Updates the specified object in the data store if it already exists.
      Specified by:
      update in interface Persistence<Object>
      Parameters:
      o - the object to be updated.
      Returns:
      the updated object if the operation is successful, or null if it does not exist or an exception occurs.
    • delete

      public Object delete(Object o)
      Delete the specified object in the data store if it already exists.
      Specified by:
      delete in interface Persistence<Object>
      Parameters:
      o - the object to be deleted.
      Returns:
      the deleted object if the operation is successful, or null if it does not exist or an exception occurs.