Package org.qi4j.api.association

Examples of org.qi4j.api.association.AssociationStateHolder


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

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

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


            {
                UnitOfWork uow = uowf.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 );
                    Object object = property.get();
                    if( object instanceof Enum )
                    {
                        object = object.toString();
                    }
View Full Code Here

            {
                UnitOfWork uow = uowf.newUnitOfWork();
                try
                {
                    EntityComposite configuration = uow.get( EntityComposite.class, identity );
                    AssociationStateHolder state = spi.stateOf( (EntityComposite) configuration );
                    AccessibleObject accessor = propertyNames.get( attribute.getName() );
                    Property<Object> property = state.propertyFor( accessor );
                    PropertyDescriptor propertyDescriptor = spi.propertyDescriptorFor( property );
                    if( EnumType.isEnum( propertyDescriptor.type() ) )
                    {
                        property.set( Enum.valueOf( (Class<Enum>) propertyDescriptor.type(), attribute.getValue()
                            .toString() ) );
View Full Code Here

    private void serializeValueComposite( Options options, Object object, OutputType output, boolean rootPass )
        throws Exception
    {
        CompositeInstance valueInstance = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map( (ValueComposite) object );
        ValueDescriptor descriptor = (ValueDescriptor) valueInstance.descriptor();
        AssociationStateHolder state = (AssociationStateHolder) valueInstance.state();

        onObjectStart( output );

        if( options.getBoolean( Options.INCLUDE_TYPE_INFO ) && !rootPass )
        {
            onFieldStart( output, "_type" );
            onValueStart( output );
            onValue( output, first( descriptor.valueType().types() ).getName() );
            onValueEnd( output );
            onFieldEnd( output );
        }

        for( PropertyDescriptor persistentProperty : descriptor.valueType().properties() )
        {
            Property<?> property = state.propertyFor( persistentProperty.accessor() );
            onFieldStart( output, persistentProperty.qualifiedName().name() );
            onValueStart( output );
            doSerialize( options, property.get(), output, false );
            onValueEnd( output );
            onFieldEnd( output );
        }
        for( AssociationDescriptor associationDescriptor : descriptor.valueType().associations() )
        {
            Association<?> association = state.associationFor( associationDescriptor.accessor() );
            Object instance = association.get();
            onFieldStart( output, associationDescriptor.qualifiedName().name() );
            onValueStart( output );
            if( instance == null )
            {
                onValue( output, null );
            }
            else
            {
                onValue( output, ( (Identity) instance ).identity().get() );
            }
            onValueEnd( output );
            onFieldEnd( output );
        }
        for( AssociationDescriptor associationDescriptor : descriptor.valueType().manyAssociations() )
        {
            ManyAssociation<?> manyAssociation = state.manyAssociationFor( associationDescriptor.accessor() );
            onFieldStart( output, associationDescriptor.qualifiedName().name() );
            onValueStart( output );
            onArrayStart( output );
            for( Object instance : manyAssociation )
            {
                onValueStart( output );
                onValue( output, ( (Identity) instance ).identity().get() );
                onValueEnd( output );
            }
            onArrayEnd( output );
            onValueEnd( output );
            onFieldEnd( output );
        }
        for( AssociationDescriptor associationDescriptor : descriptor.valueType().namedAssociations() )
        {
            NamedAssociation<?> namedAssociation = state.namedAssociationFor( associationDescriptor.accessor() );
            onFieldStart( output, associationDescriptor.qualifiedName().name() );
            onValueStart( output );
            onObjectStart( output );
            for( String name : namedAssociation )
            {
View Full Code Here

        throws DatabindingException
    {
        ValueComposite composite = (ValueComposite) object;
        writer.writeXsiType( NamespaceUtil.convertJavaTypeToQName(
            first( Qi4j.FUNCTION_DESCRIPTOR_FOR.map( composite ).types() ) ) );
        AssociationStateHolder state = spi.stateOf( composite );
        for( Property<?> property : state.properties() )
        {
            Object value = property.get();
            AegisType type = null;
            if( value instanceof ValueComposite )
            {
                ValueComposite compositeValue = (ValueComposite) value;
                type = getTypeMapping().getType( NamespaceUtil.convertJavaTypeToQName(
                    first( Qi4j.FUNCTION_DESCRIPTOR_FOR.map( compositeValue ).types() ) ) );
            }
            else if( value != null )
            {
                type = getOrCreateNonQi4jType( value );
            }

            QName childName = new QName( "", spi.propertyDescriptorFor( property ).qualifiedName().name() );
            MessageWriter cwriter = writer.getElementWriter( childName );
            if( type != null )
            {
                type.writeObject( value, cwriter, context );
            }
            else
            {
//                    cwriter.writeXsiNil();
            }
            cwriter.close();
        }

        AegisType type = getTypeMapping().getType( NamespaceUtil.convertJavaTypeToQName( String.class ) );
        for( Association<?> association : state.allAssociations() )
        {
            QName childName = new QName( "", spi.associationDescriptorFor( association ).qualifiedName().name() );
            MessageWriter cwriter = writer.getElementWriter( childName );

            if( association.get() != null )
            {
                type.writeObject( ( (Identity) association.get() ).identity().get(), cwriter, context );
            }
            cwriter.close();
        }

        for( ManyAssociation<?> association : state.allManyAssociations() )
        {
            QName childName = new QName( "", spi.associationDescriptorFor( association ).qualifiedName().name() );
            MessageWriter cwriter = writer.getElementWriter( childName );

            String ids = null;
            for( Object entity : association )
            {
                String id = EntityReference.entityReferenceFor( entity ).identity();
                if( ids != null )
                {
                    ids += ",";
                }
                ids += id;
            }
            if( ids == null )
            {
                ids = "";
            }
            type.writeObject( ids, cwriter, context );
            cwriter.close();
        }

        for( NamedAssociation<?> association : state.allNamedAssociations() )
        {
            QName childName = new QName( "", spi.associationDescriptorFor( association ).qualifiedName().name() );
            MessageWriter cwriter = writer.getElementWriter( childName );

            String ids = null;
View Full Code Here

            }
            Unqualified unqualified = valueDescriptor.metaInfo( Unqualified.class );
//            Iterable<? extends PropertyDescriptor> properties = valueDescriptor.state().properties();
            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 = module.newValueBuilderWithState(
                    valueType,
                    new Function<PropertyDescriptor, Object>()
                {
                    @Override
                    public Object map( PropertyDescriptor descriptor )
                    {
                        try
                        {
                            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();
                            }
                            AccessibleObject associationMethod = associationDescriptor.accessor();
                            NamedAssociation<Object> assoc = associationState.namedAssociationFor( associationMethod );
                            Map<String, EntityReference> refs = new LinkedHashMap<>( assoc.count() );
                            for( String name : assoc )
                            {
                                refs.put( name, EntityReference.entityReferenceFor( assoc.get( name ) ) );
                            }
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.