Package org.qi4j.api.association

Examples of org.qi4j.api.association.AssociationStateHolder


                if (includeTypeInformation)
                    key("_type").value( valueDescriptor.valueType().type().getName() );
            }

            AssociationStateHolder state = (AssociationStateHolder) Qi4j.INSTANCE_FUNCTION.map( valueComposite ).state();
            for( PropertyDescriptor persistentProperty : valueCompositeType.properties() )
            {
                Property<?> property = state.propertyFor( persistentProperty.accessor() );
                key( persistentProperty.qualifiedName().name() ).serialize( property.get(), persistentProperty.valueType() );
            }
            for( AssociationDescriptor associationDescriptor : valueCompositeType.associations() )
            {
                Association<?> association = state.associationFor( associationDescriptor.accessor() );

                Object instance = association.get();
                if (instance != null)
                    key( associationDescriptor.qualifiedName().name() ).value( ((Identity)instance).identity().get() );
            }
            for( AssociationDescriptor associationDescriptor : valueCompositeType.manyAssociations() )
            {
                ManyAssociation<?> manyAssociation = state.manyAssociationFor( associationDescriptor.accessor() );
                key( associationDescriptor.qualifiedName().name() ).serialize( manyAssociation.toList(), new CollectionType( List.class, new ValueType( String.class ) ) );
            }
            objectEnd();
        } else if( valueType instanceof CollectionType )
        {
View Full Code Here


                throw new NoSuchValueException( valueType.getName(), module.name() );
            }
            Unqualified unqualified = valueDescriptor.metaInfo( Unqualified.class );
            final EntityComposite composite = (EntityComposite) entity;
            final EntityDescriptor entityDescriptor = spi.entityDescriptorFor( composite );
            final AssociationStateHolder associationState = spi.stateOf( composite );
            ValueBuilder<?> builder;

            if( unqualified == null || !unqualified.value() )
            {
                // Copy state using qualified names
                builder = vbf.newValueBuilderWithState( valueType, new Function<PropertyDescriptor, Object>()
                {
                    @Override
                    public Object map( PropertyDescriptor descriptor )
                    {
                        try
                        {
                            return associationState.propertyFor( descriptor.accessor() ).get();
                        }
                        catch( IllegalArgumentException e )
                        {
                            if( descriptor.valueType().mainType().equals( String.class ) )
                            {
                                // Find Association and convert to string
                                AssociationDescriptor associationDescriptor;
                                try
                                {
                                    associationDescriptor = entityDescriptor.state()
                                        .getAssociationByName( descriptor.qualifiedName().name() );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return null;
                                }
                                Object entity = associationState.associationFor( associationDescriptor.accessor() ).get();
                                if( entity != null )
                                {
                                    return ( (Identity) entity ).identity().get();
                                }
                                return null;
                            }
                            else if( descriptor.valueType() instanceof CollectionType
                                     && ( (CollectionType) descriptor.valueType() ).collectedType().mainType().equals( String.class ) )
                            {
                                AssociationDescriptor associationDescriptor;
                                try
                                {
                                    associationDescriptor = entityDescriptor.state()
                                        .getManyAssociationByName( descriptor.qualifiedName().name() );
                                }
                                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;
                            }

                            // No NamedAssociation support

                            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 from )
                    {
                        // No NamedAssociation support
                        return Collections.emptyMap();
                    }
                } );
            }
            else
            {
                builder = vbf.newValueBuilderWithState( valueType, new Function<PropertyDescriptor, Object>()
                {
                    @Override
                    public Object map( PropertyDescriptor descriptor )
                    {
                        try
                        {
                            PropertyDescriptor propertyDescriptor = entityDescriptor.state()
                                .findPropertyModelByName( descriptor.qualifiedName().name() );
                            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 = entityDescriptor.state()
                                        .getAssociationByName( descriptor.qualifiedName().name() );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return null;
                                }

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

                                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;
                            }

                            // No NamedAssociation support

                            // DTO
                            Class<?> type = descriptor.valueType().mainType();
                            if( DTO.class.isAssignableFrom( type ) )
                            {
                                AssociationDescriptor associationDescriptor;
                                try
                                {
                                    associationDescriptor = entityDescriptor.state()
                                        .getAssociationByName( descriptor.qualifiedName().name() );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return null;
                                }

                                Object entity = associationState.associationFor( associationDescriptor.accessor() ).get();
                                if( entity != null )
                                {
                                    return convert( type, entity );
                                }
                            }

                            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;
                        }

                        return EntityReference.entityReferenceFor( associationState
                            .associationFor( associationDescriptor.accessor() ).get() );
                    }
                }, new Function<AssociationDescriptor, Iterable<EntityReference>>()
                {
                    @Override
                    public Iterable<EntityReference> map( AssociationDescriptor descriptor )
                    {
                        AssociationDescriptor associationDescriptor;
                        try
                        {
                            associationDescriptor = entityDescriptor.state()
                                .getManyAssociationByName( descriptor.qualifiedName().name() );
                        }
                        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 ) );
                        }
View Full Code Here

                throw new NoSuchValueException( valueType.getName(), module.name() );
            }
            Unqualified unqualified = valueDescriptor.metaInfo( Unqualified.class );
            final EntityComposite composite = (EntityComposite) entity;
            final EntityDescriptor entityDescriptor = spi.entityDescriptorFor( composite );
            final AssociationStateHolder associationState = spi.stateOf( composite );
            ValueBuilder<?> builder;

            if( unqualified == null || !unqualified.value() )
            {
                // Copy state using qualified names
                builder = vbf.newValueBuilderWithState( valueType, new Function<PropertyDescriptor, Object>()
                {
                    @Override
                    public Object map( PropertyDescriptor descriptor )
                    {
                        try
                        {
                            return associationState.propertyFor( descriptor.accessor() ).get();
                        }
                        catch( IllegalArgumentException e )
                        {
                            if( descriptor.valueType().mainType().equals( String.class ) )
                            {
                                // Find Association and convert to string
                                AssociationDescriptor associationDescriptor;
                                try
                                {
                                    associationDescriptor = entityDescriptor.state()
                                        .getAssociationByName( descriptor.qualifiedName().name() );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return null;
                                }
                                Object entity = associationState.associationFor( associationDescriptor.accessor() ).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 = entityDescriptor.state()
                                        .getManyAssociationByName( descriptor.qualifiedName().name() );
                                }
                                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;
                            }

                            // No NamedAssociation support

                            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 from )
                    {
                        throw new UnsupportedOperationException( "NamedAssociations are not supported." );
                    }
                } );
            }
            else
            {
                builder = vbf.newValueBuilderWithState( valueType, new Function<PropertyDescriptor, Object>()
                {
                    @Override
                    public Object map( PropertyDescriptor descriptor )
                    {
                        try
                        {
                            PropertyDescriptor propertyDescriptor = entityDescriptor.state()
                                .findPropertyModelByName( descriptor.qualifiedName().name() );
                            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 = entityDescriptor.state()
                                        .getAssociationByName( descriptor.qualifiedName().name() );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return null;
                                }

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

                                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;
                            }

                            // No NamedAssociation support

                            // DTO
                            Class<?> type = descriptor.valueType().mainType();
                            if( DTO.class.isAssignableFrom( type ) )
                            {
                                AssociationDescriptor associationDescriptor;
                                try
                                {
                                    associationDescriptor = entityDescriptor.state()
                                        .getAssociationByName( descriptor.qualifiedName().name() );
                                }
                                catch( IllegalArgumentException e1 )
                                {
                                    return null;
                                }

                                Object entity = associationState.associationFor( associationDescriptor.accessor() ).get();
                                if( entity != null )
                                {
                                    return convert( type, entity );
                                }
                            }

                            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;
                        }

                        return EntityReference.entityReferenceFor( associationState
                            .associationFor( associationDescriptor.accessor() ).get() );
                    }
                }, new Function<AssociationDescriptor, Iterable<EntityReference>>()
                {
                    @Override
                    public Iterable<EntityReference> map( AssociationDescriptor descriptor )
                    {
                        AssociationDescriptor associationDescriptor;
                        try
                        {
                            associationDescriptor = entityDescriptor.state()
                                .getManyAssociationByName( descriptor.qualifiedName().name() );
                        }
                        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 ) );
                        }
View Full Code Here

        {
            EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder( TestEntity.class );

            testEntity = builder.newInstance();

            AssociationStateHolder state = spi.stateOf( testEntity );

            validateState( state, spi.entityDescriptorFor( testEntity ) );

            unitOfWork.complete();
        }
View Full Code Here

    {
        DefaultTableModel model = new DefaultTableModel();

        for( Object qObj : query )
        {
            AssociationStateHolder state = qi4jspi.stateOf( (EntityComposite) qObj );
            EntityDescriptor descriptor = qi4jspi.entityDescriptorFor( (EntityComposite) qObj );
            // genereate column, first time only
            if( model.getColumnCount() < 1 )
            {
                for( PropertyDescriptor persistentPropertyDescriptor : descriptor.state()
                    .properties() )
                {
                    model.addColumn( persistentPropertyDescriptor.qualifiedName().name() );
                }
            }

            Object[] rowData = new Object[ model.getColumnCount() ];
            int i = 0;
            for( PropertyDescriptor persistentPropertyDescriptor : descriptor.state().properties() )
            {
                rowData[ i++] = state.propertyFor( persistentPropertyDescriptor.accessor() );
            }
            model.addRow( rowData );
        }

        return model;
View Full Code Here

                    throws AttributeNotFoundException, MBeanException, ReflectionException
            {
                UnitOfWork uow = module.newUnitOfWork();
                try {
                    EntityComposite configuration = uow.get( EntityComposite.class, identity );
                    AssociationStateHolder state = spi.stateOf( configuration );
                    AccessibleObject accessor = propertyNames.get( name );
                    Property<Object> property = state.propertyFor( accessor );
                    return property.get();
                } catch ( Exception ex ) {
                    throw new ReflectionException( ex, "Could not get attribute " + name );
                } finally {
                    uow.discard();
View Full Code Here

                    throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException
            {
                UnitOfWork uow = module.newUnitOfWork();
                try {
                    EntityComposite configuration = uow.get( EntityComposite.class, identity );
                    AssociationStateHolder state = spi.stateOf( configuration );
                    AccessibleObject accessor = propertyNames.get( attribute.getName() );
                    Property<Object> property = state.propertyFor( accessor );
                    property.set( attribute.getValue() );
                    try {
                        uow.complete();
                    } catch ( UnitOfWorkCompletionException e ) {
                        throw new ReflectionException( e );
View Full Code Here

    //
    @Test
    public void givenValuesOfSameTypesAndSameStateWhenTestingValueStateEqualityExpectEquals()
    {
        Some some = buildSomeValue( module );
        AssociationStateHolder someState = qi4j.spi().stateOf( (ValueComposite) some );

        Some some2 = buildSomeValue( module );
        AssociationStateHolder some2State = qi4j.spi().stateOf( (ValueComposite) some2 );

        assertThat( "ValueStates equal",
                    someState,
                    equalTo( some2State ) );
        assertThat( "ValueStates hashcode equal",
                    someState.hashCode(),
                    equalTo( some2State.hashCode() ) );
    }
View Full Code Here

    @Test
    public void givenValuesOfSameTypesAndDifferentStateWhenTestingValueStateEqualityExpectNotEquals()
    {
        Some some = buildSomeValue( module );
        AssociationStateHolder someState = qi4j.spi().stateOf( (ValueComposite) some );

        Some some2 = buildSomeValueWithDifferentState( module );
        AssociationStateHolder some2State = qi4j.spi().stateOf( (ValueComposite) some2 );

        assertThat( "ValueStates not equal",
                    someState,
                    not( equalTo( some2State ) ) );
        assertThat( "ValueStates hashcode not equal",
                    someState.hashCode(),
                    not( equalTo( some2State.hashCode() ) ) );
    }
View Full Code Here

    @Test
    public void givenValuesOfDifferentTypesAndSameStateWhenTestingValueStateEqualityExpectEquals()
    {
        Some some = buildSomeValue( module );
        AssociationStateHolder someState = qi4j.spi().stateOf( (ValueComposite) some );

        AnotherSome anotherSome = buildAnotherSomeValue( module );
        AssociationStateHolder anotherSomeState = qi4j.spi().stateOf( (ValueComposite) anotherSome );

        assertThat( "ValueStates equal",
                    someState,
                    equalTo( anotherSomeState ) );
        assertThat( "ValueStates hashcode equal",
                    someState.hashCode(),
                    equalTo( anotherSomeState.hashCode() ) );
    }
View Full Code Here

TOP

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

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.