Search in sources :

Example 31 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class RestFilterParserTest method testInOperator.

@Test
public void testInOperator() throws Exception {
    new StrictExpectations() {

        {
            RandomStringUtils.randomAlphabetic(anyInt);
            result = "paramName1";
        }
    };
    String data = readDataFromFile("data/restFilter5.json");
    MetaClass metaClass = metadata.getClass("test$TestEntity");
    RestFilterParseResult parseResult = restFilterParser.parse(data, metaClass);
    String expectedJpqlWhere = "({E}.intField in :paramName1)";
    assertEquals(expectedJpqlWhere, parseResult.getJpqlWhere());
    Map<String, Object> queryParameters = parseResult.getQueryParameters();
    List<Integer> param1Value = (List<Integer>) queryParameters.get("paramName1");
    assertEquals(Arrays.asList(1, 2, 3), param1Value);
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) List(java.util.List) Test(org.junit.Test)

Example 32 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class RestFilterParserTest method testParseSimpleFilter.

@Test
public void testParseSimpleFilter() throws Exception {
    new StrictExpectations() {

        {
            RandomStringUtils.randomAlphabetic(anyInt);
            result = "stringParamName";
            RandomStringUtils.randomAlphabetic(anyInt);
            result = "intParamName";
            RandomStringUtils.randomAlphabetic(anyInt);
            result = "booleanParamName";
        }
    };
    String data = readDataFromFile("data/restFilter1.json");
    MetaClass metaClass = metadata.getClass("test$TestEntity");
    RestFilterParseResult parseResult = restFilterParser.parse(data, metaClass);
    String expectedJpqlWhere = "({E}.stringField <> :stringParamName and " + "{E}.intField > :intParamName and " + "{E}.booleanField = :booleanParamName)";
    assertEquals(expectedJpqlWhere, parseResult.getJpqlWhere());
    Map<String, Object> queryParameters = parseResult.getQueryParameters();
    assertEquals("stringValue", queryParameters.get("stringParamName"));
    assertEquals(100, queryParameters.get("intParamName"));
    assertEquals(true, queryParameters.get("booleanParamName"));
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) Test(org.junit.Test)

Example 33 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class DeletePolicyHandler method recognizeRelatedEntityMetaClass.

@Nullable
protected MetaClass recognizeRelatedEntityMetaClass(String message) {
    Matcher matcher = getPattern().matcher(message);
    if (matcher.find()) {
        String entityName = matcher.group(2);
        if (!StringUtils.isEmpty(entityName)) {
            MetaClass metaClass = metadata.getClass(entityName);
            if (metaClass != null) {
                MetaClass originalMetaClass = metadata.getExtendedEntities().getOriginalMetaClass(metaClass);
                metaClass = originalMetaClass != null ? originalMetaClass : metaClass;
            }
            return metaClass;
        }
    }
    return null;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) Matcher(java.util.regex.Matcher) Nullable(javax.annotation.Nullable)

Example 34 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class DeletePolicyHandler method doHandle.

protected void doHandle(String message, WindowManager windowManager) {
    String caption = null;
    String notificationMessage = null;
    MetaClass deletedEntityMetaClass = recognizeDeletedEntityMetaClass(message);
    if (deletedEntityMetaClass != null) {
        String customCaptionKey = String.format("deletePolicy.caption.%s", deletedEntityMetaClass.getName());
        String customCaption = messages.getMainMessage(customCaptionKey);
        if (!customCaptionKey.equals(customCaption)) {
            caption = customCaption;
        }
        String customMessageKey = String.format("deletePolicy.references.message.%s", deletedEntityMetaClass.getName());
        String customMessage = messages.getMainMessage(customMessageKey);
        if (!customMessageKey.equals(customMessage)) {
            notificationMessage = customMessage;
        }
    }
    if (StringUtils.isEmpty(caption)) {
        caption = messages.getMainMessage("deletePolicy.caption");
    }
    if (StringUtils.isEmpty(notificationMessage)) {
        MetaClass metaClass = recognizeRelatedEntityMetaClass(message);
        if (metaClass != null) {
            String localizedEntityName = messages.getTools().getEntityCaption(metaClass);
            String referencesMessage = messages.getMainMessage("deletePolicy.references.message");
            notificationMessage = String.format(referencesMessage, localizedEntityName);
        }
    }
    windowManager.showNotification(caption, notificationMessage, Frame.NotificationType.ERROR);
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 35 with MetaClass

use of com.haulmont.chile.core.model.MetaClass 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)302 MetaProperty (com.haulmont.chile.core.model.MetaProperty)103 Entity (com.haulmont.cuba.core.entity.Entity)54 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)36 Nullable (javax.annotation.Nullable)25 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)24 Element (org.dom4j.Element)21 java.util (java.util)18 Inject (javax.inject.Inject)17 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)16 Test (org.junit.Test)15 EntityManager (com.haulmont.cuba.core.EntityManager)14 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)14 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)13 Metadata (com.haulmont.cuba.core.global.Metadata)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)12 Collectors (java.util.stream.Collectors)11 Range (com.haulmont.chile.core.model.Range)10