001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: IdSearchResults.java,v 1.4 2008/06/25 05:43:29 qcheng Exp $
026 *
027 */
028
029/*
030 * Portions Copyrighted [2011] [ForgeRock AS]
031 */
032package com.sun.identity.idm;
033
034import java.util.HashMap;
035import java.util.HashSet;
036import java.util.Map;
037import java.util.Set;
038
039/**
040 * This class <code>IdSearchResults</code> provides to obtain the search
041 * results.
042 *
043 * @supported.all.api
044 */
045public class IdSearchResults {
046
047    /**
048     * Code used to indicate a successful search
049     */
050    public static final int SUCCESS = 0;
051
052    /**
053     * Code used to indicate that the search was unsuccessful as the size limit
054     * exceeded during the search process.
055     */
056    public static final int SIZE_LIMIT_EXCEEDED = 1;
057
058    /**
059     * Code used to indicate that the search was unsuccessful as the time limit
060     * exceeded during the search process.
061     */
062    public static final int TIME_LIMIT_EXCEEDED = 2;
063
064    // Ordered set contain the result identities
065    protected Set searchResults = new HashSet(); 
066    
067    // Contains result of identities and the attributes requested as part
068    // of the search
069    protected Map resultsMap = new HashMap(); 
070                                                
071    protected int errorCode = SUCCESS;
072
073    private IdType searchType;
074
075    private String org;
076
077    public IdSearchResults(IdType type, String orgName) {
078        searchType = type;
079        org = orgName;
080    }
081
082    /**
083     * Method which returns the search results as a map containing AMIdentity
084     * objects as key and the attribute value String. The attribute value is a
085     * Set.
086     * 
087     * @return Map containing AMIdentity objects as the key and Maps of
088     *         attribute-valuesof the attributes specified as part of the
089     *         search. The Maps contains attribute names as keys and Set
090     *         containing values of those attributes. Returns an empty Map if no
091     *         attributes were specified as part of search request.
092     */
093    public Map getResultAttributes() {
094        return resultsMap;
095    }
096
097    /**
098     * Method which returns the search results as an ordered set.
099     * 
100     * @return Set of AMIdentity objects matching the search criteria
101     */
102    public Set getSearchResults() {
103        // return convertToIdentityObjects();
104        return searchResults;
105    }
106
107    /**
108     * Method which returns the error code of search.
109     * 
110     * @return Error code of search. The possible values are
111     *         <code>SUCCESS</code>, <code>SIZE_LIMIT_EXCEEDED</code> and
112     *         <code>TIME_LIMIT_EXCEEDED</code>
113     * @see #SUCCESS
114     * @see #SIZE_LIMIT_EXCEEDED
115     * @see #TIME_LIMIT_EXCEEDED
116     */
117    public int getErrorCode() {
118        return errorCode;
119    }
120
121    /**
122     * Adds an AMIdentity object to this search result.
123     * 
124     * @param id
125     *            AMIdentity representing the entity.
126     * @param attrs
127     *            Map of attrbibutes obtained while performing the search
128     */
129    public void addResult(AMIdentity id, Map attrs) {
130        searchResults.add(id);
131        resultsMap.put(id, attrs);
132    }
133
134    /**
135     * Set the error code for this Search Result
136     * 
137     * @param error
138     *            Error code of Search Result.
139     * @see #SUCCESS
140     * @see #SIZE_LIMIT_EXCEEDED
141     * @see #TIME_LIMIT_EXCEEDED
142     * 
143     */
144    public void setErrorCode(int error) {
145        errorCode = error;
146    }
147
148    protected IdType getType() {
149        return searchType;
150    }
151
152    protected String getOrgName() {
153        return org;
154    }
155    
156    /**
157     * Returns String representation of the <code>IdSearchResults</code> object.
158     * It returns identity names and attributes
159     *
160     * @return String representation of the <code>ServiceConfig</code> object.
161     */
162    public String toString() {
163        StringBuilder sb = new StringBuilder(200);
164        sb.append("IdSearchResults:");
165        sb.append("\n\tIdentities: ").append(searchResults);
166        sb.append("\n\tAttributes: ").append(resultsMap);
167        return (sb.toString());
168    }
169}