Search in sources :

Example 26 with DevelopmentException

use of io.jmix.core.DevelopmentException in project jmix by jmix-framework.

the class CubaInstanceNameProviderImpl method parseNamePattern.

@Nullable
@Override
public InstanceNameRec parseNamePattern(MetaClass metaClass) {
    InstanceNameRec namePattern = super.parseNamePattern(metaClass);
    if (namePattern != null) {
        return namePattern;
    }
    Map attributes = (Map) metaClass.getAnnotations().get(NamePattern.class.getName());
    if (attributes == null)
        return null;
    String pattern = (String) attributes.get("value");
    if (StringUtils.isBlank(pattern))
        return null;
    int pos = pattern.indexOf("|");
    if (pos < 0)
        throw new DevelopmentException("Invalid name pattern: " + pattern);
    String format = StringUtils.substring(pattern, 0, pos);
    String trimmedFormat = format.trim();
    String methodName = trimmedFormat.startsWith("#") ? trimmedFormat.substring(1) : null;
    Method method = null;
    if (methodName != null) {
        try {
            method = Stream.of(metaClass.getJavaClass().getDeclaredMethods()).filter(m -> m.getName().equals(methodName)).findFirst().orElseThrow(NoSuchMethodException::new);
        } catch (NoSuchMethodException e) {
            log.error("Instance name method {} not found in meta class {}", methodName, metaClass.getName(), e);
            throw new RuntimeException(String.format("Instance name method %s not found in meta class %s", methodName, metaClass.getName()), e);
        }
    }
    String fieldsStr = StringUtils.substring(pattern, pos + 1);
    MetaProperty[] fields = INSTANCE_NAME_SPLIT_PATTERN.splitAsStream(fieldsStr).map(metaClass::getProperty).toArray(MetaProperty[]::new);
    return new InstanceNameRec(format, method, fields);
}
Also used : NamePattern(com.haulmont.chile.core.annotations.NamePattern) MetaClass(io.jmix.core.metamodel.model.MetaClass) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) InstanceNameProvider(io.jmix.core.InstanceNameProvider) InstanceNameProviderImpl(io.jmix.core.impl.InstanceNameProviderImpl) Stream(java.util.stream.Stream) Map(java.util.Map) Pattern(java.util.regex.Pattern) MetadataLoader(io.jmix.core.impl.MetadataLoader) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) DevelopmentException(io.jmix.core.DevelopmentException) Nonnull(javax.annotation.Nonnull) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) Method(java.lang.reflect.Method) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Map(java.util.Map) DevelopmentException(io.jmix.core.DevelopmentException) Nullable(javax.annotation.Nullable)

Aggregations

DevelopmentException (io.jmix.core.DevelopmentException)26 MetaClass (io.jmix.core.metamodel.model.MetaClass)10 MethodHandle (java.lang.invoke.MethodHandle)3 DataGrid (com.haulmont.cuba.gui.components.DataGrid)2 FetchPlanProperty (io.jmix.core.FetchPlanProperty)2 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)2 EditorBuilder (io.jmix.ui.builder.EditorBuilder)2 AbstractDataGrid (io.jmix.ui.component.impl.AbstractDataGrid)2 AnnotatedMethod (io.jmix.ui.sys.UiControllerReflectionInspector.AnnotatedMethod)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 NamePattern (com.haulmont.chile.core.annotations.NamePattern)1 LoadContext (com.haulmont.cuba.core.global.LoadContext)1 ViewBuilder (com.haulmont.cuba.core.global.ViewBuilder)1 Server (com.haulmont.cuba.core.model.common.Server)1 CoreTest (com.haulmont.cuba.core.testsupport.CoreTest)1 AbstractWindow (com.haulmont.cuba.gui.components.AbstractWindow)1 TreeDataGrid (com.haulmont.cuba.gui.components.TreeDataGrid)1 LegacyFragmentAdapter (com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter)1