Coverage details for net.sourceforge.demetrix.ui.ProcessChainExcelReader

LineHitsSource
1 /*******************************************************************************
2  
3  * Demetrix process modelling system
4  
5  *
6  
7  * Copyright (c) 2003, 2004 Dimitri A. Pissarenko
8  
9  *
10  
11  * This file is part of Demetrix.
12  
13  *
14  
15  * Demetrix is free software; you can redistribute it and/or modify
16  
17  * it under the terms of the GNU General Public License as published by
18  
19  * the Free Software Foundation; either version 2.1 of the License, or
20  
21  * (at your option) any later version.
22  
23  *
24  
25  * Demetrix is distributed in the hope that it will be useful,
26  
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  
31  * GNU General Public License for more details.
32  
33  *
34  
35  * You should have received a copy of the GNU General Public License
36  
37  * along with Demetrix; if not, write to the Free Software
38  
39  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40  
41  *
42  
43  * For further information you may
44  
45  *
46  
47  * - send an e-mail in Russian, German or English to dimitri.pissarenko@gmx.net
48  
49  * - look at http://sourceforge.net/projects/demetrix/
50  
51  * - look at http://demetrix.sourceforge.net/
52  
53  * - look at http://members.inode.at/d.pissarenko/
54  
55  *
56  
57  *****************************************************************************/
58  
59  
60  
61 package net.sourceforge.demetrix.ui;
62  
63  
64  
65 import java.io.File;
66  
67 import java.io.FileInputStream;
68  
69 import java.io.FileNotFoundException;
70  
71 import java.io.IOException;
72  
73 import java.lang.reflect.Constructor;
74  
75 import java.lang.reflect.InvocationTargetException;
76  
77 import java.util.Hashtable;
78  
79 import java.util.Iterator;
80  
81  
82  
83 import org.apache.log4j.Logger;
84  
85 import org.apache.poi.hssf.usermodel.HSSFCell;
86  
87 import org.apache.poi.hssf.usermodel.HSSFRow;
88  
89 import org.apache.poi.hssf.usermodel.HSSFSheet;
90  
91 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
92  
93  
94  
95 import net.sourceforge.demetrix.model.Link;
96  
97 import net.sourceforge.demetrix.model.ProcessChain;
98  
99 import net.sourceforge.demetrix.model.Resource;
100  
101 import net.sourceforge.demetrix.model.Task;
102  
103 import net.sourceforge.demetrix.properties.BooleanProperty;
104  
105 import net.sourceforge.demetrix.properties.DemetrixPropertiesHolder;
106  
107 import net.sourceforge.demetrix.properties.DemetrixPropertiesStorage;
108  
109 import net.sourceforge.demetrix.properties.DemetrixProperty;
110  
111 import net.sourceforge.demetrix.properties.DoubleProperty;
112  
113 import net.sourceforge.demetrix.properties.StringProperty;
114  
115  
116  
117 /**
118  
119  * @author Dimitri Pissarenko
120  
121  *
122  
123  */
124  
125 public class ProcessChainExcelReader {
126  
127     private File file;
128  
129     private Logger logger=Logger.getLogger(getClass());
130  
131     public ProcessChainExcelReader(File file)
132  
1330    {
134  
1350        this.file=file;
136  
1370    }
138  
139     public ProcessChain read() throws FileNotFoundException, IOException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException
140  
141     {
142  
1430        ProcessChain processChain=null;
144  
1450        FileInputStream fileInputStream=null;
146  
1470        HSSFWorkbook workBook=null;
148  
149         
150  
1510        fileInputStream=new FileInputStream(file);
152  
1530        workBook=new HSSFWorkbook(fileInputStream);
154  
1550        fileInputStream.close();
156  
1570        processChain=new ProcessChain();
158  
159         
160  
1610        this.createDemetrixPropertiesHolders(workBook, processChain, ProcessChainExcelWriter.TASKS_SHEET_NAME, Task.class);
162  
1630        this.createDemetrixPropertiesHolders(workBook, processChain, ProcessChainExcelWriter.RESOURCES_SHEET_NAME, Resource.class);
164  
1650        this.createLinks(workBook, processChain, ProcessChainExcelWriter.LINKS_SHEET_NAME);
166  
167         
168  
1690        return processChain;
170  
171     }
172  
173     
174  
175     private void createDemetrixPropertiesHolders(HSSFWorkbook workBook, ProcessChain processChain, String sheetName, Class propertiesHolderClass) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException
176  
177     {
178  
1790        HSSFSheet sheet=null;
180  
1810        HSSFRow row=null;
182  
1830        HSSFCell cell=null;
184  
1850        Hashtable propertyNames=null;
186  
1870        Iterator cellIterator=null;
188  
1890        DemetrixProperty property=null;
190  
1910        String propertyName=null;
192  
1930        String propertyValue=null;
194  
1950        DemetrixPropertiesHolder propertiesHolder=null;
196  
1970        Constructor noArgsConstructor=null;
198  
1990        int lastRowNumber=-1;
200  
201         
202  
2030        sheet=workBook.getSheet(sheetName);
204  
2050        if (sheet==null)
206  
207         {
208  
209             this.logger.error("could not find worksheet \"" + sheetName + "\" in Microsoft(R) Excel(tm) file \"" + this.file + "\"");
210  
2110            return;
212  
213         }
214  
2150        row=sheet.getRow(sheet.getFirstRowNum()+1);
216  
2170        propertyNames=new Hashtable();
218  
2190        cellIterator=row.cellIterator();
220  
2210        while (cellIterator.hasNext())
222  
223         {
224  
2250            cell=(HSSFCell)cellIterator.next();
226  
2270            propertyNames.put(new Short(cell.getCellNum()), cell.getStringCellValue());
228  
229         }
230  
2310        lastRowNumber=sheet.getLastRowNum();
232  
2330        for (int i=sheet.getFirstRowNum()+2; i <= lastRowNumber; i++)
234  
235         {
236  
2370            row=sheet.getRow(i);
238  
2390            cellIterator=row.cellIterator();
240  
2410            noArgsConstructor=propertiesHolderClass.getConstructor(new Class[0]);
242  
2430            propertiesHolder=(DemetrixPropertiesHolder)noArgsConstructor.newInstance(new Object[0]);
244  
245  
246  
2470            while (cellIterator.hasNext())
248  
249             {
250  
2510                cell=(HSSFCell)cellIterator.next();
252  
253                 
254  
2550                property=this.createAppropriateProperty(cell, propertyNames);
256  
2570                propertiesHolder.addProperty(property);
258  
259             }
260  
2610            if (propertiesHolderClass.equals(Task.class))
262  
263             {
264  
2650                processChain.addTask((Task)propertiesHolder);
266  
267             }
268  
2690            else if (propertiesHolderClass.equals(Resource.class))
270  
271             {
272  
2730                processChain.addResource((Resource)propertiesHolder);
274  
275             }
276  
277             
278  
279         }
280  
2810    }
282  
283     private void createLinks(HSSFWorkbook workBook, ProcessChain processChain, String sheetName)
284  
285     {
286  
2870        HSSFSheet sheet=null;
288  
2890        HSSFRow row=null;
290  
2910        HSSFCell cell=null;
292  
2930        Hashtable propertyNames=null;
294  
2950        Iterator cellIterator=null;
296  
2970        Iterator propertiesIterator=null;
298  
2990        DemetrixProperty property=null;
300  
3010        String propertyName=null;
302  
3030        Link link=null;
304  
3050        DemetrixPropertiesHolder source=null;
306  
3070        DemetrixPropertiesHolder target=null;
308  
3090        DemetrixPropertiesStorage propertiesStorage=null;
310  
3110        int lastRowNumber=-1;
312  
313         
314  
315         
316  
3170        sheet=workBook.getSheet(sheetName);
318  
3190        if (sheet==null)
320  
321         {
322  
323             this.logger.error("could not find worksheet \"" + sheetName + "\" in Microsoft(R) Excel(tm) file \"" + this.file + "\"");
324  
3250            return;
326  
327         }
328  
3290        row=sheet.getRow(sheet.getFirstRowNum()+1);
330  
3310        propertyNames=new Hashtable();
332  
3330        cellIterator=row.cellIterator();
334  
3350        while (cellIterator.hasNext())
336  
337         {
338  
3390            cell=(HSSFCell)cellIterator.next();
340  
341             
342  
3430            propertyNames.put(new Short(cell.getCellNum()), cell.getStringCellValue());
344  
345         }
346  
3470        lastRowNumber=sheet.getLastRowNum();
348  
3490        for (int i=sheet.getFirstRowNum()+2; i <= lastRowNumber; i++)
350  
351         {
352  
3530            row=sheet.getRow(i);
354  
3550            cellIterator=row.cellIterator();
356  
357             
358  
3590            propertiesStorage=new DemetrixPropertiesStorage();
360  
3610            while (cellIterator.hasNext())
362  
363             {
364  
3650                cell=(HSSFCell)cellIterator.next();
366  
367                                 
368  
3690                property=this.createAppropriateProperty(cell, propertyNames);
370  
3710                propertiesStorage.addProperty(property);
372  
373             }
374  
3750            source=(DemetrixPropertiesHolder)processChain.getNodeByName(propertiesStorage.getProperty(Link.SOURCE_PROPERTY).getValue().toString());
376  
3770            target=(DemetrixPropertiesHolder)processChain.getNodeByName(propertiesStorage.getProperty(Link.DESTINATION_PROPERTY).getValue().toString());
378  
379             
380  
381     
382  
3830            propertiesStorage.removeProperty(Link.SOURCE_PROPERTY);
384  
3850            propertiesStorage.removeProperty(Link.DESTINATION_PROPERTY);
386  
387             
388  
389             
390  
3910            link=new Link(source, target);
392  
393             
394  
3950            propertiesIterator=propertiesStorage.getAllProperties();
396  
3970            while (propertiesIterator.hasNext())
398  
399             {
400  
4010                link.addProperty((DemetrixProperty)propertiesIterator.next());
402  
403             }
404  
4050            processChain.addLink(link);
406  
407         }
408  
409         
410  
4110    }
412  
413     private DemetrixProperty createAppropriateProperty(HSSFCell cell, Hashtable propertyNames)
414  
415     {
416  
4170        BooleanProperty booleanProperty=null;
418  
4190        StringProperty stringProperty=null;
420  
4210        DoubleProperty doubleProperty=null;
422  
4230        DemetrixProperty property=null;
424  
425         String propertyName;
426  
4270        String propertyValue=null;
428  
429  
430  
4310        propertyName=(String)propertyNames.get(new Short(cell.getCellNum()));
432  
433         
434  
435  
436  
437  
438  
439         try
440  
441         {
442  
4430            doubleProperty=new DoubleProperty();
444  
4450            doubleProperty.setValue(new Double(cell.getNumericCellValue()));
446  
4470            doubleProperty.setName(propertyName);
448  
4490            property=doubleProperty;
450  
451         }
452  
4530        catch (NumberFormatException exception)
454  
455         {
456  
457             
458  
4590        }
460  
461         
462  
4630        if (property!=null)
464  
465         {
466  
4670            return property;
468  
469         }
470  
4710        propertyValue=cell.getStringCellValue();
472  
4730        if (propertyValue.trim().toLowerCase().equals("true") || propertyValue.trim().toLowerCase().equals("false"))
474  
475         {
476  
477  
478  
4790            booleanProperty=new BooleanProperty();
480  
4810            booleanProperty.setName(propertyName);
482  
4830            if (propertyValue.trim().toLowerCase().equals("true"))
484  
485             {
486  
4870                booleanProperty.setValue(Boolean.TRUE);
488  
489             }
490  
491             else
492  
493             {
494  
4950                booleanProperty.setValue(Boolean.FALSE);
496  
497             }
498  
4990            property=booleanProperty;
500  
501         }
502  
503         else
504  
505         {
506  
5070            stringProperty=new StringProperty();
508  
5090            stringProperty.setName(propertyName);
510  
5110            stringProperty.setValue(propertyValue);
512  
5130            property=stringProperty;
514  
515         }
516  
5170        return property;
518  
519     }
520  
521                 
522  
523     
524  
525 }
526  

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.