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: DirectCOSDefinition.java,v 1.3 2008/06/25 05:41:47 qcheng Exp $ 026 * 027 */ 028 029/** 030 * Portions Copyrighted [2011] [ForgeRock AS] 031 */ 032package com.iplanet.ums.cos; 033 034import java.util.ArrayList; 035import java.util.Collection; 036 037import com.iplanet.services.ldap.Attr; 038import com.iplanet.services.ldap.AttrSet; 039import com.iplanet.services.ldap.ModSet; 040import com.iplanet.services.util.I18n; 041import com.iplanet.ums.CreationTemplate; 042import com.iplanet.ums.Guid; 043import com.iplanet.ums.IUMSConstants; 044import com.iplanet.ums.PersistentObject; 045import com.iplanet.ums.SearchResults; 046import com.iplanet.ums.TemplateManager; 047import com.iplanet.ums.UMSException; 048 049/** 050 * This class represents a Direct (or Classic) COS definition. 051 * @supported.api 052 */ 053public class DirectCOSDefinition extends PersistentObject implements 054 ICOSDefinition { 055 056 /** 057 * NoArg Constructor 058 */ 059 public DirectCOSDefinition() { 060 } 061 062 /** 063 * Constructor with attribute set argument. The attribute set needs to 064 * contain all the required attributes for this definition: name, 065 * cosspecifier, cosattribute (with qualifier). 066 * 067 * @param attrSet 068 * the attribute set 069 * 070 * @throws UMSException 071 * The exception thrown from the DirectCOSDefinition constructor 072 * accepting a creation template and attribute set. 073 * @see com.iplanet.ums.cos.DirectCOSDefinition#DirectCOSDefinition 074 * (CreationTemplate, AttrSet) 075 * @supported.api 076 */ 077 public DirectCOSDefinition(AttrSet attrSet) throws UMSException { 078 this(TemplateManager.getTemplateManager().getCreationTemplate(_class, 079 null), attrSet); 080 } 081 082 /** 083 * Constructor with creation template and attribute set arguments. 084 * 085 * @param template 086 * the Creation template. 087 * @param attrSet 088 * the attribute set 089 * 090 * @throws UMSException 091 * The exception thrown from the parent class constructor. 092 * @see com.iplanet.ums.PersistentObject#PersistentObject (CreationTemplate, 093 * AttrSet) 094 * @supported.api 095 */ 096 public DirectCOSDefinition(CreationTemplate template, AttrSet attrSet) 097 throws UMSException { 098 super(template, attrSet); 099 } 100 101 /** 102 * Sets the name of this COS. 103 * 104 * @param name 105 * the name of this COS. 106 * @supported.api 107 */ 108 public void setName(String name) { 109 setAttribute(new Attr(ICOSDefinition.DEFAULT_NAMING_ATTR, name)); 110 } 111 112 /** 113 * Returns the name of this COS. 114 * 115 * @return the name of this COS 116 * @supported.api 117 */ 118 public String getName() { 119 String attributeValue = null; 120 Attr attribute = getAttribute(getNamingAttribute()); 121 if (attribute != null) { 122 attributeValue = attribute.getValue(); 123 } 124 return attributeValue; 125 } 126 127 /** 128 * Adds the COS attribute to the definition. The COS attribute is the name 129 * of the attribute for which you want to generate a value. 130 * 131 * @param attrName 132 * The name of the attribute (for example, mailQuota) 133 * @param qualifier 134 * An integer representing the following values: "default" - The 135 * server only returns a generated value if there is no 136 * corresponding attribute value stored with the entry. 137 * "override" - This value will always be generated by the server 138 * (it will override entry values). "operational" - the attribute 139 * will only be returned if it is requested in the search. 140 * "operational" can be combined with "default" or "override". 141 * These values are represented as integers in the ICOSDefinition 142 * interface. 143 * 144 * @throws UMSException 145 * The exception thrown from the data layer. 146 * @supported.api 147 */ 148 public void addCOSAttribute(String attrName, int qualifier) 149 throws UMSException { 150 StringBuilder attrStr = new StringBuilder(); 151 if (qualifier < ICOSDefinition.minQualifier 152 || qualifier > ICOSDefinition.maxQualifier) { 153 String msg = i18n.getString(IUMSConstants.BAD_COS_ATTR_QUALIFIER); 154 throw new UMSException(msg); 155 } 156 157 attrStr.append(attrName); 158 attrStr.append(" "); 159 attrStr.append(ICOSDefinition.qualifiers[qualifier]); 160 modify(ICOSDefinition.COSATTRIBUTE, attrStr.toString(), ModSet.ADD); 161 } 162 163 /** 164 * Removes the COS attribute from the definition. 165 * 166 * @param attrName 167 * The name of the attribute to be removed. 168 * @supported.api 169 */ 170 public void removeCOSAttribute(String attrName) { 171 modify(new Attr(ICOSDefinition.COSATTRIBUTE, attrName), ModSet.DELETE); 172 } 173 174 /** 175 * Retrieves the COS attributes for this definition. 176 * 177 * @return String[] A string array of COS attributes (for example, 178 * mailquota). 179 * @supported.api 180 */ 181 public String[] getCOSAttributes() { 182 Attr attr = getAttribute(ICOSDefinition.COSATTRIBUTE); 183 return attr.getStringValues(); 184 } 185 186 /** 187 * Sets the COS specifier. The COS specifier is the attribute value used in 188 * conjunction with the template entry's DN, to identify the template entry. 189 * 190 * @param cosSpecifier 191 * The COS specifier. 192 * @supported.api 193 */ 194 public void setCOSSpecifier(String cosSpecifier) { 195 setAttribute(new Attr(COSSPECIFIER, cosSpecifier)); 196 } 197 198 /** 199 * Returns the COS specifier. 200 * 201 * @return the COS specifier 202 * 203 * @see DirectCOSDefinition#setCOSSpecifier(String cosSpecifier) 204 * @supported.api 205 */ 206 public String getCOSSpecifier() { 207 String attributeValue = null; 208 Attr attribute = getAttribute(COSSPECIFIER); 209 if (attribute != null) { 210 attributeValue = attribute.getValue(); 211 } 212 return attributeValue; 213 } 214 215 /** 216 * Adds a COS Template to this COS definition. This COS definition must be 217 * persistent before adding the template. 218 * 219 * @param cosTemplate 220 * The COS Template to be added. 221 * 222 * @throws UMSException 223 * The exception thrown from the data layer. 224 * @supported.api 225 */ 226 public void addCOSTemplate(COSTemplate cosTemplate) throws UMSException { 227 if (getGuid() == null) { 228 String msg = i18n 229 .getString(IUMSConstants.DEFINITION_NOT_PERSISTENT); 230 throw new UMSException(msg); 231 } 232 233 if (getAttribute(ICOSDefinition.COSTEMPLATEDN) == null) { 234 this.modify(new Attr(ICOSDefinition.COSTEMPLATEDN, getGuid() 235 .getDn()), ModSet.ADD); 236 this.save(); 237 } 238 this.addChild(cosTemplate); 239 } 240 241 /** 242 * Removes a COS Template from this COS definition. 243 * 244 * @param name 245 * The name of the template to be removed. 246 * 247 * @throws UMSException 248 * The exception thrown from the data layer. 249 * @supported.api 250 */ 251 public void removeCOSTemplate(String name) throws UMSException { 252 Guid tGuid = new Guid(COSTemplate.DEFAULT_NAMING_ATTR + "=" + name 253 + "," + this.getGuid()); 254 this.removeChild(tGuid); 255 } 256 257 /** 258 * Removes all COS Templates from this COS definition. 259 * 260 * @throws UMSException 261 * The exception thrown from the data layer. 262 * @supported.api 263 */ 264 public void removeCOSTemplates() throws UMSException { 265 ArrayList aList = (ArrayList) getCOSTemplates(); 266 for (int i = 0; i < aList.size(); i++) { 267 COSTemplate cosTemplate = (COSTemplate) aList.get(i); 268 cosTemplate.remove(); 269 } 270 } 271 272 /** 273 * Returns a template from this definition given the name of the template. 274 * 275 * @param name 276 * The name of the template to be returned. 277 * 278 * @return The COS template. 279 * 280 * @throws COSNotFoundException 281 * The exception thrown if the COS template is not found. 282 * @throws UMSException 283 * The exception thrown from the data layer. 284 * @supported.api 285 */ 286 public COSTemplate getCOSTemplate(String name) throws COSNotFoundException, 287 UMSException { 288 COSTemplate cosTemplate = null; 289 String[] resultAttributes = { "*" }; 290 SearchResults sr = this.search("(" + COSTemplate.DEFAULT_NAMING_ATTR 291 + "=" + name + ")", resultAttributes, null); 292 while (sr.hasMoreElements()) { 293 cosTemplate = (COSTemplate) sr.next(); 294 sr.abandon(); 295 } 296 if (cosTemplate == null) { 297 String msg = i18n.getString(IUMSConstants.COS_TEMPLATE_NOT_FOUND); 298 throw new COSNotFoundException(msg); 299 } 300 return cosTemplate; 301 } 302 303 /** 304 * Returns all templates for this definition. 305 * 306 * @return a collection of COS templates 307 * 308 * @throws UMSException 309 * The exception thrown from the data layer. 310 * @supported.api 311 */ 312 public Collection getCOSTemplates() throws UMSException { 313 COSTemplate cosTemplate = null; 314 Collection cosTemplates = new ArrayList(); 315 String[] resultAttributes = { "*" }; 316 SearchResults sr = this.search("(objectclass=costemplate)", 317 resultAttributes, null); 318 while (sr.hasMoreElements()) { 319 cosTemplate = (COSTemplate) sr.next(); 320 cosTemplates.add(cosTemplate); 321 } 322 return cosTemplates; 323 } 324 325 private static final Class _class = 326 com.iplanet.ums.cos.DirectCOSDefinition.class; 327 328 private static I18n i18n = I18n.getInstance(IUMSConstants.UMS_PKG); 329}