au.com.jwatmuff.eventmanager.export.CSVExporter.java Source code

Java tutorial

Introduction

Here is the source code for au.com.jwatmuff.eventmanager.export.CSVExporter.java

Source

/*
 * EventManager
 * Copyright (c) 2008-2017 James Watmuff & Leonard Hall
 *
 * This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package au.com.jwatmuff.eventmanager.export;

import au.com.jwatmuff.eventmanager.db.PlayerDAO;
import au.com.jwatmuff.eventmanager.db.ResultDAO;
import au.com.jwatmuff.eventmanager.model.cache.ResultInfoCache;
import au.com.jwatmuff.eventmanager.model.info.ResultInfo;
import au.com.jwatmuff.eventmanager.model.misc.DatabaseStateException;
import au.com.jwatmuff.eventmanager.model.vo.Player;
import au.com.jwatmuff.eventmanager.model.vo.PlayerDetails;
import au.com.jwatmuff.eventmanager.model.vo.Result;
import au.com.jwatmuff.genericdb.Database;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import org.apache.commons.lang.ArrayUtils;

/**
 *
 * @author James
 */
public class CSVExporter {
    private static final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");

    /** Creates a new instance of TextExporter */
    private CSVExporter() {
    }

    public static void generateFromTable(JTable t, OutputStream out) {
        generateFromTable(t.getModel(), out);
    }

    public static void generateFromTable(TableModel tm, OutputStream out) {
        int rows = tm.getRowCount();
        int cols = tm.getColumnCount();
        Object[][] table = new Object[rows + 1][cols];
        for (int j = 0; j < cols; j++)
            table[0][j] = tm.getColumnName(j);
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                table[i + 1][j] = tm.getValueAt(i, j);

        generateFromArray(table, out);
    }

    public static void generateFromArray(Object[][] table, OutputStream os) {
        PrintStream ps = new PrintStream(os);
        for (Object[] row : table)
            outputRow(row, ps);
    }

    public static void generatePlayerList(Database database, OutputStream out) {
        Collection<Player> players = database.findAll(Player.class, PlayerDAO.ALL);
        PrintStream ps = new PrintStream(out);

        Object[] columns = new Object[] { "id", "First Name", "Last Name", "Sex", "Grade", "DOB", "Team",
                "Home Number", "Work Number", "Mobile Number", "Street", "City", "Postcode", "State", "Email",
                "Emergency Name", "Emergency Phone", "Emergency Mobile", "Medical Conditions", "Medical Info",
                "Injury Info" };

        outputRow(columns, ps);

        for (Player player : players) {
            Object[] fields1 = new Object[] { player.getVisibleID(), player.getFirstName(), player.getLastName(),
                    player.getGender(), player.getGrade(), dateFormat.format(player.getDob()), player.getTeam() };

            PlayerDetails details = database.get(PlayerDetails.class, player.getDetailsID());
            Object[] fields2;
            if (details != null) {
                fields2 = new Object[] { details.getHomeNumber(), details.getWorkNumber(),
                        details.getMobileNumber(), details.getStreet(), details.getCity(), details.getPostcode(),
                        details.getState(), details.getEmail(), details.getEmergencyName(),
                        details.getEmergencyPhone(), details.getEmergencyMobile(), details.getMedicalConditions(),
                        details.getMedicalInfo(), details.getInjuryInfo() };
            } else {
                fields2 = new Object[14];
                Arrays.fill(fields2, null);
            }

            outputRow(ArrayUtils.addAll(fields1, fields2), ps);
        }
    }

    public static void generateResults(Database database, ResultInfoCache cache, OutputStream out) {
        PrintStream ps = new PrintStream(out);

        for (Result result : database.findAll(Result.class, ResultDAO.ALL)) {
            try {
                ResultInfo ri = cache.getResultInfo(result.getID());
                ps.print("\"" + ri.getMatName() + "\",");
                ps.print(ri.getMatFightNumber() + ",");
                ps.print("\"" + ri.getPlayerName()[0] + "\",");
                ps.print("\"" + ri.getPlayerName()[1] + "\",");
                ps.print(ri.getResult().getScores()[0] + ",");
                ps.print(ri.getResult().getScores()[1]);

                ps.println();
            } catch (DatabaseStateException e) {
                // do nothing
            }
        }
    }

    private static void outputRow(Object[] fields, PrintStream out) {
        boolean first = true;
        for (Object field : fields) {
            if (!first)
                out.print(",");
            if (field != null) {
                if (field instanceof Number) {
                    out.print(field);
                } else {
                    out.print("\"" + field + "\"");
                }
            }
            first = false;
        }
        out.println();
    }
}