・緑黄色野菜 修正
[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.cloneSheet(0);
38                 outputWorkbook.setSheetName(18, "成分表");
39                 outputWorkbook.setSheetOrder("成分表", 0);
40
41                 //「成分識別子」行
42                 Row idRow = foodComposithonSheet.getRow(10);
43                 for(int i = 4; i <= 61; i++) {
44                         Cell idCell = idRow.getCell(i);
45                         idCell.setCellValue(idCell.toString().replaceAll(" ", ""));
46                 }
47                 idRow.createCell(62).setCellValue("GROUP");
48                 idRow.createCell(63).setCellValue("BRIGHT_COLORED_VEGETABLE");
49
50                 int compSheetRowIndex = 11;
51
52                 //18 の食品群ごとの処理
53                 for(int group = 1; group <= 18; group++) {
54                         Sheet thisSheet = outputWorkbook.getSheetAt(group);
55
56                         int rowCount = 0;
57                         for (Row thisRow : thisSheet) {
58                                 rowCount++;
59                                 if(rowCount < 12) {continue;}
60
61                                 Row compRow = foodComposithonSheet.createRow(compSheetRowIndex);
62
63                                 for (int cellCount = 1; cellCount <= 61; cellCount++) {
64                                         Cell thisCell = thisRow.getCell(cellCount);
65
66                                         String cellString = thisCell.toString();
67
68                                         cellString = cellString.replaceAll("\\(", "");
69                                         cellString = cellString.replaceAll("\\)", "");
70                                         cellString = cellString.replaceAll("-", "0");
71                                         cellString = cellString.replaceAll("Tr", "0");
72
73                                         //セル値・書式 コピー
74                                         //Cell compCell = foodComposithonSheet.getRow(compSheetRowIndex).getCell(cellCount);
75                                         Cell compCell = compRow.createCell(cellCount);
76                                         compCell.setCellValue(cellString);
77                                         compCell.setCellStyle(thisCell.getCellStyle());
78                                         compCell.setCellType(thisCell.getCellTypeEnum());
79
80                                         if(cellString.matches("^[\\d\\.]+$")) {
81                                                 compCell.setCellValue(Double.parseDouble(cellString));
82                                                 CellStyle aCellStyle = compCell.getCellStyle();
83                                                 aCellStyle.setDataFormat((short) 0);
84                                                 compCell.setCellStyle(aCellStyle);
85                                         }
86
87
88                                 } //CELL
89
90
91
92                                 //食品群(1~18)書き込み
93                                 compRow.createCell(62).setCellValue(group);
94
95                                 //緑黄色野菜か?
96                                 boolean isBrightColored = false;
97                                 if(group == 6 && brightColoredVegetablesXmlFileName.length() > 0) {
98                                         String foodName = thisRow.getCell(3).getStringCellValue();
99                                         for(String aBright : this.brightColoredVegetableList) {
100                                                 if(foodName.matches(".*" + aBright + ".*")) {
101                                                         isBrightColored = true;
102                                                         break;
103                                                 }
104                                         }
105
106                                         if(isBrightColored) {
107                                                 compRow.createCell(63).setCellValue(1);
108                                         }
109                                 }
110
111
112                                 compSheetRowIndex++;
113
114                         } //ROW
115
116                 } //GROUP
117
118                 //元の表 削除
119                 for(int i = 18; i > 0; i--) {
120                         outputWorkbook.removeSheetAt(i);
121                 }
122         }
123
124
125
126 }