java.lang.Object
com.creativityfactory.swiftserver.utils.IdUtils

public class IdUtils extends Object
This class is responsible for all the operation related to the id of models, such extracting the value of an id from an object of specific model, type of id...
  • Constructor Details

    • IdUtils

      public IdUtils()
  • Method Details

    • extractIdValue

      public static Object extractIdValue(Object obj) throws Exception
      Extracts the ID value from an object by accessing the first field annotated with the @Id annotation.
      Parameters:
      obj - the object from which to extract the ID value
      Returns:
      the ID value extracted from the object
      Throws:
      Exception - if the field cannot be accessed or if the class has no field
    • getIdTypeFromModel

      public static Class<?> getIdTypeFromModel(Class<?> clazz)
      Given a model class, this method returns the type of its ID field.
      Parameters:
      clazz - the class of the model
      Returns:
      the type of the ID field, or null if the class doesn't have an ID field
    • getIdField

      public static Field getIdField(Class<?> clazz)

      Returns the ID field of a given class annotated with the Id annotation.

      If no field is annotated with Id, returns the first field in the class.

      Parameters:
      clazz - the class to retrieve the ID field from
      Returns:
      the ID field of the given class, or null if the class has no fields
    • convertId

      public static Object convertId(Class<?> model, String paramId)
      Convert the id from String to its compatible type.
      Parameters:
      model - The Class object representing the model class that contains the id field.
      paramId - The id value as a string.
      Returns:
      The id value converted to its compatible type. Returns null if either the model or paramId is null.
      Throws:
      NumberFormatException - If the id value cannot be converted to an integer (if the id type is an integer).
    • mapObjectToId

      public static String mapObjectToId(String json, Class<?> clazz)
      Maps an object's ID field to its corresponding value in a JSON string, and converts Integer fields from Double to Integer.
      Parameters:
      json - a JSON string representing an object, which must contain the ID field value.
      clazz - the class of the object that the JSON string represents.
      Returns:
      a JSON string representing the same object, but with Integer fields properly converted and BelongTo fields replaced with their referenced objects.
    • mapIdToObject

      public static Map<String,Object> mapIdToObject(Object obj, Class<?> clazz)

      Converts an object to a map, where keys are the names of the object's fields and the values are the corresponding field values.

      It only maps the fields that do not have the annotations BelongTo or HasMany.

      If a field has the BelongTo annotation, the corresponding value in the map will be the ID of the related object.

      Parameters:
      obj - The object to be mapped
      clazz - The class of the object to be mapped
      Returns:
      A map representing the object with its fields and values