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: SearchTemplate.java,v 1.3 2008/06/25 05:41:46 qcheng Exp $ 026 * 027 */ 028 029package com.iplanet.ums; 030 031import com.iplanet.services.ldap.Attr; 032import com.iplanet.services.ldap.AttrSet; 033 034/** 035 * Represents templates for searching functionality. SearchTemplate serves the 036 * purpose of defining guidelines in a search. It defines the search filter and 037 * attributes to be returned in a search query. Reusability and flexibility are 038 * serving goals in SearchTemplate. 039 * <P> 040 * 041 * @see Template 042 * @see CreationTemplate 043 * @supported.api 044 */ 045public class SearchTemplate extends Template { 046 /** 047 * Default constructor for deserialization 048 * 049 * @supported.api 050 */ 051 public SearchTemplate() { 052 super(); 053 } 054 055 /** 056 * Creates a template with an attribute set and a search filter. The 057 * Attribute set contains attributes to be returned on a search. If the 058 * search filter is null, then "objectclass=*" is assumed (return all 059 * objects). 060 * 061 * @param name 062 * Template name 063 * @param attrSet 064 * set of attributes 065 * @param filter 066 * search filter 067 */ 068 public SearchTemplate(String name, AttrSet attrSet, String filter) { 069 super(name); 070 setAttributeSet(attrSet); 071 setSearchFilter(filter); 072 } 073 074 /** 075 * Creates a template with an array of attributes and a search filter. The 076 * array of attributes contains attributes to be returned on a search. If 077 * the search filter is null, then "objectclass=*" is assumed (return all 078 * objects). 079 * 080 * @param name 081 * Template name 082 * @param attributeNames 083 * an array of attribute names 084 * @param filter 085 * search filter 086 * 087 * @supported.api 088 */ 089 public SearchTemplate(String name, String[] attributeNames, String filter) { 090 super(name); 091 setAttributeNames(attributeNames); 092 setSearchFilter(filter); 093 } 094 095 /** 096 * Sets the filter expression used to search for objects of this type, for 097 * example, "objectclass=inetorgperson" or 098 * "(&(objectclass=inetorgperson)(ou=accounting))" 099 * 100 * @param filter 101 * A UMS search expression (LDAP syntax) 102 * 103 * @supported.api 104 */ 105 public void setSearchFilter(String filter) { 106 m_searchFilter = (filter != null) ? filter : "objectclass=*"; 107 } 108 109 /** 110 * Gets the filter expression used to search for objects of this type. 111 * 112 * @return a UMS search expression (LDAP syntax) 113 * 114 * @supported.api 115 */ 116 public String getSearchFilter() { 117 return m_searchFilter; 118 } 119 120 /** 121 * Sets the attributes to be returned on a search. 122 * 123 * @param attributeNames 124 * The attribute names to be returned 125 * 126 * @supported.api 127 */ 128 public void setAttributeNames(String[] attributeNames) { 129 if (attributeNames != null) { 130 m_attrSet = new AttrSet(); 131 addAttributeNames(attributeNames); 132 } 133 } 134 135 /** 136 * Adds the attribute name to the list of attributes to be returned on a 137 * search. 138 * 139 * @param attributeName 140 * The attribute name to be added 141 * 142 * @supported.api 143 */ 144 public void addAttributeName(String attributeName) { 145 if (attributeName != null) { 146 if (m_attrSet == null) { 147 m_attrSet = new AttrSet(); 148 } 149 m_attrSet.add(new Attr(attributeName)); 150 } 151 } 152 153 /** 154 * Adds the attribute names to the list of attributes to be returned on a 155 * search. 156 * 157 * @param attributeNames 158 * The attribute names to be added 159 * 160 * @supported.api 161 */ 162 public void addAttributeNames(String[] attributeNames) { 163 if (attributeNames != null) { 164 for (int i = 0; i < attributeNames.length; i++) { 165 addAttributeName(attributeNames[i]); 166 } 167 } 168 } 169 170 /** 171 * Removes the attribute name from the list of attributes to be returned on 172 * a search. 173 * 174 * @param attributeName 175 * The attribute name to be removed 176 * 177 * @supported.api 178 */ 179 public void removeAttributeName(String attributeName) { 180 if (attributeName != null && m_attrSet != null) { 181 m_attrSet.remove(attributeName); 182 } 183 } 184 185 /** 186 * Removes the attribute names from the list of attributes to be returned on 187 * a search. 188 * 189 * @param attributeNames 190 * The attribute names to be removed 191 * 192 * @supported.api 193 */ 194 public void removeAttributeNames(String[] attributeNames) { 195 if (attributeNames != null && m_attrSet != null) { 196 for (int i = 0; i < attributeNames.length; i++) { 197 removeAttributeName(attributeNames[i]); 198 } 199 } 200 } 201 202 /** 203 * Gets a list of attribute names defined in the object. 204 * 205 * @return Names of all attributes defined 206 * 207 * @supported.api 208 */ 209 public String[] getAttributeNames() { 210 return (m_attrSet == null) ? new String[0] : m_attrSet 211 .getAttributeNames(); 212 } 213 214 /** 215 * Gets the attributes to be returned on a search. 216 * 217 * @return set of attributes to be returned on a search 218 */ 219 AttrSet getAttributeSet() { 220 return m_attrSet; 221 } 222 223 /** 224 * Sets the attributes to be returned on a search. 225 * 226 * @param attrSet 227 * set of attributes 228 */ 229 void setAttributeSet(AttrSet attrSet) { 230 // ??? Should we clone attrSet instead of keeping a reference? 231 m_attrSet = attrSet; 232 } 233 234 /** 235 * Returns a copy of the template. 236 * 237 * @return A copy of the Template 238 * 239 * @supported.api 240 */ 241 public Object clone() { 242 SearchTemplate t = (SearchTemplate) super.clone(); 243 if (m_attrSet != null) { 244 t.setAttributeSet((AttrSet) m_attrSet.clone()); 245 } 246 if (m_searchFilter != null) { 247 t.setSearchFilter(m_searchFilter); 248 } 249 return t; 250 } 251 252 /** 253 * Render the object. 254 * 255 * @return The object in printable form 256 * 257 * @supported.api 258 */ 259 public String toString() { 260 return "SearchTemplate: " + getName() + " { " + m_attrSet + " }"; 261 } 262 263 private AttrSet m_attrSet = null; 264 265 private String m_searchFilter = null; 266}
Copyright © 2010-2017, ForgeRock All Rights Reserved.