All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.javers.repository.jql.FilterDefinition Maven / Gradle / Ivy
package org.javers.repository.jql;
import org.javers.common.collections.Sets;
import org.javers.common.exception.JaversException;
import org.javers.common.exception.JaversExceptionCode;
import org.javers.common.string.ToStringBuilder;
import org.javers.common.validation.Validate;
import org.javers.core.metamodel.object.GlobalIdFactory;
import org.javers.core.metamodel.type.EntityType;
import org.javers.core.metamodel.type.ManagedType;
import org.javers.core.metamodel.type.TypeMapper;
import java.util.Set;
abstract class FilterDefinition {
abstract Filter compile(GlobalIdFactory globalIdFactory, TypeMapper typeMapper);
static class IdFilterDefinition extends FilterDefinition {
private final GlobalIdDTO globalIdDTO;
IdFilterDefinition(GlobalIdDTO globalIdDTO) {
this.globalIdDTO = globalIdDTO;
}
boolean isInstanceIdFilter() {
return globalIdDTO instanceof InstanceIdDTO;
}
Filter compile(GlobalIdFactory globalIdFactory, TypeMapper typeMapper) {
return new IdFilter(globalIdFactory.createFromDto(globalIdDTO));
}
@Override
public String toString() {
return ToStringBuilder.toString(this, "globalId", globalIdDTO.value());
}
}
static class IdAndTypeNameFilterDefinition extends FilterDefinition {
private final Object localId;
private final String typeName;
IdAndTypeNameFilterDefinition(Object localId, String typeName) {
Validate.argumentsAreNotNull(localId, typeName);
this.localId = localId;
this.typeName = typeName;
}
Filter compile(GlobalIdFactory globalIdFactory, TypeMapper typeMapper) {
return new IdFilter(globalIdFactory.createInstanceId(localId, typeName));
}
@Override
public String toString() {
return ToStringBuilder.toString(this, "typeName", typeName, "localId", localId);
}
}
static class ClassFilterDefinition extends FilterDefinition {
private final Set requiredClasses;
ClassFilterDefinition(Set requiredClasses) {
this.requiredClasses = requiredClasses;
}
@Override
Filter compile(GlobalIdFactory globalIdFactory, TypeMapper typeMapper) {
return new ClassFilter(Sets.transform(requiredClasses, javaClass -> typeMapper.getJaversManagedType(javaClass)));
}
@Override
public String toString() {
return ToStringBuilder.toString(this, "requiredClasses", requiredClasses);
}
}
static class InstanceFilterDefinition extends FilterDefinition {
private final Object instance;
InstanceFilterDefinition(Object instance) {
this.instance = instance;
}
Filter compile(GlobalIdFactory globalIdFactory, TypeMapper typeMapper) {
try {
return new IdFilter(globalIdFactory.createId(instance));
} catch (JaversException e) {
if (e.getCode() == JaversExceptionCode.MANAGED_CLASS_MAPPING_ERROR) {
throw new JaversException(JaversExceptionCode.MALFORMED_JQL,
"object passed to byInstance(Object) query should be instance of Entity or ValueObject, got "+typeMapper.getJaversType(instance.getClass()) + " - " +ToStringBuilder.format(instance)+".\nDid you mean byInstanceId(Object localId, Class entityClass)?");
}
else {
throw e;
}
}
}
@Override
public String toString() {
return ToStringBuilder.toString(this, "instance", instance);
}
}
static class VoOwnerFilterDefinition extends FilterDefinition {
private final Class ownerEntityClass;
private final String path;
VoOwnerFilterDefinition(Class ownerEntityClass, String path) {
this.ownerEntityClass = ownerEntityClass;
this.path = path;
}
@Override
Filter compile(GlobalIdFactory globalIdFactory, TypeMapper typeMapper) {
ManagedType mType = typeMapper.getJaversManagedType(ownerEntityClass);
if (! (mType instanceof EntityType)) {
throw new JaversException(
JaversExceptionCode.MALFORMED_JQL, "queryForChanges: ownerEntityClass {'"+ownerEntityClass.getName()+"'} should be an Entity");
}
return new VoOwnerFilter((EntityType)mType, path);
}
@Override
public String toString() {
return ToStringBuilder.toString(this, "ownerEntityClass", ownerEntityClass.getName(),
"path", path);
}
}
static class AnyDomainObjectFilterDefinition extends FilterDefinition {
@Override
Filter compile(GlobalIdFactory globalIdFactory, TypeMapper typeMapper) {
return new AnyDomainObjectFilter();
}
@Override
public String toString() {
return ToStringBuilder.toString(this);
}
}
}