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

info.unterrainer.commons.httpserver.daos.TenantData Maven / Gradle / Ivy

There is a newer version: 0.3.14
Show newest version
package info.unterrainer.commons.httpserver.daos;

import java.lang.reflect.Method;

import info.unterrainer.commons.httpserver.exceptions.InternalServerErrorException;
import info.unterrainer.commons.httpserver.jpas.BasicPermissionJpa;
import lombok.Getter;

public class TenantData {

	@Getter
	private Class permissionJpaType;
	@Getter
	private String mainTableIdReferenceField = "referenceId";
	@Getter
	private String tenantIdField = "tenantId";

	private Method referenceSetMethod;
	private Method tenantIdSetMethod;

	public TenantData(final Class permissionJpaType) {
		super();
		this.permissionJpaType = permissionJpaType;
	}

	public TenantData(final Class permissionJpaType,
			final String mainTableIdReferenceField, final String tenantIdField) {
		super();
		this.permissionJpaType = permissionJpaType;
		this.mainTableIdReferenceField = mainTableIdReferenceField;
		this.tenantIdField = tenantIdField;
	}

	public Method getReferenceSetMethod() {
		if (referenceSetMethod == null)
			try {
				referenceSetMethod = permissionJpaType.getMethod("set" + capitalize(mainTableIdReferenceField),
						Long.class);
			} catch (NoSuchMethodException | SecurityException e) {
				throw new InternalServerErrorException(
						String.format("Error creating permission-entry in [%s]", permissionJpaType.getSimpleName()), e);
			}
		return referenceSetMethod;
	}

	public Method getTenantIdSetMethod() {
		if (tenantIdSetMethod == null)
			try {
				tenantIdSetMethod = permissionJpaType.getMethod("set" + capitalize(tenantIdField), Long.class);
			} catch (NoSuchMethodException | SecurityException e) {
				throw new InternalServerErrorException(
						String.format("Error creating permission-entry in [%s]", permissionJpaType.getSimpleName()), e);
			}
		return tenantIdSetMethod;
	}

	private String capitalize(final String s) {
		if (s == null || s.isEmpty())
			return s;
		return s.substring(0, 1).toUpperCase() + s.substring(1);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy