KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > actions > CopyEntryAsCsvAction


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.ui.actions;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
32 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
33 import org.apache.directory.ldapstudio.browser.core.internal.model.AttributeComparator;
34 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
35 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
36 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
37 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
38 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
39 import org.apache.directory.ldapstudio.browser.core.model.IValue;
40 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils;
41 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
42 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
43 import org.eclipse.jface.resource.ImageDescriptor;
44
45
46 /**
47  * This Action copies entry(ies) as CSV.
48  *
49  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
50  * @version $Rev$, $Date$
51  */

52 public class CopyEntryAsCsvAction extends CopyEntryAsAction
53 {
54     /**
55      * Table Mode.
56      */

57     public static final int MODE_TABLE = 5;
58
59
60     /**
61      * Creates a new instance of CopyEntryAsCsvAction.
62      *
63      * @param mode
64      * the copy Mode
65      */

66     public CopyEntryAsCsvAction( int mode )
67     {
68         super( "CSV", mode );
69     }
70
71
72     /**
73      * {@inheritDoc}
74      */

75     public ImageDescriptor getImageDescriptor()
76     {
77         if ( this.mode == MODE_DN_ONLY )
78         {
79             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_CSV );
80         }
81         else if ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY )
82         {
83             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_CSV_SEARCHRESULT );
84         }
85         else if ( this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES )
86         {
87             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_CSV_OPERATIONAL );
88         }
89         else if ( this.mode == MODE_NORMAL )
90         {
91             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_CSV_USER );
92         }
93         else if ( this.mode == MODE_TABLE )
94         {
95             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_TABLE );
96         }
97         else
98         {
99             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_CSV );
100         }
101     }
102
103
104     /**
105      * {@inheritDoc}
106      */

107     public String JavaDoc getText()
108     {
109         if ( this.mode == MODE_TABLE )
110         {
111             return "Copy Table";
112         }
113
114         return super.getText();
115     }
116
117
118     /**
119      * {@inheritDoc}
120      */

121     public boolean isEnabled()
122     {
123         if ( this.mode == MODE_TABLE )
124         {
125             return getInput() != null && getInput() instanceof ISearch
126                 && ( ( ISearch ) getInput() ).getSearchResults() != null
127                 && ( ( ISearch ) getInput() ).getSearchResults().length > 0;
128         }
129
130         return super.isEnabled();
131     }
132
133
134     /**
135      * {@inheritDoc}
136      */

137     public void run()
138     {
139
140         if ( this.mode == MODE_TABLE )
141         {
142             if ( getInput() != null && getInput() instanceof ISearch
143                 && ( ( ISearch ) getInput() ).getSearchResults() != null
144                 && ( ( ISearch ) getInput() ).getSearchResults().length > 0 )
145             {
146                 List JavaDoc<IEntry> entryList = new ArrayList JavaDoc<IEntry>();
147                 ISearchResult[] results = ( ( ISearch ) getInput() ).getSearchResults();
148                 for ( int k = 0; k < results.length; k++ )
149                 {
150                     entryList.add( results[k].getEntry() );
151                 }
152                 IEntry[] entries = ( IEntry[] ) entryList.toArray( new IEntry[entryList.size()] );
153
154                 StringBuffer JavaDoc text = new StringBuffer JavaDoc();
155                 serialializeEntries( entries, text );
156                 copyToClipboard( text.toString() );
157             }
158         }
159         else
160         {
161             super.run();
162         }
163
164     }
165
166
167     /**
168      * {@inheritDoc}
169      */

170     public void serialializeEntries( IEntry[] entries, StringBuffer JavaDoc text )
171     {
172
173         String JavaDoc attributeDelimiter = BrowserCommonActivator.getDefault().getPreferenceStore().getString(
174             BrowserCommonConstants.PREFERENCE_FORMAT_TABLE_ATTRIBUTEDELIMITER );
175         String JavaDoc valueDelimiter = BrowserCommonActivator.getDefault().getPreferenceStore().getString(
176             BrowserCommonConstants.PREFERENCE_FORMAT_TABLE_VALUEDELIMITER );
177         String JavaDoc quoteCharacter = BrowserCommonActivator.getDefault().getPreferenceStore().getString(
178             BrowserCommonConstants.PREFERENCE_FORMAT_TABLE_QUOTECHARACTER );
179         String JavaDoc lineSeparator = BrowserCommonActivator.getDefault().getPreferenceStore().getString(
180             BrowserCommonConstants.PREFERENCE_FORMAT_TABLE_LINESEPARATOR );
181         int binaryEncoding = BrowserCommonActivator.getDefault().getPreferenceStore().getInt(
182             BrowserCommonConstants.PREFERENCE_FORMAT_TABLE_BINARYENCODING );
183
184         String JavaDoc[] returningAttributes = null;
185         if ( this.mode == MODE_DN_ONLY )
186         {
187             returningAttributes = new String JavaDoc[0];
188         }
189         else if ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY && getSelectedSearchResults().length > 0
190             && getSelectedEntries().length + getSelectedBookmarks().length + getSelectedSearches().length == 0 )
191         {
192             returningAttributes = getSelectedSearchResults()[0].getSearch().getReturningAttributes();
193         }
194         else if ( ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY || this.mode == MODE_TABLE )
195             && getSelectedSearches().length == 1 )
196         {
197             returningAttributes = getSelectedSearches()[0].getReturningAttributes();
198         }
199         else if ( ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY || this.mode == MODE_TABLE )
200             && ( getInput() instanceof ISearch ) )
201         {
202             returningAttributes = ( ( ISearch ) ( getInput() ) ).getReturningAttributes();
203         }
204         else
205         {
206             Map JavaDoc<String JavaDoc, IAttribute> attributeMap = new HashMap JavaDoc<String JavaDoc, IAttribute>();
207             for ( int e = 0; entries != null && e < entries.length; e++ )
208             {
209                 IAttribute[] attributes = entries[e].getAttributes();
210                 for ( int a = 0; attributes != null && a < attributes.length; a++ )
211                 {
212
213                     if ( attributes[a].isOperationalAttribute() && this.mode != MODE_INCLUDE_OPERATIONAL_ATTRIBUTES )
214                         continue;
215
216                     if ( !attributeMap.containsKey( attributes[a].getDescription() ) )
217                     {
218                         attributeMap.put( attributes[a].getDescription(), attributes[a] );
219                     }
220                 }
221             }
222             IAttribute[] attributes = ( IAttribute[] ) attributeMap.values().toArray(
223                 new IAttribute[attributeMap.size()] );
224
225             if ( attributes.length > 0 )
226             {
227                 AttributeComparator comparator = new AttributeComparator( entries[0].getConnection() );
228                 Arrays.sort( attributes, comparator );
229             }
230
231             returningAttributes = new String JavaDoc[attributes.length];
232             for ( int i = 0; i < attributes.length; i++ )
233             {
234                 returningAttributes[i] = attributes[i].getDescription();
235             }
236         }
237
238         // header
239
if ( this.mode != MODE_TABLE
240             || BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean(
241                 BrowserUIConstants.PREFERENCE_SEARCHRESULTEDITOR_SHOW_DN ) )
242         {
243             text.append( quoteCharacter );
244             text.append( "DN" );
245             text.append( quoteCharacter );
246             text.append( attributeDelimiter );
247         }
248         for ( int a = 0; returningAttributes != null && a < returningAttributes.length; a++ )
249         {
250             text.append( quoteCharacter );
251             text.append( returningAttributes[a] );
252             text.append( quoteCharacter );
253             if ( a + 1 < returningAttributes.length )
254             {
255                 text.append( attributeDelimiter );
256             }
257         }
258         text.append( lineSeparator );
259
260         for ( int e = 0; entries != null && e < entries.length; e++ )
261         {
262
263             if ( this.mode != MODE_TABLE
264                 || BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean(
265                     BrowserUIConstants.PREFERENCE_SEARCHRESULTEDITOR_SHOW_DN ) )
266             {
267                 text.append( quoteCharacter );
268                 text.append( entries[e].getDn().toString() );
269                 text.append( quoteCharacter );
270                 text.append( attributeDelimiter );
271
272             }
273             for ( int a = 0; returningAttributes != null && a < returningAttributes.length; a++ )
274             {
275
276                 AttributeComparator comparator = new AttributeComparator( entries[e] );
277                 AttributeHierarchy ah = entries[e].getAttributeWithSubtypes( returningAttributes[a] );
278                 if ( ah != null )
279                 {
280
281                     StringBuffer JavaDoc valueSB = new StringBuffer JavaDoc();
282
283                     for ( Iterator JavaDoc it = ah.iterator(); it.hasNext(); )
284                     {
285                         IAttribute attribute = ( IAttribute ) it.next();
286                         if ( attribute != null )
287                         {
288
289                             IValue[] values = attribute.getValues();
290                             Arrays.sort( values, comparator );
291
292                             for ( int v = 0; v < values.length; v++ )
293                             {
294                                 String JavaDoc val = LdifUtils.getStringValue( values[v], binaryEncoding );
295                                 valueSB.append( val );
296                                 if ( v + 1 < values.length )
297                                 {
298                                     valueSB.append( valueDelimiter );
299                                 }
300                             }
301                         }
302
303                         if ( it.hasNext() )
304                         {
305                             valueSB.append( valueDelimiter );
306                         }
307                     }
308
309                     String JavaDoc value = valueSB.toString().replaceAll( quoteCharacter, quoteCharacter + quoteCharacter );
310                     text.append( quoteCharacter );
311                     text.append( value );
312                     text.append( quoteCharacter );
313
314                 }
315
316                 // IAttribute attribute =
317
// entries[e].getAttribute(returningAttributes[a]);
318
// if (attribute != null) {
319
//
320
// IValue[] values = attribute.getValues();
321
// Arrays.sort(values, comparator);
322
//
323
// StringBuffer valueSB = new StringBuffer();
324
// for (int v = 0; v < values.length; v++) {
325
// String val = LdifUtils.getStringValue(values[v],
326
// binaryEncoding);
327
// valueSB.append(val);
328
// if (v + 1 < values.length) {
329
// valueSB.append(valueDelimiter);
330
// ;
331
// }
332
// }
333
//
334
// String value = valueSB.toString().replaceAll(quoteCharacter,
335
// quoteCharacter + quoteCharacter);
336
// text.append(quoteCharacter);
337
// text.append(value);
338
// text.append(quoteCharacter);
339
//
340
// }
341

342                 if ( a + 1 < returningAttributes.length )
343                 {
344                     text.append( attributeDelimiter );
345                 }
346             }
347
348             if ( e < entries.length )
349             {
350                 text.append( lineSeparator );
351             }
352         }
353     }
354 }
355
Popular Tags