Search in sources :

Example 1 with PublisherVO

use of com.serotonin.m2m2.vo.publish.PublisherVO 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 PublisherVO

use of com.serotonin.m2m2.vo.publish.PublisherVO in project ma-core-public by infiniteautomation.

the class PublisherEditDwr method trySave.

protected ProcessResult trySave(PublisherVO<? extends PublishedPointVO> p) {
    ProcessResult response = new ProcessResult();
    p.validate(response);
    if (!response.getHasMessages()) {
        Common.runtimeManager.savePublisher(p);
        response.addData("id", p.getId());
    }
    return response;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult)

Example 3 with PublisherVO

use of com.serotonin.m2m2.vo.publish.PublisherVO in project ma-core-public by infiniteautomation.

the class PublisherListDwr method togglePublisher.

@DwrPermission(admin = true)
public ProcessResult togglePublisher(int publisherId) {
    ProcessResult response = new ProcessResult();
    PublisherVO<? extends PublishedPointVO> publisher = Common.runtimeManager.getPublisher(publisherId);
    publisher.setEnabled(!publisher.isEnabled());
    publisher.validate(response);
    if (!response.getHasMessages())
        Common.runtimeManager.savePublisher(publisher);
    else
        publisher.setEnabled(!publisher.isEnabled());
    response.addData("enabled", publisher.isEnabled());
    response.addData("id", publisherId);
    return response;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 4 with PublisherVO

use of com.serotonin.m2m2.vo.publish.PublisherVO in project ma-modules-public by infiniteautomation.

the class EventTypeV2RestController method getAllPublisherEventTypes.

private void getAllPublisherEventTypes(List<EventTypeModel> types, User user, Integer publisherId, Integer publisherEventId) {
    List<PublisherVO<?>> publishers = PublisherDao.instance.getAll();
    final boolean admin = Permissions.hasAdmin(user);
    for (PublisherVO<?> pvo : publishers) if (publisherId == null || publisherId.intValue() == pvo.getId())
        for (EventTypeVO pet : pvo.getEventTypes()) if (publisherEventId == null || publisherEventId.intValue() == pet.getTypeRef2()) {
            EventType et = pet.createEventType();
            if (admin || Permissions.hasEventTypePermission(user, et))
                types.add(et.asModel());
        }
}
Also used : AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) PublisherVO(com.serotonin.m2m2.vo.publish.PublisherVO) EventTypeVO(com.serotonin.m2m2.vo.event.EventTypeVO)

Example 5 with PublisherVO

use of com.serotonin.m2m2.vo.publish.PublisherVO in project ma-core-public by infiniteautomation.

the class PublisherEditController method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    User user = Common.getUser(request);
    Permissions.ensureAdmin(user);
    PublisherVO<? extends PublishedPointVO> publisherVO;
    // Get the id.
    String idStr = request.getParameter("pid");
    if (idStr == null) {
        // Adding a new data source? Get the type id.
        String typeId = request.getParameter("typeId");
        if (StringUtils.isBlank(typeId))
            return new ModelAndView(new RedirectView(errorViewName));
        // A new publisher
        PublisherDefinition def = ModuleRegistry.getPublisherDefinition(typeId);
        if (def == null)
            return new ModelAndView(new RedirectView(errorViewName));
        publisherVO = def.baseCreatePublisherVO();
        publisherVO.setXid(PublisherDao.instance.generateUniqueXid());
    } else {
        // An existing configuration.
        int id = Integer.parseInt(idStr);
        publisherVO = Common.runtimeManager.getPublisher(id);
        if (publisherVO == null)
            return new ModelAndView(new RedirectView(errorViewName));
    }
    // Set the id of the data source in the user object for the DWR.
    user.setEditPublisher(publisherVO);
    // Create the model.
    Map<String, Object> model = new HashMap<>();
    model.put("publisher", publisherVO);
    if (publisherVO.getId() != Common.NEW_ID) {
        List<EventInstance> events = EventDao.instance.getPendingEventsForPublisher(publisherVO.getId(), user.getId());
        List<EventInstanceBean> beans = new ArrayList<>();
        if (events != null) {
            Translations translations = ControllerUtils.getTranslations(request);
            for (EventInstance event : events) beans.add(new EventInstanceBean(event.isActive(), event.getAlarmLevel(), Functions.getTime(event.getActiveTimestamp()), event.getMessage().translate(translations)));
        }
        model.put("publisherEvents", beans);
    }
    publisherVO.addEditContext(model);
    return new ModelAndView(getViewName(), model);
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) PublisherDefinition(com.serotonin.m2m2.module.PublisherDefinition) EventInstanceBean(com.serotonin.m2m2.web.dwr.beans.EventInstanceBean) RedirectView(org.springframework.web.servlet.view.RedirectView) Translations(com.serotonin.m2m2.i18n.Translations)

Aggregations

ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)2 PublisherDefinition (com.serotonin.m2m2.module.PublisherDefinition)2 User (com.serotonin.m2m2.vo.User)2 EventTypeVO (com.serotonin.m2m2.vo.event.EventTypeVO)2 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 JsonException (com.serotonin.json.JsonException)1 PublisherDao (com.serotonin.m2m2.db.dao.PublisherDao)1 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)1 Translations (com.serotonin.m2m2.i18n.Translations)1 EventTypeDefinition (com.serotonin.m2m2.module.EventTypeDefinition)1 EventInstance (com.serotonin.m2m2.rt.event.EventInstance)1 AuditEventType (com.serotonin.m2m2.rt.event.type.AuditEventType)1 EventType (com.serotonin.m2m2.rt.event.type.EventType)1 SystemEventType (com.serotonin.m2m2.rt.event.type.SystemEventType)1 ScriptPermissions (com.serotonin.m2m2.rt.script.ScriptPermissions)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)1