use of com.rbmhtechnology.vind.annotations.ComplexField in project vind by RBMHTechnology.
the class AnnotationUtil method createFieldDescriptor.
/**
* Builds a new FieldDescriptor object based on a given Class Field
* @param field Field a Class used as base to create a new FieldDescriptor.
* @return new FieldDescriptor build based on the Field parameter annotations.
*/
private static FieldDescriptor createFieldDescriptor(Field field) {
if (field.isAnnotationPresent(Ignore.class))
return null;
if (field.isAnnotationPresent(Id.class))
return null;
if (field.isAnnotationPresent(Score.class))
return null;
if (field.isAnnotationPresent(ComplexField.class)) {
final String fieldName;
final ComplexField cf = field.getAnnotation(ComplexField.class);
if (StringUtils.isNotBlank(cf.name())) {
fieldName = cf.name();
} else {
fieldName = field.getName();
}
Class<?> type = field.getType();
boolean multiValue = false;
if (Collection.class.isAssignableFrom(type)) {
type = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
multiValue = true;
} else if (type.isPrimitive()) {
type = ClassUtils.primitiveToWrapper(type);
}
final Operator storeOperator = cf.store();
final Operator facetOperator = cf.facet();
final Operator suggestionOperator = cf.suggestion();
final Operator fullTextOperator = cf.fullText();
final Operator filterOperator = cf.advanceFilter();
final Operator sortOperator = cf.sort();
final ComplexFieldDescriptorBuilder builder = new ComplexFieldDescriptorBuilder();
Class<?> facetType = null;
if (!NullFunction.class.isAssignableFrom(facetOperator.function())) {
try {
facetType = facetOperator.returnType();
final Function facetLambda = ((FunctionHelpers.ParameterFunction) facetOperator.function().newInstance()).setParameters(Arrays.asList(facetOperator.fieldName()));
builder.setFacet(true, facetLambda);
} catch (InstantiationException | IllegalAccessException e) {
log.error("Unable to find/access constructor method for function class [{}]", facetOperator.function().getName(), e);
throw new RuntimeException("Unable to find/access constructor method for function class [" + facetOperator.function().getName() + "]");
}
}
Class<?> storeType = null;
if (!NullFunction.class.isAssignableFrom(storeOperator.function())) {
try {
storeType = storeOperator.returnType();
final Function storeLambda = ((FunctionHelpers.ParameterFunction) storeOperator.function().newInstance()).setParameters(Arrays.asList(storeOperator.fieldName()));
builder.setStored(true, storeLambda);
} catch (InstantiationException | IllegalAccessException e) {
log.error("Unable to find/access constructor method for function class [{}]", storeOperator.function().getName(), e);
throw new RuntimeException("Unable to find/access constructor method for function class [" + storeOperator.function().getName() + "]");
}
}
if (!NullFunction.class.isAssignableFrom(suggestionOperator.function())) {
try {
final Function suggestLambda = ((FunctionHelpers.ParameterFunction) suggestionOperator.function().newInstance()).setParameters(Arrays.asList(suggestionOperator.fieldName()));
builder.setSuggest(true, suggestLambda);
} catch (InstantiationException | IllegalAccessException e) {
log.error("Unable to find/access constructor method for function class [{}]", suggestionOperator.function().getName(), e);
throw new RuntimeException("Unable to find/access constructor method for function class [" + suggestionOperator.function().getName() + "]");
}
}
if (!NullFunction.class.isAssignableFrom(fullTextOperator.function())) {
try {
final Function fullTextLambda = ((FunctionHelpers.ParameterFunction) fullTextOperator.function().newInstance()).setParameters(Arrays.asList(fullTextOperator.fieldName()));
builder.setFullText(true, fullTextLambda);
builder.setLanguage(cf.language());
builder.setBoost(cf.boost());
} catch (InstantiationException | IllegalAccessException e) {
log.error("Unable to find/access constructor method for function class [{}]", fullTextOperator.function().getName(), e);
throw new RuntimeException("Unable to find/access constructor method for function class [" + fullTextOperator.function().getName() + "]");
}
}
if (!NullFunction.class.isAssignableFrom(filterOperator.function())) {
try {
final Function filterLambda = ((FunctionHelpers.ParameterFunction) filterOperator.function().newInstance()).setParameters(Arrays.asList(filterOperator.fieldName()));
builder.setAdvanceFilter(true, filterLambda);
} catch (InstantiationException | IllegalAccessException e) {
log.error("Unable to find/access constructor method for function class [{}]", filterOperator.function().getName(), e);
throw new RuntimeException("Unable to find/access constructor method for function class [" + filterOperator.function().getName() + "]");
}
}
// set metadata
final Metadata metadata = field.getAnnotation(Metadata.class);
if (metadata != null) {
for (Entry entry : metadata.value()) {
builder.putMetadata(entry.name(), entry.value());
}
}
return buildComplex(builder, fieldName, type, facetType, storeType, multiValue, sortOperator.function());
}
final String fieldName;
final com.rbmhtechnology.vind.annotations.Field f = field.getAnnotation(com.rbmhtechnology.vind.annotations.Field.class);
if (f != null && StringUtils.isNotBlank(f.name())) {
fieldName = f.name();
} else {
fieldName = field.getName();
}
Class<?> type = field.getType();
boolean multiValue = false;
if (Collection.class.isAssignableFrom(type)) {
type = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
multiValue = true;
} else if (type.isPrimitive()) {
type = ClassUtils.primitiveToWrapper(type);
}
final FieldDescriptorBuilder builder = new FieldDescriptorBuilder();
if (f != null) {
builder.setStored(f.stored());
builder.setIndexed(f.indexed());
} else {
// Should not be needed, it has this value as default
builder.setStored(true);
builder.setIndexed(true);
}
final FullText fullText = field.getAnnotation(FullText.class);
if (fullText != null) {
if (!CharSequence.class.isAssignableFrom(type)) {
log.error("@FullText only allowed on CharSequence or String fields");
throw new IllegalArgumentException("@FullText only allowed on CharSequence or String fields");
}
builder.setFullText(true);
builder.setLanguage(fullText.language());
builder.setBoost(fullText.boost());
} else {
// Should not be needed, it has this value as default
builder.setFullText(false);
}
final Facet facet = field.getAnnotation(Facet.class);
if (facet != null) {
builder.setFacet(true);
builder.setSuggest(facet.suggestion());
} else {
// Should not be needed, it has this value as default
builder.setFacet(false);
}
// set metadata
final Metadata metadata = field.getAnnotation(Metadata.class);
if (metadata != null) {
for (Entry entry : metadata.value()) {
builder.putMetadata(entry.name(), entry.value());
}
}
return build(builder, fieldName, type, multiValue);
}
Aggregations