Package org.qi4j.api.entity

Examples of org.qi4j.api.entity.EntityDescriptor


                    Assert.assertTrue( serviceDescriptor.isInstantiateOnStartup() );
                    Assert.assertTrue( serviceDescriptor.visibility() == Visibility.layer );
                    return false;
                } else if (visited instanceof EntityDescriptor)
                {
                    EntityDescriptor entityDescriptor = (EntityDescriptor) visited;
                    Assert.assertTrue( entityDescriptor.visibility() == Visibility.application );
                    return false;
                }

                return true;
            }
View Full Code Here


                status = EntityStatus.UPDATED;
            }

            String type = jsonObject.getString( "type" );

            EntityDescriptor entityDescriptor = module.entityDescriptor( type );
            if( entityDescriptor == null )
            {
                throw new EntityTypeNotFoundException( type );
            }
View Full Code Here

        {
            JSONObject data = cacheState.json;
            try
            {
                String type = data.getString( "type" );
                EntityDescriptor entityDescriptor = unitOfWork.module().entityDescriptor( type );
                return new JSONEntityState( unitOfWork, identity, entityDescriptor, data );
            } catch( JSONException e )
            {
                // Should not be able to happen, unless internal error in the cache system.
                throw new EntityStoreException( e );
View Full Code Here

                key( "application_version" ).value( application.version() ).
                key( "type" ).value( state.entityDescriptor().type().getName() ).
                key( "version" ).value( identity ).
                key( "modified" ).value( lastModified ).
                key( "properties" ).object();
            EntityDescriptor entityType = state.entityDescriptor();
            JSONWriterSerializer serializer = new JSONWriterSerializer( json );
            for( PropertyDescriptor persistentProperty : entityType.state().properties() )
            {
                Object value = state.properties().get( persistentProperty.qualifiedName() );
                json.key( persistentProperty.qualifiedName().name() );
                serializer.serialize( value, persistentProperty.valueType() );
            }
View Full Code Here

                status = EntityStatus.UPDATED;
            }

            String type = jsonObject.getString( "type" );

            EntityDescriptor entityDescriptor = module.entityDescriptor( type );
            if( entityDescriptor == null )
            {
                throw new EntityTypeNotFoundException( type );
            }

            Map<QualifiedName, Object> properties = new HashMap<QualifiedName, Object>();
            JSONObject props = jsonObject.getJSONObject( "properties" );
            for( PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties() )
            {
                Object jsonValue;
                try
                {
                    jsonValue = props.get( propertyDescriptor.qualifiedName().name() );
                }
                catch( JSONException e )
                {
                    // Value not found, default it
                    Object initialValue = propertyDescriptor.initialValue( module );
                    properties.put( propertyDescriptor.qualifiedName(), initialValue );
                    status = EntityStatus.UPDATED;
                    continue;
                }
                if( jsonValue == JSONObject.NULL )
                {
                    properties.put( propertyDescriptor.qualifiedName(), null );
                }
                else
                {
                    Object value = deserializer.deserialize( jsonValue, propertyDescriptor.valueType() );
                    properties.put( propertyDescriptor.qualifiedName(), value );
                }
            }

            Map<QualifiedName, EntityReference> associations = new HashMap<QualifiedName, EntityReference>();
            JSONObject assocs = jsonObject.getJSONObject( "associations" );
            for( AssociationDescriptor associationType : entityDescriptor.state().associations() )
            {
                try
                {
                    Object jsonValue = assocs.get( associationType.qualifiedName().name() );
                    EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference(
                        (String) jsonValue );
                    associations.put( associationType.qualifiedName(), value );
                }
                catch( JSONException e )
                {
                    // Association not found, default it to null
                    associations.put( associationType.qualifiedName(), null );
                    status = EntityStatus.UPDATED;
                }
            }

            JSONObject manyAssocs = jsonObject.getJSONObject( "manyassociations" );
            Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<QualifiedName, List<EntityReference>>();
            for( AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations() )
            {
                List<EntityReference> references = new ArrayList<EntityReference>();
                try
                {
                    JSONArray jsonValues = manyAssocs.getJSONArray( manyAssociationType.qualifiedName().name() );
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();
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();
View Full Code Here

                    Assert.assertTrue( serviceDescriptor.visibility() == Visibility.layer );
                    return false;
                }
                else if( visited instanceof EntityDescriptor )
                {
                    EntityDescriptor entityDescriptor = (EntityDescriptor) visited;
                    Assert.assertTrue( entityDescriptor.visibility() == Visibility.application );
                    return false;
                }

                return true;
            }
View Full Code Here

                    if( visited instanceof EntityDescriptor || visited instanceof ValueDescriptor )
                    {
                        // TODO filter non-visible descriptors away.
                        if( visited instanceof EntityDescriptor )
                        {
                            EntityDescriptor entityDescriptor = (EntityDescriptor) visited;
                            if( entityDescriptor.queryable() )
                            {
                                LOGGER.debug( "THIS ONE WORKS: {}", entityDescriptor );
                                appInfo.entityDescriptors.put( first( entityDescriptor.types() )
                                    .getName(), entityDescriptor );
                            }
                        }
                        else
                        {
View Full Code Here

                status = EntityStatus.UPDATED;
            }

            String type = jsonObject.getString( JSONKeys.TYPE );

            EntityDescriptor entityDescriptor = module.entityDescriptor( type );
            if( entityDescriptor == null )
            {
                throw new EntityTypeNotFoundException( type );
            }

            Map<QualifiedName, Object> properties = new HashMap<>();
            JSONObject props = jsonObject.getJSONObject( JSONKeys.PROPERTIES );
            for( PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties() )
            {
                Object jsonValue;
                try
                {
                    jsonValue = props.get( propertyDescriptor.qualifiedName().name() );
                }
                catch( JSONException e )
                {
                    // Value not found, default it
                    Object initialValue = propertyDescriptor.initialValue( module );
                    properties.put( propertyDescriptor.qualifiedName(), initialValue );
                    status = EntityStatus.UPDATED;
                    continue;
                }
                if( JSONObject.NULL.equals( jsonValue ) )
                {
                    properties.put( propertyDescriptor.qualifiedName(), null );
                }
                else
                {
                    Object value = valueSerialization.deserialize( propertyDescriptor.valueType(), jsonValue.toString() );
                    properties.put( propertyDescriptor.qualifiedName(), value );
                }
            }

            Map<QualifiedName, EntityReference> associations = new HashMap<>();
            JSONObject assocs = jsonObject.getJSONObject( JSONKeys.ASSOCIATIONS );
            for( AssociationDescriptor associationType : entityDescriptor.state().associations() )
            {
                try
                {
                    Object jsonValue = assocs.get( associationType.qualifiedName().name() );
                    EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference(
                        (String) jsonValue );
                    associations.put( associationType.qualifiedName(), value );
                }
                catch( JSONException e )
                {
                    // Association not found, default it to null
                    associations.put( associationType.qualifiedName(), null );
                    status = EntityStatus.UPDATED;
                }
            }

            JSONObject manyAssocs = jsonObject.getJSONObject( JSONKeys.MANY_ASSOCIATIONS );
            Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<>();
            for( AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations() )
            {
                List<EntityReference> references = new ArrayList<>();
                try
                {
                    JSONArray jsonValues = manyAssocs.getJSONArray( manyAssociationType.qualifiedName().name() );
                    for( int i = 0; i < jsonValues.length(); i++ )
                    {
                        Object jsonValue = jsonValues.getString( i );
                        EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference(
                            (String) jsonValue );
                        references.add( value );
                    }
                    manyAssociations.put( manyAssociationType.qualifiedName(), references );
                }
                catch( JSONException e )
                {
                    // ManyAssociation not found, default to empty one
                    manyAssociations.put( manyAssociationType.qualifiedName(), references );
                }
            }

            JSONObject namedAssocs = jsonObject.has( JSONKeys.NAMED_ASSOCIATIONS )
                                     ? jsonObject.getJSONObject( JSONKeys.NAMED_ASSOCIATIONS )
                                     : new JSONObject();
            Map<QualifiedName, Map<String, EntityReference>> namedAssociations = new HashMap<>();
            for( AssociationDescriptor namedAssociationType : entityDescriptor.state().namedAssociations() )
            {
                Map<String, EntityReference> references = new LinkedHashMap<>();
                try
                {
                    JSONObject jsonValues = namedAssocs.getJSONObject( namedAssociationType.qualifiedName().name() );
View Full Code Here

TOP

Related Classes of org.qi4j.api.entity.EntityDescriptor

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.