Search in sources :

Example 11 with InstanceContext

use of org.apache.tapestry5.plastic.InstanceContext in project tapestry-5 by apache.

the class ApplicationStateWorker method transform.

@SuppressWarnings("unchecked")
private void transform(PlasticClass transformation, PlasticField field, final boolean create) {
    final Class fieldClass = componentClassCache.forName(field.getTypeName());
    field.setConduit(new FieldConduit() {

        public void set(Object instance, InstanceContext context, Object newValue) {
            applicationStateManager.set(fieldClass, newValue);
        }

        public Object get(Object instance, InstanceContext context) {
            return create ? applicationStateManager.get(fieldClass) : applicationStateManager.getIfExists(fieldClass);
        }
    });
    final String expectedName = field.getName() + "Exists";
    List<PlasticField> fields = F.flow(transformation.getAllFields()).filter(new Predicate<PlasticField>() {

        public boolean accept(PlasticField field) {
            return field.getTypeName().equals("boolean") && field.getName().equalsIgnoreCase(expectedName);
        }
    }).toList();
    for (PlasticField existsField : fields) {
        existsField.claim(this);
        final String className = transformation.getClassName();
        final String fieldName = existsField.getName();
        existsField.setConduit(new ReadOnlyComponentFieldConduit(className, fieldName) {

            public Object get(Object instance, InstanceContext context) {
                return applicationStateManager.exists(fieldClass);
            }
        });
    }
}
Also used : FieldConduit(org.apache.tapestry5.plastic.FieldConduit) InstanceContext(org.apache.tapestry5.plastic.InstanceContext) PlasticField(org.apache.tapestry5.plastic.PlasticField) PlasticClass(org.apache.tapestry5.plastic.PlasticClass) Predicate(org.apache.tapestry5.func.Predicate)

Example 12 with InstanceContext

use of org.apache.tapestry5.plastic.InstanceContext in project tapestry-5 by apache.

the class InjectComponentWorker method transform.

public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
    for (PlasticField field : plasticClass.getFieldsWithAnnotation(InjectComponent.class)) {
        InjectComponent annotation = field.getAnnotation(InjectComponent.class);
        field.claim(annotation);
        final String type = field.getTypeName();
        final String componentId = getComponentId(field, annotation);
        final String fieldName = field.getName();
        ComputedValue<FieldConduit<Object>> provider = new ComputedValue<FieldConduit<Object>>() {

            public FieldConduit<Object> get(InstanceContext context) {
                ComponentResources resources = context.get(ComponentResources.class);
                return new InjectedComponentFieldValueConduit(resources, fieldName, type, componentId);
            }
        };
        field.setComputedConduit(provider);
    }
}
Also used : InjectComponent(org.apache.tapestry5.annotations.InjectComponent) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 13 with InstanceContext

use of org.apache.tapestry5.plastic.InstanceContext in project tapestry-5 by apache.

the class PersistWorker method makeFieldPersistent.

private void makeFieldPersistent(PlasticField field, MutableComponentModel model) {
    Persist annotation = field.getAnnotation(Persist.class);
    field.claim(annotation);
    final String logicalFieldName = model.setFieldPersistenceStrategy(field.getName(), annotation.value());
    final Object defaultValue = determineDefaultValueFromFieldType(field);
    ComputedValue<FieldConduit<Object>> computed = new ComputedValue<FieldConduit<Object>>() {

        public FieldConduit<Object> get(InstanceContext context) {
            InternalComponentResources resources = context.get(InternalComponentResources.class);
            return new PersistentFieldConduit(resources, logicalFieldName, defaultValue);
        }
    };
    field.setComputedConduit(computed);
}
Also used : InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) Persist(org.apache.tapestry5.annotations.Persist)

Aggregations

ComponentResources (org.apache.tapestry5.ComponentResources)6 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)5 InstanceContext (org.apache.tapestry5.plastic.InstanceContext)5 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 ReadOnlyComponentFieldConduit (org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit)2 ComputedValue (org.apache.tapestry5.plastic.ComputedValue)2 PlasticField (org.apache.tapestry5.plastic.PlasticField)2 PersistenceContext (javax.persistence.PersistenceContext)1 Asset (org.apache.tapestry5.Asset)1 Binding (org.apache.tapestry5.Binding)1 BindParameter (org.apache.tapestry5.annotations.BindParameter)1 Cached (org.apache.tapestry5.annotations.Cached)1 Environmental (org.apache.tapestry5.annotations.Environmental)1 InjectComponent (org.apache.tapestry5.annotations.InjectComponent)1 Path (org.apache.tapestry5.annotations.Path)1 Persist (org.apache.tapestry5.annotations.Persist)1 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)1 Predicate (org.apache.tapestry5.func.Predicate)1 LiteralBinding (org.apache.tapestry5.internal.bindings.LiteralBinding)1 PerThreadValue (org.apache.tapestry5.ioc.services.PerThreadValue)1