KickJava   Java API By Example, From Geeks To Geeks.

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


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.EventRegistry;
29 import org.apache.directory.ldapstudio.browser.core.events.ValueRenamedEvent;
30 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
31 import org.apache.directory.ldapstudio.browser.core.internal.model.Value;
32 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
33 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
34 import org.apache.directory.ldapstudio.browser.core.model.IValue;
35 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
36
37
38 public class RenameValuesJob extends AbstractModificationJob
39 {
40
41     private IEntry entry;
42
43     private IValue[] oldValues;
44
45     private String JavaDoc newAttributeName;
46
47     private ValueRenamedEvent event;
48
49
50     public RenameValuesJob( IEntry entry, IValue[] oldValues, String JavaDoc newAttributeName )
51     {
52         this.entry = entry;
53         this.oldValues = oldValues;
54         this.newAttributeName = newAttributeName;
55
56         setName( oldValues.length == 1 ? BrowserCoreMessages.jobs__rename_value_name_1
57             : BrowserCoreMessages.jobs__rename_value_name_n );
58     }
59
60
61     protected void executeAsyncModificationJob( ExtendedProgressMonitor monitor ) throws ModelModificationException
62     {
63
64         monitor.beginTask( oldValues.length == 1 ? BrowserCoreMessages.jobs__rename_value_task_1
65             : BrowserCoreMessages.jobs__rename_value_task_n, 2 );
66         monitor.reportProgress( " " ); //$NON-NLS-1$
67
monitor.worked( 1 );
68
69         for ( int i = 0; i < oldValues.length; i++ )
70         {
71             if ( oldValues[i].getAttribute().getEntry() != this.entry )
72             {
73                 return;
74             }
75         }
76
77         IValue[] newValues = new IValue[oldValues.length];
78         for ( int i = 0; i < oldValues.length; i++ )
79         {
80
81             IAttribute newAttribute = entry.getAttribute( newAttributeName );
82             if ( newAttribute == null )
83             {
84                 newAttribute = new Attribute( entry, newAttributeName );
85                 entry.addAttribute( newAttribute );
86             }
87
88             newValues[i] = new Value( newAttribute, oldValues[i].getRawValue() );
89             newAttribute.addValue( newValues[i] );
90
91             oldValues[i].getAttribute().deleteValue( oldValues[i] );
92
93             if ( this.event == null )
94             {
95                 this.event = new ValueRenamedEvent( entry.getConnection(), entry, oldValues[0], newValues[0] );
96             }
97         }
98
99         if ( !monitor.errorsReported() )
100         {
101             entry.getConnection().create( newValues, monitor );
102         }
103         if ( !monitor.errorsReported() )
104         {
105             entry.getConnection().delete( oldValues, monitor );
106         }
107     }
108
109
110     protected IEntry getModifiedEntry()
111     {
112         return this.entry;
113     }
114
115
116     protected String JavaDoc[] getAffectedAttributeNames()
117     {
118         Set JavaDoc affectedAttributeNameSet = new HashSet JavaDoc();
119         affectedAttributeNameSet.add( newAttributeName );
120         for ( int i = 0; i < oldValues.length; i++ )
121         {
122             affectedAttributeNameSet.add( oldValues[i].getAttribute().getDescription() );
123         }
124         return ( String JavaDoc[] ) affectedAttributeNameSet.toArray( new String JavaDoc[affectedAttributeNameSet.size()] );
125     }
126
127
128     protected void runNotification()
129     {
130         if ( this.event != null )
131         {
132             EventRegistry.fireEntryUpdated( this.event, this );
133         }
134     }
135
136
137     protected String JavaDoc getErrorMessage()
138     {
139         return oldValues.length == 1 ? BrowserCoreMessages.jobs__rename_value_error_1
140             : BrowserCoreMessages.jobs__rename_value_error_n;
141     }
142
143 }
144
Popular Tags