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    package org.apache.directory.shared.converter.schema;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import org.apache.directory.shared.ldap.entry.Entry;
026    import org.apache.directory.shared.ldap.entry.EntryAttribute;
027    import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
028    import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
029    import org.apache.directory.shared.ldap.exception.LdapException;
030    import org.apache.directory.shared.ldap.ldif.LdifUtils;
031    import org.apache.directory.shared.ldap.util.StringTools;
032    
033    /**
034     * An abstract SchemaElement implementation. It contains shared
035     * elements from AttributeType and ObjectClass, like obsolete, oid, 
036     * description, names and extensions (not implemented)
037     *
038     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039     * @version $Rev$, $Date$
040     */
041    public abstract class SchemaElementImpl implements SchemaElement
042    {
043        /** The schema element oid */
044        protected String oid;
045        
046        /** The schema element description */
047        protected String description;
048        
049        /** The list of names for this schemaElements */
050        protected List<String> names = new ArrayList<String>();
051    
052        /** The obsolete flag */
053        protected boolean obsolete = false;
054        
055        /** The optional list of extensions */
056        protected List<String> extensions = new ArrayList<String>();
057        
058        /**
059         * @see SchemaElement#isObsolete()
060         */
061        public boolean isObsolete()
062        {
063            return obsolete;
064        }
065    
066        /**
067         * @see SchemaElement#setObsolete(boolean)
068         */
069        public void setObsolete( boolean obsolete )
070        {
071            this.obsolete = obsolete;
072        }
073    
074        /**
075         * @see SchemaElement#getOid()
076         */
077        public String getOid()
078        {
079            return oid;
080        }
081        
082        /**
083         * @see SchemaElement#getDescription()
084         */
085        public String getDescription()
086        {
087            return description;
088        }
089        
090        /**
091         * @see SchemaElement#setDescription(String)
092         */
093        public void setDescription( String description )
094        {
095            this.description = description;
096        }
097        
098        /**
099         * @see SchemaElement#getNames()
100         */
101        public List<String> getNames()
102        {
103            return names;
104        }
105        
106        /**
107         * @see SchemaElement#setNames(List)
108         */
109        public void setNames( List<String> names )
110        {
111            this.names = names;
112        }
113    
114        /**
115         * @see SchemaElement#getExtensions()
116         */
117        public List<String> getExtensions()
118        {
119            return extensions;
120        }
121    
122        /**
123         * @see SchemaElement#setExtensions(List)
124         */
125        public void setExtensions( List<String> extensions )
126        {
127            this.extensions = extensions;
128        }
129        
130        /**
131         * @return The OID as a Ldif line 
132         */
133        private String oidToLdif()
134        {
135            return "m-oid: " + oid + '\n';
136        }
137        
138        /**
139         * @return the Names as Ldif lines
140         */
141        private String nameToLdif() throws LdapException
142        {
143            if ( names.size() == 0 )
144            {
145                return "";
146            }
147            else
148            {
149                Entry entry = new DefaultClientEntry();
150                EntryAttribute attribute = new DefaultClientAttribute( "m-name" );
151                
152                for ( String name:names )
153                {
154                    attribute.add( name );
155                }
156                
157                entry.put( attribute );
158                
159                return LdifUtils.convertAttributesToLdif( entry );
160            }
161        }
162        
163        /**
164         * @return The description as a ldif line
165         */
166        private String descToLdif() throws LdapException
167        {
168            if ( StringTools.isEmpty( description ) )
169            {
170                return "";
171            }
172            else
173            {
174                Entry entry = new DefaultClientEntry();
175                EntryAttribute attribute = new DefaultClientAttribute( "m-description", description );
176    
177                entry.put( attribute );
178                
179                return LdifUtils.convertAttributesToLdif( entry );
180            }
181        }
182        
183        /**
184         * @return The dn as a ldif line
185         */
186        public abstract String dnToLdif( String schemaName ) throws LdapException;
187        
188        /**
189         * Return the extensions formated as Ldif lines
190         * @param ID The attributeId : can be m-objectClassExtension or
191         * m-attributeTypeExtension
192         * 
193         * @return The extensions formated as ldif lines
194         * @throws LdapException If the conversion goes wrong
195         */
196        protected String extensionsToLdif( String ID ) throws LdapException
197        {
198            StringBuilder sb = new StringBuilder();
199            
200            Entry entry = new DefaultClientEntry();
201            EntryAttribute attribute = new DefaultClientAttribute( ID ); 
202    
203            for ( String extension:extensions )
204            {
205                attribute.add( extension );
206            }
207    
208            sb.append( LdifUtils.convertAttributesToLdif( entry ) );
209            
210            return sb.toString();
211        }
212    
213        protected String schemaToLdif( String schemaName, String type ) throws LdapException
214        {
215            StringBuilder sb = new StringBuilder();
216            
217            // The DN
218            sb.append( dnToLdif( schemaName ) );
219    
220            // ObjectClasses
221            sb.append( "objectclass: " ).append( type ).append( '\n' );
222            sb.append( "objectclass: metaTop\n" );
223            sb.append( "objectclass: top\n" );
224    
225            // The oid
226            sb.append( oidToLdif() );
227    
228            // The name
229            sb.append( nameToLdif() );
230            
231            // The desc
232            sb.append( descToLdif() );
233            
234            // The obsolete flag, only if "true"
235            if ( obsolete )
236            {
237                sb.append( "m-obsolete: TRUE\n" );
238            }
239            
240            return sb.toString();
241        }
242    }