bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: COSManager.java,v 1.5 2009/01/28 05:34:51 ww203982 Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington * Portions Copyright 2015 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.iplanet.ums.cos;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.ldap.Attr;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.ldap.AttrSet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.util.I18n;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOToken;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOTokenManager;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.ums.Guid;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.ums.IUMSConstants;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.ums.PersistentObject;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.ums.SchemaManager;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.ums.SearchResults;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.ums.UMSException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.ums.UMSObject;
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Maddenimport java.security.Principal;
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Maddenimport java.util.AbstractCollection;
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Maddenimport java.util.ArrayList;
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Maddenimport java.util.Arrays;
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Maddenimport java.util.Collection;
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Maddenimport java.util.Enumeration;
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Maddenimport java.util.HashSet;
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Maddenimport java.util.StringTokenizer;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.forgerock.opendj.ldap.DN;
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Maddenimport org.forgerock.opendj.ldap.LdapException;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.forgerock.opendj.ldap.Modification;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.forgerock.opendj.ldap.ModificationType;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.forgerock.opendj.ldap.ResultCode;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.forgerock.opendj.ldap.SearchScope;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This class has the responsibility of adding, removing and replacing COS
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * definitions. It also provides search capabilities for COS definitions.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class COSManager {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This constructor sets the parent Directory entry which identifies the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * location of COS definitions which will be managed. It also gets an
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * instance of a SchemaManager which will be used to update schema entries
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * for COS assignments.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param token Authenticated principal's single sign on token.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param guid The unique identifier specifying where COS definitions will
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * be managed.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException if the token authentication fails, or if
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the guid for the parent entry is not valid.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected COSManager(SSOToken token, Guid guid) throws UMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SSOTokenManager.getInstance().validateToken(token);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException se) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(i18n.getString(IUMSConstants.INVALID_TOKEN),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster se);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster _parentObject = UMSObject.getObject(token, guid);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster _schemaManager = SchemaManager.getSchemaManager(token
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getPrincipal());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException se) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException("Bad Authentication Token "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + se.getMessage());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This constructor sets the parent Directory entry which identifies the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * location of COS definitions which will be managed. It also gets an
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * instance of a SchemaManager which will be used to update schema entries
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * for COS assignments.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param principal
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Authenticated principal
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param guid
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The unique identifier specifying where COS definitions will be
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * managed.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The exception thrown if there is a problem determining the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * parent entry, or getting the SchemaManager instance.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected COSManager(Principal principal, Guid guid) throws UMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster _parentObject = UMSObject.getObject(principal, guid);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster _schemaManager = SchemaManager.getSchemaManager(principal);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This method returns an instance of a COS Manager.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param token Authenticated principal's single sign on token.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param guid COS definitions will be managed under the level identified by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this guid.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The exception thrown from the COSManager constructor.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static COSManager getCOSManager(SSOToken token, Guid guid)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws UMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return new COSManager(token, guid);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This method returns an instance of a COS Manager.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param principal Authenticated principal.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param guid COS definitions will be managed under the level identified by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this guid.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The exception thrown from the data layer.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static COSManager getCOSManager(Principal principal, Guid guid)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws UMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return new COSManager(principal, guid);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This method adds a COS definition to the persistent store. The definition
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * is added under the specified "guid" parameter.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cosDef
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The COS definition to be added.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The exception thrown from the data layer.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void addDefinition(ICOSDefinition cosDef) throws UMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!(cosDef instanceof DirectCOSDefinition)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n.getString(IUMSConstants.INVALID_COSDEFINITION);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] cosAttributes = cosDef.getCOSAttributes();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AbstractCollection aList = (AbstractCollection) Arrays
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .asList(ICOSDefinition.qualifiers);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; i < cosAttributes.length; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cosAttribute = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String qualifier = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringTokenizer st = new StringTokenizer(cosAttributes[i]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (st.hasMoreTokens()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosAttribute = st.nextToken();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cosAttribute == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster IUMSConstants.INVALID_COS_ATTRIBUTE_QUALIFIER);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (st.hasMoreTokens())
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster qualifier = st.nextToken();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (qualifier == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster qualifier = ICOSDefinition.qualifiers[ICOSDefinition.DEFAULT];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosDef.removeCOSAttribute(cosAttribute);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosDef.addCOSAttribute(cosAttribute, ICOSDefinition.DEFAULT);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!aList.contains(qualifier)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster IUMSConstants.INVALID_COS_ATTRIBUTE_QUALIFIER);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PersistentObject po = (PersistentObject) cosDef;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster _parentObject.addChild(po);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removes the COS definition.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The name of the definition to be removed.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The exception thrown from the data layer.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void removeDefinition(String name) throws UMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Guid guid = new Guid(ICOSDefinition.DEFAULT_NAMING_ATTR + "=" + name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "," + _parentObject.getGuid().getDn());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster _parentObject.removeChild(guid);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Updates the contents of a COS definition with the new contents. The COS
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * definition must already exist in the persistent layer, before its
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * contents can be replaced.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cosDef
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The COS definition containing new contents, which will replace
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the same definition in the persistent layer.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The exception thrown from the data layer.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void updateDefinition(ICOSDefinition cosDef) throws UMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PersistentObject pObject = (PersistentObject) cosDef;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (pObject.getGuid() == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getString(IUMSConstants.REPLACE_DEFINITION_NOT_PERSISTENT);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster pObject.save();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns COS definition given the name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name Name of the COS definition.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return A COS definition with the specified name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException if exception occurred at the data layer.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws COSNotFoundException if the COS object is not found.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public ICOSDefinition getDefinition(String name) throws UMSException,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster COSNotFoundException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ICOSDefinition cosDef = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SearchResults sr = _parentObject.getChildren(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ICOSDefinition.COSSUPERDEF_NAME_SEARCH + name + ")",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster DEF_ATTRIBUTE_NAMES, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (sr.hasMoreElements()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosDef = (ICOSDefinition) sr.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cosDef.getName().equals(name)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosDef = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cosDef == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n.getString(IUMSConstants.COS_DEFINITION_NOT_FOUND);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new COSNotFoundException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sr.abandon();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return cosDef;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Retrieves all COS definitions for the current organization. This
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * COSManager instance applies to an organization.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return A collection of COS definition objects.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The exception thrown from the data layer.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Collection getDefinitions() throws UMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Collection cosDefinitions = new ArrayList();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SearchResults sr = _parentObject.search(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ICOSDefinition.COSSUPERDEF_SEARCH, DEF_ATTRIBUTE_NAMES, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (sr.hasMoreElements()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosDefinitions.add(sr.next());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return cosDefinitions;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Assigns a COS (as defined by a COS definition) to the persistent object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The COS target persistent object could be a user, group, organization,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * organizationalunit, etc. The COS target object must be persistent before
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this method can be used.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param pObject
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The COS target persistent object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cosDef
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * A COS definition.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cosTemplate
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * A COS template. This only applies for COS and Indirect COS
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * definitions. For pointer COS definitions, this parameter can
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * be null.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If a data layer exception occurs.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void assignCOSDef(PersistentObject pObject, ICOSDefinition cosDef,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster COSTemplate cosTemplate) throws UMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (pObject == null || cosDef == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getString(IUMSConstants.COS_DEF_OR_TARGET_OBJECT_NULL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Do validation....
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (pObject.getGuid() == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getString(IUMSConstants.COS_TARGET_OBJECT_NOT_PERSISTENT);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!(cosDef instanceof DirectCOSDefinition)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n.getString(IUMSConstants.INVALID_COSDEFINITION);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cosDef instanceof DirectCOSDefinition) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster assignDirectCOSDef(pObject, (DirectCOSDefinition) cosDef,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosTemplate, _schemaManager);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removes COS assignment from the persistent object. The COS target
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * persistent object could be a user, group, organization,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * organizationalunit, etc. The COS target object must be persistent before
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this method can be used.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param pObject
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The COS target persistent object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cosDef
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * A COS definition.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cosTemplate
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * A COS template.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The exception thrown if any of the following occur: o the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * target persistent object or COS definition parameter is null.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * o the target object is not persistent. o the COS definition
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * is not one of the valid COS definitions. o an exception is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * propagated from any of the "remove" methods.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void removeCOSAssignment(PersistentObject pObject,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ICOSDefinition cosDef, COSTemplate cosTemplate) throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (pObject == null || cosDef == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getString(IUMSConstants.COS_DEF_OR_TARGET_OBJECT_NULL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Do validation....
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (pObject.getGuid() == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getString(IUMSConstants.COS_TARGET_OBJECT_NOT_PERSISTENT);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!(cosDef instanceof DirectCOSDefinition)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n.getString(IUMSConstants.INVALID_COSDEFINITION);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cosDef instanceof DirectCOSDefinition) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster removeDirectCOSAssignment(pObject, (DirectCOSDefinition) cosDef,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosTemplate, _schemaManager);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removes a Direct COS assignment from a target persistent object. The COS
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * target persistent object could be a user, group, organization,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * organizationalunit, etc. The COS target object must be persistent before
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this method can be used.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param pObject
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The COS target persistent object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cosDef
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * A COS definition.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param sMgr
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * A SchemaManager object, which is used to determine object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * classes for attributes.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The exception thrown if any of the following occur: o an
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * exception occurs determining the object class for the COS
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * specifier. o an exception occurs determining the object class
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * for the COS attributes. o there is an exception thrown rom
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the data layer.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void removeDirectCOSAssignment(PersistentObject pObject,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster DirectCOSDefinition cosDef, COSTemplate cosTemplate,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SchemaManager sMgr) throws UMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ArrayList aList;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AttrSet attrSet = new AttrSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Include the attribute (whose name is the cosSpecifier)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // in the attribute set for removal (only if it exists).
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (pObject.getAttribute(cosDef.getCOSSpecifier()) != null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attrSet.add(new Attr(cosDef.getCOSSpecifier(), cosTemplate
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getName()));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get cosSpecifier object class - should only be one.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Include the cosSpecifier object class in the attribute
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // set for removal (only if itt exists).
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster aList = (ArrayList) sMgr.getObjectClasses(cosDef.getCOSSpecifier());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cosSpecObjectClass = (String) aList.get(0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (objectClassExists(cosSpecObjectClass, pObject)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attrSet.add(new Attr("objectclass", cosSpecObjectClass));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get the cos attributes from the definition (ex. mailquota).
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // For each of the attributes, get the objectclass. Include the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // object classes in the attribute set for removal (if they exist).
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] cosAttributes = cosDef.getCOSAttributes();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cosAttribute = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; i < cosAttributes.length; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Only get the attribute - not the qualifier
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringTokenizer st = new StringTokenizer(cosAttributes[i]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosAttribute = st.nextToken();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster aList = (ArrayList) sMgr.getObjectClasses(cosAttribute);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cosAttributeObjectClass = (String) aList.get(0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (objectClassExists(cosAttributeObjectClass, pObject)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attrSet
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .add(new Attr("objectclass",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosAttributeObjectClass));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (attrSet.size() > 0) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington pObject.modify(toModifications(ModificationType.DELETE, attrSet));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster pObject.save();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (UMSException e) {
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Madden LdapException le = (LdapException) e.getRootCause();
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Madden // Ignore anything that is not a COS generated attribute's object class
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Madden if (!ResultCode.OBJECTCLASS_VIOLATION.equals(le.getResult().getResultCode())) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw e;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Assigns a direct (Classic) COS definition to a persistent object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param pObject
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The target persistent object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cosDef
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The direct (Classic) COS definition.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cosTemplate
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * A COS template belonging to the definition.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param sMgr
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * A SchemaManager instance.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws UMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * if an exception occurs
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void assignDirectCOSDef(PersistentObject pObject,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster DirectCOSDefinition cosDef, COSTemplate cosTemplate,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SchemaManager sMgr) throws UMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Do validation....
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cosDef.getGuid() == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getString(IUMSConstants.COS_DEFINITION_NOT_PERSISTENT);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Make sure target entry is in same tree as COS Def parent.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington DN targetDN = DN.valueOf(pObject.getGuid().getDn());
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington DN cosParentDN = DN.valueOf(cosDef.getParentGuid().getDn());
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington if (!(targetDN.isInScopeOf(cosParentDN, SearchScope.SUBORDINATES))) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = i18n
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getString(IUMSConstants.COS_TARGET_OBJECT_DIFFERENT_TREE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new UMSException(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // If cosSpecifier is "nsRole", then we don't need to go
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // any further (we don't need to update target entries).
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cosDef.getCOSSpecifier().equalsIgnoreCase("nsrole"))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ArrayList aList;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AttrSet attrSet = new AttrSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get cosSpecifier object class - should only be one.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Update the target entry with cosSpecifier object class.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Only add it if it doesn't already exist.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster aList = (ArrayList) sMgr.getObjectClasses(cosDef.getCOSSpecifier());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cosSpecObjectClass = (String) aList.get(0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!objectClassExists(cosSpecObjectClass, pObject)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attrSet.add(new Attr("objectclass", cosSpecObjectClass));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get the cos attributes from the definition (ex. mailquota).
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // For each of the attributes, get the objectclass. These
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // will be used to attach to the target entry. This is only
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // done if the cos attribute qualifier is not "operational"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // (you don't need to add cos attribute object classes for
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // "operational" cos attribute qualifier.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] cosAttributes = cosDef.getCOSAttributes();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String qualifier = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Arrays.asList(ICOSDefinition.qualifiers);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Attr attr = cosTemplate.getAttribute("objectclass");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] cosTempObjClasses = attr.getStringValues();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; i < cosAttributes.length; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringTokenizer st = new StringTokenizer(cosAttributes[i]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster st.nextToken();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster qualifier = st.nextToken();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((!qualifier.equals(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ICOSDefinition.qualifiers[ICOSDefinition.OPERATIONAL]))) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int j = 0; j < cosTempObjClasses.length; j++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!cosTempObjClasses[j].equalsIgnoreCase("top")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster && !cosTempObjClasses[j].equalsIgnoreCase("costemplate")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster && !objectClassExists(cosTempObjClasses[j], pObject))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!attrSet.contains("objectclass",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosTempObjClasses[j])) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attrSet.add(new Attr("objectclass",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cosTempObjClasses[j]));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Add the attribute name (cosSpecifier value) and attribute
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // value (cosTemplate name) only if it doesn't exist.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (pObject.getAttribute(cosDef.getCOSSpecifier()) == null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attrSet.add(new Attr(cosDef.getCOSSpecifier(), cosTemplate
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getName()));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (attrSet.size() > 0) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington pObject.modify(toModifications(ModificationType.ADD, attrSet));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster pObject.save();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington private Collection<Modification> toModifications(ModificationType type, AttrSet attrSet) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington Collection<Modification> modifications = new HashSet<>();
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington Enumeration<Attr> attributes = attrSet.getAttributes();
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington while (attributes.hasMoreElements()) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington modifications.add(new Modification(type, attributes.nextElement().toLDAPAttribute()));
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington }
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington return modifications;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington }
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Utility method to check if an object class exists in a persistent object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
ce4d3fddc8fe2eddd68a20af9570b3cc63ece5abNeil Madden * @param objectClass
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The object class.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param pObject
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The persistent object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private boolean objectClassExists(String objectClass,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PersistentObject pObject) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Attr attr = pObject.getAttribute("objectclass");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] vals = attr.getStringValues();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; i < vals.length; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (objectClass.equalsIgnoreCase(vals[i])) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Definition Search Attributes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String[] DEF_ATTRIBUTE_NAMES = { "objectclass",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ICOSDefinition.DEFAULT_NAMING_ATTR, ICOSDefinition.COSTEMPLATEDN,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ICOSDefinition.COSSPECIFIER, ICOSDefinition.COSATTRIBUTE,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ICOSDefinition.ICOSSPECIFIER };
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private PersistentObject _parentObject;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private SchemaManager _schemaManager;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static I18n i18n = I18n.getInstance(IUMSConstants.UMS_PKG);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}