リファクタリング。
[nucalgen] / nucalgen / src / main / java / jp / satomichan / nucalgen / MextStdFoodCompTable.java
1 package jp.satomichan.nucalgen;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.commons.configuration.ConfigurationException;
7 import org.apache.commons.configuration.XMLConfiguration;
8 import org.apache.poi.ss.usermodel.Cell;
9 import org.apache.poi.ss.usermodel.CellStyle;
10 import org.apache.poi.ss.usermodel.Row;
11 import org.apache.poi.ss.usermodel.Sheet;
12 import org.apache.poi.ss.usermodel.Workbook;
13
14 public class MextStdFoodCompTable {
15         private String brightColoredVegetablesXmlFileName = "";
16         private List<String> brightColoredVegetableList = new ArrayList<String>();
17
18         MextStdFoodCompTable(String brightColoredVegetablesXmlFileName_){
19                 this.brightColoredVegetablesXmlFileName = brightColoredVegetablesXmlFileName_;
20
21                 try {
22                         XMLConfiguration config = new XMLConfiguration(this.brightColoredVegetablesXmlFileName);
23                         List<Object> vegetableNames = config.getList("bright-colored-vegetable.name");
24                         for(Object vegeObj : vegetableNames) {
25                                 this.brightColoredVegetableList.add(vegeObj.toString());
26                         }
27
28                 } catch (ConfigurationException e) {
29                         e.printStackTrace();
30                 }
31         }
32
33
34
35         //変換
36         void processInto(Workbook outputWorkbook) {
37                 Sheet foodComposithonSheet = outputWorkbook.getSheetAt(0);
38                 outputWorkbook.setSheetName(0, "成分表");
39
40                 //「成分識別子」行
41                 Row idRow = foodComposithonSheet.getRow(11);
42                 for(int i = 4; i <= 61; i++) {
43                         Cell idCell = idRow.getCell(i);
44                         idCell.setCellValue(idCell.toString().replaceAll(" ", ""));
45                 }
46                 idRow.createCell(62).setCellValue("GROUP");
47                 idRow.createCell(63).setCellValue("BRIGHT_COLORED_VEGETABLE");
48
49
50                 //セル加工
51                 for(int compSheetRowIndex = 12; compSheetRowIndex <= 9999; compSheetRowIndex++) {
52                         Row compRow = foodComposithonSheet.getRow(compSheetRowIndex);
53                         if(compRow == null) {
54                                 break;
55                         }
56
57                         int group = Integer.parseInt(compRow.getCell(1).toString()) / 1000;
58
59                         for (int cellCount = 1; cellCount <= 61; cellCount++) {
60                                 Cell thisCell = compRow.getCell(cellCount);
61
62                                 String cellString = thisCell.toString();
63
64                                 cellString = cellString.replaceAll("\\(", "");
65                                 cellString = cellString.replaceAll("\\)", "");
66                                 cellString = cellString.replaceAll("-", "0");
67                                 cellString = cellString.replaceAll("Tr", "0");
68
69                                 //セル値・書式 コピー
70                                 //Cell compCell = foodComposithonSheet.getRow(compSheetRowIndex).getCell(cellCount);
71                                 Cell compCell = compRow.createCell(cellCount);
72                                 compCell.setCellValue(cellString);
73                                 compCell.setCellStyle(thisCell.getCellStyle());
74                                 compCell.setCellType(thisCell.getCellTypeEnum());
75
76                                 if(cellString.matches("^[\\d\\.]+$")) {
77                                         compCell.setCellValue(Double.parseDouble(cellString));
78                                         CellStyle aCellStyle = compCell.getCellStyle();
79                                         aCellStyle.setDataFormat((short) 0);
80                                         compCell.setCellStyle(aCellStyle);
81                                 }
82
83
84                         } //for cellCount
85
86
87
88                         //食品群(1~18)書き込み
89                         compRow.createCell(62).setCellValue(group);
90
91                         //緑黄色野菜か?
92                         boolean isBrightColored = false;
93                         if(group == 6 && brightColoredVegetablesXmlFileName.length() > 0) {
94                                 String foodName = compRow.getCell(3).getStringCellValue();
95                                 for(String aBright : this.brightColoredVegetableList) {
96                                         if(foodName.matches(".*" + aBright + ".*")) {
97                                                 isBrightColored = true;
98                                                 break;
99                                         }
100                                 }
101
102                                 if(isBrightColored) {
103                                         compRow.createCell(63).setCellValue(1);
104                                 }
105                         }
106
107
108                 } //for compSheetRowIndex
109
110
111                 //元の表 削除
112                 for(int i = outputWorkbook.getNumberOfSheets() - 1; i > 0; i--) {
113                         outputWorkbook.removeSheetAt(i);
114                 }
115         }
116
117
118
119 }