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

tools.dynamia.zk.reports.ui.BIRTReportViewer 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.ui;

import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.*;
import tools.dynamia.commons.Messages;
import tools.dynamia.commons.StringUtils;
import tools.dynamia.io.FileInfo;
import tools.dynamia.reports.ReportExplorer;
import tools.dynamia.reports.birt.BIRTExplorerFilter;
import tools.dynamia.reports.repo.DefaultReportRepository;
import tools.dynamia.reports.repo.ReportsRepository;
import tools.dynamia.ui.MessageType;
import tools.dynamia.ui.UIMessages;
import tools.dynamia.zk.reports.BIRTReportUtils;
import tools.dynamia.zk.util.ZKUtil;

import java.text.DateFormat;
import java.util.List;

public class BIRTReportViewer extends Div {

    /**
     *
     */
    private static final long serialVersionUID = -4279530105262380784L;

    private ReportsRepository reportsRepository;

    private String location;
    private String viewerURL;
    private String resourceFolder;
    private List model;
    private Listbox listbox;

    public BIRTReportViewer() {
        try {
            initParameters();
            initRepository();
            initUI();
            initModel();

        } catch (Exception e) {
            UIMessages.showMessage(Messages.get(BIRTReportViewer.class, "NoConfigError"), MessageType.ERROR);
            e.printStackTrace();
        }
    }

    private void initParameters() {
        location = BIRTReportUtils.getReportsPath();
        viewerURL = BIRTReportUtils.getViewerURL();
        resourceFolder = BIRTReportUtils.getResourcesPath();
        if (!viewerURL.endsWith("/")) {
            viewerURL = viewerURL + "/";
        }

    }

    private void initRepository() {
        ReportExplorer explorer = new ReportExplorer(new BIRTExplorerFilter());
        reportsRepository = new DefaultReportRepository(location, explorer);
    }

    private void initModel() {
        model = reportsRepository.scan("");
        ZKUtil.fillListbox(listbox, model, true);
    }

    private void initUI() {
        setClass("birtReportViewer");
        setVflex("1");

        listbox = new Listbox();
        listbox.setHflex("1");
        listbox.setVflex("1");

        Listhead head = new Listhead();
        head.setParent(listbox);

        Listheader header = new Listheader("", "", "30px");
        header.setParent(head);

        header = new Listheader(Messages.get(BIRTReportViewer.class, "Reports"));
        header.setParent(head);

        header = new Listheader(Messages.get(BIRTReportViewer.class, "LastUpdate"), "", "250px");
        header.setParent(head);

        listbox.setItemRenderer((item, data, index) -> {
            FileInfo info = (FileInfo) data;
            item.setValue(data);

            String label = info.getDescription();

            Listcell cell = new Listcell((index + 1) + "");
            cell.setParent(item);

            cell = new Listcell();
            cell.setParent(item);
            A link = renderLink(label, info);
            link.setParent(cell);

            cell = new Listcell(DateFormat.getDateTimeInstance().format(info.getFileDate()));
            cell.setParent(item);
        });

        listbox.setParent(this);

    }

    private A renderLink(String label, FileInfo info) {
        A link = new A(label);
        link.setTooltiptext(Messages.get(BIRTReportViewer.class, "ViewReport"));
        link.setClass("birtReportLink");
        link.addEventListener(Events.ON_CLICK, evt -> viewReport(label, info));
        return link;
    }

    private void viewReport(String label, FileInfo info) {
        StringBuilder reportURL = new StringBuilder();
        reportURL.append(viewerURL)
                .append("frameset?__report=")
                .append(StringUtils.cleanPath(info.getFile().getAbsolutePath()))
                .append("&__resourceFolder=")
                .append(resourceFolder)
                .append("&__title=")
                .append(label);

        Iframe frame = new Iframe(reportURL.toString());
        frame.setHflex("1");
        frame.setVflex("1");

        ZKUtil.showDialog(Messages.get(BIRTReportViewer.class, "ViewReport"), frame, "99%", "99%");

    }

    public FileInfo getSelectedReport() {
        try {
            return listbox.getSelectedItem().getValue();
        } catch (Exception e) {
            return null;
        }
    }

    public void refresh() {
        initModel();

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy