io.heming.accountbook.util.Importer.java Source code

Java tutorial

Introduction

Here is the source code for io.heming.accountbook.util.Importer.java

Source

/*
 * Copyright 2015 heming.keh@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.heming.accountbook.util;

import io.heming.accountbook.entity.Category;
import io.heming.accountbook.entity.Record;
import io.heming.accountbook.facade.CategoryFacade;
import io.heming.accountbook.facade.FacadeUtil;
import io.heming.accountbook.facade.RecordFacade;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;

import java.io.*;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Keh on 2015/6/3.
 * <p>
 * ????CSV
 */
public class Importer {
    private RecordFacade recordFacade;
    private CategoryFacade categoryFacade;

    public Importer() {
        recordFacade = new RecordFacade();
        categoryFacade = new CategoryFacade();
    }

    public int importRecords(File file) throws Exception {
        Iterable<CSVRecord> csvRecords;
        List<Record> records = new ArrayList<>();
        try (Reader in = new FileReader(file)) {
            csvRecords = CSVFormat.EXCEL.withQuote('"')
                    .withHeader("ID", "CATEGORY", "PRICE", "PHONE", "DATE", "NOTE").parse(in);
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            // ??
            categoryFacade.clear();
            int i = 0;
            for (CSVRecord csvRecord : csvRecords) {
                Integer id = Integer.parseInt(csvRecord.get("ID"));
                Category category = new Category(csvRecord.get("CATEGORY"));
                Double price = Double.parseDouble(csvRecord.get("PRICE"));
                String phone = csvRecord.get("PHONE");
                Date date = new Date(format.parse(csvRecord.get("DATE")).getTime());
                String note = csvRecord.get("NOTE");
                // category?
                if (!categoryFacade.list().contains(category)) {
                    categoryFacade.add(category);
                }
                Record record = new Record(category, price, phone, date);
                record.setId(id);
                record.setNote(note);
                records.add(record);
            }
        }

        // ??
        recordFacade.clear();
        int i = 0;
        int total = records.size();
        for (Record record : records) {
            recordFacade.add(record);
        }
        return records.size();
    }

    public int exportRecords(File file) throws Exception {
        int count = 0;
        try (Writer out = new FileWriter(file)) {
            CSVPrinter printer = new CSVPrinter(out, CSVFormat.EXCEL.withQuote('"'));
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            List<Record> records = recordFacade.list();
            int total = records.size();
            for (Record record : records) {
                Integer id = record.getId();
                String category = record.getCategory().getName();
                Double price = record.getPrice();
                String phone = record.getPhone();
                String date = format.format(record.getDate());
                String note = record.getNote();
                printer.printRecord(id, category, price, phone, date, note);
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) throws Exception {
        Importer importer = new Importer();
        importer.importRecords(new File("D:\\AccountBook\\tools\\data_gen.csv"));
        FacadeUtil.shutdown();
    }
}