Search in sources :

Example 6 with RestValidationResult

use of com.infiniteautomation.mango.rest.v2.model.RestValidationResult in project ma-modules-public by infiniteautomation.

the class PointValueRestController method latestPointValuesForMultiplePointsAsMultipleArrays.

private ResponseEntity<ObjectStream<Map<String, List<PointValueTime>>>> latestPointValuesForMultiplePointsAsMultipleArrays(HttpServletRequest request, String[] xids, boolean useRendered, boolean unitConversion, int limit, boolean useCache, String dateTimeFormat, String timezone) {
    RestProcessResult<ObjectStream<Map<String, List<PointValueTime>>>> result = new RestProcessResult<ObjectStream<Map<String, List<PointValueTime>>>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (dateTimeFormat != null) {
            try {
                DateTimeFormatter.ofPattern(dateTimeFormat);
            } catch (IllegalArgumentException e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalid", "dateTimeFormat");
                throw new ValidationFailedRestException(vr);
            }
        }
        if (timezone != null) {
            try {
                ZoneId.of(timezone);
            } catch (Exception e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalidValue", "timezone");
                throw new ValidationFailedRestException(vr);
            }
        }
        Map<Integer, DataPointVO> pointIdMap = new HashMap<Integer, DataPointVO>(xids.length);
        DataPointVO vo;
        for (String xid : xids) {
            vo = DataPointDao.instance.getByXid(xid);
            if (vo != null) {
                if (Permissions.hasDataPointReadPermission(user, vo))
                    pointIdMap.put(vo.getId(), vo);
                else {
                    // Abort, invalid permissions
                    result.addRestMessage(getUnauthorizedMessage());
                    return result.createResponseEntity();
                }
            }
        }
        // Do we have any valid points?
        if (pointIdMap.size() == 0) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        try {
            XidPointValueTimeLatestPointFacadeStream pvtDatabaseStream = new XidPointValueTimeLatestPointFacadeStream(pointIdMap, useRendered, unitConversion, limit, useCache, dateTimeFormat, timezone);
            return result.createResponseEntity(pvtDatabaseStream);
        } catch (PermissionException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : RestValidationResult(com.infiniteautomation.mango.rest.v2.model.RestValidationResult) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) User(com.serotonin.m2m2.vo.User) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) HashMap(java.util.HashMap) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) RTException(com.serotonin.m2m2.rt.RTException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) XidPointValueTimeLatestPointFacadeStream(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeLatestPointFacadeStream) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ObjectStream(com.serotonin.m2m2.web.mvc.rest.v1.model.ObjectStream) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with RestValidationResult

use of com.infiniteautomation.mango.rest.v2.model.RestValidationResult in project ma-modules-public by infiniteautomation.

the class PointValueRestController method pointValuesForMultiplePointsAsMultipleArrays.

public ResponseEntity<ObjectStream<Map<String, List<PointValueTime>>>> pointValuesForMultiplePointsAsMultipleArrays(HttpServletRequest request, String[] xids, boolean useRendered, boolean unitConversion, DateTime from, DateTime to, RollupEnum rollup, TimePeriodType timePeriodType, Integer timePeriods, String timezone, Integer limit, String dateTimeFormat) {
    RestProcessResult<ObjectStream<Map<String, List<PointValueTime>>>> result = new RestProcessResult<ObjectStream<Map<String, List<PointValueTime>>>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (dateTimeFormat != null) {
            try {
                DateTimeFormatter.ofPattern(dateTimeFormat);
            } catch (IllegalArgumentException e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalid", "dateTimeFormat");
                throw new ValidationFailedRestException(vr);
            }
        }
        if (timezone != null) {
            try {
                ZoneId.of(timezone);
            } catch (Exception e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalidValue", "timezone");
                throw new ValidationFailedRestException(vr);
            }
        }
        Map<Integer, DataPointVO> pointIdMap = new HashMap<Integer, DataPointVO>(xids.length);
        DataPointVO vo;
        for (String xid : xids) {
            vo = DataPointDao.instance.getByXid(xid);
            if (vo != null) {
                if (Permissions.hasDataPointReadPermission(user, vo))
                    pointIdMap.put(vo.getId(), vo);
                else {
                    // Abort, invalid permissions
                    result.addRestMessage(getUnauthorizedMessage());
                    return result.createResponseEntity();
                }
            }
        }
        // Do we have any valid points?
        if (pointIdMap.size() == 0) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        try {
            long current = Common.timer.currentTimeMillis();
            if (from == null)
                from = new DateTime(current);
            if (to == null)
                to = new DateTime(current);
            // better not to for RESTfulness
            if (timezone != null) {
                DateTimeZone zone = DateTimeZone.forID(timezone);
                from = from.withZone(zone);
                to = to.withZone(zone);
            }
            // Are we using rollup
            if ((rollup != null) && (rollup != RollupEnum.NONE)) {
                if (rollup == RollupEnum.FFT) {
                // Special Rollup for FFT's with no time rollup action
                // TODO Need a way to return frequency or period values
                // IdPointValueFftCalculator calc = new
                // IdPointValueFftCalculator(pointIdMap, from.getTime(), to.getTime(),
                // true);
                // return result.createResponseEntity(calc);
                } else {
                    TimePeriod timePeriod = null;
                    if ((timePeriodType != null) && (timePeriods != null)) {
                        timePeriod = new TimePeriod(timePeriods, timePeriodType);
                    }
                    XidPointValueMapRollupCalculator calc = new XidPointValueMapRollupCalculator(pointIdMap, useRendered, unitConversion, rollup, timePeriod, from, to, limit, dateTimeFormat, timezone);
                    return result.createResponseEntity(calc);
                }
                return result.createResponseEntity();
            } else {
                XidPointValueTimeMapDatabaseStream pvtDatabaseStream = new XidPointValueTimeMapDatabaseStream(pointIdMap, useRendered, unitConversion, from.getMillis(), to.getMillis(), this.dao, limit, dateTimeFormat, timezone);
                return result.createResponseEntity(pvtDatabaseStream);
            }
        } catch (PermissionException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : RestValidationResult(com.infiniteautomation.mango.rest.v2.model.RestValidationResult) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) User(com.serotonin.m2m2.vo.User) XidPointValueTimeMapDatabaseStream(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeMapDatabaseStream) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) HashMap(java.util.HashMap) TimePeriod(com.serotonin.m2m2.web.mvc.rest.v1.model.time.TimePeriod) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) RTException(com.serotonin.m2m2.rt.RTException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) DateTime(org.joda.time.DateTime) DateTimeZone(org.joda.time.DateTimeZone) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ObjectStream(com.serotonin.m2m2.web.mvc.rest.v1.model.ObjectStream) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) List(java.util.List) ArrayList(java.util.ArrayList) XidPointValueMapRollupCalculator(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueMapRollupCalculator) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with RestValidationResult

use of com.infiniteautomation.mango.rest.v2.model.RestValidationResult in project ma-core-public by infiniteautomation.

the class ModuleQueryModel method ensureValid.

/**
 * To be called prior to creating the query
 */
public void ensureValid(User user, String tableName) {
    RestValidationResult result = new RestValidationResult();
    if (queryType == null)
        result.addRequiredError("queryType");
    this.definition = ModuleRegistry.getModuleQueryDefinition(queryType);
    if (this.definition == null) {
        result.addInvalidValueError("definition");
        result.ensureValid();
    }
    definition.ensurePermission(user);
    definition.validate(user, tableName, parameters, result);
    result.ensureValid();
}
Also used : RestValidationResult(com.infiniteautomation.mango.rest.v2.model.RestValidationResult)

Example 9 with RestValidationResult

use of com.infiniteautomation.mango.rest.v2.model.RestValidationResult in project ma-modules-public by infiniteautomation.

the class Log4JResetActionDefinition method validateImpl.

/* (non-Javadoc)
	 * @see com.serotonin.m2m2.module.SystemActionDefinition#validate(com.fasterxml.jackson.databind.JsonNode)
	 */
@Override
protected RestValidationResult validateImpl(JsonNode input) throws ValidationFailedRestException {
    RestValidationResult result = new RestValidationResult();
    JsonNode node = input.get("action");
    if (node == null)
        result.addRequiredError("action");
    else {
        switch(node.asText()) {
            case "RESET":
            case "TEST_DEBUG":
            case "TEST_INFO":
            case "TEST_WARN":
            case "TEST_ERROR":
            case "TEST_FATAL":
                break;
            default:
                result.addInvalidValueError("action");
        }
    }
    return result;
}
Also used : RestValidationResult(com.infiniteautomation.mango.rest.v2.model.RestValidationResult) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 10 with RestValidationResult

use of com.infiniteautomation.mango.rest.v2.model.RestValidationResult in project ma-modules-public by infiniteautomation.

the class PointValueRestController method firstAndLastPointValues.

@ApiOperation(value = "First and last point values", notes = "Retrieves the first and last point values within a time range, used to read accumulators")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}/first-last", produces = { "application/json", "text/csv" })
public ResponseEntity<List<PointValueTimeModel>> firstAndLastPointValues(HttpServletRequest request, @ApiParam(value = "Point xid", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Return rendered value as String", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean useRendered, @ApiParam(value = "Return converted value using displayed unit", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean unitConversion, @ApiParam(value = "From time", required = false, allowMultiple = false) @RequestParam(value = "from", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) DateTime from, @ApiParam(value = "To time", required = false, allowMultiple = false) @RequestParam(value = "to", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) DateTime to, @ApiParam(value = "Time zone of output, used if formatted times are returned", required = false, allowMultiple = false) @RequestParam(value = "timezone", required = false) String timezone) {
    RestProcessResult<List<PointValueTimeModel>> result = new RestProcessResult<List<PointValueTimeModel>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (timezone != null) {
            try {
                ZoneId.of(timezone);
            } catch (Exception e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalidValue", "timezone");
                throw new ValidationFailedRestException(vr);
            }
        }
        DataPointVO vo = DataPointDao.instance.getByXid(xid);
        if (vo == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        try {
            if (Permissions.hasDataPointReadPermission(user, vo)) {
                long current = Common.timer.currentTimeMillis();
                if (from == null)
                    from = new DateTime(current);
                if (to == null)
                    to = new DateTime(current);
                // better not to for RESTfulness
                if (timezone != null) {
                    DateTimeZone zone = DateTimeZone.forID(timezone);
                    from = from.withZone(zone);
                    to = to.withZone(zone);
                }
                PointValueFacade pointValueFacade = new PointValueFacade(vo.getId(), false);
                PointValueTime first = pointValueFacade.getPointValueAfter(from.getMillis());
                PointValueTime last = pointValueFacade.getPointValueBefore(to.getMillis());
                List<PointValueTimeModel> models = new ArrayList<PointValueTimeModel>(2);
                if (useRendered) {
                    if (first != null) {
                        PointValueTimeModel model = new PointValueTimeModel();
                        model.setType(DataTypeEnum.convertTo(first.getValue().getDataType()));
                        model.setValue(Functions.getRenderedText(vo, first));
                        model.setTimestamp(first.getTime());
                        if (first.isAnnotated())
                            model.setAnnotation(((AnnotatedPointValueTime) first).getAnnotation(Common.getTranslations()));
                        models.add(model);
                    }
                    if (last != null) {
                        PointValueTimeModel model = new PointValueTimeModel();
                        model.setType(DataTypeEnum.convertTo(last.getValue().getDataType()));
                        model.setValue(Functions.getRenderedText(vo, last));
                        model.setTimestamp(last.getTime());
                        if (last.isAnnotated())
                            model.setAnnotation(((AnnotatedPointValueTime) last).getAnnotation(Common.getTranslations()));
                        models.add(model);
                    }
                } else if (unitConversion) {
                    if (first != null) {
                        PointValueTimeModel model = new PointValueTimeModel();
                        model.setType(DataTypeEnum.convertTo(first.getValue().getDataType()));
                        model.setValue(vo.getUnit().getConverterTo(vo.getRenderedUnit()).convert(first.getValue().getDoubleValue()));
                        model.setTimestamp(first.getTime());
                        if (first.isAnnotated())
                            model.setAnnotation(((AnnotatedPointValueTime) first).getAnnotation(Common.getTranslations()));
                        models.add(model);
                    }
                    if (last != null) {
                        PointValueTimeModel model = new PointValueTimeModel();
                        model.setType(DataTypeEnum.convertTo(last.getValue().getDataType()));
                        model.setValue(vo.getUnit().getConverterTo(vo.getRenderedUnit()).convert(last.getValue().getDoubleValue()));
                        model.setTimestamp(last.getTime());
                        if (last.isAnnotated())
                            model.setAnnotation(((AnnotatedPointValueTime) last).getAnnotation(Common.getTranslations()));
                        models.add(model);
                    }
                } else {
                    models.add(first == null ? null : new PointValueTimeModel(first));
                    models.add(last == null ? null : new PointValueTimeModel(last));
                }
                if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE) {
                    // If we are an image type we should build the URLS
                    UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/hst{ts}_{id}.jpg");
                    for (PointValueTimeModel model : models) {
                        model.setValue(imageServletBuilder.buildAndExpand(model.getTimestamp(), vo.getId()).toUri());
                    }
                }
                return result.createResponseEntity(models);
            } else {
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        } catch (PermissionException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : RestValidationResult(com.infiniteautomation.mango.rest.v2.model.RestValidationResult) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) User(com.serotonin.m2m2.vo.User) XidPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel) RecentPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel) PointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) ArrayList(java.util.ArrayList) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) RTException(com.serotonin.m2m2.rt.RTException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) DateTime(org.joda.time.DateTime) DateTimeZone(org.joda.time.DateTimeZone) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) List(java.util.List) ArrayList(java.util.ArrayList) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RestValidationResult (com.infiniteautomation.mango.rest.v2.model.RestValidationResult)13 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)10 ValidationFailedRestException (com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException)10 RTException (com.serotonin.m2m2.rt.RTException)10 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)10 User (com.serotonin.m2m2.vo.User)10 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)10 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)10 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)10 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)6 HashMap (java.util.HashMap)6 DateTime (org.joda.time.DateTime)6 DateTimeZone (org.joda.time.DateTimeZone)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 PointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel)5 RecentPointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel)5 XidPointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel)5 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)4 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)4 QueryArrayStream (com.serotonin.m2m2.web.mvc.rest.v1.model.QueryArrayStream)4