use of fi.otavanopisto.muikku.search.annotations.IndexField in project muikku by otavanopisto.
the class IndexEntityProcessor method process.
public Map<String, Object> process(Object entity) throws IllegalArgumentException, IllegalAccessException, IntrospectionException, InvocationTargetException, IndexIdMissingException {
if (isIndexable(entity)) {
String id = getEnitityId(entity);
if (StringUtils.isBlank(id)) {
throw new IndexIdMissingException("Indexable object is missing an id.");
}
Map<String, Object> indexObject = new HashMap<String, Object>();
indexObject.put("id", id);
for (Method indexableGetter : getIndexableGetters(entity)) {
String fieldName = StringUtils.uncapitalize(indexableGetter.getName().substring(3));
IndexField indexField = findIndexField(indexableGetter);
if (indexField != null) {
if (indexField.skip()) {
continue;
}
String name = indexField.name();
if (StringUtils.isNotBlank(name)) {
fieldName = name;
}
}
Object fieldValue = indexableGetter.invoke(entity);
if (indexField != null && indexField.toId()) {
if (fieldValue != null) {
if (fieldValue instanceof Collection) {
Collection<?> collection = (Collection<?>) fieldValue;
Set<String> ids = new HashSet<String>();
for (Object o : collection) {
if (o != null) {
if (o instanceof SchoolDataIdentifier)
ids.add(((SchoolDataIdentifier) o).toId());
else {
logger.severe(String.format("@Indexable toId for Collection must be Collection<SchoolDataIdentifier> but was Collection<%s>", o.getClass().getName()));
}
}
}
fieldValue = ids;
} else if (fieldValue instanceof SchoolDataIdentifier) {
fieldValue = ((SchoolDataIdentifier) fieldValue).toId();
} else {
logger.severe(String.format("@Indexable toId must be SchoolDataIdentifier but was %s", fieldValue.getClass().getName()));
}
}
}
indexObject.put(fieldName, fieldValue);
}
return indexObject;
}
return null;
}
use of fi.otavanopisto.muikku.search.annotations.IndexField in project muikku by otavanopisto.
the class IndexEntityProcessor method findIndexField.
private IndexField findIndexField(Method method) {
IndexField indexField = method.getAnnotation(IndexField.class);
if (indexField != null) {
return indexField;
}
Class<?> declaringClass = method.getDeclaringClass();
if (declaringClass != null) {
if ((declaringClass.getSuperclass() != null) && (!declaringClass.getSuperclass().equals(Object.class))) {
Class<?> superclass = declaringClass.getSuperclass();
try {
Method superclassMethod = superclass.getMethod(method.getName());
indexField = superclassMethod.getAnnotation(IndexField.class);
if (indexField != null) {
return indexField;
}
} catch (NoSuchMethodException | SecurityException e) {
}
}
for (Class<?> declaringClassInterface : declaringClass.getInterfaces()) {
try {
Method declaringClassInterfaceMethod = declaringClassInterface.getMethod(method.getName());
indexField = declaringClassInterfaceMethod.getAnnotation(IndexField.class);
if (indexField != null) {
return indexField;
}
} catch (NoSuchMethodException | SecurityException e) {
}
}
}
return null;
}
Aggregations