Search in sources :

Example 1 with Property

use of org.qi4j.api.property.Property in project qi4j-sdk by Qi4j.

the class PropertyMapper method toJavaProperties.

/**
     * Create Properties object which is backed by the given Composite.
     *
     * @param composite the instance
     *
     * @return properties instance
     */
public static Properties toJavaProperties(final Composite composite) {
    return new Properties() {

        private static final long serialVersionUID = 3550125427530538865L;

        @Override
        public Object get(Object o) {
            try {
                Method propertyMethod = composite.getClass().getMethod(o.toString());
                Property<?> property = (Property<?>) propertyMethod.invoke(composite);
                return property.get();
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                return null;
            }
        }

        @Override
        public Object put(Object o, Object o1) {
            Object oldValue = get(o);
            try {
                Method propertyMethod = composite.getClass().getMethod(o.toString(), Object.class);
                propertyMethod.invoke(composite, o1);
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
            return oldValue;
        }
    };
}
Also used : Method(java.lang.reflect.Method) Properties(java.util.Properties) Property(org.qi4j.api.property.Property) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with Property

use of org.qi4j.api.property.Property in project qi4j-sdk by Qi4j.

the class PropertyMapper method map.

/**
     * Populate the Composite with properties from the given properties object.
     *
     * @param props     properties object
     * @param composite the composite instance
     *
     * @throws IllegalArgumentException if properties could not be transferred to composite
     */
public static void map(Properties props, Composite composite) throws IllegalArgumentException {
    for (Map.Entry<Object, Object> objectObjectEntry : props.entrySet()) {
        try {
            String methodName = objectObjectEntry.getKey().toString();
            Method propertyMethod = composite.getClass().getInterfaces()[0].getMethod(methodName);
            propertyMethod.setAccessible(true);
            Object value = objectObjectEntry.getValue();
            Type propertyType = GenericPropertyInfo.propertyTypeOf(propertyMethod);
            value = mapToType(composite, propertyType, value.toString());
            @SuppressWarnings("unchecked") Property<Object> property = (Property<Object>) propertyMethod.invoke(composite);
            property.set(value);
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException("Could not find any property named " + objectObjectEntry.getKey());
        } catch (IllegalAccessException e) {
            //noinspection ThrowableInstanceNeverThrown
            throw new IllegalArgumentException("Could not populate property named " + objectObjectEntry.getKey(), e);
        } catch (InvocationTargetException e) {
            //noinspection ThrowableInstanceNeverThrown
            String message = "Could not populate property named " + objectObjectEntry.getKey();
            throw new IllegalArgumentException(message, e);
        }
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.qi4j.api.property.Property)

Example 3 with Property

use of org.qi4j.api.property.Property in project qi4j-sdk by Qi4j.

the class ModuleInstance method newTransientBuilder.

// Implementation of TransientBuilderFactory
@Override
public <T> TransientBuilder<T> newTransientBuilder(Class<T> mixinType) throws NoSuchTransientException {
    NullArgumentException.validateNotNull("mixinType", mixinType);
    ModelModule<TransientModel> modelModule = typeLookup.lookupTransientModel(mixinType);
    if (modelModule == null) {
        throw new NoSuchTransientException(mixinType.getName(), name());
    }
    Map<AccessibleObject, Property<?>> properties = new HashMap<>();
    for (PropertyModel propertyModel : modelModule.model().state().properties()) {
        Property<?> property = new PropertyInstance<>(propertyModel.getBuilderInfo(), propertyModel.initialValue(modelModule.module()));
        properties.put(propertyModel.accessor(), property);
    }
    TransientStateInstance state = new TransientStateInstance(properties);
    return new TransientBuilderInstance<>(modelModule, state, UsesInstance.EMPTY_USES);
}
Also used : NoSuchTransientException(org.qi4j.api.composite.NoSuchTransientException) TransientStateInstance(org.qi4j.runtime.composite.TransientStateInstance) TransientModel(org.qi4j.runtime.composite.TransientModel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) PropertyModel(org.qi4j.runtime.property.PropertyModel) TransientBuilderInstance(org.qi4j.runtime.composite.TransientBuilderInstance) AccessibleObject(java.lang.reflect.AccessibleObject) PropertyInstance(org.qi4j.runtime.property.PropertyInstance) Property(org.qi4j.api.property.Property)

Example 4 with Property

use of org.qi4j.api.property.Property in project qi4j-sdk by Qi4j.

the class PropertyInstance method equals.

/**
     * Perform equals with {@code o} argument.
     * <p>
     *     The definition of equals() for the Property is that if both the state and descriptor are equal,
     *     then the properties are equal.
     * </p>
     *
     * @param o The other object to compare.
     * @return Returns a {@code boolean} indicator whether this object is equals the other.
     */
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    Property<?> that = (Property<?>) o;
    // Unwrap if needed
    while (that instanceof PropertyWrapper) {
        that = ((PropertyWrapper) that).next();
    }
    // Descriptor equality
    PropertyDescriptor thatDescriptor = (PropertyDescriptor) ((PropertyInstance) that).propertyInfo();
    if (!model.equals(thatDescriptor)) {
        return false;
    }
    // State equality
    T value = get();
    if (value == null) {
        return that.get() == null;
    }
    return value.equals(that.get());
}
Also used : PropertyWrapper(org.qi4j.api.property.PropertyWrapper) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) Property(org.qi4j.api.property.Property)

Example 5 with Property

use of org.qi4j.api.property.Property in project qi4j-sdk by Qi4j.

the class EntityStateInstance method associationFor.

@Override
@SuppressWarnings("unchecked")
public <T> Association<T> associationFor(AccessibleObject accessor) throws IllegalArgumentException {
    Map<AccessibleObject, Object> state = state();
    Association<T> association = (Association<T>) state.get(accessor);
    if (association == null) {
        final AssociationModel associationModel = stateModel.getAssociation(accessor);
        association = new AssociationInstance<>(entityState instanceof BuilderEntityState ? associationModel.getBuilderInfo() : associationModel, entityFunction, new Property<EntityReference>() {

            @Override
            public EntityReference get() {
                return entityState.associationValueOf(associationModel.qualifiedName());
            }

            @Override
            public void set(EntityReference newValue) throws IllegalArgumentException, IllegalStateException {
                entityState.setAssociationValue(associationModel.qualifiedName(), newValue);
            }
        });
        state.put(accessor, association);
    }
    return association;
}
Also used : Association(org.qi4j.api.association.Association) ManyAssociation(org.qi4j.api.association.ManyAssociation) NamedAssociation(org.qi4j.api.association.NamedAssociation) EntityReference(org.qi4j.api.entity.EntityReference) AccessibleObject(java.lang.reflect.AccessibleObject) AccessibleObject(java.lang.reflect.AccessibleObject) BuilderEntityState(org.qi4j.runtime.unitofwork.BuilderEntityState) Property(org.qi4j.api.property.Property) NamedAssociationModel(org.qi4j.runtime.association.NamedAssociationModel) AssociationModel(org.qi4j.runtime.association.AssociationModel) ManyAssociationModel(org.qi4j.runtime.association.ManyAssociationModel)

Aggregations

Property (org.qi4j.api.property.Property)11 AccessibleObject (java.lang.reflect.AccessibleObject)5 Method (java.lang.reflect.Method)4 HashMap (java.util.HashMap)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Association (org.qi4j.api.association.Association)3 ManyAssociation (org.qi4j.api.association.ManyAssociation)3 NamedAssociation (org.qi4j.api.association.NamedAssociation)3 PropertyModel (org.qi4j.runtime.property.PropertyModel)3 Collection (java.util.Collection)2 Map (java.util.Map)2 TransientStateInstance (org.qi4j.runtime.composite.TransientStateInstance)2 PropertyInstance (org.qi4j.runtime.property.PropertyInstance)2 BuilderEntityState (org.qi4j.runtime.unitofwork.BuilderEntityState)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ResultSet (java.sql.ResultSet)1 Properties (java.util.Properties)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1