Search in sources :

Example 1 with TemplateDefinition

use of com.serotonin.m2m2.module.TemplateDefinition in project ma-core-public by infiniteautomation.

the class TemplateImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = TemplateDao.instance.generateUniqueXid();
    BaseTemplateVO<?> vo = TemplateDao.instance.getByXid(xid);
    if (vo == null) {
        String typeStr = json.getString("templateType");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.template.missingType", xid, ModuleRegistry.getTemplateDefinitionTypes());
        else {
            TemplateDefinition def = ModuleRegistry.getTemplateDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.template.invalidType", xid, typeStr, ModuleRegistry.getTemplateDefinitionTypes());
            else {
                vo = def.baseCreateTemplateVO();
                vo.setXid(xid);
            }
        }
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            // Now validate it. Use a new response object so we can distinguish errors in this vo from
            // other errors.
            ProcessResult voResponse = new ProcessResult();
            vo.validate(voResponse);
            if (voResponse.getHasMessages())
                setValidationMessages(voResponse, "emport.template.prefix", xid);
            else {
                // Sweet. Save it.
                boolean isnew = vo.isNew();
                TemplateDao.instance.save(vo);
                addSuccessMessage(isnew, "emport.template.prefix", xid);
            }
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.template.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.template.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) TemplateDefinition(com.serotonin.m2m2.module.TemplateDefinition)

Example 2 with TemplateDefinition

use of com.serotonin.m2m2.module.TemplateDefinition in project ma-core-public by infiniteautomation.

the class TemplateRestorer method buildNewVO.

@Override
protected BaseTemplateVO<?> buildNewVO(JsonObject json) {
    BaseTemplateVO<?> vo = null;
    String xid = json.getString("xid");
    String typeStr = json.getString("templateType");
    if (StringUtils.isBlank(typeStr))
        addFailureMessage("emport.template.missingType", xid, ModuleRegistry.getTemplateDefinitionTypes());
    else {
        TemplateDefinition def = ModuleRegistry.getTemplateDefinition(typeStr);
        if (def == null)
            addFailureMessage("emport.template.invalidType", xid, typeStr, ModuleRegistry.getTemplateDefinitionTypes());
        else {
            vo = def.baseCreateTemplateVO();
        }
    }
    return vo;
}
Also used : TemplateDefinition(com.serotonin.m2m2.module.TemplateDefinition)

Example 3 with TemplateDefinition

use of com.serotonin.m2m2.module.TemplateDefinition in project ma-core-public by infiniteautomation.

the class ModuleRegistry method ensureTemplateDefinitions.

private static void ensureTemplateDefinitions() {
    if (TEMPLATE_DEFINITIONS == null) {
        synchronized (LOCK) {
            if (TEMPLATE_DEFINITIONS == null) {
                Map<String, TemplateDefinition> map = new HashMap<String, TemplateDefinition>();
                for (TemplateDefinition def : Module.getDefinitions(preDefaults, TemplateDefinition.class)) {
                    map.put(def.getTemplateTypeName(), def);
                }
                for (Module module : MODULES.values()) {
                    for (TemplateDefinition def : module.getDefinitions(TemplateDefinition.class)) map.put(def.getTemplateTypeName(), def);
                }
                for (TemplateDefinition def : Module.getDefinitions(postDefaults, TemplateDefinition.class)) {
                    map.put(def.getTemplateTypeName(), def);
                }
                TEMPLATE_DEFINITIONS = map;
            }
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) DataPointPropertiesTemplateDefinition(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateDefinition)

Aggregations

TemplateDefinition (com.serotonin.m2m2.module.TemplateDefinition)2 JsonException (com.serotonin.json.JsonException)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 DataPointPropertiesTemplateDefinition (com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateDefinition)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1