Search in sources :

Example 6 with CompositeDescriptor

use of org.qi4j.api.composite.CompositeDescriptor in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method stringToCompositeDescriptor.

protected static <TCompositeDescriptor extends CompositeDescriptor> TCompositeDescriptor stringToCompositeDescriptor(final Class<TCompositeDescriptor> descriptorClass, ApplicationDescriptor appDesc, String str) {
    Matcher matcher = DESCRIPTOR_TEXTUAL_REGEXP.matcher(str);
    if (!matcher.matches()) {
        throw new IllegalArgumentException("Descriptor textual description " + str + " was invalid.");
    }
    final String layerName = matcher.group(1);
    final String moduleName = matcher.group(2);
    final Set<String> classNames = new HashSet<>();
    Matcher typesMatcher = DESCRIPTOR_TYPES_REGEXP.matcher(matcher.group(3));
    while (typesMatcher.find()) {
        classNames.add(typesMatcher.group(0));
    }
    final CompositeDescriptor[] result = new CompositeDescriptor[1];
    appDesc.accept(new HierarchicalVisitorAdapter<Object, Object, RuntimeException>() {

        @Override
        public boolean visitEnter(Object visited) {
            boolean thisResult = true;
            if (visited instanceof LayerDescriptor) {
                thisResult = ((LayerDescriptor) visited).name().equals(layerName);
            } else if (visited instanceof ModuleDescriptor) {
                thisResult = ((ModuleDescriptor) visited).name().equals(moduleName);
            } else if (descriptorClass.isAssignableFrom(visited.getClass())) {
                CompositeDescriptor desc = (CompositeDescriptor) visited;
                if (classNames.equals(new HashSet<>(Iterables.toList(Iterables.map(new Function<Class<?>, String>() {

                    @Override
                    public String map(Class<?> from) {
                        return from.getName();
                    }
                }, desc.types()))))) {
                    result[0] = desc;
                    thisResult = false;
                }
            }
            return thisResult;
        }

        @Override
        public boolean visitLeave(Object visited) {
            return result[0] == null;
        }
    });
    return (TCompositeDescriptor) result[0];
}
Also used : Matcher(java.util.regex.Matcher) ModuleDescriptor(org.qi4j.api.structure.ModuleDescriptor) CompositeDescriptor(org.qi4j.api.composite.CompositeDescriptor) HashSet(java.util.HashSet) LayerDescriptor(org.qi4j.api.structure.LayerDescriptor)

Aggregations

CompositeDescriptor (org.qi4j.api.composite.CompositeDescriptor)6 HashSet (java.util.HashSet)2 QualifiedName (org.qi4j.api.common.QualifiedName)2 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)2 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)2 QNameInfo (org.qi4j.index.sql.support.common.QNameInfo)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 BigInteger (java.math.BigInteger)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 Identity (org.qi4j.api.entity.Identity)1 ObjectDescriptor (org.qi4j.api.object.ObjectDescriptor)1 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)1 ImportedServiceDescriptor (org.qi4j.api.service.ImportedServiceDescriptor)1