Search in sources :

Example 1 with Query

use of lucee.runtime.type.Query in project Lucee by lucee.

the class LDAPClient method query.

/**
 * @param dn
 * @param strAttributes
 * @param scope
 * @param startrow
 * @param maxrows
 * @param timeout
 * @param sort
 * @param sortType
 * @param sortDirection
 * @param start
 * @param separator
 * @param filter
 * @return
 * @throws NamingException
 * @throws PageException
 * @throws IOException
 */
public Query query(String strAttributes, int scope, int startrow, int maxrows, int timeout, String[] sort, int sortType, int sortDirection, String start, String separator, String filter) throws NamingException, PageException, IOException {
    // strAttributes=strAttributes.trim();
    boolean attEQAsterix = strAttributes.trim().equals("*");
    String[] attributes = attEQAsterix ? new String[] { "name", "value" } : toStringAttributes(strAttributes, ",");
    // Control
    SearchControls controls = new SearchControls();
    controls.setReturningObjFlag(true);
    controls.setSearchScope(scope);
    if (!attEQAsterix)
        controls.setReturningAttributes(toStringAttributes(strAttributes, ","));
    if (maxrows > 0)
        controls.setCountLimit(startrow + maxrows + 1);
    if (timeout > 0)
        controls.setTimeLimit(timeout);
    InitialLdapContext context = new InitialLdapContext(env, null);
    // Search
    Query qry = new QueryImpl(attributes, 0, "query");
    try {
        NamingEnumeration results = context.search(start, filter, controls);
        // Fill result
        int row = 1;
        if (!attEQAsterix) {
            while (results.hasMoreElements()) {
                SearchResult resultRow = (SearchResult) results.next();
                if (row++ < startrow)
                    continue;
                int len = qry.addRow();
                NamingEnumeration rowEnum = resultRow.getAttributes().getAll();
                String dn = resultRow.getNameInNamespace();
                qry.setAtEL("dn", len, dn);
                while (rowEnum.hasMore()) {
                    Attribute attr = (Attribute) rowEnum.next();
                    Collection.Key key = KeyImpl.init(attr.getID());
                    Enumeration values = attr.getAll();
                    Object value;
                    String existing, strValue;
                    while (values.hasMoreElements()) {
                        value = values.nextElement();
                        strValue = Caster.toString(value, null);
                        existing = Caster.toString(qry.getAt(key, len, null), null);
                        if (!StringUtil.isEmpty(existing) && !StringUtil.isEmpty(strValue)) {
                            value = existing + separator + strValue;
                        } else if (!StringUtil.isEmpty(existing))
                            value = existing;
                        qry.setAtEL(key, len, value);
                    }
                }
                if (maxrows > 0 && len >= maxrows)
                    break;
            }
        } else {
            outer: while (results.hasMoreElements()) {
                SearchResult resultRow = (SearchResult) results.next();
                if (row++ < startrow)
                    continue;
                Attributes attributesRow = resultRow.getAttributes();
                NamingEnumeration rowEnum = attributesRow.getIDs();
                while (rowEnum.hasMoreElements()) {
                    int len = qry.addRow();
                    String name = Caster.toString(rowEnum.next());
                    Object value = null;
                    try {
                        value = attributesRow.get(name).get();
                    } catch (Exception e) {
                    }
                    qry.setAtEL("name", len, name);
                    qry.setAtEL("value", len, value);
                    if (maxrows > 0 && len >= maxrows)
                        break outer;
                }
                qry.setAtEL("name", qry.size(), "dn");
            }
        }
    } finally {
        context.close();
    }
    // Sort
    if (sort != null && sort.length > 0) {
        int order = sortDirection == SORT_DIRECTION_ASC ? Query.ORDER_ASC : Query.ORDER_DESC;
        for (int i = sort.length - 1; i >= 0; i--) {
            String item = sort[i];
            if (item.indexOf(' ') != -1)
                item = ListUtil.first(item, " ", true);
            qry.sort(KeyImpl.getInstance(item), order);
        // keys[i] = new SortKey(item);
        }
    }
    return qry;
}
Also used : Enumeration(java.util.Enumeration) NamingEnumeration(javax.naming.NamingEnumeration) Query(lucee.runtime.type.Query) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) ClassException(lucee.commons.lang.ClassException) NamingException(javax.naming.NamingException) PageException(lucee.runtime.exp.PageException) IOException(java.io.IOException) QueryImpl(lucee.runtime.type.QueryImpl) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Collection(lucee.runtime.type.Collection) SearchControls(javax.naming.directory.SearchControls)

Example 2 with Query

use of lucee.runtime.type.Query in project Lucee by lucee.

the class AxisCaster method toLuceeType.

public static Object toLuceeType(PageContext pc, String customType, Object value) throws PageException {
    pc = ThreadLocalPageContext.get(pc);
    if (pc != null && value instanceof Pojo) {
        if (!StringUtil.isEmpty(customType)) {
            Component cfc = toComponent(pc, (Pojo) value, customType, null);
            if (cfc != null)
                return cfc;
        }
    /*
    		// try package/class name as component name
    		String compPath=value.getClass().getName();
    		Component cfc = toComponent(pc, (Pojo)value, compPath, null);
    		if(cfc!=null) return cfc;
    		
    		// try class name as component name
    		compPath=ListUtil.last(compPath, '.');
    		cfc = toComponent(pc, (Pojo)value, compPath, null);
    		if(cfc!=null) return cfc;
    		*/
    }
    if (value instanceof Date || value instanceof Calendar) {
        // do not change to caster.isDate
        return Caster.toDate(value, null);
    }
    if (value instanceof Object[]) {
        Object[] arr = (Object[]) value;
        if (!ArrayUtil.isEmpty(arr)) {
            boolean allTheSame = true;
            // byte
            if (arr[0] instanceof Byte) {
                for (int i = 1; i < arr.length; i++) {
                    if (!(arr[i] instanceof Byte)) {
                        allTheSame = false;
                        break;
                    }
                }
                if (allTheSame) {
                    byte[] bytes = new byte[arr.length];
                    for (int i = 0; i < arr.length; i++) {
                        bytes[i] = Caster.toByteValue(arr[i]);
                    }
                    return bytes;
                }
            }
        }
    }
    if (value instanceof Byte[]) {
        Byte[] arr = (Byte[]) value;
        if (!ArrayUtil.isEmpty(arr)) {
            byte[] bytes = new byte[arr.length];
            for (int i = 0; i < arr.length; i++) {
                bytes[i] = arr[i].byteValue();
            }
            return bytes;
        }
    }
    if (value instanceof byte[]) {
        return value;
    }
    if (Decision.isArray(value)) {
        Array a = Caster.toArray(value);
        int len = a.size();
        Object o;
        String ct;
        for (int i = 1; i <= len; i++) {
            o = a.get(i, null);
            if (o != null) {
                ct = customType != null && customType.endsWith("[]") ? customType.substring(0, customType.length() - 2) : null;
                a.setEL(i, toLuceeType(pc, ct, o));
            }
        }
        return a;
    }
    if (value instanceof Map) {
        Struct sct = new StructImpl();
        Iterator it = ((Map) value).entrySet().iterator();
        Map.Entry entry;
        while (it.hasNext()) {
            entry = (Entry) it.next();
            sct.setEL(Caster.toString(entry.getKey()), toLuceeType(pc, null, entry.getValue()));
        }
        return sct;
    // return StructUtil.copyToStruct((Map)value);
    }
    if (isQueryBean(value)) {
        QueryBean qb = (QueryBean) value;
        String[] strColumns = qb.getColumnList();
        Object[][] data = qb.getData();
        int recorcount = data.length;
        Query qry = new QueryImpl(strColumns, recorcount, "QueryBean");
        QueryColumn[] columns = new QueryColumn[strColumns.length];
        for (int i = 0; i < columns.length; i++) {
            columns[i] = qry.getColumn(strColumns[i]);
        }
        int row;
        for (row = 1; row <= recorcount; row++) {
            for (int i = 0; i < columns.length; i++) {
                columns[i].set(row, toLuceeType(pc, null, data[row - 1][i]));
            }
        }
        return qry;
    }
    if (Decision.isQuery(value)) {
        Query q = Caster.toQuery(value);
        int recorcount = q.getRecordcount();
        String[] strColumns = q.getColumns();
        QueryColumn col;
        int row;
        for (int i = 0; i < strColumns.length; i++) {
            col = q.getColumn(strColumns[i]);
            for (row = 1; row <= recorcount; row++) {
                col.set(row, toLuceeType(pc, null, col.get(row, null)));
            }
        }
        return q;
    }
    return value;
}
Also used : Entry(java.util.Map.Entry) Query(lucee.runtime.type.Query) Struct(lucee.runtime.type.Struct) QueryBean(coldfusion.xml.rpc.QueryBean) QueryImpl(lucee.runtime.type.QueryImpl) Iterator(java.util.Iterator) Component(lucee.runtime.Component) Calendar(java.util.Calendar) Date(java.util.Date) Array(lucee.runtime.type.Array) StructImpl(lucee.runtime.type.StructImpl) QueryColumn(lucee.runtime.type.QueryColumn) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with Query

use of lucee.runtime.type.Query in project Lucee by lucee.

the class AxisCaster method toQueryBean.

private static QueryBean toQueryBean(TypeMapping tm, Object value, Set<Object> done) throws PageException {
    Query query = Caster.toQuery(value);
    int recordcount = query.getRecordcount();
    String[] columnList = query.getColumns();
    QueryColumn[] columns = new QueryColumn[columnList.length];
    Object[][] data = new Object[recordcount][columnList.length];
    for (int i = 0; i < columnList.length; i++) {
        columns[i] = query.getColumn(columnList[i]);
    }
    int row;
    for (row = 1; row <= recordcount; row++) {
        for (int i = 0; i < columns.length; i++) {
            data[row - 1][i] = _toAxisType(tm, null, null, null, null, columns[i].get(row, null), done);
        }
    }
    QueryBean qb = new QueryBean();
    qb.setColumnList(columnList);
    qb.setData(data);
    return qb;
}
Also used : Query(lucee.runtime.type.Query) QueryColumn(lucee.runtime.type.QueryColumn) QueryBean(coldfusion.xml.rpc.QueryBean)

Example 4 with Query

use of lucee.runtime.type.Query in project Lucee by lucee.

the class Admin method doGetCustomTagMappings.

/**
 * @throws PageException
 */
private void doGetCustomTagMappings() throws PageException {
    Mapping[] mappings = config.getCustomTagMappings();
    lucee.runtime.type.Query qry = new QueryImpl(new String[] { "archive", "strarchive", "physical", "strphysical", "virtual", "hidden", "physicalFirst", "readonly", "inspect" }, mappings.length, "query");
    for (int i = 0; i < mappings.length; i++) {
        MappingImpl m = (MappingImpl) mappings[i];
        int row = i + 1;
        qry.setAt("archive", row, m.getArchive());
        qry.setAt("strarchive", row, m.getStrArchive());
        qry.setAt("physical", row, m.getPhysical());
        qry.setAt("strphysical", row, m.getStrPhysical());
        qry.setAt("virtual", row, m.getVirtual());
        qry.setAt("hidden", row, Caster.toBoolean(m.isHidden()));
        qry.setAt("physicalFirst", row, Caster.toBoolean(m.isPhysicalFirst()));
        qry.setAt("readonly", row, Caster.toBoolean(m.isReadonly()));
        qry.setAt("inspect", row, ConfigWebUtil.inspectTemplate(m.getInspectTemplateRaw(), ""));
    }
    pageContext.setVariable(getString("admin", action, "returnVariable"), qry);
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) Mapping(lucee.runtime.Mapping) MappingImpl(lucee.runtime.MappingImpl) Query(lucee.runtime.type.Query)

Example 5 with Query

use of lucee.runtime.type.Query in project Lucee by lucee.

the class Admin method doGetLocalExtensions.

private void doGetLocalExtensions() throws PageException {
    List<RHExtension> locals = RHExtension.toRHExtensions(DeployHandler.getLocalExtensions(config));
    Query qry = RHExtension.toQuery(config, locals.toArray(new RHExtension[locals.size()]));
    pageContext.setVariable(getString("admin", action, "returnVariable"), qry);
}
Also used : RHExtension(lucee.runtime.extension.RHExtension) Query(lucee.runtime.type.Query)

Aggregations

Query (lucee.runtime.type.Query)82 QueryImpl (lucee.runtime.type.QueryImpl)52 Struct (lucee.runtime.type.Struct)19 Array (lucee.runtime.type.Array)16 Collection (lucee.runtime.type.Collection)14 Iterator (java.util.Iterator)13 PageException (lucee.runtime.exp.PageException)12 Map (java.util.Map)11 StructImpl (lucee.runtime.type.StructImpl)11 ApplicationException (lucee.runtime.exp.ApplicationException)10 Stopwatch (lucee.runtime.timer.Stopwatch)10 Key (lucee.runtime.type.Collection.Key)9 List (java.util.List)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 QueryColumn (lucee.runtime.type.QueryColumn)7 Enumeration (java.util.Enumeration)6 Entry (java.util.Map.Entry)6 FunctionException (lucee.runtime.exp.FunctionException)6