KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > controller > actions > ExportSchemaForADSAction


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.schemas.controller.actions;
22
23
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.text.DateFormat JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import javax.naming.NamingException JavaDoc;
36
37 import org.apache.directory.ldapstudio.schemas.Activator;
38 import org.apache.directory.ldapstudio.schemas.PluginConstants;
39 import org.apache.directory.ldapstudio.schemas.model.AttributeType;
40 import org.apache.directory.ldapstudio.schemas.model.ObjectClass;
41 import org.apache.directory.ldapstudio.schemas.model.Schema;
42 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
43 import org.apache.directory.ldapstudio.schemas.view.ViewUtils;
44 import org.apache.directory.ldapstudio.schemas.view.views.SchemasView;
45 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemaWrapper;
46 import org.apache.directory.shared.converter.schema.AttributeTypeHolder;
47 import org.apache.directory.shared.converter.schema.ObjectClassHolder;
48 import org.apache.log4j.Logger;
49 import org.eclipse.jface.action.Action;
50 import org.eclipse.jface.viewers.TreeSelection;
51 import org.eclipse.swt.SWT;
52 import org.eclipse.swt.widgets.FileDialog;
53 import org.eclipse.ui.PlatformUI;
54 import org.eclipse.ui.plugin.AbstractUIPlugin;
55
56
57 /**
58  * This class implements the Action for Exporting a schema For ADS.
59  */

60 public class ExportSchemaForADSAction extends Action
61 {
62     private static Logger logger = Logger.getLogger( ExportSchemaForADSAction.class );
63
64     /** The associated view */
65     private SchemasView view;
66
67
68     /**
69      * Creates a new instance of ExportSchemaForADSAction.
70      *
71      * @param view
72      * the associated view
73      */

74     public ExportSchemaForADSAction( SchemasView view )
75     {
76         super( "Export For Apache DS..." );
77         this.view = view;
78         setToolTipText( getText() );
79         setId( PluginConstants.CMD_EXPORT_FOR_ADS );
80         setImageDescriptor( AbstractUIPlugin.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
81             PluginConstants.IMG_EXPORT_SCHEMA_FOR_ADS ) );
82         setEnabled( true );
83     }
84
85
86     /* (non-Javadoc)
87      * @see org.eclipse.jface.action.Action#run()
88      */

89     public void run()
90     {
91         Object JavaDoc selection = ( ( TreeSelection ) view.getViewer().getSelection() ).getFirstElement();
92
93         if ( selection != null )
94         {
95             if ( selection instanceof SchemaWrapper )
96             {
97                 Schema schema = ( ( SchemaWrapper ) selection ).getMySchema();
98
99                 // Opening the FileDialog to let the user choose the destination file
100
FileDialog fd = new FileDialog( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
101                     SWT.SAVE );
102                 fd.setText( "Select a file" );
103                 fd.setFilterPath( Activator.getDefault().getPreferenceStore().getString(
104                     PluginConstants.PREFS_SAVE_FILE_DIALOG ) );
105                 fd.setFileName( schema.getName() + ".ldif" ); //$NON-NLS-1$
106
fd.setFilterExtensions( new String JavaDoc[]
107                     { "*.ldif", "*.*" } ); //$NON-NLS-1$ //$NON-NLS-2$
108
fd.setFilterNames( new String JavaDoc[]
109                     { "LDIF files", "All_files" } );
110                 String JavaDoc savePath = fd.open();
111                 if ( savePath != null )
112                 {
113                     File JavaDoc selectedFile = new File JavaDoc( savePath );
114                     if ( selectedFile.exists() )
115                     {
116                         int response = ViewUtils.displayQuestionMessageBox( SWT.OK | SWT.CANCEL,
117                             "Overwrite existing file ?",
118                             "The file you have choosen already exists. Do you want to overwrite it?" );
119                         if ( response == SWT.CANCEL )
120                         {
121                             return;
122                         }
123                     }
124
125                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
126                     sb.append( "# " + schema.getName() + "\n" );
127                     DateFormat JavaDoc format = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.MEDIUM );
128                     Date JavaDoc date = new Date JavaDoc();
129                     sb.append( "# Generated by LDAP Studio on " + format.format( date ) + "\n" );
130                     sb.append( "\n" );
131
132                     // Generation the Schema Node
133
sb.append( "dn: cn=" + schema.getName() + ", ou=schema\n" );
134                     sb.append( "objectclass: metaSchema\n" );
135                     sb.append( "objectclass: top\n" );
136                     sb.append( "cn: " + schema.getName() + "\n" );
137                     String JavaDoc[] schemaDependencies = getSchemaDependencies( schema );
138                     for (String JavaDoc schemaName : schemaDependencies )
139                     {
140                         sb.append( "m-dependencies: " + schemaName + "\n" );
141                     }
142                     sb.append( "\n" );
143
144                     try
145                     {
146                         // Generation the Attribute Types Node
147
sb.append( "dn: ou=attributeTypes, cn=" + schema.getName() + ", ou=schema\n" );
148                         sb.append( "objectclass: organizationalUnit\n" );
149                         sb.append( "objectclass: top\n" );
150                         sb.append( "ou: attributetypes\n" );
151                         sb.append( "\n" );
152
153                         // Generating LDIF for Attributes Types
154
for ( AttributeType at : schema.getAttributeTypesAsArray() )
155                         {
156                             AttributeTypeHolder holder = new AttributeTypeHolder( at.getOid() );
157                             holder.setCollective( at.isCollective() );
158                             holder.setDescription( at.getDescription() );
159                             holder.setEquality( at.getEquality() );
160                             List JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>();
161                             for ( String JavaDoc name : at.getNames() )
162                             {
163                                 names.add( name );
164                             }
165                             holder.setNames( names );
166                             holder.setNoUserModification( at.isNoUserModification() );
167                             holder.setObsolete( at.isObsolete() );
168                             holder.setOrdering( at.getOrdering() );
169                             holder.setSingleValue( at.isSingleValue() );
170                             holder.setSubstr( at.getSubstr() );
171                             holder.setSuperior( at.getSuperior() );
172                             holder.setSyntax( at.getSyntax() );
173                             holder.setOidLen( at.getLength() );
174                             holder.setUsage( at.getUsage() );
175
176                             sb.append( holder.toLdif( schema.getName() ) + "\n" );
177                         }
178
179                         // Generation the Comparators Node
180
sb.append( "dn: ou=comparators, cn=" + schema.getName() + ", ou=schema\n" );
181                         sb.append( "objectclass: organizationalUnit\n" );
182                         sb.append( "objectclass: top\n" );
183                         sb.append( "ou: comparators\n" );
184                         sb.append( "\n" );
185
186                         // Generation the DIT Content Rules Node
187
sb.append( "dn: ou=ditContentRules, cn=" + schema.getName() + ", ou=schema\n" );
188                         sb.append( "objectclass: organizationalUnit\n" );
189                         sb.append( "objectclass: top\n" );
190                         sb.append( "ou: ditcontentrules\n" );
191                         sb.append( "\n" );
192
193                         // Generation the DIT Structure RulesNode
194
sb.append( "dn: ou=ditStructureRules, cn=" + schema.getName() + ", ou=schema\n" );
195                         sb.append( "objectclass: organizationalUnit\n" );
196                         sb.append( "objectclass: top\n" );
197                         sb.append( "ou: ditstructurerules\n" );
198                         sb.append( "\n" );
199
200                         // Generation the Matching Rules Node
201
sb.append( "dn: ou=matchingRules, cn=" + schema.getName() + ", ou=schema\n" );
202                         sb.append( "objectclass: organizationalUnit\n" );
203                         sb.append( "objectclass: top\n" );
204                         sb.append( "ou: matchingrules\n" );
205                         sb.append( "\n" );
206
207                         // Generation the Matching Rule Use Node
208
sb.append( "dn: ou=matchingRuleUse, cn=" + schema.getName() + ", ou=schema\n" );
209                         sb.append( "objectclass: organizationalUnit\n" );
210                         sb.append( "objectclass: top\n" );
211                         sb.append( "ou: matchingruleuse\n" );
212                         sb.append( "\n" );
213
214                         // Generation the Name Forms Node
215
sb.append( "dn: ou=nameForms, cn=" + schema.getName() + ", ou=schema\n" );
216                         sb.append( "objectclass: organizationalUnit\n" );
217                         sb.append( "objectclass: top\n" );
218                         sb.append( "ou: nameforms\n" );
219                         sb.append( "\n" );
220
221                         // Generation the Normalizers Node
222
sb.append( "dn: ou=normalizers, cn=" + schema.getName() + ", ou=schema\n" );
223                         sb.append( "objectclass: organizationalUnit\n" );
224                         sb.append( "objectclass: top\n" );
225                         sb.append( "ou: normalizers\n" );
226                         sb.append( "\n" );
227
228                         // Generation the Object Classes Node
229
sb.append( "dn: ou=objectClasses, cn=" + schema.getName() + ", ou=schema\n" );
230                         sb.append( "objectclass: organizationalUnit\n" );
231                         sb.append( "objectclass: top\n" );
232                         sb.append( "ou: objectClasses\n" );
233                         sb.append( "\n" );
234
235                         // Generating LDIF for Object Classes
236
for ( ObjectClass oc : schema.getObjectClassesAsArray() )
237                         {
238                             ObjectClassHolder holder = new ObjectClassHolder( oc.getOid() );
239                             holder.setClassType( oc.getClassType() );
240                             holder.setDescription( oc.getDescription() );
241                             List JavaDoc<String JavaDoc> mayList = new ArrayList JavaDoc<String JavaDoc>();
242                             for ( String JavaDoc may : oc.getMay() )
243                             {
244                                 mayList.add( may );
245                             }
246                             holder.setMay( mayList );
247                             List JavaDoc<String JavaDoc> mustList = new ArrayList JavaDoc<String JavaDoc>();
248                             for ( String JavaDoc must : oc.getMust() )
249                             {
250                                 mustList.add( must );
251                             }
252                             holder.setMust( mustList );
253                             List JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>();
254                             for ( String JavaDoc name : oc.getNames() )
255                             {
256                                 names.add( name );
257                             }
258                             holder.setNames( names );
259                             List JavaDoc<String JavaDoc> superiorList = new ArrayList JavaDoc<String JavaDoc>();
260                             for ( String JavaDoc superior : oc.getSuperiors() )
261                             {
262                                 superiorList.add( superior );
263                             }
264                             holder.setSuperiors( superiorList );
265                             holder.setObsolete( oc.isObsolete() );
266
267                             sb.append( holder.toLdif( schema.getName() ) + "\n" );
268                         }
269
270                         // Generation the Syntax Checkers Node
271
sb.append( "dn: ou=syntaxCheckers, cn=" + schema.getName() + ", ou=schema\n" );
272                         sb.append( "objectclass: organizationalUnit\n" );
273                         sb.append( "objectclass: top\n" );
274                         sb.append( "ou: syntaxcheckers\n" );
275                         sb.append( "\n" );
276
277                         // Generation the Syntaxes Node
278
sb.append( "dn: ou=syntaxes, cn=" + schema.getName() + ", ou=schema\n" );
279                         sb.append( "objectclass: organizationalUnit\n" );
280                         sb.append( "objectclass: top\n" );
281                         sb.append( "ou: syntaxes\n" );
282                         sb.append( "\n" );
283                     }
284                     catch ( NamingException JavaDoc e )
285                     {
286                         logger.error( "An error occurred when generating the LDIF associated to the schema.", e );
287                         ViewUtils
288                             .displayErrorMessageBox( "Export Failed!",
289                                 "The file couldn't be saved. An error occurred when generating the LDIF associated to the schema." );
290                         return;
291                     }
292
293                     // Writing generated LDIF to the specified file
294
BufferedWriter JavaDoc bufferedWriter;
295                     try
296                     {
297                         bufferedWriter = new BufferedWriter JavaDoc( new FileWriter JavaDoc( savePath ) );
298                         bufferedWriter.write( sb.toString() );
299                         bufferedWriter.close();
300                     }
301                     catch ( IOException JavaDoc e )
302                     {
303                         logger.error( "The file couldn't be saved. An error occurred when writing file to disk.", e );
304                         ViewUtils.displayErrorMessageBox( "Export Failed!",
305                             "The file couldn't be saved. An error occurred when writing file to disk." );
306                         return;
307                     }
308
309                     // Export Successful
310
ViewUtils.displayInformationMessageBox( "Export Successful",
311                         "The schema has been sucessfully exported." );
312
313                     Activator.getDefault().getPreferenceStore().putValue( PluginConstants.PREFS_SAVE_FILE_DIALOG,
314                         selectedFile.getParent() );
315                 }
316             }
317         }
318     }
319
320
321     /**
322      * Gets the schema dependencies.
323      *
324      * @param schema
325      * the schema
326      * @return
327      * an array containing the names of all the schemas which the given
328      * schema depends on
329      */

330     private String JavaDoc[] getSchemaDependencies( Schema schema )
331     {
332         Set JavaDoc<String JavaDoc> schemaNames = new HashSet JavaDoc<String JavaDoc>();
333         SchemaPool schemaPool = SchemaPool.getInstance();
334
335         // Looping on Attribute Types
336
for ( AttributeType at : schema.getAttributeTypesAsArray() )
337         {
338             // Superior
339
String JavaDoc supName = at.getSuperior();
340             if ( supName != null )
341             {
342                 AttributeType sup = schemaPool.getAttributeType( supName );
343                 if ( sup != null )
344                 {
345                     if ( !schema.equals( sup.getOriginatingSchema() ) )
346                     {
347                         schemaNames.add( sup.getOriginatingSchema().getName() );
348                     }
349                 }
350             }
351         }
352
353         // Looping on Object Classes
354
for ( ObjectClass oc : schema.getObjectClassesAsArray() )
355         {
356             // Superiors
357
for ( String JavaDoc supName : oc.getSuperiors() )
358             {
359                 ObjectClass sup = schemaPool.getObjectClass( supName );
360                 if ( sup != null )
361                 {
362                     if ( !schema.equals( sup.getOriginatingSchema() ) )
363                     {
364                         schemaNames.add( sup.getOriginatingSchema().getName() );
365                     }
366                 }
367             }
368
369             // Mays
370
for ( String JavaDoc mayName : oc.getMay() )
371             {
372                 AttributeType may = schemaPool.getAttributeType( mayName );
373                 if ( may != null )
374                 {
375                     if ( !schema.equals( may.getOriginatingSchema() ) )
376                     {
377                         schemaNames.add( may.getOriginatingSchema().getName() );
378                     }
379                 }
380             }
381
382             // Musts
383
for ( String JavaDoc mustName : oc.getMust() )
384             {
385                 AttributeType must = schemaPool.getAttributeType( mustName );
386                 if ( must != null )
387                 {
388                     if ( !schema.equals( must.getOriginatingSchema() ) )
389                     {
390                         schemaNames.add( must.getOriginatingSchema().getName() );
391                     }
392                 }
393             }
394         }
395
396         return schemaNames.toArray( new String JavaDoc[0] );
397     }
398 }
399
Popular Tags