Search in sources :

Example 1 with IndexField

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;
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) HashMap(java.util.HashMap) Collection(java.util.Collection) Method(java.lang.reflect.Method) IndexField(fi.otavanopisto.muikku.search.annotations.IndexField) HashSet(java.util.HashSet)

Example 2 with IndexField

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;
}
Also used : Method(java.lang.reflect.Method) IndexField(fi.otavanopisto.muikku.search.annotations.IndexField)

Aggregations

IndexField (fi.otavanopisto.muikku.search.annotations.IndexField)2 Method (java.lang.reflect.Method)2 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1