Search in sources :

Example 1 with SortKey

use of javax.naming.ldap.SortKey in project teiid by teiid.

the class IQueryToLdapSearchParser method getSortKeysFromOrderByClause.

/**
 * get SortKeys from the supplied ORDERBY clause.
 * @param orderBy the OrderBy clause
 * @param the array of SortKeys
 */
private SortKey[] getSortKeysFromOrderByClause(OrderBy orderBy) throws TranslatorException {
    SortKey[] sortKeys = null;
    if (orderBy != null) {
        List<SortSpecification> orderItems = orderBy.getSortSpecifications();
        if (orderItems == null) {
            return null;
        }
        SortKey sortKey = null;
        sortKeys = new SortKey[orderItems.size()];
        Iterator<SortSpecification> orderItr = orderItems.iterator();
        int i = 0;
        while (orderItr.hasNext()) {
            SortSpecification item = orderItr.next();
            String itemName = getExpressionQueryString(item.getExpression());
            // $NON-NLS-1$
            LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Adding sort key for item:", itemName);
            if (item.getOrdering() == Ordering.ASC) {
                // $NON-NLS-1$
                LogManager.logTrace(LogConstants.CTX_CONNECTOR, "with ASC ordering.");
                sortKey = new SortKey(itemName, true, null);
            } else if (item.getOrdering() == Ordering.DESC) {
                // $NON-NLS-1$
                LogManager.logTrace(LogConstants.CTX_CONNECTOR, "with DESC ordering.");
                sortKey = new SortKey(itemName, false, null);
            }
            sortKeys[i] = sortKey;
            i++;
        }
    } else {
    // Insert a default? No, allow the Execution to do this. Just return a null list.
    }
    return sortKeys;
}
Also used : SortKey(javax.naming.ldap.SortKey)

Example 2 with SortKey

use of javax.naming.ldap.SortKey in project teiid by teiid.

the class IQueryToLdapSearchParser method translateSQLQueryToLDAPSearch.

/**
 * Public entry point to the parser.
 *  Parses the IQuery object, and constructs an equivalent LDAP search filter,
 *  keeping track of the attributes of interest.
 *  Here are some example SQL queries, and the equivalent LDAP search info:
 *  SQL: select cn, managerName from people_table where managerName LIKE "John%" and cn!="Mar()"
 *  Context name: [people_table's NameInSource, e.g. (ou=people,dc=company,dc=com)]
 *  LDAP attributes: (cn, String), (managerName, String)
 *  LDAP search filter: (&(managerName="John*")(!(cn="Mar\(\)")))
 *
 *  @param query the query
 *  @return the LDAPSearchDetails object
 */
// GHH 20080326 - added ability to restrict queries to only values where
// objectClass = table name.  This is done by adding a third parameter,
// RESTRICT, to the NameInSource property in the model:
// ou=people,dc=company,dc=com?SUBTREE_SCOPE?RESTRICT
// TODO - change method for calling RESTRICT to also specify
// object class name (RESTRICT=inetOrgPerson)
public LDAPSearchDetails translateSQLQueryToLDAPSearch(Select query) throws TranslatorException {
    // Parse SELECT symbols.
    // The columns will be translated into LDAP attributes of interest.
    ArrayList<Column> elementList = getElementsFromSelectSymbols(query);
    // Parse FROM table.
    // Only one table is expected here.
    List<TableReference> fromList = query.getFrom();
    Iterator<TableReference> itr = fromList.iterator();
    if (!itr.hasNext()) {
        // $NON-NLS-1$
        final String msg = LDAPPlugin.Util.getString("IQueryToLdapSearchParser.noTablesInFromError");
        throw new TranslatorException(msg);
    }
    TableReference fItm = itr.next();
    if (itr.hasNext()) {
        // $NON-NLS-1$
        final String msg = LDAPPlugin.Util.getString("IQueryToLdapSearchParser.multiItemsInFromError");
        throw new TranslatorException(msg);
    }
    LDAPSearchDetails sd = null;
    NamedTable tbl = null;
    NamedTable tblRight = null;
    if (fItm instanceof NamedTable) {
        tbl = (NamedTable) fItm;
    } else if (fItm instanceof Join) {
        Join join = (Join) fItm;
        if (!(join.getLeftItem() instanceof NamedTable) || !(join.getRightItem() instanceof NamedTable)) {
            // should not happen
            // $NON-NLS-1$
            final String msg = LDAPPlugin.Util.getString("IQueryToLdapSearchParser.groupCountExceededError");
            throw new TranslatorException(msg);
        }
        tbl = (NamedTable) join.getLeftItem();
        tblRight = (NamedTable) join.getRightItem();
    } else {
        // $NON-NLS-1$
        throw new AssertionError("Unsupported construct");
    }
    String contextName = getContextNameFromFromItem(tbl);
    int searchScope = getSearchScopeFromFromItem(tbl);
    // GHH 20080326 - added check for RESTRICT parameter in
    // NameInSource of from item
    String classRestriction = getRestrictToNamedClass(tbl);
    if (tblRight != null) {
        String contextName1 = getContextNameFromFromItem(tblRight);
        int searchScope1 = getSearchScopeFromFromItem(tblRight);
        String classRestriction1 = getRestrictToNamedClass(tblRight);
        if (!EquivalenceUtil.areEqual(contextName, contextName1) || searchScope != searchScope1 || !EquivalenceUtil.areEqual(classRestriction, classRestriction1)) {
            // $NON-NLS-1$
            final String msg = LDAPPlugin.Util.getString("IQueryToLdapSearchParser.not_same", tbl.getMetadataObject().getFullName(), tblRight.getMetadataObject().getFullName());
            throw new TranslatorException(msg);
        }
    }
    // Parse the WHERE clause.
    // Create an equivalent LDAP search filter.
    List<String> searchStringList = new LinkedList<String>();
    searchStringList = getSearchFilterFromWhereClause(query.getWhere(), searchStringList);
    StringBuilder filterBuilder = new StringBuilder();
    for (String string : searchStringList) {
        filterBuilder.append(string);
    }
    // add it to the search filter
    if (classRestriction != null && classRestriction.trim().length() > 0) {
        // $NON-NLS-1$  //$NON-NLS-2$  //$NON-NLS-3$
        filterBuilder.insert(0, "(&").append("(objectClass=").append(classRestriction).append("))");
    }
    // Parse the ORDER BY clause.
    // Create an ordered sort list.
    OrderBy orderBy = query.getOrderBy();
    // Referenced the JNDI standard...arguably, this should not be done inside this
    // class, and we should make our own key class. In practice, this makes things simpler.
    SortKey[] sortKeys = getSortKeysFromOrderByClause(orderBy);
    // Parse LIMIT clause.
    // Note that offsets are not supported.
    Limit limit = query.getLimit();
    long countLimit = -1;
    if (limit != null) {
        countLimit = limit.getRowLimit();
    }
    // Create Search Details
    sd = new LDAPSearchDetails(contextName, searchScope, filterBuilder.toString(), sortKeys, countLimit, elementList, 0);
    // Search Details logging
    sd.printDetailsToLog();
    return sd;
}
Also used : SortKey(javax.naming.ldap.SortKey) LinkedList(java.util.LinkedList) Column(org.teiid.metadata.Column) TranslatorException(org.teiid.translator.TranslatorException)

Example 3 with SortKey

use of javax.naming.ldap.SortKey in project teiid by teiid.

the class LDAPQueryExecution method setRequestControls.

/**
 * Set the standard request controls
 */
private void setRequestControls(byte[] cookie) throws TranslatorException {
    List<Control> ctrl = new ArrayList<Control>();
    SortKey[] keys = searchDetails.getSortKeys();
    try {
        if (keys != null) {
            ctrl.add(new SortControl(keys, Control.NONCRITICAL));
        }
        if (this.executionFactory.usePagination()) {
            ctrl.add(new PagedResultsControl(this.executionContext.getBatchSize(), cookie, Control.CRITICAL));
        }
        if (!ctrl.isEmpty()) {
            this.ldapCtx.setRequestControls(ctrl.toArray(new Control[ctrl.size()]));
            // $NON-NLS-1$
            LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Sort/pagination controls were created successfully.");
        }
    } catch (NamingException ne) {
        final String msg = // $NON-NLS-1$
        LDAPPlugin.Util.getString("LDAPSyncQueryExecution.setControlsError") + " : " + // $NON-NLS-1$
        ne.getExplanation();
        throw new TranslatorException(ne, msg);
    } catch (IOException e) {
        throw new TranslatorException(e);
    }
}
Also used : SortControl(javax.naming.ldap.SortControl) Control(javax.naming.ldap.Control) SortControl(javax.naming.ldap.SortControl) PagedResultsControl(javax.naming.ldap.PagedResultsControl) PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) ArrayList(java.util.ArrayList) SortKey(javax.naming.ldap.SortKey) NamingException(javax.naming.NamingException) TranslatorException(org.teiid.translator.TranslatorException) IOException(java.io.IOException) PagedResultsControl(javax.naming.ldap.PagedResultsControl)

Aggregations

SortKey (javax.naming.ldap.SortKey)3 TranslatorException (org.teiid.translator.TranslatorException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 NamingException (javax.naming.NamingException)1 Control (javax.naming.ldap.Control)1 PagedResultsControl (javax.naming.ldap.PagedResultsControl)1 PagedResultsResponseControl (javax.naming.ldap.PagedResultsResponseControl)1 SortControl (javax.naming.ldap.SortControl)1 Column (org.teiid.metadata.Column)1