001    package org.apache.ojb.broker.metadata;
002    
003    /* Copyright 2003-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.commons.lang.builder.ToStringBuilder;
019    import org.apache.commons.lang.builder.ToStringStyle;
020    import java.io.Serializable;
021    import java.util.Iterator;
022    
023    /**
024     * An InsertProcedureDescriptor contains information that is related to the
025     * procedure/function that is used to handle the insertion of new records.
026     * <br>
027     * Note: Be careful when use InsertProcedureDescriptor variables or caching
028     * InsertProcedureDescriptor instances, because instances could become invalid
029     * during runtime (see {@link MetadataManager}).
030     *
031     * @author <a href="mailto:rongallagher@bellsouth.net">Ron Gallagher<a>
032     * @version $Id: InsertProcedureDescriptor.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
033     */
034    public class InsertProcedureDescriptor
035        extends ProcedureDescriptor
036        implements Serializable, XmlCapable
037    {
038            private static final long serialVersionUID = -3808311052971075269L;
039        //---------------------------------------------------------------
040        /**
041         * The value that indicates if the argument list for this procedure
042         * includes all field-descriptors from the related class-descriptor.
043         */
044        private boolean includeAllFields;
045    
046        //---------------------------------------------------------------
047        /**
048         * Constructor declaration
049         */
050        public InsertProcedureDescriptor(
051            ClassDescriptor classDescriptor,
052            String name,
053            boolean includeAllFields)
054        {
055            super(classDescriptor, name);
056            if (includeAllFields)
057            {
058                this.addArguments(this.getClassDescriptor().getFieldDescriptions());
059            }
060            this.includeAllFields = includeAllFields;
061        }
062    
063        //---------------------------------------------------------------
064        /**
065         * Retrieve the value that indicates if the argument list for this
066         * procedure includes all field-descriptors from the related
067         * class-descriptor.
068         *
069         * @return The current value
070         */
071        public boolean getIncludeAllFields()
072        {
073            return this.includeAllFields;
074        }
075    
076        //---------------------------------------------------------------
077        /**
078         * Add an argument
079         * <p>
080         * The argument will be added only if this procedure is not configured
081         * to {@link #getIncludeAllFields() include all arguments}.
082         */
083        public final void addArgument(ArgumentDescriptor argument)
084        {
085            if (!this.getIncludeAllFields())
086            {
087                super.addArgument(argument);
088            }
089        }
090    
091        /*
092         * @see XmlCapable#toXML()
093         */
094        public String toXML()
095        {
096            RepositoryTags tags = RepositoryTags.getInstance();
097            String eol = System.getProperty( "line.separator" );
098    
099            // The result
100            StringBuffer result = new StringBuffer( 1024 );
101            result.append( eol );
102            result.append( "   " );
103    
104            // Opening tag and attributes
105            result.append( " " );
106            result.append( tags.getOpeningTagNonClosingById( INSERT_PROCEDURE ) );
107            result.append( " " );
108            result.append( tags.getAttribute( NAME, this.getName() ) );
109            if( this.hasReturnValue() )
110            {
111                result.append( " " );
112                result.append( tags.getAttribute( RETURN_FIELD_REF, this.getReturnValueFieldRefName() ) );
113            }
114            result.append( " " );
115            result.append( tags.getAttribute( INCLUDE_ALL_FIELDS, String.valueOf( this.getIncludeAllFields() ) ) );
116            result.append( ">" );
117            result.append( eol );
118    
119            // Write all arguments only if we're not including all fields.
120            if( !this.getIncludeAllFields() )
121            {
122                Iterator args = this.getArguments().iterator();
123                while( args.hasNext() )
124                {
125                    result.append( ( ( ArgumentDescriptor ) args.next() ).toXML() );
126                }
127            }
128    
129            // Closing tag
130            result.append( "    " );
131            result.append( tags.getClosingTagById( INSERT_PROCEDURE ) );
132            result.append( eol );
133            return result.toString();
134        }
135    
136        //---------------------------------------------------------------
137        /**
138         * Provide a string representation of this object
139         *
140         * @return a string representation of this object
141         */
142        public String toString()
143        {
144            ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
145            buf.append("name", this.getName());
146            buf.append("includeAllFields", this.getIncludeAllFields());
147            if (this.hasReturnValue())
148            {
149                buf.append("returnFieldRefName", this.getReturnValueFieldRefName());
150            }
151            return buf.toString();
152        }
153    }