ph.com.nightowlstudios.entity.Entity Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of edge Show documentation
Show all versions of edge Show documentation
A simple library for building REST API using Vertx.
package ph.com.nightowlstudios.entity;
import io.vavr.control.Try;
import io.vertx.core.json.JsonObject;
import org.apache.commons.lang3.StringUtils;
import ph.com.nightowlstudios.utils.Utils;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* This just serves as a tagging type for Persistence Entities.
* Subclasses should implement {@link ph.com.nightowlstudios.entity.Table} and {@link ph.com.nightowlstudios.entity.Column} as
* the {@code tableName} and {@code column} respectively.
*
* @author Joseph Harvey Angeles - @yev
* @see ph.com.nightowlstudios.entity.Table
* @see ph.com.nightowlstudios.entity.Column
* @since 7/3/20
*/
public interface Entity {
static String getTableName(Class entity) {
return entity.getAnnotation(Table.class).value();
}
static String[] getColumns(Class clasz) {
return Arrays
.stream(clasz.getDeclaredFields())
.filter(field -> field.isAnnotationPresent(Column.class))
.map(field -> field.getDeclaredAnnotation(Column.class).value())
.toArray(String[]::new);
}
static JsonObject toJson(T entity) {
return Utils.camelCaseKeys(JsonObject.mapFrom(entity));
}
static T fromJson(JsonObject json, Class entityClass) {
return Utils.camelCaseKeys(json).mapTo(entityClass);
}
}