Package org.qi4j.api.entity

Examples of org.qi4j.api.entity.EntityDescriptor


            Preferences entityPrefs = root.node( identity.identity() );

            String type = entityPrefs.get( "type", null );
            EntityStatus status = EntityStatus.LOADED;

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

            Map<QualifiedName, Object> properties = new HashMap<>();
            Preferences propsPrefs = null;
            for( PropertyDescriptor persistentPropertyDescriptor : entityDescriptor.state().properties() )
            {
                if( persistentPropertyDescriptor.qualifiedName().name().equals( "identity" ) )
                {
                    // Fake identity property
                    properties.put( persistentPropertyDescriptor.qualifiedName(), identity.identity() );
                    continue;
                }

                if( propsPrefs == null )
                {
                    propsPrefs = entityPrefs.node( "properties" );
                }

                ValueType propertyType = persistentPropertyDescriptor.valueType();
                Class<?> mainType = propertyType.mainType();
                if( Number.class.isAssignableFrom( mainType ) )
                {
                    if( mainType.equals( Long.class ) )
                    {
                        properties.put( persistentPropertyDescriptor.qualifiedName(),
                                        this.getNumber( propsPrefs, persistentPropertyDescriptor, LONG_PARSER ) );
                    }
                    else if( mainType.equals( Integer.class ) )
                    {
                        properties.put( persistentPropertyDescriptor.qualifiedName(),
                                        this.getNumber( propsPrefs, persistentPropertyDescriptor, INT_PARSER ) );
                    }
                    else if( mainType.equals( Double.class ) )
                    {
                        properties.put( persistentPropertyDescriptor.qualifiedName(),
                                        this.getNumber( propsPrefs, persistentPropertyDescriptor, DOUBLE_PARSER ) );
                    }
                    else if( mainType.equals( Float.class ) )
                    {
                        properties.put( persistentPropertyDescriptor.qualifiedName(),
                                        this.getNumber( propsPrefs, persistentPropertyDescriptor, FLOAT_PARSER ) );
                    }
                    else
                    {
                        // Load as string even though it's a number
                        String json = propsPrefs.get( persistentPropertyDescriptor.qualifiedName().name(), null );
                        Object value;
                        if( json == null )
                        {
                            value = null;
                        }
                        else
                        {
                            value = valueSerialization.deserialize( persistentPropertyDescriptor.valueType(), json );
                        }
                        properties.put( persistentPropertyDescriptor.qualifiedName(), value );
                    }
                }
                else if( mainType.equals( Boolean.class ) )
                {
                    Boolean initialValue = (Boolean) persistentPropertyDescriptor.initialValue( module );
                    properties.put( persistentPropertyDescriptor.qualifiedName(),
                                    propsPrefs.getBoolean( persistentPropertyDescriptor.qualifiedName().name(),
                                                           initialValue == null ? false : initialValue ) );
                }
                else if( propertyType instanceof ValueCompositeType
                         || propertyType instanceof MapType
                         || propertyType instanceof CollectionType
                         || propertyType instanceof EnumType )
                {
                    String json = propsPrefs.get( persistentPropertyDescriptor.qualifiedName().name(), null );
                    Object value;
                    if( json == null )
                    {
                        value = null;
                    }
                    else
                    {
                        value = valueSerialization.deserialize( persistentPropertyDescriptor.valueType(), json );
                    }
                    properties.put( persistentPropertyDescriptor.qualifiedName(), value );
                }
                else
                {
                    String json = propsPrefs.get( persistentPropertyDescriptor.qualifiedName().name(), null );
                    if( json == null )
                    {
                        if( persistentPropertyDescriptor.initialValue( module ) != null )
                        {
                            properties.put( persistentPropertyDescriptor.qualifiedName(), persistentPropertyDescriptor.initialValue( module ) );
                        }
                        else
                        {
                            properties.put( persistentPropertyDescriptor.qualifiedName(), null );
                        }
                    }
                    else
                    {
                        Object value = valueSerialization.deserialize( propertyType, json );
                        properties.put( persistentPropertyDescriptor.qualifiedName(), value );
                    }
                }
            }

            // Associations
            Map<QualifiedName, EntityReference> associations = new HashMap<>();
            Preferences assocs = null;
            for( AssociationDescriptor associationType : entityDescriptor.state().associations() )
            {
                if( assocs == null )
                {
                    assocs = entityPrefs.node( "associations" );
                }

                String associatedEntity = assocs.get( associationType.qualifiedName().name(), null );
                EntityReference value = associatedEntity == null
                                        ? null
                                        : EntityReference.parseEntityReference( associatedEntity );
                associations.put( associationType.qualifiedName(), value );
            }

            // ManyAssociations
            Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<>();
            Preferences manyAssocs = null;
            for( AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations() )
            {
                if( manyAssocs == null )
                {
                    manyAssocs = entityPrefs.node( "manyassociations" );
                }

                List<EntityReference> references = new ArrayList<>();
                String entityReferences = manyAssocs.get( manyAssociationType.qualifiedName().name(), null );
                if( entityReferences == null )
                {
                    // ManyAssociation not found, default to empty one
                    manyAssociations.put( manyAssociationType.qualifiedName(), references );
                }
                else
                {
                    String[] refs = entityReferences.split( "\n" );
                    for( String ref : refs )
                    {
                        EntityReference value = ref == null
                                                ? null
                                                : EntityReference.parseEntityReference( ref );
                        references.add( value );
                    }
                    manyAssociations.put( manyAssociationType.qualifiedName(), references );
                }
            }

            // NamedAssociations
            Map<QualifiedName, Map<String, EntityReference>> namedAssociations = new HashMap<>();
            Preferences namedAssocs = null;
            for( AssociationDescriptor namedAssociationType : entityDescriptor.state().namedAssociations() )
            {
                if( namedAssocs == null )
                {
                    namedAssocs = entityPrefs.node( "namedassociations" );
                }
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 );
            }
View Full Code Here

        {
            JSONObject data = cacheState.json;
            try
            {
                String type = data.getString( JSONKeys.TYPE );
                EntityDescriptor entityDescriptor = unitOfWork.module().entityDescriptor( type );
                return new JSONEntityState( unitOfWork, valueSerialization, identity, entityDescriptor, data );
            }
            catch( JSONException e )
            {
                // Should not be able to happen, unless internal error in the cache system.
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 );
        }
View Full Code Here

            writeString( "- startup: " + ( (ServiceDetailDescriptor) objectDesciptor ).descriptor()
                .isInstantiateOnStartup() );
        }
        else if( objectDesciptor instanceof EntityDetailDescriptor )
        {
            EntityDescriptor descriptor = ( (EntityDetailDescriptor) objectDesciptor ).descriptor();
            writeString( "- name: " + descriptor.toString() );
            writeString( "- class: " + descriptor.toString() );
            writeString( "- visibility: " + descriptor.visibility().toString() );
        }
        else if( objectDesciptor instanceof ValueDetailDescriptor )
        {
            ValueDescriptor descriptor = ( (ValueDetailDescriptor) objectDesciptor ).descriptor();
            writeString( "- name: " + descriptor.toString() );
            writeString( "- class: " + descriptor.toString() );
            writeString( "- visibility: " + descriptor.visibility().toString() );
        }
        else if( objectDesciptor instanceof ObjectDetailDescriptor )
        {
            ObjectDescriptor descriptor = ( (ObjectDetailDescriptor) objectDesciptor ).descriptor();
            writeString( "- name: " + descriptor.toString() );
            writeString( "- class: " + descriptor.toString() );
            writeString( "- visibility: " + descriptor.visibility().toString() );
        }
        else if( objectDesciptor instanceof CompositeDetailDescriptor )
        {
            CompositeDescriptor descriptor = ( (CompositeDetailDescriptor) objectDesciptor ).descriptor();
            writeString( "- name: " + descriptor.toString() );
            writeString( "- class: " + descriptor.toString() );
            writeString( "- visibility: " + descriptor.visibility().toString() );
        }
    }
View Full Code Here

                JSONObject json = new JSONObject();

                json.put( "_identity", state.identity().identity() );
                json.put( "_types", Iterables.toList( Iterables.map( Classes.toClassName(), state.entityDescriptor().mixinTypes() ) ) );

                EntityDescriptor entityType = state.entityDescriptor();

                // Properties
                for( PropertyDescriptor propDesc : entityType.state().properties() )
                {
                    if( propDesc.queryable() )
                    {
                        String key = propDesc.qualifiedName().name();
                        Object value = state.propertyValueOf( propDesc.qualifiedName() );
                        if( value == null || ValueType.isPrimitiveValue( value ) )
                        {
                            json.put( key, value );
                        }
                        else
                        {
                            String serialized = valueSerializer.serialize( new Options().withoutTypeInfo(), value );
                            // TODO Theses tests are pretty fragile, find a better way to fix this, Jackson API should behave better
                            if( serialized.startsWith( "{" ) )
                            {
                                json.put( key, new JSONObject( serialized ) );
                            }
                            else if( serialized.startsWith( "[" ) )
                            {
                                json.put( key, new JSONArray( serialized ) );
                            }
                            else
                            {
                                json.put( key, serialized );
                            }
                        }
                    }
                }

                // Associations
                for( AssociationDescriptor assocDesc : entityType.state().associations() )
                {
                    if( assocDesc.queryable() )
                    {
                        String key = assocDesc.qualifiedName().name();
                        EntityReference associated = state.associationValueOf( assocDesc.qualifiedName() );
                        Object value;
                        if( associated == null )
                        {
                            value = null;
                        }
                        else
                        {
                            if( assocDesc.isAggregated() || support.indexNonAggregatedAssociations() )
                            {
                                if( newStates.containsKey( associated.identity() ) )
                                {
                                    value = new JSONObject( toJSON( newStates.get( associated.identity() ), newStates, uow ) );
                                }
                                else
                                {
                                    EntityState assocState = uow.entityStateOf( EntityReference.parseEntityReference( associated.identity() ) );
                                    value = new JSONObject( toJSON( assocState, newStates, uow ) );
                                }
                            }
                            else
                            {
                                value = new JSONObject( Collections.singletonMap( "identity", associated.identity() ) );
                            }
                        }
                        json.put( key, value );
                    }
                }

                // ManyAssociations
                for( AssociationDescriptor manyAssocDesc : entityType.state().manyAssociations() )
                {
                    if( manyAssocDesc.queryable() )
                    {
                        String key = manyAssocDesc.qualifiedName().name();
                        JSONArray array = new JSONArray();
                        ManyAssociationState associateds = state.manyAssociationValueOf( manyAssocDesc.qualifiedName() );
                        for( EntityReference associated : associateds )
                        {
                            if( manyAssocDesc.isAggregated() || support.indexNonAggregatedAssociations() )
                            {
                                if( newStates.containsKey( associated.identity() ) )
                                {
                                    array.put( new JSONObject( toJSON( newStates.get( associated.identity() ), newStates, uow ) ) );
                                }
                                else
                                {
                                    EntityState assocState = uow.entityStateOf( EntityReference.parseEntityReference( associated.identity() ) );
                                    array.put( new JSONObject( toJSON( assocState, newStates, uow ) ) );
                                }
                            }
                            else
                            {
                                array.put( new JSONObject( Collections.singletonMap( "identity", associated.identity() ) ) );
                            }
                        }
                        json.put( key, array );
                    }
                }

                // NamedAssociations
                for( AssociationDescriptor namedAssocDesc : entityType.state().namedAssociations() )
                {
                    if( namedAssocDesc.queryable() )
                    {
                        String key = namedAssocDesc.qualifiedName().name();
                        JSONArray array = new JSONArray();
View Full Code Here

                rows.add( new TableRow( 2, moduleRow, ( (ImportedServiceDetailDescriptor) objectDesciptor ).module() ) );
                rows.add( new TableRow( 2, layerRow, ( (ImportedServiceDetailDescriptor) objectDesciptor ).module().layer() ) );
            }
            else if( objectDesciptor instanceof EntityDetailDescriptor )
            {
                EntityDescriptor descriptor = ( (EntityDetailDescriptor) objectDesciptor ).descriptor();
                Class<?> type = first( descriptor.types() );
                rows.add( new TableRow( 2, nameRow, type.getSimpleName() ) );
                rows.add( new TableRow( 2, classRow, type.getName() ) );
                rows.add( new TableRow( 2, visibilityRow, descriptor.visibility().toString() ) );
                rows.add( new TableRow( 2, moduleRow, ( (EntityDetailDescriptor) objectDesciptor ).module() ) );
                rows.add( new TableRow( 2, layerRow, ( (EntityDetailDescriptor) objectDesciptor ).module().layer() ) );
            }
            else if( objectDesciptor instanceof ValueDetailDescriptor )
            {
                ValueDescriptor descriptor = ( (ValueDetailDescriptor) objectDesciptor ).descriptor();
                Class<?> type = first( descriptor.types() );
                rows.add( new TableRow( 2, new Object[]
                {
                    nameRow, type.getSimpleName()
                } ) );
                rows.add( new TableRow( 2, new Object[]
                {
                    classRow, type.getName()
                } ) );
                rows.add( new TableRow( 2, new Object[]
                {
                    visibilityRow, descriptor.visibility().toString()
                } ) );
                rows.add( new TableRow( 2, new Object[]
                {
                    moduleRow, ( (ValueDetailDescriptor) objectDesciptor ).module()
                } ) );
                rows.add( new TableRow( 2, new Object[]
                {
                    layerRow, ( (ValueDetailDescriptor) objectDesciptor ).module().layer()
                } ) );
            }
            else if( objectDesciptor instanceof ObjectDetailDescriptor )
            {
                ObjectDescriptor descriptor = ( (ObjectDetailDescriptor) objectDesciptor ).descriptor();
                Class<?> type = first( descriptor.types() );
                rows.add( new TableRow( 2, nameRow, type.getSimpleName() ) );
                rows.add( new TableRow( 2, classRow, type.getName() ) );
                rows.add( new TableRow( 2, visibilityRow, descriptor.visibility().toString() ) );
                rows.add( new TableRow( 2, moduleRow, ( (ObjectDetailDescriptor) objectDesciptor ).module() ) );
                rows.add( new TableRow( 2, layerRow, ( (ObjectDetailDescriptor) objectDesciptor ).module().layer() ) );
            }
            else if( objectDesciptor instanceof CompositeDetailDescriptor )
            {
                CompositeDescriptor descriptor = ( (CompositeDetailDescriptor) objectDesciptor ).descriptor();
                Class<?> type = first( descriptor.types() );
                rows.add( new TableRow( 2, nameRow, type.getSimpleName() ) );
                rows.add( new TableRow( 2, classRow, type.getName() ) );
                rows.add( new TableRow( 2, visibilityRow, descriptor.visibility().toString() ) );
                rows.add( new TableRow( 2, moduleRow, ( (CompositeDetailDescriptor) objectDesciptor ).module() ) );
                rows.add( new TableRow( 2, layerRow, ( (CompositeDetailDescriptor) objectDesciptor ).module().layer() ) );
            }

            fireTableDataChanged();
View Full Code Here

                key( JSONKeys.APPLICATION_VERSION ).value( application.version() ).
                key( JSONKeys.TYPE ).value( first( state.entityDescriptor().types() ).getName() ).
                key( JSONKeys.VERSION ).value( version ).
                key( JSONKeys.MODIFIED ).value( lastModified ).
                key( JSONKeys.PROPERTIES ).object();
            EntityDescriptor entityType = state.entityDescriptor();
            for( PropertyDescriptor persistentProperty : entityType.state().properties() )
            {
                Object value = state.properties().get( persistentProperty.qualifiedName() );
                json.key( persistentProperty.qualifiedName().name() );
                if( value == null || ValueType.isPrimitiveValue( value ) )
                {
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.getJSONObject( JSONKeys.NAMED_ASSOCIATIONS );
            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

                throws MalformedObjectNameException, MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException
        {
            for ( ServiceReference<DataSource> dataSource : dataSources ) {
                String name = dataSource.identity();
                Module module = ( Module ) spi.moduleOf( dataSource );
                EntityDescriptor descriptor = module.entityDescriptor( DataSourceConfiguration.class.getName() );
                List<MBeanAttributeInfo> attributes = new ArrayList<MBeanAttributeInfo>();
                Map<String, AccessibleObject> properties = new LinkedHashMap<String, AccessibleObject>();
                for ( PropertyDescriptor persistentProperty : descriptor.state().properties() ) {
                    if ( !persistentProperty.isImmutable() ) {
                        String propertyName = persistentProperty.qualifiedName().name();
                        String type = persistentProperty.valueType().mainType().getName();
                        attributes.add( new MBeanAttributeInfo( propertyName, type, propertyName, true, true, type.equals( "java.lang.Boolean" ) ) );
                        properties.put( propertyName, persistentProperty.accessor() );
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.