2020-11-29
[nucalgen] / nucalgen / src / main / java / jp / satomichan / nucalgen / MoeStdFoodCompTable.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 MoeStdFoodCompTable {
15         private String brightColoredVegetablesXmlFileName = "";
16         private List<String> brightColoredVegetableList = new ArrayList<String>();
17
18         MoeStdFoodCompTable(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 mainSheet = outputWorkbook.getSheet("本表");
38                 int rowCount = 0;
39                 int lastCol = mainSheet.getRow(5).getLastCellNum();
40                 mainSheet.getRow(5).getCell(4).setCellValue("廃棄率");
41                 mainSheet.getRow(5).createCell(lastCol + 1).setCellValue("食品群");
42                 mainSheet.getRow(5).createCell(lastCol + 2).setCellValue("緑黄色野菜");
43
44
45                 for (Row row : mainSheet) {
46                         rowCount++;
47                         if(rowCount < 8) {continue;}
48
49                         for (Cell cell : row) {
50                                 String cellString = cell.toString();
51
52                                 cellString = cellString.replaceAll("\\(", "");
53                                 cellString = cellString.replaceAll("\\)", "");
54                                 cellString = cellString.replaceAll("-", "0");
55                                 cellString = cellString.replaceAll("Tr", "0");
56
57
58                                 if(cellString.matches("^[\\d\\.]+$")) {
59                                         cell.setCellValue(Double.parseDouble(cellString));
60                                         CellStyle aCellStyle = cell.getCellStyle();
61                                         aCellStyle.setDataFormat((short) 0);
62                                         cell.setCellStyle(aCellStyle);
63                                 }
64
65                         }
66
67                         int gun = (int) row.getCell(0).getNumericCellValue();
68                         row.createCell(lastCol + 1).setCellValue(gun);
69
70
71                         //緑黄色野菜
72                         if(brightColoredVegetablesXmlFileName.length() > 0) {
73                                 boolean isBrightColored = false;
74                                 String foodName = row.getCell(3).getStringCellValue();
75                                 for(String aBright : this.brightColoredVegetableList) {
76                                         if(foodName.matches(aBright + ".*")) {
77                                                 isBrightColored = true;
78                                                 break;
79                                         }
80                                 }
81
82                                 if(isBrightColored) {
83                                         row.createCell(lastCol + 2).setCellValue(1);
84                                 }
85                         }
86
87                 }
88
89
90         }
91 }