Search in sources :

Example 61 with NotFoundException

use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by infiniteautomation.

the class UserDao method update.

@Override
public void update(User existing, User vo) {
    // ensure passwords prefixed with {PLAINTEXT} are always hashed before database insertion/update
    // we hash plain text passwords after validation has taken place so we can check complexity etc
    vo.hashPlainText();
    try {
        User old = getTransactionTemplate().execute(status -> {
            User old1 = get(vo.getId());
            if (old1 == null) {
                return null;
            }
            boolean passwordChanged = !old1.getPassword().equals(vo.getPassword());
            if (passwordChanged) {
                vo.setPasswordChangeTimestamp(Common.timer.currentTimeMillis());
                vo.setPasswordVersion(old1.getPasswordVersion() + 1);
            } else {
                vo.setPasswordChangeTimestamp(old1.getPasswordChangeTimestamp());
                vo.setPasswordVersion(old1.getPasswordVersion());
            }
            UserDao.super.update(old1, vo);
            // Set the last login time so it is available on the saved user
            vo.setLastLogin(old1.getLastLogin());
            if (passwordChanged || vo.isDisabled()) {
                expireSessionsForUser(old1);
            }
            return old1;
        });
        if (old == null) {
            throw new NotFoundException();
        }
    } catch (DataIntegrityViolationException e) {
        // Log some information about the user object.
        LOG.error("Error updating user: " + vo, e);
        throw e;
    }
}
Also used : User(com.serotonin.m2m2.vo.User) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 62 with NotFoundException

use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by MangoAutomation.

the class PublisherImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    PublisherVO vo = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        String typeStr = json.getString("type");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.publisher.missingType", xid, ModuleRegistry.getPublisherDefinitionTypes());
        else {
            PublisherDefinition<?> def = ModuleRegistry.getPublisherDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.publisher.invalidType", xid, typeStr, ModuleRegistry.getPublisherDefinitionTypes());
            else {
                vo = def.baseCreatePublisherVO();
                vo.setXid(xid);
            }
        }
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            boolean isnew = vo.isNew();
            if (Common.runtimeManager.getLifecycleState() == ILifecycleState.RUNNING) {
                if (isnew) {
                    service.insert(vo);
                } else {
                    service.update(vo.getId(), vo);
                }
                addSuccessMessage(isnew, "emport.publisher.prefix", xid);
                // Handle embedded points (pre Mango 4.3 this was the case)
                JsonArray arr = json.getJsonArray("points");
                if (arr != null) {
                    for (JsonValue jv : arr) {
                        String dataPointXid = jv.getJsonValue("dataPointId").toString();
                        try {
                            DataPointVO dataPointVO = dataPointService.get(dataPointXid);
                            PublishedPointVO point = vo.getDefinition().createPublishedPointVO(vo, dataPointVO);
                            point.setName(dataPointVO.getName());
                            ctx.getReader().readInto(point, jv.toJsonObject());
                            point.setXid(publishedPointService.generateUniqueXid());
                            publishedPointService.insert(point);
                            addSuccessMessage(isnew, "emport.publishedPoint.prefix", point.getXid());
                        } catch (NotFoundException e) {
                            addFailureMessage("emport.publisher.prefix", xid, new TranslatableMessage("emport.error.missingPoint", dataPointXid));
                        } catch (ValidationException e) {
                            for (ProcessMessage m : e.getValidationResult().getMessages()) {
                                addFailureMessage(new ProcessMessage("emport.publisher.prefix", new TranslatableMessage("literal", xid), m));
                            }
                        }
                    }
                }
            } else {
                addFailureMessage("emport.publisher.runtimeManagerNotRunning", xid);
            }
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.publisher.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.publisher.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.publisher.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) JsonValue(com.serotonin.json.type.JsonValue) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonArray(com.serotonin.json.type.JsonArray) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage) PublisherVO(com.serotonin.m2m2.vo.publish.PublisherVO) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) PublishedPointVO(com.serotonin.m2m2.vo.publish.PublishedPointVO)

Example 63 with NotFoundException

use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by MangoAutomation.

the class RoleImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    String name = json.getString("name");
    RoleVO vo = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        vo = new RoleVO(Common.NEW_ID, xid, name);
    }
    try {
        // Read into the VO to get all properties
        ctx.getReader().readInto(vo, json);
        boolean isnew = vo.getId() == Common.NEW_ID;
        if (isnew) {
            service.insert(vo);
        } else {
            service.update(vo.getId(), vo);
        }
        addSuccessMessage(isnew, "emport.role.prefix", xid);
    } catch (ValidationException e) {
        setValidationMessages(e.getValidationResult(), "emport.role.prefix", xid);
    } catch (JsonException e) {
        addFailureMessage("emport.role.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : JsonException(com.serotonin.json.JsonException) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException)

Example 64 with NotFoundException

use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by MangoAutomation.

the class EventHandlerImporter method importImpl.

@Override
protected void importImpl() {
    AbstractEventHandlerVO handler = null;
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            handler = service.get(xid);
        } catch (NotFoundException e) {
        // Nothing, done below
        }
    }
    if (handler == null) {
        String typeStr = json.getString("handlerType");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.eventHandler.missingType", xid, ModuleRegistry.getEventHandlerDefinitionTypes());
        else {
            EventHandlerDefinition<?> def = ModuleRegistry.getEventHandlerDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.eventHandler.invalidType", xid, typeStr, ModuleRegistry.getEventHandlerDefinitionTypes());
            else {
                handler = def.baseCreateEventHandlerVO();
                handler.setXid(xid);
            }
        }
    }
    JsonObject et = json.getJsonObject("eventType");
    JsonArray ets = json.getJsonArray("eventTypes");
    if (handler != null) {
        try {
            ctx.getReader().readInto(handler, json);
            Set<EventTypeMatcher> eventTypes = new HashSet<>(handler.getEventTypes());
            // Find the event type.
            if (et != null) {
                eventTypes.add(new EventTypeMatcher(ctx.getReader().read(EventType.class, et)));
            } else if (ets != null) {
                for (JsonValue jsonValue : ets) {
                    eventTypes.add(new EventTypeMatcher(ctx.getReader().read(EventType.class, jsonValue)));
                }
            }
            handler.setEventTypes(new ArrayList<>(eventTypes));
            boolean isnew = handler.getId() == Common.NEW_ID;
            if (isnew) {
                service.insert(handler);
                addSuccessMessage(true, "emport.eventHandler.prefix", xid);
            } else {
                service.update(handler.getId(), handler);
                addSuccessMessage(false, "emport.eventHandler.prefix", xid);
            }
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.eventHandler.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.eventHandler.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.eventHandler.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) EventType(com.serotonin.m2m2.rt.event.type.EventType) JsonValue(com.serotonin.json.type.JsonValue) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO) JsonArray(com.serotonin.json.type.JsonArray) HashSet(java.util.HashSet)

Example 65 with NotFoundException

use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by MangoAutomation.

the class JsonDataImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    JsonDataVO vo = null;
    boolean isNew = false;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        isNew = true;
        vo = new JsonDataVO();
        vo.setXid(xid);
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            // Ensure we have a default permission since null is valid in Mango 3.x
            if (vo.getReadPermission() == null) {
                vo.setReadPermission(new MangoPermission());
            }
            if (vo.getEditPermission() == null) {
                vo.setEditPermission(new MangoPermission());
            }
            if (isNew) {
                service.insert(vo);
            } else {
                service.update(vo.getId(), vo);
            }
            addSuccessMessage(isNew, "emport.jsondata.prefix", xid);
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.jsondata.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.jsondata.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.jsondata.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) JsonDataVO(com.serotonin.m2m2.vo.json.JsonDataVO) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Aggregations

NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)74 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)26 JsonException (com.serotonin.json.JsonException)20 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)18 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)18 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)15 User (com.serotonin.m2m2.vo.User)15 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)15 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)12 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)10 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)9 JsonArray (com.serotonin.json.type.JsonArray)8 TranslatableIllegalArgumentException (com.infiniteautomation.mango.util.exception.TranslatableIllegalArgumentException)6 TranslatableRuntimeException (com.infiniteautomation.mango.util.exception.TranslatableRuntimeException)6 FileStore (com.serotonin.m2m2.vo.FileStore)6 AbstractEventHandlerVO (com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)6 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)6 PublisherVO (com.serotonin.m2m2.vo.publish.PublisherVO)6 ApiOperation (io.swagger.annotations.ApiOperation)6 IOException (java.io.IOException)6