Search in sources :

Example 1 with ModelNotFoundException

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

the class VirtualSerialPortConfigDeserializer method deserialize.

/* (non-Javadoc)
	 * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
	 */
@Override
public VirtualSerialPortConfig deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode tree = jp.readValueAsTree();
    String typeName = tree.get("portType").asText();
    Class<?> clazz;
    switch(typeName) {
        case "SERIAL_SOCKET_BRIDGE":
            clazz = SerialSocketBridgeConfig.class;
            break;
        case "SERIAL_SERVER_SOCKET_BRIDGE":
            clazz = SerialServerSocketBridgeConfig.class;
            break;
        default:
            throw new ModelNotFoundException(typeName);
    }
    VirtualSerialPortConfig model = (VirtualSerialPortConfig) mapper.treeToValue(tree, clazz);
    return model;
}
Also used : ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) VirtualSerialPortConfig(com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ModelNotFoundException

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

the class AbstractEventHandlerModelDeserializer method deserialize.

/* (non-Javadoc)
	 * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
	 */
@Override
public AbstractEventHandlerModel<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode tree = jp.readValueAsTree();
    String typeName = tree.get("handlerType").asText();
    EventHandlerDefinition<?> def = ModuleRegistry.getEventHandlerDefinition(typeName);
    if (def == null)
        throw new ModelNotFoundException(typeName);
    AbstractEventHandlerModel<?> model = (AbstractEventHandlerModel<?>) mapper.treeToValue(tree, def.getModelClass());
    model.setDefinition(def);
    return model;
}
Also used : AbstractEventHandlerModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.handlers.AbstractEventHandlerModel) ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ModelNotFoundException

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

the class EmailRecipientModelDeserializer method deserialize.

/* (non-Javadoc)
		 * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
		 */
@Override
public EmailRecipientModel<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode tree = jp.readValueAsTree();
    String type = tree.get("type").asText();
    int code = EmailRecipient.TYPE_CODES.getId(type);
    switch(code) {
        case EmailRecipient.TYPE_ADDRESS:
            return (AddressEntryModel) mapper.treeToValue(tree, AddressEntryModel.class);
        case EmailRecipient.TYPE_MAILING_LIST:
            return (MailingListModel) mapper.treeToValue(tree, MailingListModel.class);
        case EmailRecipient.TYPE_USER:
            return (UserEntryModel) mapper.treeToValue(tree, UserEntryModel.class);
        default:
            throw new ModelNotFoundException(type);
    }
}
Also used : AddressEntryModel(com.serotonin.m2m2.web.mvc.rest.v1.model.email.AddressEntryModel) ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) UserEntryModel(com.serotonin.m2m2.web.mvc.rest.v1.model.email.UserEntryModel) JsonNode(com.fasterxml.jackson.databind.JsonNode) MailingListModel(com.serotonin.m2m2.web.mvc.rest.v1.model.email.MailingListModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with ModelNotFoundException

use of com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException 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)

Example 5 with ModelNotFoundException

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

the class CSVPojoReader method readNext.

/**
 * Read the next pojo from the csv file
 *
 * @return The next pojo from the csv file or null if end of file
 * @throws IOException
 */
public T readNext() throws IOException {
    if (headers == null) {
        headers = reader.readNext();
        if (headers == null)
            return null;
        // Make sure we don't have any accidental whitespaces
        for (int i = 0; i < headers.length; i++) {
            headers[i] = headers[i].trim();
        }
        if (headers.length > 0) {
            if (!headers[0].equalsIgnoreCase("modelType"))
                throw new IOException("Invalid header format, first column must be modelType.");
        } else {
            throw new IOException("No headers provided.");
        }
    }
    String[] line = reader.readNext();
    T pojo = null;
    if (line != null) {
        if (line.length != headers.length) {
            String columnString = new String();
            for (String col : line) columnString += col + ",";
            if (line.length > headers.length) {
                throw new IOException("Header and row length must match.\nRow has too many columns.\n" + columnString);
            } else {
                throw new IOException("Header and row length must match.\nRow has too few columns.\n" + columnString);
            }
        }
        if (!pojoHandler.isInitialized()) {
            // Always needs to be
            String typeName = line[0];
            // Find the model
            ModelDefinition definition = null;
            List<ModelDefinition> definitions = ModuleRegistry.getModelDefinitions();
            for (ModelDefinition d : definitions) {
                if (d.getModelTypeName().equalsIgnoreCase(typeName)) {
                    definition = d;
                    break;
                }
            }
            if (definition == null)
                throw new ModelNotFoundException(typeName);
            else
                pojoHandler.initialize(line, definition.createModel());
        }
        pojo = (T) pojoHandler.newInstance();
        for (int i = 1; i < headers.length; i++) {
            try {
                pojoHandler.setField(pojo, headers[i], line[i].trim());
            } catch (CSVException e) {
                throw new CSVException("Row " + i + " column '" + headers[i] + "' " + e.getMessage());
            }
        }
    }
    return pojo;
}
Also used : ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) ModelDefinition(com.serotonin.m2m2.module.ModelDefinition) IOException(java.io.IOException)

Aggregations

ModelNotFoundException (com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException)9 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 ModelDefinition (com.serotonin.m2m2.module.ModelDefinition)2 PublisherDefinition (com.serotonin.m2m2.module.PublisherDefinition)2 VirtualSerialPortConfig (com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig)1 DataSourceDefinition (com.serotonin.m2m2.module.DataSourceDefinition)1 EventTypeDefinition (com.serotonin.m2m2.module.EventTypeDefinition)1 AbstractDataSourceModel (com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel)1 AddressEntryModel (com.serotonin.m2m2.web.mvc.rest.v1.model.email.AddressEntryModel)1 MailingListModel (com.serotonin.m2m2.web.mvc.rest.v1.model.email.MailingListModel)1 UserEntryModel (com.serotonin.m2m2.web.mvc.rest.v1.model.email.UserEntryModel)1 AuditEventTypeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.AuditEventTypeModel)1 DataPointEventTypeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.DataPointEventTypeModel)1 DataSourceEventTypeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.DataSourceEventTypeModel)1 EventTypeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.EventTypeModel)1 MissingEventTypeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.MissingEventTypeModel)1 PublisherEventTypeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.PublisherEventTypeModel)1 SystemEventTypeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.SystemEventTypeModel)1 AbstractEventDetectorModel (com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel)1