001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    
021    
022    package org.apache.directory.shared.ldap.trigger;
023    
024    import javax.naming.NamingException;
025    import javax.naming.directory.Attribute;
026    import javax.naming.directory.Attributes;
027    import javax.naming.directory.BasicAttribute;
028    import javax.naming.directory.BasicAttributes;
029    import javax.naming.directory.DirContext;
030    import javax.naming.ldap.LdapContext;
031    
032    import org.apache.directory.shared.ldap.constants.SchemaConstants;
033    import org.apache.directory.shared.ldap.util.AttributeUtils;
034    
035    /**
036     * A utility class for working with Triggers Execution Administrative Points
037     * Trigger Execution Subentries and Trigger Specifications.
038     *
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev:$
041     */
042    public class TriggerUtils
043    {
044        public static final String ADMINISTRATIVE_ROLE_ATTR = "administrativeRole";
045        public static final String TRIGGER_EXECUTION_SPECIFIC_AREA_ATTR_VALUE = "triggerExecutionSpecificArea";
046        public static final String TRIGGER_EXECUTION_SUBENTRY_OC = "triggerExecutionSubentry";
047        public static final String ENTRY_TRIGGER_SPECIFICATION_ATTR = "entryTriggerSpecification";
048        public static final String PRESCRIPTIVE_TRIGGER_SPECIFICATION_ATTR = "prescriptiveTriggerSpecification";
049        
050        
051        public static void defineTriggerExecutionSpecificPoint( LdapContext apCtx ) throws NamingException
052        {
053            Attributes ap = apCtx.getAttributes( "", new String[] { ADMINISTRATIVE_ROLE_ATTR } );
054            Attribute administrativeRole = ap.get( ADMINISTRATIVE_ROLE_ATTR );
055            if ( administrativeRole == null || 
056                !AttributeUtils.containsValueCaseIgnore( administrativeRole, TRIGGER_EXECUTION_SPECIFIC_AREA_ATTR_VALUE ) )
057            {
058                Attributes changes = new BasicAttributes( ADMINISTRATIVE_ROLE_ATTR, TRIGGER_EXECUTION_SPECIFIC_AREA_ATTR_VALUE, true );
059                apCtx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
060            }
061        }
062        
063        
064        public static void createTriggerExecutionSubentry(
065            LdapContext apCtx,
066            String subentryCN,
067            String subtreeSpec,
068            String prescriptiveTriggerSpec ) throws NamingException
069        {
070            Attributes subentry = new BasicAttributes( SchemaConstants.CN_AT, subentryCN, true );
071            Attribute objectClass = new BasicAttribute( SchemaConstants.OBJECT_CLASS_AT );
072            subentry.put( objectClass );
073            objectClass.add( SchemaConstants.TOP_OC );
074            objectClass.add( SchemaConstants.SUBENTRY_OC );
075            objectClass.add( TRIGGER_EXECUTION_SUBENTRY_OC );
076            subentry.put( SchemaConstants.SUBTREE_SPECIFICATION_AT, subtreeSpec );
077            subentry.put( PRESCRIPTIVE_TRIGGER_SPECIFICATION_ATTR, prescriptiveTriggerSpec );
078            apCtx.createSubcontext( "cn=" + subentryCN, subentry );
079        }
080        
081        
082        public static void loadPrescriptiveTriggerSpecification(
083            LdapContext apCtx,
084            String subentryCN,
085            String triggerSpec ) throws NamingException
086        {        
087            Attributes changes = new BasicAttributes( PRESCRIPTIVE_TRIGGER_SPECIFICATION_ATTR, triggerSpec, true );
088            apCtx.modifyAttributes( "cn=" + subentryCN, DirContext.ADD_ATTRIBUTE, changes );
089        }
090        
091        
092        public static void loadEntryTriggerSpecification(
093            LdapContext ctx,
094            String triggerSpec ) throws NamingException
095        {        
096            Attributes changes = new BasicAttributes( ENTRY_TRIGGER_SPECIFICATION_ATTR, triggerSpec, true );
097            ctx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
098        }
099    }