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

tools.dynamia.zk.reports.actions.ExportCSVAction Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
/*
 * Copyright (C) 2009 - 2019 Dynamia Soluciones IT S.A.S - NIT 900302344-1
 * Colombia - South America
 * All Rights Reserved.
 *
 * DynamiaTools is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL v3) as
 * published by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DynamiaTools 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with DynamiaTools.  If not, see .
 */
package tools.dynamia.zk.reports.actions;

import org.zkoss.zul.Filedownload;
import tools.dynamia.actions.InstallAction;
import tools.dynamia.commons.ClassMessages;
import tools.dynamia.commons.Messages;
import tools.dynamia.crud.CrudActionEvent;
import tools.dynamia.integration.ProgressMonitor;
import tools.dynamia.reports.ExporterColumn;
import tools.dynamia.reports.PlainFileExporter;
import tools.dynamia.reports.ReportOutputType;
import tools.dynamia.ui.MessageType;
import tools.dynamia.ui.UIMessages;
import tools.dynamia.viewers.DataSetView;
import tools.dynamia.viewers.Field;
import tools.dynamia.viewers.ViewDescriptor;
import tools.dynamia.viewers.util.Viewers;
import tools.dynamia.zk.crud.CrudView;
import tools.dynamia.zk.ui.LongOperationMonitorWindow;
import tools.dynamia.zk.util.LongOperation;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

@InstallAction
public class ExportCSVAction extends AbstractExportAction {

	private static final int LARGE = 5000;
	private static final ClassMessages MESSAGES = ClassMessages.get(ExportExcelAction.class);

	public ExportCSVAction() {
		setName(Messages.get(getClass(), "export_csv"));

	}

	@Override
	public ReportOutputType getOuputType() {
		return ReportOutputType.CSV;
	}

	@Override
	public void actionPerformed(CrudActionEvent evt) {
		CrudView crudView = (CrudView) evt.getCrudView();
		DataSetView dataSetView = crudView.getDataSetView();
		if (dataSetView.getValue() != null && dataSetView.getValue() instanceof Collection) {
			Collection data = (Collection) dataSetView.getValue();

			if (data.size() > LARGE) {
				UIMessages.showQuestion(MESSAGES.get("confirm_large_export"),
						() -> export(data, getViewDescriptor(evt)));
			} else {
				export(data, getViewDescriptor(evt));
			}
		}
	}

	public static void export(Collection data, ViewDescriptor descriptor) {

		PlainFileExporter exporter = new PlainFileExporter();

		Viewers.getFields(descriptor).stream().filter(f -> f.isVisible() && !f.isCollection()).forEach(f -> {
			ExporterColumn column = new ExporterColumn(f.getName(), f.getLocalizedLabel(), getFormatPattern(f),
					f.getFieldClass());
			if (f.getParams().get("entityAlias") != null) {
				column.setEntityAlias((String) f.getParams().get("entityAlias"));
			}
			exporter.addColumn(column);
		});

		File temp = createTempFile();

		ProgressMonitor monitor = new ProgressMonitor();

		LongOperation operation = LongOperation.create().execute(() -> exporter.export(temp, data, monitor))
				.onFinish(() -> download(temp)).onException(ex -> ex.printStackTrace()).start();

		// Update ui each 4 secs
		LongOperationMonitorWindow monitorWindow = new LongOperationMonitorWindow(operation, monitor);
		monitorWindow.setMessageTemplate(MESSAGES.get("exporting_progress"));
		monitorWindow.setTitle(MESSAGES.get("exporting"));
		monitorWindow.doModal();
	}

	private static String getFormatPattern(Field f) {
		String formatPattern = (String) f.getParams().get(Viewers.PARAM_FORMAT_PATTERN);
		if (formatPattern == null || formatPattern.isEmpty()) {
			String converter = (String) f.getParams().get(Viewers.PARAM_CONVERTER);
			if (converters.Date.class.getName().equals(converter)) {
				formatPattern = "dd/MM/yyyy";
			} else if (converters.Time.class.getName().equals(converter)) {
				formatPattern = "h:mm a";
			}
		}

		return formatPattern;
	}

	private static File createTempFile() {
		try {
			return File.createTempFile("export_" + System.currentTimeMillis(), ".xlsx");
		} catch (IOException e) {

			e.printStackTrace();
			UIMessages.showMessage("Error: " + e.getMessage(), MessageType.ERROR);
			return null;
		}
	}

	private static void download(File temp) {
		try {
			Filedownload.save(temp, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy