Package org.qi4j.api.association

Examples of org.qi4j.api.association.AssociationStateDescriptor


    }

    private void removeAggregatedEntities( UnitOfWork unitOfWork )
    {
        // Calculate aggregated Entities
        AssociationStateDescriptor stateDescriptor = entityModel.state();
        Set<Object> aggregatedEntities = new HashSet<Object>();
        Iterable<? extends AssociationDescriptor> associations = stateDescriptor.associations();
        for( AssociationDescriptor association : associations )
        {
            if( association.isAggregated() )
            {
                Association assoc = state.associationFor( association.accessor() );
                Object aggregatedEntity = assoc.get();
                if( aggregatedEntity != null )
                {
                    aggregatedEntities.add( aggregatedEntity );
                }
            }
        }
        Iterable<? extends AssociationDescriptor> manyAssociations = stateDescriptor.manyAssociations();
        for( AssociationDescriptor association : manyAssociations )
        {
            if( association.isAggregated() )
            {
                ManyAssociation manyAssoc = state.manyAssociationFor( association.accessor() );
View Full Code Here


        {
            Property<?> prop = state.propertyFor( propertyDescriptor.accessor() );
            assertThat( "Properties could be listed", prop, CoreMatchers.notNullValue() );
        }

        AssociationStateDescriptor descriptor = entityDescriptor.state();
        for( AssociationDescriptor associationDescriptor : descriptor.associations() )
        {
            AbstractAssociation assoc = state.associationFor( associationDescriptor.accessor() );
            assertThat( "Assocs could be listed", assoc, CoreMatchers.notNullValue() );
        }
    }
View Full Code Here

            return new PropertyInjectionProvider( propertyDescriptor );
        }
        else if( Association.class.isAssignableFrom( dependencyModel.rawInjectionType() ) )
        {
            // @State Association<MyEntity> name;
            AssociationStateDescriptor descriptor = ( (EntityDescriptor) resolution.model() ).state();
            State annotation = (State) dependencyModel.injectionAnnotation();
            String name;
            if( annotation.value().equals( "" ) )
            {
                name = resolution.field().getName();
            }
            else
            {
                name = annotation.value();
            }
            AssociationDescriptor model = descriptor.getAssociationByName( name );

            // No such association found
            if( model == null )
            {
                return null;
            }

            return new AssociationInjectionProvider( model );
        }
        else if( ManyAssociation.class.isAssignableFrom( dependencyModel.rawInjectionType() ) )
        {
            // @State ManyAssociation<MyEntity> name;
            AssociationStateDescriptor descriptor = ( (EntityDescriptor) resolution.model() ).state();
            State annotation = (State) dependencyModel.injectionAnnotation();
            String name;
            if( annotation.value().equals( "" ) )
            {
                name = resolution.field().getName();
            }
            else
            {
                name = annotation.value();
            }
            AssociationDescriptor model = descriptor.getManyAssociationByName( name );

            // No such association found
            if( model == null )
            {
                return null;
View Full Code Here

            return new PropertyInjectionProvider( propertyDescriptor );
        }
        else if( Association.class.isAssignableFrom( dependencyModel.rawInjectionType() ) )
        {
            // @State Association<MyEntity> name;
            AssociationStateDescriptor descriptor = ( (EntityDescriptor) resolution.model() ).state();
            State annotation = (State) dependencyModel.injectionAnnotation();
            String name;
            if( annotation.value().isEmpty() )
            {
                name = resolution.field().getName();
            }
            else
            {
                name = annotation.value();
            }
            AssociationDescriptor model = descriptor.getAssociationByName( name );
            return new AssociationInjectionProvider( model );
        }
        else if( ManyAssociation.class.isAssignableFrom( dependencyModel.rawInjectionType() ) )
        {
            // @State ManyAssociation<MyEntity> name;
            AssociationStateDescriptor descriptor = ( (EntityDescriptor) resolution.model() ).state();
            State annotation = (State) dependencyModel.injectionAnnotation();
            String name;
            if( annotation.value().isEmpty() )
            {
                name = resolution.field().getName();
            }
            else
            {
                name = annotation.value();
            }
            AssociationDescriptor model = descriptor.getManyAssociationByName( name );
            return new ManyAssociationInjectionProvider( model );
        }
        else if( NamedAssociation.class.isAssignableFrom( dependencyModel.rawInjectionType() ) )
        {
            // @State NamedAssociation<MyEntity> name;
            AssociationStateDescriptor descriptor = ( (EntityDescriptor) resolution.model() ).state();
            State annotation = (State) dependencyModel.injectionAnnotation();
            String name;
            if( annotation.value().isEmpty() )
            {
                name = resolution.field().getName();
            }
            else
            {
                name = annotation.value();
            }
            AssociationDescriptor model = descriptor.getNamedAssociationByName( name );
            return new NamedAssociationInjectionProvider( model );
        }

        throw new InjectionProviderException( "Injected value has invalid type" );
    }
View Full Code Here

    }

    private void removeAggregatedEntities( UnitOfWork unitOfWork )
    {
        // Calculate aggregated Entities
        AssociationStateDescriptor stateDescriptor = entityModel.state();
        Set<Object> aggregatedEntities = new HashSet<>();
        Iterable<? extends AssociationDescriptor> associations = stateDescriptor.associations();
        for( AssociationDescriptor association : associations )
        {
            if( association.isAggregated() )
            {
                Association<?> assoc = state.associationFor( association.accessor() );
                Object aggregatedEntity = assoc.get();
                if( aggregatedEntity != null )
                {
                    aggregatedEntities.add( aggregatedEntity );
                }
            }
        }
        Iterable<? extends AssociationDescriptor> manyAssociations = stateDescriptor.manyAssociations();
        for( AssociationDescriptor association : manyAssociations )
        {
            if( association.isAggregated() )
            {
                ManyAssociation<?> manyAssoc = state.manyAssociationFor( association.accessor() );
                for( Object entity : manyAssoc )
                {
                    aggregatedEntities.add( entity );
                }
            }
        }
        Iterable<? extends AssociationDescriptor> namedAssociations = stateDescriptor.namedAssociations();
        for( AssociationDescriptor association : namedAssociations )
        {
            if( association.isAggregated() )
            {
                NamedAssociation<?> namedAssoc = state.namedAssociationFor( association.accessor() );
View Full Code Here

        {
            Property<?> prop = state.propertyFor( propertyDescriptor.accessor() );
            assertThat( "Properties could be listed", prop, CoreMatchers.notNullValue() );
        }

        AssociationStateDescriptor descriptor = entityDescriptor.state();
        for( AssociationDescriptor associationDescriptor : descriptor.associations() )
        {
            AbstractAssociation assoc = state.associationFor( associationDescriptor.accessor() );
            assertThat( "Assocs could be listed", assoc, CoreMatchers.notNullValue() );
        }
    }
View Full Code Here

                        {
                            return associationState.propertyFor( descriptor.accessor() ).get();
                        }
                        catch( IllegalArgumentException e )
                        {
                            AssociationStateDescriptor entityState = entityDescriptor.state();
                            String associationName = descriptor.qualifiedName().name();
                            if( descriptor.valueType().mainType().equals( String.class ) )
                            {
                                // Find Association and convert to string
                                AssociationDescriptor associationDescriptor;
                                try
                                {
                                    associationDescriptor = entityState.getAssociationByName( associationName );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return null;
                                }
                                AccessibleObject associationMethod = associationDescriptor.accessor();
                                Object entity = associationState.associationFor( associationMethod ).get();
                                if( entity != null )
                                {
                                    return ( (Identity) entity ).identity().get();
                                }
                                else
                                {
                                    return null;
                                }
                            }
                            else if( descriptor.valueType() instanceof CollectionType
                                     && ( (CollectionType) descriptor.valueType() ).collectedType()
                                .mainType()
                                .equals( String.class ) )
                            {
                                AssociationDescriptor associationDescriptor;
                                try
                                {
                                    associationDescriptor = entityState.getManyAssociationByName( associationName );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return Collections.emptyList();
                                }

                                ManyAssociation<?> state = associationState.manyAssociationFor( associationDescriptor.accessor() );
                                List<String> entities = new ArrayList<>( state.count() );
                                for( Object entity : state )
                                {
                                    entities.add( ( (Identity) entity ).identity().get() );
                                }
                                return entities;
                            }
                            else if( descriptor.valueType() instanceof MapType
                                     && ( (MapType) descriptor.valueType() ).keyType().mainType().equals( String.class )
                                     && ( (MapType) descriptor.valueType() ).valueType().mainType().equals( String.class ) )
                            {
                                AssociationDescriptor associationDescriptor;
                                try
                                {
                                    associationDescriptor = entityState.getNamedAssociationByName( associationName );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return Collections.emptyMap();
                                }

                                NamedAssociation<?> state = associationState.namedAssociationFor( associationDescriptor.accessor() );
                                Map<String, String> entities = new LinkedHashMap<>( state.count() );
                                for( String name : state )
                                {
                                    entities.put( name, ( (Identity) state.get( name ) ).identity().get() );
                                }
                                return entities;
                            }

                            return null;
                        }
                    }
                    },
                    new Function<AssociationDescriptor, EntityReference>()
                    {
                        @Override
                        public EntityReference map( AssociationDescriptor associationDescriptor )
                        {
                            return EntityReference.entityReferenceFor(
                                associationState.associationFor( associationDescriptor.accessor() ).get() );
                        }
                    },
                    new Function<AssociationDescriptor, Iterable<EntityReference>>()
                    {
                        @Override
                        public Iterable<EntityReference> map( AssociationDescriptor associationDescriptor )
                        {
                            ManyAssociation<?> state = associationState.manyAssociationFor( associationDescriptor.accessor() );
                            List<EntityReference> refs = new ArrayList<>( state.count() );
                            for( Object entity : state )
                            {
                                refs.add( EntityReference.entityReferenceFor( entity ) );
                            }
                            return refs;
                        }
                    },
                    new Function<AssociationDescriptor, Map<String, EntityReference>>()
                    {
                        @Override
                        public Map<String, EntityReference> map( AssociationDescriptor associationDescriptor )
                        {
                            NamedAssociation<?> assoc = associationState.namedAssociationFor( associationDescriptor.accessor() );
                            Map<String, EntityReference> refs = new LinkedHashMap<>( assoc.count() );
                            for( String name : assoc )
                            {
                                refs.put( name, EntityReference.entityReferenceFor( assoc.get( name ) ) );
                            }
                            return refs;
                        }
                    } );
            }
            else
            {
                builder = module.newValueBuilderWithState(
                    valueType,
                    new Function<PropertyDescriptor, Object>()
                {
                    @Override
                    public Object map( final PropertyDescriptor descriptor )
                    {
                        AssociationStateDescriptor entityState = entityDescriptor.state();
                        String propertyName = descriptor.qualifiedName().name();
                        try
                        {
                            PropertyDescriptor propertyDescriptor = entityState.findPropertyModelByName( propertyName );
                            return associationState.propertyFor( propertyDescriptor.accessor() ).get();
                        }
                        catch( IllegalArgumentException e )
                        {
                            if( descriptor.valueType().mainType().equals( String.class ) )
                            {
                                // Find Association and convert to string
                                AssociationDescriptor associationDescriptor;
                                try
                                {
                                    associationDescriptor = entityState.getAssociationByName( propertyName );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return null;
                                }

                                AccessibleObject associationMethod = associationDescriptor.accessor();
                                Object entity = associationState.associationFor( associationMethod ).get();
                                if( entity != null )
                                {
                                    return ( (Identity) entity ).identity().get();
                                }
                                else
                                {
                                    return null;
                                }
                            }
                            else if( descriptor.valueType() instanceof CollectionType
                                     && ( (CollectionType) descriptor.valueType() ).collectedType().mainType().equals( String.class ) )
                            {
                                AssociationDescriptor associationDescriptor;
                                try
                                {
                                    associationDescriptor = entityState.getManyAssociationByName( propertyName );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return null;
                                }

                                AccessibleObject associationMethod = associationDescriptor.accessor();
                                ManyAssociation<?> state = associationState.manyAssociationFor( associationMethod );
                                List<String> entities = new ArrayList<>( state.count() );
                                for( Object entity : state )
                                {
                                    entities.add( ( (Identity) entity ).identity().get() );
                                }
                                return entities;
                            }
                            else if( descriptor.valueType() instanceof MapType
                                     && ( (MapType) descriptor.valueType() ).keyType().mainType().equals( String.class )
                                     && ( (MapType) descriptor.valueType() ).valueType().mainType().equals( String.class ) )
                            {
                                AssociationDescriptor associationDescriptor;
                                try
                                {
                                    associationDescriptor = entityState.getNamedAssociationByName( propertyName );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return null;
                                }

                                AccessibleObject associationMethod = associationDescriptor.accessor();
                                NamedAssociation<?> state = associationState.namedAssociationFor( associationMethod );
                                Map<String, String> entities = new LinkedHashMap<>( state.count() );
                                for( String name : state )
                                {
                                    entities.put( name, ( (Identity) state.get( name ) ).identity().get() );
                                }
                                return entities;
                            }
                            return null;
                        }
                    }
                    },
                    new Function<AssociationDescriptor, EntityReference>()
                    {
                        @Override
                        public EntityReference map( AssociationDescriptor descriptor )
                        {
                            AssociationDescriptor associationDescriptor;
                            try
                            {
                                associationDescriptor = entityDescriptor.state()
                                    .getAssociationByName( descriptor.qualifiedName().name() );
                            }
                            catch( IllegalArgumentException e )
                            {
                                return null;
                            }

                            AccessibleObject associationMethod = associationDescriptor.accessor();
                            Association<Object> association = associationState.associationFor( associationMethod );
                            return EntityReference.entityReferenceFor( association.get() );
                        }
                    },
                    new Function<AssociationDescriptor, Iterable<EntityReference>>()
                    {
                        @Override
                        public Iterable<EntityReference> map( final AssociationDescriptor descriptor )
                        {
                            AssociationDescriptor associationDescriptor;
                            try
                            {
                                String associationName = descriptor.qualifiedName().name();
                                AssociationStateDescriptor entityState = entityDescriptor.state();
                                associationDescriptor = entityState.getManyAssociationByName( associationName );
                            }
                            catch( IllegalArgumentException e )
                            {
                                return Iterables.empty();
                            }

                            ManyAssociation<?> state = associationState.manyAssociationFor( associationDescriptor.accessor() );
                            List<EntityReference> refs = new ArrayList<>( state.count() );
                            for( Object entity : state )
                            {
                                refs.add( EntityReference.entityReferenceFor( entity ) );
                            }
                            return refs;
                        }
                    },
                    new Function<AssociationDescriptor, Map<String, EntityReference>>()
                    {
                        @Override
                        public Map<String, EntityReference> map( AssociationDescriptor descriptor )
                        {
                            AssociationDescriptor associationDescriptor;
                            try
                            {
                                String associationName = descriptor.qualifiedName().name();
                                AssociationStateDescriptor entityState = entityDescriptor.state();
                                associationDescriptor = entityState.getNamedAssociationByName( associationName );
                            }
                            catch( IllegalArgumentException e )
                            {
                                return Collections.emptyMap();
                            }
View Full Code Here

TOP

Related Classes of org.qi4j.api.association.AssociationStateDescriptor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.