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