KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > jobs > DeleteAttributesValueJob


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.browser.core.jobs;
22
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
28 import org.apache.directory.ldapstudio.browser.core.events.AttributeDeletedEvent;
29 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent;
30 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
31 import org.apache.directory.ldapstudio.browser.core.events.ValueDeletedEvent;
32 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
33 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
34 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
35 import org.apache.directory.ldapstudio.browser.core.model.IValue;
36 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
37
38
39 public class DeleteAttributesValueJob extends AbstractModificationJob
40 {
41
42     private IEntry entry;
43
44     private IAttribute[] attributes;
45
46     private IValue[] values;
47
48     private EntryModificationEvent event;
49
50
51     public DeleteAttributesValueJob( IAttribute attributes[], IValue[] values )
52     {
53         this.attributes = attributes;
54         this.values = values;
55         for ( int i = 0; attributes != null && i < attributes.length; i++ )
56         {
57             if ( this.entry == null )
58             {
59                 this.entry = attributes[i].getEntry();
60             }
61         }
62         for ( int i = 0; values != null && i < values.length; i++ )
63         {
64             if ( this.entry == null )
65             {
66                 this.entry = values[i].getAttribute().getEntry();
67             }
68         }
69
70         setName( attributes.length + values.length == 1 ? BrowserCoreMessages.jobs__delete_attributes_name_1
71             : BrowserCoreMessages.jobs__delete_attributes_name_n );
72     }
73
74
75     public DeleteAttributesValueJob( AttributeHierarchy ah )
76     {
77         this( ah.getAttributes(), new IValue[0] );
78     }
79
80
81     public DeleteAttributesValueJob( IValue value )
82     {
83         this( new IAttribute[0], new IValue[]
84             { value } );
85     }
86
87
88     protected void executeAsyncModificationJob( ExtendedProgressMonitor monitor ) throws ModelModificationException
89     {
90
91         monitor.beginTask( attributes.length + values.length == 1 ? BrowserCoreMessages.jobs__delete_attributes_task_1
92             : BrowserCoreMessages.jobs__delete_attributes_task_n, 2 );
93         monitor.reportProgress( " " ); //$NON-NLS-1$
94
monitor.worked( 1 );
95
96         for ( int i = 0; attributes != null && i < attributes.length; i++ )
97         {
98             attributes[i].getEntry().deleteAttribute( attributes[i] );
99         }
100         for ( int i = 0; values != null && i < values.length; i++ )
101         {
102             values[i].getAttribute().deleteValue( values[i] );
103         }
104
105         entry.getConnection().delete( attributes, monitor );
106         entry.getConnection().delete( values, monitor );
107
108         if ( values.length > 0 )
109         {
110             this.event = new ValueDeletedEvent( entry.getConnection(), entry, values[0].getAttribute(), values[0] );
111         }
112         else if ( attributes.length > 0 )
113         {
114             this.event = new AttributeDeletedEvent( entry.getConnection(), entry, attributes[0] );
115         }
116     }
117
118
119     protected IEntry getModifiedEntry()
120     {
121         return entry;
122     }
123
124
125     protected String JavaDoc[] getAffectedAttributeNames()
126     {
127         Set JavaDoc affectedAttributeNameSet = new HashSet JavaDoc();
128         for ( int i = 0; i < attributes.length; i++ )
129         {
130             affectedAttributeNameSet.add( attributes[i].getDescription() );
131         }
132         for ( int i = 0; i < values.length; i++ )
133         {
134             affectedAttributeNameSet.add( values[i].getAttribute().getDescription() );
135         }
136         return ( String JavaDoc[] ) affectedAttributeNameSet.toArray( new String JavaDoc[affectedAttributeNameSet.size()] );
137     }
138
139
140     protected void runNotification()
141     {
142         if ( this.event != null )
143         {
144             EventRegistry.fireEntryUpdated( this.event, this );
145         }
146     }
147
148
149     protected String JavaDoc getErrorMessage()
150     {
151         return attributes.length + values.length == 1 ? BrowserCoreMessages.jobs__delete_attributes_error_1
152             : BrowserCoreMessages.jobs__delete_attributes_error_n;
153     }
154
155 }
156
Popular Tags