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
021package org.apache.directory.api.ldap.trigger;
022
023
024import javax.naming.NamingException;
025import javax.naming.directory.Attribute;
026import javax.naming.directory.Attributes;
027import javax.naming.directory.BasicAttribute;
028import javax.naming.directory.BasicAttributes;
029import javax.naming.directory.DirContext;
030import javax.naming.ldap.LdapContext;
031
032import org.apache.directory.api.ldap.model.constants.SchemaConstants;
033import org.apache.directory.api.ldap.model.entry.AttributeUtils;
034
035
036/**
037 * A utility class for working with Triggers Execution Administrative Points
038 * Trigger Execution Subentries and Trigger Specifications.
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042public final class TriggerUtils
043{
044    /**
045     * Private constructor.
046     */
047    private TriggerUtils()
048    {
049    }
050
051
052    /**
053     * Defines the Administration point and administrative role for the TriggerExecution specific point
054     * @param apCtx The administrative point context
055     * @throws NamingException If the operation failed
056     */
057    public static void defineTriggerExecutionSpecificPoint( LdapContext apCtx ) throws NamingException
058    {
059        Attributes ap = apCtx.getAttributes( "", new String[] { SchemaConstants.ADMINISTRATIVE_ROLE_AT } );
060        Attribute administrativeRole = ap.get( SchemaConstants.ADMINISTRATIVE_ROLE_AT );
061        
062        if ( administrativeRole == null
063            || !AttributeUtils.containsValueCaseIgnore( administrativeRole, SchemaConstants.TRIGGER_EXECUTION_SPECIFIC_AREA ) )
064        {
065            Attributes changes = new BasicAttributes( SchemaConstants.ADMINISTRATIVE_ROLE_AT,
066                SchemaConstants.TRIGGER_EXECUTION_SPECIFIC_AREA, true );
067            apCtx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
068        }
069    }
070
071
072    /**
073     * Create the Trigger execution subentry
074     * 
075     * @param apCtx The administration point context
076     * @param subentryCN The CN used by the suentry
077     * @param subtreeSpec The subtree specification
078     * @param prescriptiveTriggerSpec The prescriptive trigger specification
079     * @throws NamingException If the operation failed
080     */
081    public static void createTriggerExecutionSubentry(
082        LdapContext apCtx,
083        String subentryCN,
084        String subtreeSpec,
085        String prescriptiveTriggerSpec ) throws NamingException
086    {
087        Attributes subentry = new BasicAttributes( SchemaConstants.CN_AT, subentryCN, true );
088        Attribute objectClass = new BasicAttribute( SchemaConstants.OBJECT_CLASS_AT );
089        subentry.put( objectClass );
090        objectClass.add( SchemaConstants.TOP_OC );
091        objectClass.add( SchemaConstants.SUBENTRY_OC );
092        objectClass.add( SchemaConstants.TRIGGER_EXECUTION_SUBENTRY_OC );
093        subentry.put( SchemaConstants.SUBTREE_SPECIFICATION_AT, subtreeSpec );
094        subentry.put( SchemaConstants.PRESCRIPTIVE_TRIGGER_SPECIFICATION_AT, prescriptiveTriggerSpec );
095        apCtx.createSubcontext( "cn=" + subentryCN, subentry );
096    }
097
098
099    /**
100     * Load an prescriptive trigger specification
101     * 
102     * @param apCtx The administrative point context
103     * @param subentryCN The subentry CN
104     * @param triggerSpec The trigger specification
105     * @throws NamingException If the operation failed
106     */
107    public static void loadPrescriptiveTriggerSpecification(
108        LdapContext apCtx,
109        String subentryCN,
110        String triggerSpec ) throws NamingException
111    {
112        Attributes changes = new BasicAttributes( SchemaConstants.PRESCRIPTIVE_TRIGGER_SPECIFICATION_AT, triggerSpec, true );
113        apCtx.modifyAttributes( "cn=" + subentryCN, DirContext.ADD_ATTRIBUTE, changes );
114    }
115
116
117    /**
118     * Load the trigger specification entry
119     * 
120     * @param ctx The context
121     * @param triggerSpec The trigger specification
122     * @throws NamingException If the operation failed
123     */
124public static void loadEntryTriggerSpecification(
125        LdapContext ctx,
126        String triggerSpec ) throws NamingException
127    {
128        Attributes changes = new BasicAttributes( SchemaConstants.ENTRY_TRIGGER_SPECIFICATION_AT, triggerSpec, true );
129        ctx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
130    }
131}