All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kuali.common.impex.service.DefaultDataHandler Maven / Gradle / Ivy

package org.kuali.common.impex.service;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.torque.engine.database.model.Column;
import org.kuali.common.impex.DumpTableContext;
import org.kuali.common.util.FormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultDataHandler implements DataHandler {

	private static final Logger logger = LoggerFactory.getLogger(DefaultDataHandler.class);

	private static final String FS = File.separator;
	private static final String LF = "\n";

	@Override
	public File getFileForTable(ImpexContext context, String tableName) {
		String filename = getFilename(context.getWorkingDir(), tableName);
		return new File(filename);
	}

	protected String getFilename(File workingDir, String tableName) {
        // to keep file names consistent, capitalize the name of the table before requesting a file name
        tableName = tableName.toUpperCase();
        return workingDir.getAbsolutePath() + FS + tableName + ".mpx";
    }

	@Override
	public void startData(DumpTableContext context) throws IOException {
		Column[] columns = context.getColumns();
		String encoding = context.getImpexContext().getEncoding();
		String header = getColumnsHeader(columns) + LF;
		OutputStream out = context.getOutputStream();
		out.write(header.getBytes(encoding));
	}

	@Override
	public void doData(DumpTableContext context) throws IOException {
		String encoding = context.getImpexContext().getEncoding();
		format(context.getCurrentData());
		writeRows(context.getCurrentData(), encoding, context.getOutputStream());
	}

	protected void writeRows(List rows, String encoding, OutputStream out) throws IOException {
		for (String[] row : rows) {
			String line = getLine(row);
			byte[] bytes = line.getBytes(encoding);
			out.write(bytes);
		}
	}

	/**
	 * Comma separated, all values enclosed in double quotes, always terminated by a linefeed
	 */
	protected String getLine(String[] row) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < row.length; i++) {
			if (i != 0) {
				sb.append(",");
			}
			sb.append('"');
			sb.append(row[i]);
			sb.append('"');
		}
		sb.append(LF);
		return sb.toString();
	}

	@Override
	public void finishData(DumpTableContext context) throws IOException {
		if (!CollectionUtils.isEmpty(context.getCurrentData())) {
			String encoding = context.getImpexContext().getEncoding();
			format(context.getCurrentData());
			writeRows(context.getCurrentData(), encoding, context.getOutputStream());
		}
		if (context.getTotalRowCount() > 0) {
			long threadId = Thread.currentThread().getId();
			String tableName = context.getTableContext().getName();
			String trc = FormatUtils.getCount(context.getTotalRowCount());
			String tds = FormatUtils.getSize(context.getTotalDataSize());
			Object[] args = { threadId, tableName, trc, tds };
			logger.info("[{}] - Dumped [{}] Total Rows: {}  Total Size: {}", args);
		} else {
			String filename = getFilename(context.getImpexContext().getWorkingDir(), context.getTableContext().getName());
			File emptyFile = new File(filename);
			FileUtils.forceDelete(emptyFile);
		}
	}

	protected void format(List rows) {
		for (String[] row : rows) {
			format(row);
		}
	}

	protected void format(String[] row) {
		for (int i = 0; i < row.length; i++) {
			row[i] = ImpexUtils.format(row[i]);
		}
	}

	protected String getColumnsHeader(Column[] columns) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < columns.length; i++) {
			if (i != 0) {
				sb.append(",");
			}
			sb.append(columns[i].getName());
		}
		return sb.toString();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy