Line | Hits | Source |
---|---|---|
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 | ||
133 | 0 | { |
134 | ||
135 | 0 | this.file=file; |
136 | ||
137 | 0 | } |
138 | ||
139 | public ProcessChain read() throws FileNotFoundException, IOException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException | |
140 | ||
141 | { | |
142 | ||
143 | 0 | ProcessChain processChain=null; |
144 | ||
145 | 0 | FileInputStream fileInputStream=null; |
146 | ||
147 | 0 | HSSFWorkbook workBook=null; |
148 | ||
149 | ||
150 | ||
151 | 0 | fileInputStream=new FileInputStream(file); |
152 | ||
153 | 0 | workBook=new HSSFWorkbook(fileInputStream); |
154 | ||
155 | 0 | fileInputStream.close(); |
156 | ||
157 | 0 | processChain=new ProcessChain(); |
158 | ||
159 | ||
160 | ||
161 | 0 | this.createDemetrixPropertiesHolders(workBook, processChain, ProcessChainExcelWriter.TASKS_SHEET_NAME, Task.class); |
162 | ||
163 | 0 | this.createDemetrixPropertiesHolders(workBook, processChain, ProcessChainExcelWriter.RESOURCES_SHEET_NAME, Resource.class); |
164 | ||
165 | 0 | this.createLinks(workBook, processChain, ProcessChainExcelWriter.LINKS_SHEET_NAME); |
166 | ||
167 | ||
168 | ||
169 | 0 | 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 | ||
179 | 0 | HSSFSheet sheet=null; |
180 | ||
181 | 0 | HSSFRow row=null; |
182 | ||
183 | 0 | HSSFCell cell=null; |
184 | ||
185 | 0 | Hashtable propertyNames=null; |
186 | ||
187 | 0 | Iterator cellIterator=null; |
188 | ||
189 | 0 | DemetrixProperty property=null; |
190 | ||
191 | 0 | String propertyName=null; |
192 | ||
193 | 0 | String propertyValue=null; |
194 | ||
195 | 0 | DemetrixPropertiesHolder propertiesHolder=null; |
196 | ||
197 | 0 | Constructor noArgsConstructor=null; |
198 | ||
199 | 0 | int lastRowNumber=-1; |
200 | ||
201 | ||
202 | ||
203 | 0 | sheet=workBook.getSheet(sheetName); |
204 | ||
205 | 0 | if (sheet==null) |
206 | ||
207 | { | |
208 | ||
209 | this.logger.error("could not find worksheet \"" + sheetName + "\" in Microsoft(R) Excel(tm) file \"" + this.file + "\""); | |
210 | ||
211 | 0 | return; |
212 | ||
213 | } | |
214 | ||
215 | 0 | row=sheet.getRow(sheet.getFirstRowNum()+1); |
216 | ||
217 | 0 | propertyNames=new Hashtable(); |
218 | ||
219 | 0 | cellIterator=row.cellIterator(); |
220 | ||
221 | 0 | while (cellIterator.hasNext()) |
222 | ||
223 | { | |
224 | ||
225 | 0 | cell=(HSSFCell)cellIterator.next(); |
226 | ||
227 | 0 | propertyNames.put(new Short(cell.getCellNum()), cell.getStringCellValue()); |
228 | ||
229 | } | |
230 | ||
231 | 0 | lastRowNumber=sheet.getLastRowNum(); |
232 | ||
233 | 0 | for (int i=sheet.getFirstRowNum()+2; i <= lastRowNumber; i++) |
234 | ||
235 | { | |
236 | ||
237 | 0 | row=sheet.getRow(i); |
238 | ||
239 | 0 | cellIterator=row.cellIterator(); |
240 | ||
241 | 0 | noArgsConstructor=propertiesHolderClass.getConstructor(new Class[0]); |
242 | ||
243 | 0 | propertiesHolder=(DemetrixPropertiesHolder)noArgsConstructor.newInstance(new Object[0]); |
244 | ||
245 | ||
246 | ||
247 | 0 | while (cellIterator.hasNext()) |
248 | ||
249 | { | |
250 | ||
251 | 0 | cell=(HSSFCell)cellIterator.next(); |
252 | ||
253 | ||
254 | ||
255 | 0 | property=this.createAppropriateProperty(cell, propertyNames); |
256 | ||
257 | 0 | propertiesHolder.addProperty(property); |
258 | ||
259 | } | |
260 | ||
261 | 0 | if (propertiesHolderClass.equals(Task.class)) |
262 | ||
263 | { | |
264 | ||
265 | 0 | processChain.addTask((Task)propertiesHolder); |
266 | ||
267 | } | |
268 | ||
269 | 0 | else if (propertiesHolderClass.equals(Resource.class)) |
270 | ||
271 | { | |
272 | ||
273 | 0 | processChain.addResource((Resource)propertiesHolder); |
274 | ||
275 | } | |
276 | ||
277 | ||
278 | ||
279 | } | |
280 | ||
281 | 0 | } |
282 | ||
283 | private void createLinks(HSSFWorkbook workBook, ProcessChain processChain, String sheetName) | |
284 | ||
285 | { | |
286 | ||
287 | 0 | HSSFSheet sheet=null; |
288 | ||
289 | 0 | HSSFRow row=null; |
290 | ||
291 | 0 | HSSFCell cell=null; |
292 | ||
293 | 0 | Hashtable propertyNames=null; |
294 | ||
295 | 0 | Iterator cellIterator=null; |
296 | ||
297 | 0 | Iterator propertiesIterator=null; |
298 | ||
299 | 0 | DemetrixProperty property=null; |
300 | ||
301 | 0 | String propertyName=null; |
302 | ||
303 | 0 | Link link=null; |
304 | ||
305 | 0 | DemetrixPropertiesHolder source=null; |
306 | ||
307 | 0 | DemetrixPropertiesHolder target=null; |
308 | ||
309 | 0 | DemetrixPropertiesStorage propertiesStorage=null; |
310 | ||
311 | 0 | int lastRowNumber=-1; |
312 | ||
313 | ||
314 | ||
315 | ||
316 | ||
317 | 0 | sheet=workBook.getSheet(sheetName); |
318 | ||
319 | 0 | if (sheet==null) |
320 | ||
321 | { | |
322 | ||
323 | this.logger.error("could not find worksheet \"" + sheetName + "\" in Microsoft(R) Excel(tm) file \"" + this.file + "\""); | |
324 | ||
325 | 0 | return; |
326 | ||
327 | } | |
328 | ||
329 | 0 | row=sheet.getRow(sheet.getFirstRowNum()+1); |
330 | ||
331 | 0 | propertyNames=new Hashtable(); |
332 | ||
333 | 0 | cellIterator=row.cellIterator(); |
334 | ||
335 | 0 | while (cellIterator.hasNext()) |
336 | ||
337 | { | |
338 | ||
339 | 0 | cell=(HSSFCell)cellIterator.next(); |
340 | ||
341 | ||
342 | ||
343 | 0 | propertyNames.put(new Short(cell.getCellNum()), cell.getStringCellValue()); |
344 | ||
345 | } | |
346 | ||
347 | 0 | lastRowNumber=sheet.getLastRowNum(); |
348 | ||
349 | 0 | for (int i=sheet.getFirstRowNum()+2; i <= lastRowNumber; i++) |
350 | ||
351 | { | |
352 | ||
353 | 0 | row=sheet.getRow(i); |
354 | ||
355 | 0 | cellIterator=row.cellIterator(); |
356 | ||
357 | ||
358 | ||
359 | 0 | propertiesStorage=new DemetrixPropertiesStorage(); |
360 | ||
361 | 0 | while (cellIterator.hasNext()) |
362 | ||
363 | { | |
364 | ||
365 | 0 | cell=(HSSFCell)cellIterator.next(); |
366 | ||
367 | ||
368 | ||
369 | 0 | property=this.createAppropriateProperty(cell, propertyNames); |
370 | ||
371 | 0 | propertiesStorage.addProperty(property); |
372 | ||
373 | } | |
374 | ||
375 | 0 | source=(DemetrixPropertiesHolder)processChain.getNodeByName(propertiesStorage.getProperty(Link.SOURCE_PROPERTY).getValue().toString()); |
376 | ||
377 | 0 | target=(DemetrixPropertiesHolder)processChain.getNodeByName(propertiesStorage.getProperty(Link.DESTINATION_PROPERTY).getValue().toString()); |
378 | ||
379 | ||
380 | ||
381 | ||
382 | ||
383 | 0 | propertiesStorage.removeProperty(Link.SOURCE_PROPERTY); |
384 | ||
385 | 0 | propertiesStorage.removeProperty(Link.DESTINATION_PROPERTY); |
386 | ||
387 | ||
388 | ||
389 | ||
390 | ||
391 | 0 | link=new Link(source, target); |
392 | ||
393 | ||
394 | ||
395 | 0 | propertiesIterator=propertiesStorage.getAllProperties(); |
396 | ||
397 | 0 | while (propertiesIterator.hasNext()) |
398 | ||
399 | { | |
400 | ||
401 | 0 | link.addProperty((DemetrixProperty)propertiesIterator.next()); |
402 | ||
403 | } | |
404 | ||
405 | 0 | processChain.addLink(link); |
406 | ||
407 | } | |
408 | ||
409 | ||
410 | ||
411 | 0 | } |
412 | ||
413 | private DemetrixProperty createAppropriateProperty(HSSFCell cell, Hashtable propertyNames) | |
414 | ||
415 | { | |
416 | ||
417 | 0 | BooleanProperty booleanProperty=null; |
418 | ||
419 | 0 | StringProperty stringProperty=null; |
420 | ||
421 | 0 | DoubleProperty doubleProperty=null; |
422 | ||
423 | 0 | DemetrixProperty property=null; |
424 | ||
425 | String propertyName; | |
426 | ||
427 | 0 | String propertyValue=null; |
428 | ||
429 | ||
430 | ||
431 | 0 | propertyName=(String)propertyNames.get(new Short(cell.getCellNum())); |
432 | ||
433 | ||
434 | ||
435 | ||
436 | ||
437 | ||
438 | ||
439 | try | |
440 | ||
441 | { | |
442 | ||
443 | 0 | doubleProperty=new DoubleProperty(); |
444 | ||
445 | 0 | doubleProperty.setValue(new Double(cell.getNumericCellValue())); |
446 | ||
447 | 0 | doubleProperty.setName(propertyName); |
448 | ||
449 | 0 | property=doubleProperty; |
450 | ||
451 | } | |
452 | ||
453 | 0 | catch (NumberFormatException exception) |
454 | ||
455 | { | |
456 | ||
457 | ||
458 | ||
459 | 0 | } |
460 | ||
461 | ||
462 | ||
463 | 0 | if (property!=null) |
464 | ||
465 | { | |
466 | ||
467 | 0 | return property; |
468 | ||
469 | } | |
470 | ||
471 | 0 | propertyValue=cell.getStringCellValue(); |
472 | ||
473 | 0 | if (propertyValue.trim().toLowerCase().equals("true") || propertyValue.trim().toLowerCase().equals("false")) |
474 | ||
475 | { | |
476 | ||
477 | ||
478 | ||
479 | 0 | booleanProperty=new BooleanProperty(); |
480 | ||
481 | 0 | booleanProperty.setName(propertyName); |
482 | ||
483 | 0 | if (propertyValue.trim().toLowerCase().equals("true")) |
484 | ||
485 | { | |
486 | ||
487 | 0 | booleanProperty.setValue(Boolean.TRUE); |
488 | ||
489 | } | |
490 | ||
491 | else | |
492 | ||
493 | { | |
494 | ||
495 | 0 | booleanProperty.setValue(Boolean.FALSE); |
496 | ||
497 | } | |
498 | ||
499 | 0 | property=booleanProperty; |
500 | ||
501 | } | |
502 | ||
503 | else | |
504 | ||
505 | { | |
506 | ||
507 | 0 | stringProperty=new StringProperty(); |
508 | ||
509 | 0 | stringProperty.setName(propertyName); |
510 | ||
511 | 0 | stringProperty.setValue(propertyValue); |
512 | ||
513 | 0 | property=stringProperty; |
514 | ||
515 | } | |
516 | ||
517 | 0 | return property; |
518 | ||
519 | } | |
520 | ||
521 | ||
522 | ||
523 | ||
524 | ||
525 | } | |
526 |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |