001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.externaldata.data.ldap;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Map;
023
024import javax.naming.NamingException;
025import javax.naming.directory.Attribute;
026import javax.naming.directory.Attributes;
027import javax.naming.directory.SearchResult;
028
029import org.ametys.plugins.externaldata.data.DataInclusionException;
030import org.ametys.plugins.externaldata.data.QueryResult;
031import org.ametys.plugins.externaldata.data.QueryResultRow;
032import org.ametys.plugins.externaldata.data.Query.ResultType;
033
034/**
035 * LDAP query result.
036 */
037public class LdapQueryResult implements QueryResult
038{
039    
040    /** The result type. */
041    protected ResultType _resultType;
042    
043    /** The result rows. */
044    protected List<QueryResultRow> _rows;
045    
046    /** The column names (ordered) */
047    protected List<String> _columnNames;
048    
049    /** The attribute labels. */
050    protected Map<String, String> _attributes;
051
052    /**
053     * Constructs a query result around a ResultSet.
054     * @param results the list of search results
055     * @param columnNames the names of the columns
056     * @param attributes the attributes
057     */
058    public LdapQueryResult(List<SearchResult> results, List<String> columnNames, Map<String, String> attributes)
059    {
060        try
061        {
062            _columnNames = columnNames;
063            _attributes = attributes;
064            _extractResults(results, columnNames, attributes);
065        }
066        catch (NamingException e)
067        {
068            throw new RuntimeException(e);
069        }
070    }
071    
072    @Override
073    public ResultType getType()
074    {
075        return _resultType;
076    }
077    
078    /**
079     * Set the result type.
080     * @param resultType the result type.
081     */
082    public void setType(ResultType resultType)
083    {
084        this._resultType = resultType;
085    }
086    
087    @Override
088    public int getSize()
089    {
090        return _rows.size();
091    }
092    
093    @Override
094    public Collection<String> getColumnNames() throws DataInclusionException
095    {
096        return _columnNames;
097    }
098    
099    @Override
100    public Iterator<QueryResultRow> iterator()
101    {
102        return _rows.iterator();
103    }
104    
105    @Override
106    public void close()
107    {
108        // Does nothing.
109    }
110    
111    /**
112     * Extract the column names and results from a LDAP naming enumeration.
113     * @param results the LDAP naming enumeration.
114     * @param columnNames the names of the columns
115     * @param attributes the attributes
116     * @throws NamingException if something goes wrong when manipulating the context
117     */
118    protected void _extractResults(List<SearchResult> results, List<String> columnNames, Map<String, String> attributes) throws NamingException
119    {
120        // Extract the results.
121        _rows = new ArrayList<>();
122        
123        for (SearchResult result : results)
124        {
125            // Get attributes
126            Attributes attrs = result.getAttributes();
127            
128            LdapQueryResultRow row = new LdapQueryResultRow();
129            
130            for (String colName : _columnNames)
131            {
132                String attrId = attributes.get(colName);
133                Attribute attrValue = attrs.get(attrId);
134                if (attrValue != null)
135                {
136                    row.put(colName, (String) attrValue.get());
137                }
138            }
139            
140            _rows.add(row);
141        }
142    }
143    
144}