1 20 21 package org.apache.directory.ldapstudio.schemas.controller.actions; 22 23 24 import java.io.BufferedWriter ; 25 import java.io.File ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.text.DateFormat ; 29 import java.util.ArrayList ; 30 import java.util.Date ; 31 import java.util.HashSet ; 32 import java.util.List ; 33 import java.util.Set ; 34 35 import javax.naming.NamingException ; 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 60 public class ExportSchemaForADSAction extends Action 61 { 62 private static Logger logger = Logger.getLogger( ExportSchemaForADSAction.class ); 63 64 65 private SchemasView view; 66 67 68 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 89 public void run() 90 { 91 Object 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 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" ); fd.setFilterExtensions( new String [] 107 { "*.ldif", "*.*" } ); fd.setFilterNames( new String [] 109 { "LDIF files", "All_files" } ); 110 String savePath = fd.open(); 111 if ( savePath != null ) 112 { 113 File selectedFile = new File ( 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 sb = new StringBuffer (); 126 sb.append( "# " + schema.getName() + "\n" ); 127 DateFormat format = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.MEDIUM ); 128 Date date = new Date (); 129 sb.append( "# Generated by LDAP Studio on " + format.format( date ) + "\n" ); 130 sb.append( "\n" ); 131 132 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 [] schemaDependencies = getSchemaDependencies( schema ); 138 for (String schemaName : schemaDependencies ) 139 { 140 sb.append( "m-dependencies: " + schemaName + "\n" ); 141 } 142 sb.append( "\n" ); 143 144 try 145 { 146 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 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 <String > names = new ArrayList <String >(); 161 for ( String 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 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 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 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 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 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 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 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 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 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 <String > mayList = new ArrayList <String >(); 242 for ( String may : oc.getMay() ) 243 { 244 mayList.add( may ); 245 } 246 holder.setMay( mayList ); 247 List <String > mustList = new ArrayList <String >(); 248 for ( String must : oc.getMust() ) 249 { 250 mustList.add( must ); 251 } 252 holder.setMust( mustList ); 253 List <String > names = new ArrayList <String >(); 254 for ( String name : oc.getNames() ) 255 { 256 names.add( name ); 257 } 258 holder.setNames( names ); 259 List <String > superiorList = new ArrayList <String >(); 260 for ( String 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 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 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 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 BufferedWriter bufferedWriter; 295 try 296 { 297 bufferedWriter = new BufferedWriter ( new FileWriter ( savePath ) ); 298 bufferedWriter.write( sb.toString() ); 299 bufferedWriter.close(); 300 } 301 catch ( IOException 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 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 330 private String [] getSchemaDependencies( Schema schema ) 331 { 332 Set <String > schemaNames = new HashSet <String >(); 333 SchemaPool schemaPool = SchemaPool.getInstance(); 334 335 for ( AttributeType at : schema.getAttributeTypesAsArray() ) 337 { 338 String 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 for ( ObjectClass oc : schema.getObjectClassesAsArray() ) 355 { 356 for ( String 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 for ( String 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 for ( String 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 [0] ); 397 } 398 } 399 | Popular Tags |