Search in sources :

Example 1 with LocalizedConstraintMessage

use of com.haulmont.cuba.security.entity.LocalizedConstraintMessage in project cuba by cuba-platform.

the class RowLevelSecurityExceptionHandler method doHandle.

@Override
protected void doHandle(String className, String message, @Nullable Throwable throwable, WindowManager windowManager) {
    try {
        Messages messages = AppBeans.get(Messages.NAME);
        String userCaption = null;
        String userMessage = null;
        if (throwable != null) {
            // noinspection ThrowableResultOfMethodCallIgnored
            Throwable rootCause = ExceptionUtils.getRootCause(throwable);
            RowLevelSecurityException exception = null;
            if (throwable instanceof RowLevelSecurityException) {
                exception = (RowLevelSecurityException) throwable;
            } else if (rootCause instanceof RowLevelSecurityException) {
                exception = (RowLevelSecurityException) rootCause;
            }
            if (exception != null) {
                String entity = exception.getEntity();
                MetaClass entityClass = AppBeans.get(Metadata.NAME, Metadata.class).getClassNN(entity);
                String entityCaption = messages.getTools().getEntityCaption(entityClass);
                ExtendedEntities extendedEntities = AppBeans.get(ExtendedEntities.NAME);
                MetaClass mainMetaClass = extendedEntities.getOriginalOrThisMetaClass(entityClass);
                String originalEntity = mainMetaClass.getName();
                String entityName = originalEntity.split("\\$")[1];
                ConstraintOperationType operationType = exception.getOperationType();
                if (operationType != null) {
                    ConstraintLocalizationService service = AppBeans.get(ConstraintLocalizationService.NAME);
                    LocalizedConstraintMessage localizedMessage = service.findLocalizedConstraintMessage(originalEntity, operationType);
                    if (localizedMessage != null) {
                        UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
                        Locale locale = userSessionSource.getLocale();
                        String localeCode = messages.getTools().localeToString(locale);
                        userCaption = localizedMessage.getLocalizedCaption(localeCode);
                        userMessage = localizedMessage.getLocalizedMessage(localeCode);
                    }
                    String operationId = operationType.getId();
                    if (StringUtils.isEmpty(userCaption)) {
                        String customCaptionKey = String.format("rowLevelSecurity.caption.%s.%s", entityName, operationId);
                        String customCaption = messages.getMainMessage(customCaptionKey);
                        if (!customCaptionKey.equals(customCaption)) {
                            userCaption = customCaption;
                        }
                    }
                    if (StringUtils.isEmpty(userMessage)) {
                        String customMessageKey = String.format("rowLevelSecurity.entityAndOperationMessage.%s.%s", entityName, operationId);
                        String customMessage = messages.getMainMessage(customMessageKey);
                        if (!customMessageKey.equals(customMessage)) {
                            userMessage = customMessage;
                        } else {
                            userMessage = messages.formatMainMessage("rowLevelSecurity.entityAndOperationMessage", messages.getMessage(operationType), entityCaption);
                        }
                    }
                } else {
                    String customCaptionKey = String.format("rowLevelSecurity.caption.%s", entityName);
                    String customCaption = messages.getMainMessage(customCaptionKey);
                    if (!customCaptionKey.equals(customCaption)) {
                        userCaption = customCaption;
                    }
                    String customMessageKey = String.format("rowLevelSecurity.entityMessage.%s", entityName);
                    String customMessage = messages.getMainMessage(customMessageKey);
                    if (!customMessageKey.equals(customMessage)) {
                        userMessage = customMessage;
                    } else {
                        userMessage = messages.formatMainMessage("rowLevelSecurity.entityMessage", entityCaption);
                    }
                }
            }
        }
        if (StringUtils.isEmpty(userCaption)) {
            userCaption = messages.getMainMessage("rowLevelSecurity.caption");
        }
        windowManager.showNotification(userCaption, userMessage, Frame.NotificationType.ERROR);
    } catch (Throwable th) {
        log.error("Unable to handle RowLevelSecurityException", throwable);
        log.error("Exception in RowLevelSecurityExceptionHandler", th);
    }
}
Also used : Locale(java.util.Locale) MetaClass(com.haulmont.chile.core.model.MetaClass) ConstraintOperationType(com.haulmont.cuba.security.entity.ConstraintOperationType) LocalizedConstraintMessage(com.haulmont.cuba.security.entity.LocalizedConstraintMessage) ConstraintLocalizationService(com.haulmont.cuba.core.app.ConstraintLocalizationService)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)1 ConstraintLocalizationService (com.haulmont.cuba.core.app.ConstraintLocalizationService)1 ConstraintOperationType (com.haulmont.cuba.security.entity.ConstraintOperationType)1 LocalizedConstraintMessage (com.haulmont.cuba.security.entity.LocalizedConstraintMessage)1 Locale (java.util.Locale)1