Package org.qi4j.api.value

Examples of org.qi4j.api.value.ValueDescriptor


        this.includeTypeInformation = includeTypeInformation;
    }

    public void serialize( ValueComposite value ) throws JSONException
    {
        ValueDescriptor valueDescriptor = (ValueDescriptor) Qi4j.DESCRIPTOR_FUNCTION.map(value);

        ValueType valueType = valueDescriptor.valueType();

        serialize( value, valueType );
    }
View Full Code Here


            ValueComposite valueComposite = (ValueComposite) value;

            if( !valueCompositeType.type().equals( Qi4j.DESCRIPTOR_FUNCTION.map( valueComposite ).type() ) )
            {
                // Actual value is a subtype - use it instead
                ValueDescriptor valueDescriptor = (ValueDescriptor) Qi4j.DESCRIPTOR_FUNCTION.map( valueComposite );

                valueCompositeType = valueDescriptor.valueType();

                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 )
        {
            CollectionType collectionType = (CollectionType) valueType;

            arrayStart();

            Collection collection = (Collection) value;
            for( Object collectionValue : collection )
            {
                serialize( collectionValue, collectionType.collectedType() );
            }

            arrayEnd();

        } else if( valueType instanceof MapType )
        {
            arrayStart();

            MapType mapType = (MapType) valueType;
            Map map = (Map) value;
            Set<Map.Entry> set = map.entrySet();
            for( Map.Entry<Object, Object> entry : set )
            {
                objectStart();
                key( "key" ).serialize( entry.getKey(), mapType.getKeyType() );
                key( "value" ).serialize( entry.getValue(), mapType.getValueType() );
                objectEnd();
            }

            arrayEnd();

        } else if (valueType instanceof EnumType )
        {
            value( value.toString() );
        } else
        {
/* TODO How to handle deserialization?
            // Try for the actual value class first
            typeFunction = typeFunctions.get( value.getClass() );
            if( typeFunction != null )
            {
                value( typeFunction.map( value ) );
                return;
            }
*/

            // Check if we are serializing an Entity
            if( value instanceof EntityComposite )
            {
                // Store reference instead
                value = EntityReference.getEntityReference( value );
            } else if( value instanceof ValueComposite )
            {
                // Serialize ValueComposite JSON instead
                try
                {
                    JSONObjectSerializer JSONObjectSerializer = new JSONObjectSerializer();
                    JSONObjectSerializer.serialize( (ValueComposite) value );

                    JSONObject object = (JSONObject) JSONObjectSerializer.getRoot();

                    ValueDescriptor descriptor = (ValueDescriptor) Qi4j.DESCRIPTOR_FUNCTION.map( (Compositevalue );

                    if (includeTypeInformation)
                        object.put( "_type", descriptor.type().getName() );
                    value( object );
                    return;
                } catch( JSONException e )
                {
                    throw new IllegalStateException( "Could not JSON serialize value", e );
View Full Code Here

            ValueCompositeType actualValueType = (ValueCompositeType) valueType;
            String actualType = jsonObject.optString( "_type" );
            if( !actualType.equals( "" ) )
            {
                ValueDescriptor descriptor = module.valueDescriptor( actualType );

                if( descriptor == null )
                {
                    throw new IllegalArgumentException( "Could not find any value of type '" + actualType + "' in module" + module );
                }

                actualValueType = descriptor.valueType();
            }

            final Map<QualifiedName, Object> values = new HashMap<QualifiedName, Object>();
            for( PropertyDescriptor persistentProperty : actualValueType.properties() )
            {
                Object valueJson = null;
                try
                {
                    valueJson = jsonObject.opt( persistentProperty.qualifiedName().name() );

                    Object value = null;
                    if( valueJson != null && !valueJson.equals( JSONObject.NULL ) )
                    {
                        value = deserialize( valueJson, persistentProperty.valueType() );

                        if (persistentProperty.isImmutable())
                        {
                            if (value instanceof Set)
                            {
                                value = Collections.unmodifiableSet( (Set<? extends Object>) value);
                            } else if (value instanceof List)
                            {
                                value = Collections.unmodifiableList( (List<? extends Object>) value);
                            } else if (value instanceof Map)
                            {
                                value = Collections.unmodifiableMap( (Map<? extends Object, ? extends Object>) value);
                            }
                        }
                    }

                    values.put( persistentProperty.qualifiedName(), value );
                } catch( JSONException e )
                {
                    // Not found in JSON or wrong format - try defaulting it
                    try
                    {
                        Object defaultValue = DefaultValues.getDefaultValue( persistentProperty.valueType().type() );
                        values.put( persistentProperty.qualifiedName(), defaultValue );
                    } catch( RuntimeException e1 )
                    {
                        // Didn't work, throw the exception
                        throw e;
                    }
                }
            }

            for( AssociationDescriptor associationDescriptor : actualValueType.associations() )
            {
                Object valueJson = jsonObject.optString( associationDescriptor.qualifiedName().name() );
                if (valueJson != null)
                    values.put( associationDescriptor.qualifiedName(), EntityReference.parseEntityReference( valueJson.toString() ) );
            }

            for( AssociationDescriptor associationDescriptor : actualValueType.manyAssociations() )
            {
                JSONArray jsonArray = jsonObject.optJSONArray( associationDescriptor.qualifiedName().name() );
                if (jsonArray != null)
                {
                    List<EntityReference> refs = new ArrayList<EntityReference>();
                    for (int i = 0; i < jsonArray.length(); i++)
                    {
                        refs.add( EntityReference.parseEntityReference( jsonArray.getString( i ) ) );
                    }
                }
            }

            ValueBuilder valueBuilder = module
                    .newValueBuilderWithState( actualValueType.type(), new Function<PropertyDescriptor, Object>()
                    {
                        @Override
                        public Object map( PropertyDescriptor descriptor )
                        {
                            return values.get( descriptor.qualifiedName() );
                        }
                    },new Function<AssociationDescriptor, EntityReference>()
                    {
                        @Override
                        public EntityReference map( AssociationDescriptor associationDescriptor )
                        {
                            Object ref = values.get( associationDescriptor.qualifiedName() );
                            if (ref == null)
                                return null;
                            else
                                return (EntityReference) ref;
                        }
                    },new Function<AssociationDescriptor, Iterable<EntityReference>>()
                    {
                        @Override
                        public Iterable<EntityReference> map( AssociationDescriptor associationDescriptor )
                        {
                            Object ref = values.get( associationDescriptor.qualifiedName() );
                            if (ref == null)
                                return Iterables.empty();
                            else
                                return (Iterable<EntityReference>) ref;
                        }
                    });

            return valueBuilder.newInstance();
        } else
        {
            try
            {
                if( json instanceof JSONObject )
                {
                    // ValueComposite deserialization
                    JSONObject jsonObject = (JSONObject) json;
                    String type = jsonObject.getString( "_type" );

                    ValueDescriptor valueDescriptor = module.valueDescriptor( type );
                    return deserialize( json, valueDescriptor.valueType() );
                } else
                {
                    String serializedString = (String) json;
                    byte[] bytes = serializedString.getBytes( "UTF-8" );
                    bytes = Base64Encoder.decode( bytes );
View Full Code Here

        private Module module;

        @Override
        public <T> T convert( final Class<T> valueType, Object entity )
        {
            ValueDescriptor valueDescriptor = module.valueDescriptor( valueType.getName() );
            if( valueDescriptor == null )
            {
                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;
View Full Code Here

        private Module module;

        @Override
        public <T> T convert( final Class<T> valueType, Object entity )
        {
            ValueDescriptor valueDescriptor = module.valueDescriptor( valueType.getName() );
            if( valueDescriptor == null )
            {
                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;
View Full Code Here

                    for( CompositeDescriptorInfo descInfo : vDescriptors )
                    {
                        CompositeDescriptor desc = descInfo.composite;
                        if( desc instanceof ValueDescriptor )
                        {
                            ValueDescriptor vDesc = (ValueDescriptor) desc;
                            // TODO this doesn't understand, say, Map<String, String>, or indeed,
                            // any
                            // other Serializable
                            if( Iterables.matchesAny( new Specification<Class<?>>()
                            {
                                @Override
                                public boolean satisfiedBy( Class<?> item )
                                {
                                    return vTypeClass.isAssignableFrom( item );
                                }
                            }, vDesc.types() ) )
                            {
                                for( PropertyDescriptor subPDesc : vDesc.state().properties() )
                                {
                                    this.processPropertyTypeForQNames( //
                                        subPDesc, //
                                        qNameInfos, //
                                        newQNames, //
View Full Code Here

                                         Long entityPK,
                                         Object property
    )
        throws SQLException
    {
        ValueDescriptor vDesc = this._qi4SPI.valueDescriptorFor( (ValueComposite) property );
        StateHolder state = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map( (ValueComposite) property ).state();
        Integer originalPropertyPK = propertyPK;
        ++propertyPK;
        for( PropertyDescriptor pDesc : vDesc.state().properties() )
        {

            propertyPK = this.insertProperty(
                qNameInsertPSs,
                insertAllQNamesPS,
View Full Code Here

        {
            ps.setNull( nextFreeIndex, Types.INTEGER );
        }
        else
        {
            ValueDescriptor vDesc = this._qi4SPI.valueDescriptorFor( vc );
            Integer classID = this._state.usedClassesPKs().get().get( vDesc );
            ps.setInt( nextFreeIndex, classID );
        }
    }
View Full Code Here

            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

                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

TOP

Related Classes of org.qi4j.api.value.ValueDescriptor

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.