Search in sources :

Example 1 with EventTypeDefinition

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

the class EventHandlersDwr method getInitData.

@DwrPermission(user = true)
public Map<String, Object> getInitData() {
    User user = Common.getHttpUser();
    Permissions.ensureDataSourcePermission(user);
    Map<String, Object> model = new HashMap<>();
    // Get the data sources.
    List<DataSourceVO<?>> dss = DataSourceDao.instance.getDataSources();
    // Create a lookup of data sources to quickly determine data point permissions.
    Map<Integer, DataSourceVO<?>> dslu = new HashMap<>();
    for (DataSourceVO<?> ds : dss) dslu.put(ds.getId(), ds);
    // Get the data points
    List<DataPointBean> allPoints = new ArrayList<>();
    List<EventSourceBean> dataPoints = new ArrayList<>();
    List<DataPointVO> dps = DataPointDao.instance.getDataPoints(DataPointExtendedNameComparator.instance, true);
    final boolean admin = Permissions.hasAdmin(user);
    for (DataPointVO dp : dps) {
        if (!admin && !Permissions.hasDataSourcePermission(user, dslu.get(dp.getDataSourceId())))
            continue;
        allPoints.add(new DataPointBean(dp));
        if (dp.getEventDetectors().size() > 0) {
            EventSourceBean source = new EventSourceBean();
            source.setId(dp.getId());
            source.setName(dp.getExtendedName());
            for (AbstractPointEventDetectorVO<?> ped : dp.getEventDetectors()) {
                EventTypeVO dpet = ped.getEventType();
                dpet.setHandlers(EventHandlerDao.instance.getEventHandlers(dpet));
                source.getEventTypes().add(dpet);
            }
            dataPoints.add(source);
        }
    }
    // Get the data sources
    List<EventSourceBean> dataSources = new ArrayList<>();
    for (DataSourceVO<?> ds : dss) {
        if (!admin && !Permissions.hasDataSourcePermission(user, ds))
            continue;
        if (ds.getEventTypes().size() > 0) {
            EventSourceBean source = new EventSourceBean();
            source.setId(ds.getId());
            source.setName(ds.getName());
            for (EventTypeVO dset : ds.getEventTypes()) {
                dset.setHandlers(EventHandlerDao.instance.getEventHandlers(dset));
                source.getEventTypes().add(dset);
            }
            dataSources.add(source);
        }
    }
    Map<String, Map<String, Object>> userEventTypes = new LinkedHashMap<>();
    model.put("userEventTypes", userEventTypes);
    for (EventTypeDefinition def : ModuleRegistry.getDefinitions(EventTypeDefinition.class)) {
        if (!def.getHandlersRequireAdmin()) {
            List<EventTypeVO> vos = def.getEventTypeVOs();
            for (EventTypeVO vo : vos) vo.setHandlers(EventHandlerDao.instance.getEventHandlers(vo));
            Map<String, Object> info = new HashMap<>();
            info.put("vos", vos);
            info.put("iconPath", def.getIconPath());
            info.put("description", translate(def.getDescriptionKey()));
            userEventTypes.put(def.getTypeName(), info);
        }
    }
    if (admin) {
        // Get the publishers
        List<EventSourceBean> publishers = new ArrayList<>();
        for (PublisherVO<? extends PublishedPointVO> p : PublisherDao.instance.getPublishers(new PublisherDao.PublisherNameComparator())) {
            if (p.getEventTypes().size() > 0) {
                EventSourceBean source = new EventSourceBean();
                source.setId(p.getId());
                source.setName(p.getName());
                for (EventTypeVO pet : p.getEventTypes()) {
                    pet.setHandlers(EventHandlerDao.instance.getEventHandlers(pet));
                    source.getEventTypes().add(pet);
                }
                publishers.add(source);
            }
        }
        model.put(SchemaDefinition.PUBLISHERS_TABLE, publishers);
        // Get the system events
        List<EventTypeVO> systemEvents = new ArrayList<>();
        for (EventTypeVO sets : SystemEventType.EVENT_TYPES) {
            sets.setHandlers(EventHandlerDao.instance.getEventHandlers(sets));
            systemEvents.add(sets);
        }
        model.put("systemEvents", systemEvents);
        // Get the audit events
        List<EventTypeVO> auditEvents = new ArrayList<>();
        for (EventTypeVO aets : AuditEventType.EVENT_TYPES) {
            aets.setHandlers(EventHandlerDao.instance.getEventHandlers(aets));
            auditEvents.add(aets);
        }
        model.put("auditEvents", auditEvents);
        Map<String, Map<String, Object>> adminEventTypes = new LinkedHashMap<>();
        model.put("adminEventTypes", adminEventTypes);
        for (EventTypeDefinition def : ModuleRegistry.getDefinitions(EventTypeDefinition.class)) {
            if (def.getHandlersRequireAdmin()) {
                List<EventTypeVO> vos = def.getEventTypeVOs();
                for (EventTypeVO vo : vos) vo.setHandlers(EventHandlerDao.instance.getEventHandlers(vo));
                Map<String, Object> info = new HashMap<>();
                info.put("vos", vos);
                info.put("iconPath", def.getIconPath());
                info.put("description", translate(def.getDescriptionKey()));
                adminEventTypes.put(def.getTypeName(), info);
            }
        }
    }
    model.put("userNewScriptPermissions", new ScriptPermissions(user));
    // Get the mailing lists.
    model.put(SchemaDefinition.MAILING_LISTS_TABLE, MailingListDao.instance.getMailingLists());
    // Get the users.
    model.put(SchemaDefinition.USERS_TABLE, UserDao.instance.getUsers());
    model.put("allPoints", allPoints);
    model.put(SchemaDefinition.DATAPOINTS_TABLE, dataPoints);
    model.put(SchemaDefinition.DATASOURCES_TABLE, dataSources);
    return model;
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) DataPointBean(com.serotonin.m2m2.web.dwr.beans.DataPointBean) EventTypeVO(com.serotonin.m2m2.vo.event.EventTypeVO) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition) LinkedHashMap(java.util.LinkedHashMap) EventSourceBean(com.serotonin.m2m2.web.dwr.beans.EventSourceBean) PublisherDao(com.serotonin.m2m2.db.dao.PublisherDao) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ScriptPermissions(com.serotonin.m2m2.rt.script.ScriptPermissions) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 2 with EventTypeDefinition

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

the class EventInstanceDwr method getEventTypeLink.

/**
 * @param divId - Id of div to place the link
 * @param type
 * @param subtype
 * @param ref1
 * @param ref2
 * @return
 */
@DwrPermission(user = true)
public static ProcessResult getEventTypeLink(String divId, String type, String subtype, int ref1, int ref2) {
    ProcessResult result = new ProcessResult();
    result.addData("divId", divId);
    EventTypeDefinition def = ModuleRegistry.getEventTypeDefinition(type);
    if (def != null)
        result.addData("link", def.getEventListLink(subtype, ref1, ref2, Common.getTranslations()));
    return result;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) AuditEventTypeDefinition(com.serotonin.m2m2.module.AuditEventTypeDefinition) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 3 with EventTypeDefinition

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

the class EventInstanceDao method createEventType.

static EventType createEventType(ResultSet rs, int offset) throws SQLException {
    String typeName = rs.getString(offset);
    String subtypeName = rs.getString(offset + 1);
    EventType type;
    if (typeName.equals(EventType.EventTypeNames.DATA_POINT))
        type = new DataPointEventType(rs.getInt(offset + 2), rs.getInt(offset + 3));
    else if (typeName.equals(EventType.EventTypeNames.DATA_SOURCE))
        type = new DataSourceEventType(rs.getInt(offset + 2), rs.getInt(offset + 3));
    else if (typeName.equals(EventType.EventTypeNames.SYSTEM))
        type = new SystemEventType(subtypeName, rs.getInt(offset + 2));
    else if (typeName.equals(EventType.EventTypeNames.PUBLISHER))
        type = new PublisherEventType(rs.getInt(offset + 2), rs.getInt(offset + 3));
    else if (typeName.equals(EventType.EventTypeNames.AUDIT))
        throw new ShouldNeverHappenException("AUDIT events should not exist here. Consider running the SQL: DELETE FROM events WHERE typeName='AUDIT';");
    else {
        EventTypeDefinition def = ModuleRegistry.getEventTypeDefinition(typeName);
        if (def == null) {
            // Create Missing Event Type
            type = new MissingEventType(typeName, null, rs.getInt(offset + 2), rs.getInt(offset + 3));
        } else {
            type = def.createEventType(subtypeName, rs.getInt(offset + 2), rs.getInt(offset + 3));
            if (type == null) {
                // Create Missing Event type
                type = new MissingEventType(typeName, subtypeName, rs.getInt(offset + 2), rs.getInt(offset + 3));
            }
        }
    }
    return type;
}
Also used : DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) MissingEventType(com.serotonin.m2m2.rt.event.type.MissingEventType) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) MissingEventType(com.serotonin.m2m2.rt.event.type.MissingEventType) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition)

Example 4 with EventTypeDefinition

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

the class EventTypeResolver method resolve.

@Override
public Type resolve(JsonValue jsonValue) throws JsonException {
    if (jsonValue == null)
        throw new TranslatableJsonException("emport.error.eventType.null");
    JsonObject json = jsonValue.toJsonObject();
    String text = json.getString("sourceType");
    if (text == null)
        throw new TranslatableJsonException("emport.error.eventType.missing", "sourceType", EventType.SOURCE_NAMES.getCodeList());
    if (!EventType.SOURCE_NAMES.hasCode(text))
        throw new TranslatableJsonException("emport.error.eventType.invalid", "sourceType", text, EventType.SOURCE_NAMES.getCodeList());
    if (text.equalsIgnoreCase(EventTypeNames.DATA_POINT))
        return DataPointEventType.class;
    if (text.equalsIgnoreCase(EventTypeNames.DATA_SOURCE))
        return DataSourceEventType.class;
    if (text.equalsIgnoreCase(EventTypeNames.SYSTEM))
        return SystemEventType.class;
    if (text.equalsIgnoreCase(EventTypeNames.PUBLISHER))
        return PublisherEventType.class;
    if (text.equalsIgnoreCase(EventTypeNames.AUDIT))
        return AuditEventType.class;
    EventTypeDefinition def = ModuleRegistry.getEventTypeDefinition(text);
    if (def != null)
        return def.getEventTypeClass();
    return null;
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition)

Example 5 with EventTypeDefinition

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

the class EventTypeModelDeserializer method deserialize.

/*
     * (non-Javadoc)
     * 
     * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.
     * JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
     */
@Override
public EventTypeModel deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode tree = jp.readValueAsTree();
    String typeName = tree.get("typeName").asText();
    if (StringUtils.isEmpty(typeName))
        throw new ModelNotFoundException(typeName);
    EventTypeModel model = null;
    switch(typeName) {
        case EventType.EventTypeNames.DATA_POINT:
            model = mapper.treeToValue(tree, DataPointEventTypeModel.class);
            break;
        case EventType.EventTypeNames.DATA_SOURCE:
            model = mapper.treeToValue(tree, DataSourceEventTypeModel.class);
            break;
        case EventType.EventTypeNames.AUDIT:
            model = mapper.treeToValue(tree, AuditEventTypeModel.class);
            break;
        case EventType.EventTypeNames.PUBLISHER:
            model = mapper.treeToValue(tree, PublisherEventTypeModel.class);
            break;
        case EventType.EventTypeNames.SYSTEM:
            model = mapper.treeToValue(tree, SystemEventTypeModel.class);
            break;
        case EventType.EventTypeNames.MISSING:
            model = mapper.treeToValue(tree, MissingEventTypeModel.class);
            break;
    }
    if (model == null) {
        EventTypeDefinition def = ModuleRegistry.getEventTypeDefinition(typeName);
        if (def == null)
            throw new ModelNotFoundException(typeName);
        return (EventTypeModel) mapper.treeToValue(tree, def.getModelClass());
    } else
        return model;
}
Also used : DataPointEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.DataPointEventTypeModel) SystemEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.SystemEventTypeModel) DataSourceEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.DataSourceEventTypeModel) AuditEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.AuditEventTypeModel) MissingEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.MissingEventTypeModel) EventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.EventTypeModel) PublisherEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.PublisherEventTypeModel) SystemEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.SystemEventTypeModel) DataSourceEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.DataSourceEventTypeModel) PublisherEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.PublisherEventTypeModel) ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) MissingEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.MissingEventTypeModel) JsonNode(com.fasterxml.jackson.databind.JsonNode) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataPointEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.DataPointEventTypeModel) AuditEventTypeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.eventType.AuditEventTypeModel)

Aggregations

EventTypeDefinition (com.serotonin.m2m2.module.EventTypeDefinition)7 DataPointEventType (com.serotonin.m2m2.rt.event.type.DataPointEventType)2 DataSourceEventType (com.serotonin.m2m2.rt.event.type.DataSourceEventType)2 EventType (com.serotonin.m2m2.rt.event.type.EventType)2 MissingEventType (com.serotonin.m2m2.rt.event.type.MissingEventType)2 PublisherEventType (com.serotonin.m2m2.rt.event.type.PublisherEventType)2 SystemEventType (com.serotonin.m2m2.rt.event.type.SystemEventType)2 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 JsonObject (com.serotonin.json.type.JsonObject)1 PublisherDao (com.serotonin.m2m2.db.dao.PublisherDao)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 AuditEventTypeDefinition (com.serotonin.m2m2.module.AuditEventTypeDefinition)1 SystemEventTypeDefinition (com.serotonin.m2m2.module.SystemEventTypeDefinition)1 AuditEventType (com.serotonin.m2m2.rt.event.type.AuditEventType)1 ScriptPermissions (com.serotonin.m2m2.rt.script.ScriptPermissions)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1