KickJava   Java API By Example, From Geeks To Geeks.

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


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.ValueAddedEvent;
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 CreateValuesJob extends AbstractModificationJob
39 {
40
41     private IEntry entry;
42
43     private String JavaDoc[] attributeDescriptions;
44
45     private Object JavaDoc[] rawValues;
46
47     private ValueAddedEvent event;
48
49
50     public CreateValuesJob( IEntry entry, String JavaDoc[] attributeDescriptions, Object JavaDoc[] rawValues )
51     {
52         this.entry = entry;
53         this.attributeDescriptions = attributeDescriptions;
54         this.rawValues = rawValues;
55
56         setName( rawValues.length == 1 ? BrowserCoreMessages.jobs__create_values_name_1
57             : BrowserCoreMessages.jobs__create_values_name_n );
58     }
59
60
61     public CreateValuesJob( IAttribute attribute, Object JavaDoc newValue )
62     {
63         this( attribute.getEntry(), new String JavaDoc[]
64             { attribute.getDescription() }, new Object JavaDoc[]
65             { newValue } );
66     }
67
68
69     protected void executeAsyncModificationJob( ExtendedProgressMonitor monitor ) throws ModelModificationException
70     {
71
72         monitor.beginTask( rawValues.length == 1 ? BrowserCoreMessages.jobs__create_values_task_1
73             : BrowserCoreMessages.jobs__create_values_task_n, 2 );
74         monitor.reportProgress( " " ); //$NON-NLS-1$
75
monitor.worked( 1 );
76
77         IValue[] newValues = new IValue[rawValues.length];
78         for ( int i = 0; i < newValues.length; i++ )
79         {
80             IAttribute attribute = entry.getAttribute( attributeDescriptions[i] );
81             if ( attribute == null )
82             {
83                 // String[] possibleAttributeNames =
84
// entry.getSubschema().getAllAttributeNames();
85
// if(!Arrays.asList(possibleAttributeNames).contains(attributeNames[i]))
86
// {
87
// throw new ModelModificationException("Attribute
88
// "+attributeNames[i]+" is not in subschema");
89
// }
90
attribute = new Attribute( entry, attributeDescriptions[i] );
91                 entry.addAttribute( attribute );
92             }
93
94             newValues[i] = new Value( attribute, rawValues[i] );
95             attribute.addValue( newValues[i] );
96
97             if ( this.event == null )
98             {
99                 event = new ValueAddedEvent( entry.getConnection(), entry, attribute, newValues[i] );
100             }
101         }
102
103         entry.getConnection().create( newValues, monitor );
104     }
105
106
107     protected IEntry getModifiedEntry()
108     {
109         return entry;
110     }
111
112
113     protected String JavaDoc[] getAffectedAttributeNames()
114     {
115         Set JavaDoc affectedAttributeNameSet = new HashSet JavaDoc();
116         for ( int i = 0; i < attributeDescriptions.length; i++ )
117         {
118             affectedAttributeNameSet.add( attributeDescriptions[i] );
119         }
120         return ( String JavaDoc[] ) affectedAttributeNameSet.toArray( new String JavaDoc[affectedAttributeNameSet.size()] );
121     }
122
123
124     protected void runNotification()
125     {
126         if ( this.event != null )
127         {
128             EventRegistry.fireEntryUpdated( this.event, this );
129         }
130     }
131
132
133     protected String JavaDoc getErrorMessage()
134     {
135         return attributeDescriptions.length == 1 ? BrowserCoreMessages.jobs__create_values_error_1
136             : BrowserCoreMessages.jobs__create_values_error_n;
137     }
138
139 }
140
Popular Tags