1 20 21 package org.apache.directory.ldapstudio.browser.common.actions; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.LinkedHashSet ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants; 35 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 36 import org.apache.directory.ldapstudio.browser.core.jobs.DeleteAttributesValueJob; 37 import org.apache.directory.ldapstudio.browser.core.jobs.DeleteEntriesJob; 38 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy; 39 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 40 import org.apache.directory.ldapstudio.browser.core.model.IBookmark; 41 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 42 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 43 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 44 import org.apache.directory.ldapstudio.browser.core.model.IValue; 45 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 46 import org.apache.directory.ldapstudio.browser.core.model.RDNPart; 47 import org.apache.directory.ldapstudio.browser.core.model.schema.ObjectClassDescription; 48 import org.apache.directory.ldapstudio.browser.core.model.schema.SchemaUtils; 49 import org.eclipse.jface.dialogs.MessageDialog; 50 import org.eclipse.jface.resource.ImageDescriptor; 51 import org.eclipse.ui.ISharedImages; 52 import org.eclipse.ui.PlatformUI; 53 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds; 54 55 56 62 public class DeleteAction extends BrowserAction 63 { 64 67 public String getText() 68 { 69 try 70 { 71 IConnection[] connections = getConnections(); 72 IEntry[] entries = getEntries(); 73 ISearch[] searches = getSearches(); 74 IBookmark[] bookmarks = getBookmarks(); 75 IAttribute[] attributes = getAttributes(); 76 IValue[] values = getValues(); 77 78 if ( connections.length > 0 && entries.length == 0 && searches.length == 0 && bookmarks.length == 0 79 && attributes.length == 0 && values.length == 0 ) 80 { 81 return connections.length > 1 ? "Delete Connections" : "Delete Connection"; 82 } 83 if ( entries.length > 0 && connections.length == 0 && searches.length == 0 && bookmarks.length == 0 84 && attributes.length == 0 && values.length == 0 ) 85 { 86 return entries.length > 1 ? "Delete Entries" : "Delete Entry"; 87 } 88 if ( searches.length > 0 && connections.length == 0 && entries.length == 0 && bookmarks.length == 0 89 && attributes.length == 0 && values.length == 0 ) 90 { 91 return searches.length > 1 ? "Delete Searches" : "Delete Search"; 92 } 93 if ( bookmarks.length > 0 && connections.length == 0 && entries.length == 0 && searches.length == 0 94 && attributes.length == 0 && values.length == 0 ) 95 { 96 return bookmarks.length > 1 ? "Delete Bookmarks" : "Delete Bookmark"; 97 } 98 if ( attributes.length > 0 && connections.length == 0 && entries.length == 0 && searches.length == 0 99 && bookmarks.length == 0 && values.length == 0 ) 100 { 101 return attributes.length > 1 ? "Delete Attributes" : "Delete Attribute"; 102 } 103 if ( values.length > 0 && connections.length == 0 && entries.length == 0 && searches.length == 0 104 && bookmarks.length == 0 && attributes.length == 0 ) 105 { 106 return values.length > 1 ? "Delete Values" : "Delete Value"; 107 } 108 } 109 catch ( Exception e ) 110 { 111 } 112 113 return "Delete"; 114 } 115 116 117 120 public ImageDescriptor getImageDescriptor() 121 { 122 return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor( ISharedImages.IMG_TOOL_DELETE ); 123 } 124 125 126 129 public String getCommandId() 130 { 131 return IWorkbenchActionDefinitionIds.DELETE; 132 } 133 134 135 138 public void run() 139 { 140 try 141 { 142 IConnection[] connections = getConnections(); 143 IEntry[] entries = getEntries(); 144 ISearch[] searches = getSearches(); 145 IBookmark[] bookmarks = getBookmarks(); 146 IAttribute[] attributes = getAttributes(); 147 IValue[] values = getValues(); 148 149 StringBuffer message = new StringBuffer (); 150 151 if ( connections.length > 0 ) 152 { 153 if ( connections.length <= 5 ) 154 { 155 message.append( connections.length == 1 ? "Are your sure to delete the following connection?" 156 : "Are your sure to delete the following connections?" ); 157 for ( int i = 0; i < connections.length; i++ ) 158 { 159 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 160 message.append( " - " ); 161 message.append( connections[i].getName() ); 162 } 163 } 164 else 165 { 166 message.append( "Are your sure to delete the selected connections?" ); 167 } 168 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 169 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 170 } 171 172 if ( entries.length > 0 ) 173 { 174 if ( entries.length <= 5 ) 175 { 176 message 177 .append( entries.length == 1 ? "Are your sure to delete the following entry, including all children?" 178 : "Are your sure to delete the following entries, including all children?" ); 179 for ( int i = 0; i < entries.length; i++ ) 180 { 181 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 182 message.append( " - " ); 183 message.append( entries[i].getDn() ); 184 } 185 } 186 else 187 { 188 message.append( "Are your sure to delete the selected entries, including all children?" ); 189 } 190 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 191 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 192 } 193 194 if ( searches.length > 0 ) 195 { 196 if ( searches.length <= 5 ) 197 { 198 message.append( searches.length == 1 ? "Are your sure to delete the following search?" 199 : "Are your sure to delete the following searches?" ); 200 for ( int i = 0; i < searches.length; i++ ) 201 { 202 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 203 message.append( " - " ); 204 message.append( searches[i].getName() ); 205 } 206 } 207 else 208 { 209 message.append( "Are your sure to delete the selected searches?" ); 210 } 211 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 212 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 213 } 214 215 if ( bookmarks.length > 0 ) 216 { 217 if ( bookmarks.length <= 5 ) 218 { 219 message.append( bookmarks.length == 1 ? "Are your sure to delete the following bookmark?" 220 : "Are your sure to delete the following bookmarks?" ); 221 for ( int i = 0; i < bookmarks.length; i++ ) 222 { 223 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 224 message.append( " - " ); 225 message.append( bookmarks[i].getName() ); 226 } 227 } 228 else 229 { 230 message.append( "Are your sure to delete the selected bookmarks?" ); 231 } 232 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 233 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 234 } 235 236 if ( attributes.length > 0 ) 237 { 238 if ( attributes.length <= 5 ) 239 { 240 message.append( attributes.length == 1 ? "Are your sure to delete the following attribute?" 241 : "Are your sure to delete the following attribute?" ); 242 for ( int i = 0; i < attributes.length; i++ ) 243 { 244 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 245 message.append( " - " ); 246 message.append( attributes[i].getDescription() ); 247 } 248 } 249 else 250 { 251 message.append( "Are your sure to delete the selected attributes?" ); 252 } 253 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 254 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 255 } 256 257 if ( values.length > 0 ) 258 { 259 boolean emptyValuesOnly = true; 260 for ( int i = 0; i < values.length; i++ ) 261 { 262 if ( !values[i].isEmpty() ) 263 { 264 emptyValuesOnly = false; 265 } 266 } 267 if ( !emptyValuesOnly ) 268 { 269 if ( values.length <= 5 ) 270 { 271 message.append( values.length == 1 ? "Are your sure to delete the following value?" 272 : "Are your sure to delete the following values?" ); 273 for ( int i = 0; i < values.length; i++ ) 274 { 275 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 276 message.append( " - " ); 277 message.append( values[i].toString() ); 278 } 279 } 280 else 281 { 282 message.append( "Are your sure to delete the selected values?" ); 283 } 284 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 285 message.append( BrowserCoreConstants.LINE_SEPARATOR ); 286 } 287 } 288 289 if ( message.length() == 0 || MessageDialog.openConfirm( getShell(), getText(), message.toString() ) ) 290 { 291 292 if ( connections.length > 0 ) 293 { 294 deleteConnections( connections ); 295 } 296 if ( entries.length > 0 ) 297 { 298 deleteEntries( entries ); 299 } 300 if ( searches.length > 0 ) 301 { 302 deleteSearches( searches ); 303 } 304 if ( bookmarks.length > 0 ) 305 { 306 deleteBookmarks( bookmarks ); 307 } 308 if ( attributes.length + values.length > 0 ) 309 { 310 311 List attributeList = new ArrayList ( Arrays.asList( attributes ) ); 312 List valueList = new ArrayList ( Arrays.asList( values ) ); 313 314 for ( Iterator it = attributeList.iterator(); it.hasNext(); ) 316 { 317 IAttribute att = ( IAttribute ) it.next(); 318 IValue[] vals = att.getValues(); 319 for ( int i = 0; i < vals.length; i++ ) 320 { 321 if ( vals[i].isEmpty() ) 322 { 323 att.deleteEmptyValue(); 324 } 325 } 326 if ( att.getValueSize() == 0 ) 327 { 328 try 329 { 330 att.getEntry().deleteAttribute( att ); 331 } 332 catch ( ModelModificationException e ) 333 { 334 } 335 it.remove(); 336 } 337 } 338 for ( Iterator it = valueList.iterator(); it.hasNext(); ) 339 { 340 IValue value = ( IValue ) it.next(); 341 if ( value.isEmpty() ) 342 { 343 value.getAttribute().deleteEmptyValue(); 344 it.remove(); 345 } 346 } 347 348 if ( !attributeList.isEmpty() || !valueList.isEmpty() ) 349 { 350 deleteAttributesAndValues( ( IAttribute[] ) attributeList.toArray( new IAttribute[attributeList 351 .size()] ), ( IValue[] ) valueList.toArray( new IValue[valueList.size()] ) ); 352 } 353 } 354 } 355 } 356 catch ( Exception e ) 357 { 358 } 359 } 360 361 362 365 public boolean isEnabled() 366 { 367 try 368 { 369 IConnection[] connections = getConnections(); 370 IEntry[] entries = getEntries(); 371 ISearch[] searches = getSearches(); 372 IBookmark[] bookmarks = getBookmarks(); 373 IAttribute[] attributes = getAttributes(); 374 IValue[] values = getValues(); 375 376 return connections.length + entries.length + searches.length + bookmarks.length + attributes.length 377 + values.length > 0; 378 379 } 380 catch ( Exception e ) 381 { 382 return false; 384 } 385 } 386 387 388 396 protected IConnection[] getConnections() throws Exception 397 { 398 for ( int i = 0; i < getSelectedConnections().length; i++ ) 399 { 400 if ( getSelectedConnections()[i].isOpened() ) 401 { 402 throw new Exception (); 403 } 404 } 405 406 return getSelectedConnections(); 407 } 408 409 410 416 protected void deleteConnections( IConnection[] connections ) 417 { 418 for ( int i = 0; i < connections.length; i++ ) 419 { 420 BrowserCorePlugin.getDefault().getConnectionManager().removeConnection( connections[i] ); 421 } 422 } 423 424 425 433 protected IEntry[] getEntries() throws Exception 434 { 435 LinkedHashSet entriesSet = new LinkedHashSet (); 436 for ( int i = 0; i < getSelectedEntries().length; i++ ) 437 { 438 if ( !getSelectedEntries()[i].hasParententry() ) 439 { 440 throw new Exception (); 441 } 442 entriesSet.add( getSelectedEntries()[i] ); 443 } 444 for ( int i = 0; i < this.getSelectedSearchResults().length; i++ ) 445 { 446 if ( !getSelectedSearchResults()[i].getEntry().hasParententry() ) 447 { 448 throw new Exception (); 449 } 450 entriesSet.add( this.getSelectedSearchResults()[i].getEntry() ); 451 } 452 453 IEntry[] allEntries = ( IEntry[] ) entriesSet.toArray( new IEntry[entriesSet.size()] ); 454 for ( int i = 0; i < allEntries.length; i++ ) 455 { 456 IEntry entry = allEntries[i]; 457 if ( entriesSet.contains( entry.getParententry() ) ) 458 { 459 entriesSet.remove( entry ); 460 } 461 } 462 463 return ( IEntry[] ) entriesSet.toArray( new IEntry[entriesSet.size()] ); 464 } 465 466 467 473 protected void deleteEntries( IEntry[] entries ) 474 { 475 new DeleteEntriesJob( entries ).execute(); 476 } 477 478 479 486 protected ISearch[] getSearches() throws Exception 487 { 488 return getSelectedSearches(); 489 } 490 491 492 498 protected void deleteSearches( ISearch[] searches ) 499 { 500 for ( int i = 0; i < searches.length; i++ ) 501 { 502 ISearch search = searches[i]; 503 search.getConnection().getSearchManager().removeSearch( search ); 504 } 505 } 506 507 508 514 protected IBookmark[] getBookmarks() throws Exception 515 { 516 return getSelectedBookmarks(); 517 } 518 519 520 526 protected void deleteBookmarks( IBookmark[] bookmarks ) 527 { 528 for ( int i = 0; i < bookmarks.length; i++ ) 529 { 530 IBookmark bookmark = bookmarks[i]; 531 bookmark.getConnection().getBookmarkManager().removeBookmark( bookmark ); 532 } 533 } 534 535 536 543 protected IAttribute[] getAttributes() throws Exception 544 { 545 546 for ( int i = 0; i < getSelectedAttributes().length; i++ ) 547 { 548 IAttribute att = getSelectedAttributes()[i]; 551 if ( !SchemaUtils.isModifyable( att.getAttributeTypeDescription() ) || att.isMustAttribute() 552 || att.isObjectClassAttribute() ) 553 { 554 throw new Exception (); 555 } 556 } 557 558 for ( int i = 0; i < getSelectedAttributeHierarchies().length; i++ ) 559 { 560 AttributeHierarchy ah = getSelectedAttributeHierarchies()[i]; 563 for ( Iterator it = ah.iterator(); it.hasNext(); ) 564 { 565 IAttribute attribute = ( IAttribute ) it.next(); 566 if ( !SchemaUtils.isModifyable( attribute.getAttributeTypeDescription() ) 567 || attribute.isMustAttribute() || attribute.isObjectClassAttribute() ) 568 { 569 throw new Exception (); 570 } 571 } 572 } 573 574 List attributeList = new ArrayList (); 575 576 for ( int i = 0; i < getSelectedAttributes().length; i++ ) 578 { 579 IAttribute attribute = getSelectedAttributes()[i]; 580 if ( attribute != null && attribute.getValueSize() > 0 ) 581 { 582 attributeList.add( attribute ); 583 } 584 } 585 586 for ( int i = 0; i < getSelectedAttributeHierarchies().length; i++ ) 588 { 589 AttributeHierarchy ah = getSelectedAttributeHierarchies()[i]; 592 for ( Iterator it = ah.iterator(); it.hasNext(); ) 593 { 594 IAttribute attribute = ( IAttribute ) it.next(); 595 if ( attribute != null && attribute.getValueSize() > 0 ) 596 { 597 attributeList.add( attribute ); 598 } 599 } 600 } 601 602 Map attributeNameToSelectedValuesCountMap = new HashMap (); 605 for ( int i = 0; i < getSelectedValues().length; i++ ) 606 { 607 if ( !attributeNameToSelectedValuesCountMap.containsKey( getSelectedValues()[i].getAttribute() 608 .getDescription() ) ) 609 { 610 attributeNameToSelectedValuesCountMap.put( getSelectedValues()[i].getAttribute().getDescription(), 611 new Integer ( 0 ) ); 612 } 613 int count = ( ( Integer ) attributeNameToSelectedValuesCountMap.get( getSelectedValues()[i].getAttribute() 614 .getDescription() ) ).intValue() + 1; 615 attributeNameToSelectedValuesCountMap.put( getSelectedValues()[i].getAttribute().getDescription(), 616 new Integer ( count ) ); 617 if ( count >= getSelectedValues()[i].getAttribute().getValueSize() ) 618 { 619 IAttribute attribute = getSelectedValues()[i].getAttribute(); 620 if ( attribute != null && !attributeList.contains( attribute ) ) 621 { 622 attributeList.add( attribute ); 623 } 624 } 625 } 626 627 return ( IAttribute[] ) attributeList.toArray( new IAttribute[attributeList.size()] ); 628 } 629 630 631 638 protected IValue[] getValues() throws Exception 639 { 640 641 Map attributeNameToSelectedValuesCountMap = new HashMap (); 642 Set selectedObjectClasses = new HashSet (); 643 for ( int i = 0; i < getSelectedValues().length; i++ ) 644 { 645 if ( !SchemaUtils.isModifyable( getSelectedValues()[i].getAttribute().getAttributeTypeDescription() ) ) 647 { 648 throw new Exception (); 649 } 650 651 RDNPart[] parts = this.getSelectedValues()[i].getAttribute().getEntry().getRdn().getParts(); 653 for ( int p = 0; p < parts.length; p++ ) 654 { 655 if ( getSelectedValues()[i].getAttribute().getDescription().equals( parts[p].getType() ) 656 && getSelectedValues()[i].getStringValue().equals( parts[p].getValue() ) ) 657 { 658 throw new Exception (); 659 } 660 } 661 662 if ( getSelectedValues()[i].getAttribute().isObjectClassAttribute() ) 664 { 665 selectedObjectClasses.add( getSelectedValues()[i].getStringValue() ); 666 } 667 668 if ( !attributeNameToSelectedValuesCountMap.containsKey( getSelectedValues()[i].getAttribute() 671 .getDescription() ) ) 672 { 673 attributeNameToSelectedValuesCountMap.put( getSelectedValues()[i].getAttribute().getDescription(), 674 new Integer ( 0 ) ); 675 } 676 int count = ( ( Integer ) attributeNameToSelectedValuesCountMap.get( getSelectedValues()[i].getAttribute() 677 .getDescription() ) ).intValue() + 1; 678 attributeNameToSelectedValuesCountMap.put( getSelectedValues()[i].getAttribute().getDescription(), 679 new Integer ( count ) ); 680 if ( ( getSelectedValues()[i].getAttribute().isObjectClassAttribute() || getSelectedValues()[i] 681 .getAttribute().isMustAttribute() ) 685 && count >= getSelectedValues()[i].getAttribute().getValueSize() ) 686 { 687 throw new Exception (); 688 } 689 } 690 if ( getSelectedValues().length > 0 && !selectedObjectClasses.isEmpty() ) 692 { 693 IEntry entry = getSelectedValues()[0].getAttribute().getEntry(); 694 String [] ocValues = entry.getSubschema().getObjectClassNames(); 696 Set remainingObjectClassesSet = new HashSet ( Arrays.asList( ocValues ) ); 697 remainingObjectClassesSet.removeAll( selectedObjectClasses ); 698 Set remainingAttributeSet = new HashSet (); 699 for ( Iterator it = remainingObjectClassesSet.iterator(); it.hasNext(); ) 700 { 701 String oc = ( String ) it.next(); 702 ObjectClassDescription ocd = entry.getConnection().getSchema().getObjectClassDescription( oc ); 703 if ( ocd != null ) 704 { 705 remainingAttributeSet 706 .addAll( Arrays.asList( ocd.getMustAttributeTypeDescriptionNamesTransitive() ) ); 707 remainingAttributeSet.addAll( Arrays.asList( ocd.getMayAttributeTypeDescriptionNamesTransitive() ) ); 708 } 709 } 710 IAttribute[] attributes = entry.getAttributes(); 712 for ( int i = 0; i < attributes.length; i++ ) 713 { 714 IAttribute attribute = attributes[i]; 715 if ( attribute.isMayAttribute() || attribute.isMustAttribute() ) 716 { 717 if ( !remainingAttributeSet.contains( attribute.getType() ) ) 718 { 719 throw new Exception (); 720 } 721 } 722 } 723 } 724 725 List valueList = new ArrayList (); 726 727 Set attributeSet = new HashSet ( Arrays.asList( getAttributes() ) ); 729 for ( int i = 0; i < getSelectedValues().length; i++ ) 730 { 731 if ( !attributeSet.contains( getSelectedValues()[i].getAttribute() ) ) 732 { 733 valueList.add( getSelectedValues()[i] ); 734 } 735 } 736 737 return ( IValue[] ) valueList.toArray( new IValue[valueList.size()] ); 738 } 739 740 741 749 protected void deleteAttributesAndValues( IAttribute[] attributes, IValue[] values ) 750 { 751 new DeleteAttributesValueJob( attributes, values ).execute(); 752 } 753 } 754 | Popular Tags |