All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ph.com.nightowlstudios.entity.Entity Maven / Gradle / Ivy

There is a newer version: 6.21.1
Show newest version
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  String column(Class entityClass, String column) {
    return String.format("%s.%s", Entity.getTableName(entityClass), column);
  }

  static  JsonObject toJson(T entity) {
    return Utils.camelCaseKeys(JsonObject.mapFrom(entity));
  }

  static  T fromJson(JsonObject json, Class entityClass) {
    return Utils.camelCaseKeys(json).mapTo(entityClass);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy