Search in sources :

Example 11 with Property

use of lucee.runtime.component.Property in project Lucee by lucee.

the class JSONConverter method _serializeStruct.

/**
 * serialize a Struct
 * @param struct Struct to serialize
 * @param sb
 * @param serializeQueryByColumns
 * @param addUDFs
 * @param done
 * @throws ConverterException
 */
public void _serializeStruct(PageContext pc, Set test, Struct struct, StringBuilder sb, boolean serializeQueryByColumns, boolean addUDFs, Set<Object> done) throws ConverterException {
    // Component
    if (struct instanceof Component) {
        String res = castToJson(pc, (Component) struct, NULL_STRING);
        if (res != NULL_STRING) {
            sb.append(res);
            return;
        }
    }
    sb.append(goIn());
    sb.append("{");
    // Key[] keys = struct.keys();
    // Key key;
    Iterator<Entry<Key, Object>> it = struct.entryIterator();
    Entry<Key, Object> e;
    Object value;
    boolean doIt = false;
    while (it.hasNext()) {
        e = it.next();
        // key=keys[i];
        value = e.getValue();
        if (!addUDFs && (value instanceof UDF || value == null))
            continue;
        if (doIt)
            sb.append(',');
        doIt = true;
        sb.append(StringUtil.escapeJS(e.getKey().getString(), '"', charsetEncoder));
        sb.append(':');
        _serialize(pc, test, value, sb, serializeQueryByColumns, done);
    }
    if (struct instanceof Component) {
        Boolean remotingFetch;
        Component comp = (Component) struct;
        boolean isPeristent = false;
        isPeristent = comp.isPersistent();
        Property[] props = comp.getProperties(false);
        ComponentScope scope = comp.getComponentScope();
        for (int i = 0; i < props.length; i++) {
            if (!ignoreRemotingFetch) {
                remotingFetch = Caster.toBoolean(props[i].getDynamicAttributes().get(REMOTING_FETCH, null), null);
                if (remotingFetch == null) {
                    if (isPeristent && ORMUtil.isRelated(props[i]))
                        continue;
                } else if (!remotingFetch.booleanValue())
                    continue;
            }
            Key key = KeyImpl.getInstance(props[i].getName());
            value = scope.get(key, null);
            if (!addUDFs && (value instanceof UDF || value == null))
                continue;
            if (doIt)
                sb.append(',');
            doIt = true;
            sb.append(StringUtil.escapeJS(key.getString(), '"', charsetEncoder));
            sb.append(':');
            _serialize(pc, test, value, sb, serializeQueryByColumns, done);
        }
    }
    sb.append('}');
}
Also used : Entry(java.util.Map.Entry) ComponentScope(lucee.runtime.ComponentScope) UDF(lucee.runtime.type.UDF) JavaObject(lucee.runtime.java.JavaObject) Component(lucee.runtime.Component) Property(lucee.runtime.component.Property) Key(lucee.runtime.type.Collection.Key)

Example 12 with Property

use of lucee.runtime.component.Property in project Lucee by lucee.

the class AxisCaster method toComponent.

public static Component toComponent(PageContext pc, Pojo pojo, String compPath, Component defaultValue) {
    try {
        Component cfc = pc.loadComponent(compPath);
        Property[] props = cfc.getProperties(false, true, false, false);
        PojoIterator it = new PojoIterator(pojo);
        // only when the same amount of properties
        if (props.length == it.size()) {
            Map<Collection.Key, Property> propMap = toMap(props);
            Property p;
            Pair<Collection.Key, Object> pair;
            ComponentScope scope = cfc.getComponentScope();
            while (it.hasNext()) {
                pair = it.next();
                p = propMap.get(pair.getName());
                if (p == null)
                    return defaultValue;
                Object val = null;
                try {
                    val = Caster.castTo(pc, p.getType(), pair.getValue(), false);
                } catch (PageException e) {
                }
                // store in variables and this scope
                scope.setEL(pair.getName(), val);
                cfc.setEL(pair.getName(), val);
            }
            return cfc;
        }
    } catch (PageException e) {
    }
    return defaultValue;
}
Also used : PageException(lucee.runtime.exp.PageException) ComponentScope(lucee.runtime.ComponentScope) Component(lucee.runtime.Component) Property(lucee.runtime.component.Property) Key(lucee.runtime.type.Collection.Key)

Example 13 with Property

use of lucee.runtime.component.Property in project Lucee by lucee.

the class AxisCaster method _initPojo.

private static void _initPojo(PageContext pc, TypeEntry typeEntry, QName type, Pojo pojo, Property[] props, Struct sct, Component comp, TypeMapping tm, Set<Object> done) throws PageException {
    Property p;
    Object v;
    Collection.Key k;
    CFMLExpressionInterpreter interpreter = new CFMLExpressionInterpreter(false);
    for (int i = 0; i < props.length; i++) {
        p = props[i];
        k = Caster.toKey(p.getName());
        // value
        v = sct.get(k, null);
        if (v == null && comp != null)
            v = comp.get(k, null);
        if (v != null)
            v = Caster.castTo(pc, p.getType(), v, false);
        else {
            if (!StringUtil.isEmpty(p.getDefault())) {
                try {
                    v = Caster.castTo(pc, p.getType(), p.getDefault(), false);
                } catch (PageException pe) {
                    try {
                        v = interpreter.interpret(pc, p.getDefault());
                        v = Caster.castTo(pc, p.getType(), v, false);
                    } catch (PageException pe2) {
                        throw new ExpressionException("can not use default value [" + p.getDefault() + "] for property [" + p.getName() + "] with type [" + p.getType() + "]");
                    }
                }
            }
        }
        // set or throw
        if (v == null) {
            if (p.isRequired())
                throw new ExpressionException("required property [" + p.getName() + "] is not defined");
        } else {
            TypeEntry childTE = null;
            QName childT = null;
            if (typeEntry != null) {
                childTE = AxisUtil.getContainedElement(typeEntry, p.getName(), null);
                if (childTE != null)
                    childT = childTE.getQName();
            }
            Reflector.callSetter(pojo, p.getName().toLowerCase(), _toAxisType(tm, null, childTE, childT, null, v, done));
        }
    }
}
Also used : Key(lucee.runtime.type.Collection.Key) PageException(lucee.runtime.exp.PageException) QName(javax.xml.namespace.QName) Collection(lucee.runtime.type.Collection) CFMLExpressionInterpreter(lucee.runtime.interpreter.CFMLExpressionInterpreter) Property(lucee.runtime.component.Property) ExpressionException(lucee.runtime.exp.ExpressionException) TypeEntry(org.apache.axis.wsdl.symbolTable.TypeEntry)

Aggregations

Property (lucee.runtime.component.Property)13 Key (lucee.runtime.type.Collection.Key)7 ComponentScope (lucee.runtime.ComponentScope)6 PageException (lucee.runtime.exp.PageException)5 Struct (lucee.runtime.type.Struct)5 UDF (lucee.runtime.type.UDF)5 IOException (java.io.IOException)4 Component (lucee.runtime.Component)4 Collection (lucee.runtime.type.Collection)4 UDFGSProperty (lucee.runtime.type.UDFGSProperty)4 Entry (java.util.Map.Entry)3 ComponentSpecificAccess (lucee.runtime.ComponentSpecificAccess)3 ArrayList (java.util.ArrayList)2 ClassException (lucee.commons.lang.ClassException)2 StructImpl (lucee.runtime.type.StructImpl)2 TypeEntry (org.apache.axis.wsdl.symbolTable.TypeEntry)2 URL (java.net.URL)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1