Class IdUtils
java.lang.Object
com.creativityfactory.swiftserver.utils.IdUtils
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Object
Convert the id from String to its compatible type.static Object
extractIdValue
(Object obj) Extracts the ID value from an object by accessing the first field annotated with the @Id annotation.static Field
getIdField
(Class<?> clazz) Returns the ID field of a given class annotated with theId
annotation.static Class<?>
getIdTypeFromModel
(Class<?> clazz) Given a model class, this method returns the type of its ID field.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.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.
-
Constructor Details
-
IdUtils
public IdUtils()
-
-
Method Details
-
extractIdValue
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
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
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
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
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
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 mappedclazz
- The class of the object to be mapped- Returns:
- A map representing the object with its fields and values
-