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
022package org.apache.directory.shared.ldap.trigger;
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.shared.ldap.model.constants.SchemaConstants;
033import org.apache.directory.shared.ldap.model.entry.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 */
041public final class TriggerUtils
042{
043    public static final String TRIGGER_EXECUTION_SPECIFIC_AREA_VALUE = SchemaConstants.TRIGGER_EXECUTION_SPECIFIC_AREA;
044    public static final String TRIGGER_EXECUTION_SUBENTRY_OC = "triggerExecutionSubentry";
045    public static final String ENTRY_TRIGGER_SPECIFICATION_ATTR = "entryTriggerSpecification";
046    public static final String PRESCRIPTIVE_TRIGGER_SPECIFICATION_ATTR = "prescriptiveTriggerSpecification";
047
048
049    /**
050     * Private constructor.
051     */
052    private TriggerUtils()
053    {
054    }
055
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        if ( administrativeRole == null
062            || !AttributeUtils.containsValueCaseIgnore(administrativeRole, TRIGGER_EXECUTION_SPECIFIC_AREA_VALUE) )
063        {
064            Attributes changes = new BasicAttributes( SchemaConstants.ADMINISTRATIVE_ROLE_AT, TRIGGER_EXECUTION_SPECIFIC_AREA_VALUE, true );
065            apCtx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
066        }
067    }
068
069
070    public static void createTriggerExecutionSubentry(
071        LdapContext apCtx,
072        String subentryCN,
073        String subtreeSpec,
074        String prescriptiveTriggerSpec ) throws NamingException
075    {
076        Attributes subentry = new BasicAttributes( SchemaConstants.CN_AT, subentryCN, true );
077        Attribute objectClass = new BasicAttribute( SchemaConstants.OBJECT_CLASS_AT );
078        subentry.put( objectClass );
079        objectClass.add( SchemaConstants.TOP_OC );
080        objectClass.add( SchemaConstants.SUBENTRY_OC );
081        objectClass.add( TRIGGER_EXECUTION_SUBENTRY_OC );
082        subentry.put( SchemaConstants.SUBTREE_SPECIFICATION_AT, subtreeSpec );
083        subentry.put( PRESCRIPTIVE_TRIGGER_SPECIFICATION_ATTR, prescriptiveTriggerSpec );
084        apCtx.createSubcontext( "cn=" + subentryCN, subentry );
085    }
086
087
088    public static void loadPrescriptiveTriggerSpecification(
089        LdapContext apCtx,
090        String subentryCN,
091        String triggerSpec ) throws NamingException
092    {
093        Attributes changes = new BasicAttributes( PRESCRIPTIVE_TRIGGER_SPECIFICATION_ATTR, triggerSpec, true );
094        apCtx.modifyAttributes( "cn=" + subentryCN, DirContext.ADD_ATTRIBUTE, changes );
095    }
096
097
098    public static void loadEntryTriggerSpecification(
099        LdapContext ctx,
100        String triggerSpec ) throws NamingException
101    {
102        Attributes changes = new BasicAttributes( ENTRY_TRIGGER_SPECIFICATION_ATTR, triggerSpec, true );
103        ctx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
104    }
105}