Search in sources :

Example 6 with DataPointChangeDefinition

use of com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition in project ma-core-public by infiniteautomation.

the class DataPointDao method deleteBatch.

/**
 * Delete a batch of data points in bulk (for performance)
 */
protected void deleteBatch(List<DataPointVO> batch, List<Integer> ids, Set<Integer> permissionIds, Set<Integer> seriesIds) {
    // delete event handler mappings
    create.deleteFrom(eventHandlersMapping).where(eventHandlersMapping.eventTypeName.eq(EventTypeNames.DATA_POINT), eventHandlersMapping.eventTypeRef1.in(ids)).execute();
    // delete user comments
    create.deleteFrom(userComments).where(userComments.commentType.eq(2), userComments.typeKey.in(ids)).execute();
    // delete event detectors
    create.deleteFrom(eventDetectors).where(eventDetectors.dataPointId.in(ids)).execute();
    for (DataPointVO vo : batch) {
        DataSourceDefinition<? extends DataSourceVO> def = ModuleRegistry.getDataSourceDefinition(vo.getPointLocator().getDataSourceType());
        if (def != null) {
            def.deleteRelationalData(vo);
        }
        // Run Pre Delete Change definitions
        for (DataPointChangeDefinition changeDefinition : changeDefinitions) {
            changeDefinition.preDelete(vo);
        }
    }
    // delete the points in bulk
    int deleted = create.deleteFrom(table).where(table.id.in(ids)).execute();
    if (this.countMonitor != null) {
        this.countMonitor.addValue(-deleted);
    }
    for (Integer id : permissionIds) {
        permissionService.deletePermissionId(id);
    }
    // Try to delete the timeSeries.id row
    for (Integer seriesId : seriesIds) {
        try {
            this.create.deleteFrom(TimeSeries.TIME_SERIES).where(TimeSeries.TIME_SERIES.id.eq(seriesId)).execute();
        } catch (Exception e) {
        // Probably in use by another point (2 points on the same series)
        }
    }
    // Run Post Delete Change definitions
    for (DataPointVO vo : batch) {
        for (DataPointChangeDefinition changeDefinition : changeDefinitions) {
            changeDefinition.postDelete(vo);
        }
    }
    // Audit Events/Dao events
    for (DataPointVO vo : batch) {
        publishEvent(createDaoEvent(DaoEventType.DELETE, vo, null));
        publishAuditEvent(new DeleteAuditEvent<DataPointVO>(this.auditEventType, Common.getUser(), vo));
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointChangeDefinition(com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition) NoDataFoundException(org.jooq.exception.NoDataFoundException) ModuleNotLoadedException(com.serotonin.ModuleNotLoadedException) LicenseViolatedException(com.serotonin.m2m2.LicenseViolatedException)

Example 7 with DataPointChangeDefinition

use of com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition in project ma-core-public by infiniteautomation.

the class DataPointService method update.

@Override
protected DataPointVO update(DataPointVO existing, DataPointVO vo) throws PermissionException, ValidationException {
    PermissionHolder user = Common.getUser();
    ensureEditPermission(user, existing);
    vo.setId(existing.getId());
    for (DataPointChangeDefinition def : changeDefinitions) {
        def.preUpdate(vo);
    }
    ensureValid(existing, vo);
    getRuntimeManager().stopDataPoint(vo.getId());
    dao.update(existing, vo);
    for (DataPointChangeDefinition def : changeDefinitions) {
        def.postUpdate(vo);
    }
    if (vo.isEnabled()) {
        List<AbstractPointEventDetectorVO> detectors = eventDetectorDao.getWithSource(vo.getId(), vo);
        getRuntimeManager().startDataPoint(new DataPointWithEventDetectors(vo, detectors));
    }
    return vo;
}
Also used : DataPointChangeDefinition(com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Example 8 with DataPointChangeDefinition

use of com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition in project ma-core-public by infiniteautomation.

the class DataPointService method delete.

@Override
protected DataPointVO delete(DataPointVO vo) throws PermissionException, NotFoundException {
    PermissionHolder user = Common.getUser();
    ensureDeletePermission(user, vo);
    for (DataPointChangeDefinition def : changeDefinitions) {
        def.preDelete(vo);
    }
    getRuntimeManager().stopDataPoint(vo.getId());
    dao.delete(vo);
    for (DataPointChangeDefinition def : changeDefinitions) {
        def.postDelete(vo);
    }
    pointValueCache.deleteCache(vo);
    getEventManager().cancelEventsForDataPoint(vo.getId());
    return vo;
}
Also used : DataPointChangeDefinition(com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Example 9 with DataPointChangeDefinition

use of com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition in project ma-core-public by infiniteautomation.

the class DataPointDao method updateDataPoint.

void updateDataPoint(final DataPointVO dp) {
    for (DataPointChangeDefinition def : ModuleRegistry.getDefinitions(DataPointChangeDefinition.class)) def.beforeUpdate(dp);
    DataPointVO old = getDataPoint(dp.getId());
    // If have a new data type we will wipe our history
    if (old.getPointLocator().getDataTypeId() != dp.getPointLocator().getDataTypeId())
        Common.databaseProxy.newPointValueDao().deletePointValues(dp.getId());
    // Save the VO information.
    updateDataPointShallow(dp);
    AuditEventType.raiseChangedEvent(AuditEventType.TYPE_DATA_POINT, old, dp);
    // Save the relational information.
    saveRelationalData(dp, false);
    for (DataPointChangeDefinition def : ModuleRegistry.getDefinitions(DataPointChangeDefinition.class)) def.afterUpdate(dp);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointChangeDefinition(com.serotonin.m2m2.module.DataPointChangeDefinition)

Example 10 with DataPointChangeDefinition

use of com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition in project ma-core-public by infiniteautomation.

the class DataPointService method insert.

@Override
public DataPointVO insert(DataPointVO vo) throws PermissionException, ValidationException {
    PermissionHolder user = Common.getUser();
    // Ensure they can create
    ensureCreatePermission(user, vo);
    // Ensure id is not set
    if (vo.getId() != Common.NEW_ID) {
        ProcessResult result = new ProcessResult();
        result.addContextualMessage("id", "validate.invalidValue");
        throw new ValidationException(result);
    }
    // Generate an Xid if necessary
    if (StringUtils.isEmpty(vo.getXid()))
        vo.setXid(dao.generateUniqueXid());
    for (DataPointChangeDefinition def : changeDefinitions) {
        def.preInsert(vo);
    }
    ensureValid(vo);
    dao.insert(vo);
    List<AbstractPointEventDetectorVO> detectors = new ArrayList<>();
    for (DataPointChangeDefinition def : changeDefinitions) {
        for (var detector : def.postInsert(vo)) {
            if (detector.isNew()) {
                log.warn("Detector added via postInsert hook was not saved");
            } else if (detector.getDataPoint().getId() != vo.getId()) {
                log.warn("Detector added via postInsert hook was for a different data point");
            } else {
                detectors.add(detector);
            }
        }
    }
    if (vo.isEnabled()) {
        // the data point cannot have detectors if it was just inserted, don't query for detectors
        getRuntimeManager().startDataPoint(new DataPointWithEventDetectors(vo, detectors));
    }
    return vo;
}
Also used : DataPointChangeDefinition(com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ArrayList(java.util.ArrayList) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Aggregations

DataPointChangeDefinition (com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition)8 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)4 DataPointWithEventDetectors (com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors)4 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)4 DataPointChangeDefinition (com.serotonin.m2m2.module.DataPointChangeDefinition)3 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)2 ModuleNotLoadedException (com.serotonin.ModuleNotLoadedException)2 LicenseViolatedException (com.serotonin.m2m2.LicenseViolatedException)2 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)2 ArrayList (java.util.ArrayList)2 NoDataFoundException (org.jooq.exception.NoDataFoundException)2 TransactionStatus (org.springframework.transaction.TransactionStatus)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1