KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedHashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.apache.commons.codec.digest.DigestUtils;
33 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
34 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
35 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
36 import org.apache.directory.ldapstudio.browser.core.internal.model.ConnectionException;
37 import org.apache.directory.ldapstudio.browser.core.internal.model.ReferralException;
38 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
39 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
40 import org.apache.directory.ldapstudio.browser.core.model.SearchParameter;
41 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifEnumeration;
42 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer;
43 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord;
44 import org.apache.poi.hssf.usermodel.HSSFCell;
45 import org.apache.poi.hssf.usermodel.HSSFRow;
46 import org.apache.poi.hssf.usermodel.HSSFSheet;
47 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
48 import org.eclipse.core.runtime.Preferences;
49
50
51 public class ExportXlsJob extends AbstractEclipseJob
52 {
53
54     public static final int MAX_COUNT_LIMIT = 65000;
55
56     private String JavaDoc exportLdifFilename;
57
58     private IConnection connection;
59
60     private SearchParameter searchParameter;
61
62     private boolean exportDn;
63
64
65     public ExportXlsJob( String JavaDoc exportLdifFilename, IConnection connection, SearchParameter searchParameter,
66         boolean exportDn )
67     {
68         this.exportLdifFilename = exportLdifFilename;
69         this.connection = connection;
70         this.searchParameter = searchParameter;
71         this.exportDn = exportDn;
72
73         setName( BrowserCoreMessages.jobs__export_xls_name );
74     }
75
76
77     protected IConnection[] getConnections()
78     {
79         return new IConnection[]
80             { connection };
81     }
82
83
84     protected Object JavaDoc[] getLockedObjects()
85     {
86         List JavaDoc l = new ArrayList JavaDoc();
87         l.add( connection.getUrl() + "_" + DigestUtils.shaHex( exportLdifFilename ) );
88         return l.toArray();
89     }
90
91
92     protected void executeAsyncJob( ExtendedProgressMonitor monitor )
93     {
94
95         monitor.beginTask( BrowserCoreMessages.jobs__export_xls_task, 2 );
96         monitor.reportProgress( " " ); //$NON-NLS-1$
97
monitor.worked( 1 );
98
99         Preferences coreStore = BrowserCorePlugin.getDefault().getPluginPreferences();
100         String JavaDoc valueDelimiter = coreStore.getString( BrowserCoreConstants.PREFERENCE_FORMAT_XLS_VALUEDELIMITER );
101         int binaryEncoding = coreStore.getInt( BrowserCoreConstants.PREFERENCE_FORMAT_XLS_BINARYENCODING );
102
103         HSSFWorkbook wb = new HSSFWorkbook();
104         HSSFSheet sheet = wb.createSheet( "Export" ); //$NON-NLS-1$
105

106         // header
107
HSSFRow headerRow = sheet.createRow( 0 );
108         LinkedHashMap JavaDoc attributeNameMap = new LinkedHashMap JavaDoc();
109         if ( this.exportDn )
110         {
111             short cellNum = ( short ) 0;
112             attributeNameMap.put( "dn", new Short JavaDoc( cellNum ) ); //$NON-NLS-1$
113
headerRow.createCell( cellNum ).setCellValue( "dn" ); //$NON-NLS-1$
114
}
115
116         // String[] exportAttributes =
117
// this.searchParameter.getReturningAttributes();
118
// exportAttributes = null;
119
// for (int i = 0; exportAttributes != null && i <
120
// exportAttributes.length; i++) {
121
// short cellNum = (short)attributeNameMap.size();
122
// attributeNameMap.put(exportAttributes[i], new Short(cellNum));
123
// headerRow.createCell(cellNum).setCellValue(exportAttributes[i]);
124
// }
125

126         // max export
127
if ( searchParameter.getCountLimit() < 1 || searchParameter.getCountLimit() > MAX_COUNT_LIMIT )
128         {
129             searchParameter.setCountLimit( MAX_COUNT_LIMIT );
130         }
131
132         // export
133
try
134         {
135
136             int count = 0;
137             export( connection, searchParameter, sheet, headerRow, count, monitor, attributeNameMap, valueDelimiter,
138                 binaryEncoding, this.exportDn );
139         }
140         catch ( Exception JavaDoc e )
141         {
142             monitor.reportError( e );
143         }
144
145         // column width
146
for ( int i = 0; i <= sheet.getLastRowNum(); i++ )
147         {
148             HSSFRow row = sheet.getRow( i );
149             for ( short j = 0; row != null && j <= row.getLastCellNum(); j++ )
150             {
151                 HSSFCell cell = row.getCell( j );
152                 if ( cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_STRING )
153                 {
154                     String JavaDoc value = cell.getStringCellValue();
155
156                     if ( ( short ) ( value.length() * 256 * 1.1 ) > sheet.getColumnWidth( j ) )
157                     {
158                         sheet.setColumnWidth( j, ( short ) ( value.length() * 256 * 1.1 ) );
159                     }
160                 }
161             }
162         }
163
164         try
165         {
166             FileOutputStream JavaDoc fileOut = new FileOutputStream JavaDoc( exportLdifFilename );
167             wb.write( fileOut );
168             fileOut.close();
169         }
170         catch ( Exception JavaDoc e )
171         {
172             monitor.reportError( e );
173         }
174     }
175
176
177     private static void export( IConnection connection, SearchParameter searchParameter, HSSFSheet sheet,
178         HSSFRow headerRow, int count, ExtendedProgressMonitor monitor, LinkedHashMap JavaDoc attributeNameMap,
179         String JavaDoc valueDelimiter, int binaryEncoding, boolean exportDn ) throws IOException JavaDoc, ConnectionException
180     {
181         try
182         {
183
184             LdifEnumeration enumeration = connection.exportLdif( searchParameter, monitor );
185             while ( !monitor.isCanceled() && enumeration.hasNext( monitor ) )
186             {
187                 LdifContainer container = enumeration.next( monitor );
188
189                 if ( container instanceof LdifContentRecord )
190                 {
191                     LdifContentRecord record = ( LdifContentRecord ) container;
192                     recordToHSSFRow( connection, record, sheet, headerRow, attributeNameMap, valueDelimiter, binaryEncoding,
193                         exportDn );
194
195                     count++;
196                     monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__export_progress,
197                         new String JavaDoc[]
198                             { Integer.toString( count ) } ) );
199                 }
200             }
201
202         }
203         catch ( ConnectionException ce )
204         {
205
206             if ( ce.getLdapStatusCode() == 3 || ce.getLdapStatusCode() == 4 || ce.getLdapStatusCode() == 11 )
207             {
208                 // nothing
209
}
210             else if ( ce instanceof ReferralException )
211             {
212
213                 if ( searchParameter.getReferralsHandlingMethod() == IConnection.HANDLE_REFERRALS_FOLLOW )
214                 {
215
216                     ReferralException re = ( ReferralException ) ce;
217                     ISearch[] referralSearches = re.getReferralSearches();
218                     for ( int i = 0; i < referralSearches.length; i++ )
219                     {
220                         ISearch referralSearch = referralSearches[i];
221
222                         // open connection
223
if ( !referralSearch.getConnection().isOpened() )
224                         {
225                             referralSearch.getConnection().open( monitor );
226                         }
227
228                         // export recursive
229
export( referralSearch.getConnection(), referralSearch.getSearchParameter(), sheet, headerRow,
230                             count, monitor, attributeNameMap, valueDelimiter, binaryEncoding, exportDn );
231                     }
232                 }
233             }
234             else
235             {
236                 monitor.reportError( ce );
237             }
238         }
239
240     }
241
242
243     private static void recordToHSSFRow( IConnection connection, LdifContentRecord record, HSSFSheet sheet, HSSFRow headerRow,
244         Map JavaDoc headerRowAttributeNameMap, String JavaDoc valueDelimiter, int binaryEncoding, boolean exportDn )
245     {
246
247         // group multi-valued attributes
248
Map JavaDoc attributeMap = ExportCsvJob.getAttributeMap( null, record, valueDelimiter, "UTF-16", binaryEncoding );
249
250         // output attributes
251
HSSFRow row = sheet.createRow( sheet.getLastRowNum() + 1 );
252         if ( exportDn )
253         {
254             HSSFCell cell = row.createCell( ( short ) 0 );
255             cell.setEncoding( HSSFCell.ENCODING_UTF_16 );
256             cell.setCellValue( record.getDnLine().getValueAsString() );
257         }
258         for ( Iterator JavaDoc it = attributeMap.keySet().iterator(); it.hasNext(); )
259         {
260             String JavaDoc attributeName = ( String JavaDoc ) it.next();
261             String JavaDoc value = ( String JavaDoc ) attributeMap.get( attributeName );
262
263             if ( !headerRowAttributeNameMap.containsKey( attributeName ) )
264             {
265                 short cellNum = ( short ) headerRowAttributeNameMap.size();
266                 headerRowAttributeNameMap.put( attributeName, new Short JavaDoc( cellNum ) );
267                 HSSFCell cell = headerRow.createCell( cellNum );
268                 cell.setEncoding( HSSFCell.ENCODING_UTF_16 );
269                 cell.setCellValue( attributeName );
270             }
271
272             if ( headerRowAttributeNameMap.containsKey( attributeName ) )
273             {
274                 short cellNum = ( ( Short JavaDoc ) headerRowAttributeNameMap.get( attributeName ) ).shortValue();
275                 HSSFCell cell = row.createCell( cellNum );
276                 cell.setEncoding( HSSFCell.ENCODING_UTF_16 );
277                 cell.setCellValue( value );
278             }
279         }
280
281         // for (int i = 0; i < attributes.length; i++) {
282
//
283
// String attributeName = attributes[i];
284
// if (attributeMap.containsKey(attributeName)) {
285
// String value = (String)attributeMap.get(attributeName);
286
// short cellNum = (short)(i + (exportDn?1:0));
287
// row.createCell(cellNum).setCellValue(value);
288
// }
289
// }
290

291     }
292
293
294     protected String JavaDoc getErrorMessage()
295     {
296         return BrowserCoreMessages.jobs__export_xls_error;
297     }
298
299 }
300
Popular Tags