11package root .javafx .CustomView ;
22
33import java .io .File ;
4- import java .io .FileReader ;
54import java .io .IOException ;
65import java .time .LocalDate ;
76import java .time .format .DateTimeFormatter ;
109import java .util .HashMap ;
1110import java .util .List ;
1211import java .util .Map ;
12+ import java .util .stream .Collectors ;
1313
1414import com .jfoenix .controls .JFXButton ;
1515import com .jfoenix .controls .JFXComboBox ;
16- import com .opencsv .bean .CsvToBeanBuilder ;
1716
1817import javafx .event .ActionEvent ;
1918import javafx .fxml .FXML ;
2928import lombok .EqualsAndHashCode ;
3029import lombok .extern .slf4j .Slf4j ;
3130import root .core .domain .ArchiveUsage ;
31+ import root .core .domain .MonitoringResult ;
32+ import root .core .repository .constracts .ReportRepository ;
33+ import root .core .repository .implement .ReportRepositoryImpl ;
3234import root .javafx .Model .TypeAndFieldName ;
3335import root .utils .AlertUtils ;
36+ import root .utils .CsvUtils ;
3437
35- @ EqualsAndHashCode (callSuper = false )
38+ @ EqualsAndHashCode (callSuper = false )
3639@ Data
3740@ Slf4j
38- public class MonitoringAnchorPane <T > extends AnchorPane {
41+ public class MonitoringAnchorPane <T extends MonitoringResult > extends AnchorPane {
42+
43+ private ReportRepository reportRepository = ReportRepositoryImpl .getInstance ();
3944
4045 @ FXML
4146 Label label ;
@@ -55,7 +60,7 @@ public class MonitoringAnchorPane<T> extends AnchorPane {
5560
5661 @ FXML
5762 TableView <T > monitoringResultTV ;
58-
63+
5964 @ FXML
6065 DatePicker inquiryDatePicker ;
6166
@@ -78,10 +83,10 @@ public MonitoringAnchorPane(Class<T> clazz) {
7883 monitoringResultTV .getItems ().addAll (tableDataMap .get (newValue ));
7984 }
8085 });
81-
86+
8287 // Setting inquiry datepicker initial value
8388 this .inquiryDatePicker .setValue (LocalDate .now ().minusDays (1 ));
84-
89+
8590 } catch (IOException e ) {
8691 }
8792 }
@@ -217,41 +222,43 @@ public void setAnchor(double left, double top, double right, double bottom) {
217222 * @param e
218223 */
219224 public void run (ActionEvent e ) {
220-
225+
221226 // Get selected inquiry condition
222227 String inquiryDate = this .inquiryDatePicker .getValue ().format (DateTimeFormatter .ofPattern ("yyyyMMdd" ));
223228 String selected = getComboBox ().getSelectionModel ().getSelectedItem ();
224- String fileRootDir = getReportFilePath () + selected + "/" ;
225- File reportList = new File (fileRootDir );
229+ File reportFile = new File (getReportFilePath () + "/" + selected + ".txt" );
230+
231+ if (!reportFile .exists ()) {
232+ AlertUtils .showAlert (AlertType .INFORMATION , "조회결과 없음" , String .format ("%s 의 모니터링 기록이 없습니다." , selected ));
233+ return ;
234+ }
235+
236+ // TODO Show Progress UI
226237
227238 // Clear data
228239 clearTableData (selected );
229240
230241 // Read csv report file and add table data
231- List <T > tableDataList = new ArrayList <>();
232- for (String fileName : reportList .list ()) {
233-
234- if (!fileName .startsWith (inquiryDate )) {
235- continue ;
236- }
237-
238- String filePath = fileRootDir + fileName ;
239- File reportFile = new File (filePath );
240-
241- List <T > data = parseCsvReportFile (reportFile );
242- if (data != null ) {
243- tableDataList .addAll (data );
244- }
242+ List <T > allDataList = parseCsvReportFile (reportFile );
243+ if (allDataList == null ) {
244+ AlertUtils .showAlert (AlertType .ERROR , "모니터링 기록 조회" , "모니터링 기록 조회에 실패했습니다.\n 데이터를 확인해주세요." );
245+ return ;
245246 }
246247
248+ // Find data on inquiry date
249+ List <T > tableDataList = allDataList .stream ()
250+ .filter (data -> data .getMonitoringDate ().equals (inquiryDate ))
251+ .collect (Collectors .toList ());
252+
247253 if (tableDataList .size () == 0 ) {
248254 AlertUtils .showAlert (AlertType .INFORMATION , "조회결과 없음" , "해당일자의 모니터링 기록이 없습니다." );
249255 }
250256
257+ // Add and Sync data
251258 addTableDataSet (selected , tableDataList );
252259 syncTableData (selected );
253260 }
254-
261+
255262 /**
256263 * csv 파일을 읽어 Model 객체로 변환한다.
257264 *
@@ -260,22 +267,20 @@ public void run(ActionEvent e) {
260267 */
261268 private List <T > parseCsvReportFile (File file ) {
262269 List <T > result = null ;
270+
263271 try {
272+ List <String > headers = reportRepository .getReportHeaders (file );
273+ String csvString = reportRepository .getReportContentsInCsv (file );
264274
265- result = new CsvToBeanBuilder <T >(new FileReader (file ))
266- .withSkipLines (1 )
267- .withSeparator (',' )
268- .withIgnoreEmptyLine (true )
269- .withType (getClazz ())
270- .build ()
271- .parse ();
272-
275+ result = CsvUtils .parseCsvToBeanList (headers , csvString , getClazz ());
276+
273277 } catch (Exception e ) {
274278 log .error ("Parsing error!" + file );
275279 }
280+
276281 return result ;
277282 }
278-
283+
279284 /**
280285 * 현재 선택된 조회조건으로 재검색한다.
281286 *
@@ -284,13 +289,13 @@ private List<T> parseCsvReportFile(File file) {
284289 public void refresh (ActionEvent e ) {
285290 run (e );
286291 }
287-
292+
288293 /**
289294 * 현재 TableView에 세팅된 값을 Excel파일로 다운로드한다.
290295 *
291296 * @param e
292297 */
293298 public void excelDownload (ActionEvent e ) {
294-
299+
295300 }
296301}
0 commit comments