001    /*
002     * Copyright (c) 2005 Stephen J. McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.metro.tools;
020    
021    import org.apache.tools.ant.BuildException;
022    
023    import net.dpml.state.Trigger.TriggerEvent;
024    import net.dpml.state.DefaultTrigger;
025    import net.dpml.state.DefaultTransition;
026    import net.dpml.state.DefaultOperation;
027    import net.dpml.state.ApplyAction;
028    import net.dpml.state.ExecAction;
029    
030    /**
031     * Utility datatype supporting a Transition instance construction.
032     *
033     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
034     * @version 1.2.0
035     */
036    public class TriggerDataType
037    {
038        private String m_event;
039        private OperationDataType m_operation;
040        private TransitionDataType m_transition;
041        private ApplyDataType m_apply;
042        private ExecDataType m_exec;
043        
044       /**
045        * Set the trigger event class.
046        * @param event the event name
047        */
048        public void setEvent( final String event )
049        {
050            if( null == event )
051            {
052                throw new NullPointerException( "event" );
053            }
054            m_event = event;
055        }
056        
057       /**
058        * Add an operation to the trigger.
059        * @return the operation datatype
060        */
061        public OperationDataType createOperation()
062        {
063            if( ( null != m_operation ) || ( null != m_transition ) || ( null != m_apply ) || ( null != m_exec ) )
064            {
065                final String error = 
066                  "Trigger action is already defined.";
067                throw new IllegalStateException( error );
068            }
069            m_operation = new OperationDataType();
070            return m_operation;
071        }
072        
073       /**
074        * Add an transition to the trigger.
075        * @return the transition datatype
076        */
077        public TransitionDataType createTransition()
078        {
079            if( ( null != m_operation ) || ( null != m_transition ) || ( null != m_apply ) || ( null != m_exec ) )
080            {
081                final String error = 
082                  "Trigger action is already defined.";
083                throw new IllegalStateException( error );
084            }
085            m_transition = new TransitionDataType();
086            return m_transition;
087        }
088        
089       /**
090        * Add an apply to the trigger.
091        * @return the apply datatype
092        */
093        public ApplyDataType createApply()
094        {
095            if( ( null != m_operation ) || ( null != m_transition ) || ( null != m_apply ) || ( null != m_exec ) )
096            {
097                final String error = 
098                  "Trigger action is already defined.";
099                throw new IllegalStateException( error );
100            }
101            m_apply = new ApplyDataType();
102            return m_apply;
103        }
104        
105       /**
106        * Add an exec to the trigger.
107        * @return the exec datatype
108        */
109        public ExecDataType createExec()
110        {
111            if( ( null != m_operation ) || ( null != m_transition ) || ( null != m_apply ) || ( null != m_exec ) )
112            {
113                final String error = 
114                  "Trigger action is already defined.";
115                throw new IllegalStateException( error );
116            }
117            m_exec = new ExecDataType();
118            return m_exec;
119        }
120    
121        DefaultTrigger getTrigger()
122        {
123            TriggerEvent event = getTriggerEvent();
124            if( m_apply != null )
125            {
126                ApplyAction action = m_apply.getAction();
127                return new DefaultTrigger( event, action );
128            }
129            else if( m_exec != null )
130            {
131                ExecAction action = m_exec.getAction();
132                return new DefaultTrigger( event, action );
133            }
134            else if( m_operation != null )
135            {
136                DefaultOperation action = m_operation.getOperation();
137                return new DefaultTrigger( event, action );
138            }
139            else if( m_transition != null )
140            {
141                DefaultTransition action = m_transition.getTransition();
142                return new DefaultTrigger( event, action );
143            }
144            else
145            {
146                final String error = 
147                  "Missing action ('operation'|transition'|'apply'|'exec') element in trigger.";
148                throw new BuildException( error );
149            }
150        }
151        
152        TriggerEvent getTriggerEvent()
153        {
154            if( null == m_event )
155            {
156                throw new BuildException( "Missing event attribute in trigger." );
157            }
158            else
159            {
160                return TriggerEvent.parse( m_event );
161            }
162        }
163    }