Search in sources :

Example 1 with NamePattern

use of com.haulmont.chile.core.annotations.NamePattern in project cuba by cuba-platform.

the class SwaggerGeneratorBean method getNamePatternProperty.

protected Property getNamePatternProperty(MetaClass entityClass) {
    Property namePatternProperty = new StringProperty();
    Class<?> javaClass = entityClass.getJavaClass();
    NamePattern namePatternAnnotation = javaClass.getAnnotation(NamePattern.class);
    if (namePatternAnnotation != null) {
        namePatternProperty.setDefault(namePatternAnnotation.value());
    }
    return namePatternProperty;
}
Also used : NamePattern(com.haulmont.chile.core.annotations.NamePattern) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 2 with NamePattern

use of com.haulmont.chile.core.annotations.NamePattern 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

NamePattern (com.haulmont.chile.core.annotations.NamePattern)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 DevelopmentException (io.jmix.core.DevelopmentException)1 InstanceNameProvider (io.jmix.core.InstanceNameProvider)1 InstanceNameProviderImpl (io.jmix.core.impl.InstanceNameProviderImpl)1 MetadataLoader (io.jmix.core.impl.MetadataLoader)1 MetaClass (io.jmix.core.metamodel.model.MetaClass)1 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1