Search in sources :

Example 1 with NoSupportingModelException

use of com.serotonin.m2m2.web.mvc.rest.v1.exception.NoSupportingModelException in project ma-core-public by infiniteautomation.

the class CSVPojoHandler method buildMapping.

private void buildMapping(String[] line) throws ModelNotFoundException, NoSupportingModelException {
    for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
        if (c.getAnnotation(CSVEntity.class) != null) {
            for (Field field : c.getDeclaredFields()) {
                CSVColumn csvColumn = field.getAnnotation(CSVColumn.class);
                if (csvColumn == null)
                    continue;
                // hidden)
                if (!Modifier.isTransient(field.getModifiers()) && (csvColumn == null || !csvColumn.hidden())) {
                    PropertyEditor editor;
                    try {
                        // Try to determine PropertyEditor for field
                        editor = (csvColumn == null || csvColumn.editor() == CSVPropertyEditor.class) ? PropertyEditorManager.findEditor(field.getType()) : csvColumn.editor().newInstance();
                    } catch (InstantiationException ex) {
                        throw new InstantiationError(ex.getLocalizedMessage());
                    } catch (IllegalAccessException ex) {
                        throw new IllegalAccessError(ex.getLocalizedMessage());
                    }
                    if (editor != null) {
                        String header = (csvColumn != null && csvColumn.header().length() > 0) ? csvColumn.header() : field.getName();
                        CSVFieldHandler handler = new CSVFieldHandler(csvColumn.order(), header, field, editor);
                        handlers.add(handler);
                        mapping.put(header, handler);
                    } else {
                        // If no default or specified editor available check
                        // if this is CSVEntity child class
                        CSVEntity csvEntity = field.getType().getAnnotation(CSVEntity.class);
                        if (csvEntity != null) {
                            CSVPojoHandler handler = new CSVPojoHandler();
                            PojoChildField child = new PojoChildField(csvColumn.header(), csvColumn.order(), field, handler, csvEntity);
                            handler.initialize(line, field.getType());
                            children.add(child);
                        }
                    }
                }
            }
            // Sort the Getters and Setters Into a map with Col Order for them
            Map<Integer, CSVColumnMethodAnnotations> methodMap = new HashMap<Integer, CSVColumnMethodAnnotations>();
            for (Method method : c.getDeclaredMethods()) {
                CSVColumnGetter csvColumnGetter = method.getAnnotation(CSVColumnGetter.class);
                if (csvColumnGetter == null)
                    continue;
                boolean isCSVEntity = false;
                if (method.getReturnType().getAnnotation(CSVEntity.class) != null) {
                    isCSVEntity = true;
                }
                CSVColumnMethodAnnotations annos = methodMap.get(csvColumnGetter.order());
                if (annos != null) {
                    annos.setGetterAnnotation(csvColumnGetter);
                    annos.setGetter(method);
                    annos.setCSVEntity(isCSVEntity);
                } else {
                    // Create new one
                    annos = new CSVColumnMethodAnnotations(csvColumnGetter, method, null, null, isCSVEntity);
                    methodMap.put(csvColumnGetter.order(), annos);
                }
            }
            for (Method method : c.getDeclaredMethods()) {
                CSVColumnSetter csvColumnSetter = method.getAnnotation(CSVColumnSetter.class);
                if (csvColumnSetter == null)
                    continue;
                boolean isCSVEntity = false;
                if (method.getReturnType().getAnnotation(CSVEntity.class) != null) {
                    isCSVEntity = true;
                }
                CSVColumnMethodAnnotations annos = methodMap.get(csvColumnSetter.order());
                if (annos != null) {
                    annos.setSetterAnnotation(csvColumnSetter);
                    annos.setSetter(method);
                } else {
                    // Create new one
                    annos = new CSVColumnMethodAnnotations(null, null, csvColumnSetter, method, isCSVEntity);
                    methodMap.put(csvColumnSetter.order(), annos);
                }
            }
            // Now map the matched methods
            Iterator<Integer> it = methodMap.keySet().iterator();
            while (it.hasNext()) {
                Integer order = it.next();
                CSVColumnMethodAnnotations method = methodMap.get(order);
                CSVColumnGetter csvColumnGetter = method.getGetterAnnotation();
                CSVColumnSetter csvColumnSetter = method.getSetterAnnotation();
                if (csvColumnGetter != null) {
                    // Getter First
                    if (!Modifier.isTransient(method.getGetter().getModifiers()) && (csvColumnGetter == null || !csvColumnGetter.hidden())) {
                        PropertyEditor editor;
                        try {
                            // Try to determine PropertyEditor for field
                            editor = (csvColumnGetter == null || csvColumnGetter.editor() == CSVPropertyEditor.class) ? PropertyEditorManager.findEditor(method.getGetter().getReturnType()) : csvColumnGetter.editor().newInstance();
                        } catch (InstantiationException ex) {
                            throw new InstantiationError(ex.getLocalizedMessage());
                        } catch (IllegalAccessException ex) {
                            throw new IllegalAccessError(ex.getLocalizedMessage());
                        }
                        if (editor != null) {
                            String header = (csvColumnGetter != null && csvColumnGetter.header().length() > 0) ? csvColumnGetter.header() : method.getGetter().getName();
                            CSVMethodHandler handler = new CSVMethodHandler(csvColumnGetter.order(), header, method.getGetter(), method.getSetter(), editor);
                            handlers.add(handler);
                            mapping.put(header, handler);
                        } else {
                            // If no default or specified editor available check
                            // if this is CSVEntity child class
                            CSVEntity csvEntity = method.getGetter().getReturnType().getAnnotation(CSVEntity.class);
                            if (csvEntity != null) {
                                CSVPojoHandler handler = new CSVPojoHandler();
                                PojoChildMethod child = new PojoChildMethod(csvColumnGetter.header(), order, method.getGetter(), method.getSetter(), handler, csvEntity);
                                children.add(child);
                                // Check Runtime Type for return value
                                if (csvEntity.derived()) {
                                    ModelDefinition definition = null;
                                    if (line.length > method.getGetterAnnotation().order())
                                        definition = this.findModelDefinition(line[method.getGetterAnnotation().order()]);
                                    if (definition != null) {
                                        handler.initialize(line, definition.createModel());
                                        editor = new CsvEntityAnnotationPropertyEditor(csvEntity.typeName());
                                        CSVMethodHandler methodHandler = new CSVMethodHandler(csvColumnGetter.order(), csvColumnGetter.header(), method.getGetter(), method.getSetter(), editor);
                                        handlers.add(methodHandler);
                                        mapping.put(csvColumnGetter.header(), methodHandler);
                                    }
                                } else {
                                    if (!csvEntity.typeName().equals("")) {
                                        ModelDefinition definition = this.findModelDefinition(csvEntity.typeName());
                                        handler.initialize(line, definition.createModel());
                                        editor = new CsvEntityAnnotationPropertyEditor(csvEntity.typeName());
                                        CSVMethodHandler methodHandler = new CSVMethodHandler(csvColumnGetter.order(), csvColumnGetter.header(), method.getGetter(), method.getSetter(), editor);
                                        handlers.add(methodHandler);
                                        mapping.put(csvColumnGetter.header(), methodHandler);
                                    }
                                }
                            } else {
                                throw new IllegalArgumentException("The method: " + c.getName() + "." + method.getGetter().getName() + " of type: " + method.getGetter().getReturnType().getName() + " is not a CSVEntity or has no PropertyEditor available.");
                            }
                        }
                    }
                }
            // End if getter != null
            }
        }
    }
    // Sort children so we always output them in the same order
    Collections.sort(this.children);
}
Also used : HashMap(java.util.HashMap) Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) ModelDefinition(com.serotonin.m2m2.module.ModelDefinition) PropertyEditor(java.beans.PropertyEditor)

Aggregations

ModelDefinition (com.serotonin.m2m2.module.ModelDefinition)1 PropertyEditor (java.beans.PropertyEditor)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1