talehunter / jmesa

Automatically exported from code.google.com/p/jmesa
0 stars 0 forks source link

Incorrect export to PDF, Excel or CSV #229

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi!! At the begining, Sorry!! I'm spanish and I don´t speck english very
well, but i will try to explain my "fatal error" with JMesa I can. I hope
you can understand me :D

OK..... this is my problem: I have working jmesa (2.4.5 version). It show
all the information, row by row, column by column... but when i "make
click" on "PDF export", excel or CSV, it doesn't show me the information in
a new window, asking me if i want save or open that information in a PDF
file, excel or csv. It works showing me the information by screen, in the
same place where was de jmesa list. If I use :
- PDF export --> Show "com.lowagie.text.pdf.PdfPTable@15b5783".
- JExcel export --> Show "jxl.write.biff.WritableWorkbookImpl@187fafc".
(with exce, not jexcel, it makes me an exception:
javax.servlet.ServletException: java.lang.NoSuchMethodError:
org.apache.poi.hssf.usermodel.HSSFCell.setCellValue(Lorg/apache/poi/hssf/usermod
el/HSSFRichTextString;)V)
- CSV export --> Show the information line by line but en the navigation
window, wih all the web design.

I have read only one case about this, but i can't solve. This is my code:
.....

<script type="text/javascript" src="<cms:link><%=rutaInicio
%>dibujos/jmesa/js/2.4.5/jquery.jmesa.min.js</cms:link>"></script>
    <script type="text/javascript" src="<cms:link><%=rutaInicio
%>dibujos/jmesa/js/2.4.5/jquery.jmesa.js</cms:link>"></script>
    <script type="text/javascript" src="<cms:link><%=rutaInicio
%>dibujos/jmesa/js/2.4.5/jmesa.js</cms:link>"></script>
    <script type="text/javascript" src="<cms:link><%=rutaInicio
%>dibujos/jmesa/js/2.4.5/jmesa.min.js</cms:link>"></script>

......

//The JSP where use JMesa
<jmesa:tableFacade toolbar="es.sopde.opencms.tags.ToolbarJMesa"
id="jmesa_noticias" stateAttr="restore" items="${listado}" var="noticias"
editable="true" exportTypes="jexcel,pdf,csv">
    <jmesa:htmlTable>
        <jmesa:htmlRow uniqueProperty="id">
            <jmesa:htmlColumn property="checkbox" title="&nbsp;"
worksheetEditor="org.jmesa.worksheet.editor.CheckboxWorksheetEditor"
filterable="false" sortable="false" width="2%"/>
            <jmesa:htmlColumn property="fecha" title="Fecha" pattern="dd/MM/yyyy"
editable="false" cellEditor="org.jmesa.view.editor.DateCellEditor"
sortable="true" width="10%" headerClass="sortable"
sortOrder="asc,desc,none"  />
            <jmesa:htmlColumn property="titular" title="Titular" sortable="true"
editable="false" headerClass="sortable" sortOrder="asc,desc,none" width="70%"/>
            <jmesa:htmlColumn property="prioridad" filterable="false"
title="Prioridad" editable="false" sortable="true" headerClass="sortable"
sortOrder="desc,asc,none"  width="8%"/>
            <jmesa:htmlColumn property="visible" title="Visible" editable="false"
sortable="true" width="10%"><img
src="<cms:link>${rutaImagen}dibujos/${noticias.mostrarVisible}</cms:link>"
/></jmesa:htmlColumn> 
        </jmesa:htmlRow>
    </jmesa:htmlTable>
</jmesa:tableFacade>

....

<script type="text/javascript">
    function onInvokeAction(id,action)
    {
        setExportToLimit(id, '');

        //Con este IF hacemos que sólo se pueda ordenar las columnas de una
en una, 
        //no manteniendo ordenaciones de columnas ya pinchadas
        if (action == 'sort') {
            var limit = jQuery.jmesa.getTableFacade(id).limit;
            var sortSet = limit.getSortSet();
            if (sortSet) {
                var sort = limit.sortSet[limit.sortSet.length - 1];
                removeAllSortsFromLimit(id)
                limit.sortSet[0] = sort;
            }
        }

        createHiddenInputFieldsForLimitAndSubmit(id);
    }

    function onInvokeExportAction(id,action) {
        var parameterString = createParameterStringForLimit(id);
        //me index.html is where i use jmesa too
        location.href = 'index.html?' + parameterString;
    }
</script>

My custom toolbar:

package es.sopde.opencms.tags;

import java.util.List;

import org.eclipse.tptp.trace.arm.internal.model.ArmWrapper;
import org.jmesa.core.CoreContext;
import org.jmesa.view.ViewUtils;
import org.jmesa.view.component.Row;
import org.jmesa.view.html.HtmlUtils;
import org.jmesa.view.html.toolbar.AbstractItemRenderer;
import org.jmesa.view.html.toolbar.AbstractToolbar;
import org.jmesa.view.html.toolbar.ImageItem;
import org.jmesa.view.html.toolbar.ImageItemImpl;
import org.jmesa.view.html.toolbar.MaxRowsItem;
import org.jmesa.view.html.toolbar.ToolbarItem;
import org.jmesa.view.html.toolbar.ToolbarItemRenderer;
import org.jmesa.view.html.toolbar.ToolbarItemType;
import org.jmesa.web.WebContext;

public class ToolbarJMesa extends AbstractToolbar {

    /**
     * @author 
     */
    @Override
    public String render() {
        int armStatus = ArmWrapper.FAILED;
        try {
            ArmWrapper.startTransaction(this,
                    "es.sopde.opencms.tags.ToolbarJMesa", "render",
                    new Object[] {});

            addToolbarItem(ToolbarItemType.FIRST_PAGE_ITEM);
            addToolbarItem(ToolbarItemType.PREV_PAGE_ITEM);
            addToolbarItem(ToolbarItemType.PAGE_NUMBER_ITEMS);
            addToolbarItem(ToolbarItemType.NEXT_PAGE_ITEM);
            addToolbarItem(ToolbarItemType.LAST_PAGE_ITEM);
            addToolbarItem(ToolbarItemType.SEPARATOR);

            MaxRowsItem maxRowsItem = (MaxRowsItem)
addToolbarItem(ToolbarItemType.MAX_ROWS_ITEM);
            if (getMaxRowsIncrements() != null) {
                maxRowsItem.setIncrements(getMaxRowsIncrements());
            }

            boolean exportable = ViewUtils.isExportable(getExportTypes());

            if (exportable) {
                addToolbarItem(ToolbarItemType.SEPARATOR);
                addExportToolbarItems(getExportTypes());
            }

            Row row = getTable().getRow();
            List columns = row.getColumns();

            boolean filterable = ViewUtils.isFilterable(columns);

            if (filterable) {
                addToolbarItem(ToolbarItemType.SEPARATOR);
                addToolbarItem(ToolbarItemType.FILTER_ITEM);
                addToolbarItem(ToolbarItemType.CLEAR_ITEM);
            }

            boolean editable = ViewUtils.isEditable(getCoreContext()
                    .getWorksheet());

            if (editable) {
                addToolbarItem(ToolbarItemType.SEPARATOR);
                addToolbarItem(ToolbarItemType.SAVE_WORKSHEET_ITEM);
                addToolbarItem(ToolbarItemType.FILTER_WORKSHEET_ITEM);
            }

            //addToolbarItem(ToolbarItemType.SEPARATOR);

            //   addToolbarItem(createCustomItem());

            armStatus = ArmWrapper.GOOD;
            return super.render();
        } finally {
            ArmWrapper.stopTransaction("es.sopde.opencms.tags.ToolbarJMesa",
                    "render", armStatus);
        }
    }

    private ImageItem createCustomItem() {
        int armStatus = ArmWrapper.FAILED;
        try {
            ArmWrapper.startTransaction(this,
                    "es.sopde.opencms.tags.ToolbarJMesa", "createCustomItem",
                    new Object[] {});
            //Crea un nuevo icono en la barra de herramientas
            ImageItemImpl item = new ImageItemImpl();
            item.setCode("custom-item");
            item.setTooltip("Hello World");
            item.setImage(getImage("custom.png", getWebContext(),
                    getCoreContext()));
            item.setAlt("custom");

            ToolbarItemRenderer renderer = new ToolbarItemJMesa(item,
                    getCoreContext());
            renderer.setOnInvokeAction("onInvokeAction");
            item.setToolbarItemRenderer(renderer);

            armStatus = ArmWrapper.GOOD;
            return item;
        } finally {
            ArmWrapper.stopTransaction("es.sopde.opencms.tags.ToolbarJMesa",
                    "createCustomItem", armStatus);
        }
    }

    private static String getImage(String image, WebContext webContext,
            CoreContext coreContext) {
        int armStatus = ArmWrapper.FAILED;
        try {
            ArmWrapper.startTransaction(null,
                    "es.sopde.opencms.tags.ToolbarJMesa", "getImage",
                    new Object[] { image, webContext, coreContext });
            String imagesPath = HtmlUtils.imagesPath(webContext, coreContext);
            armStatus = ArmWrapper.GOOD;
            return imagesPath + image;
        } finally {
            ArmWrapper.stopTransaction("es.sopde.opencms.tags.ToolbarJMesa",
                    "getImage", armStatus);
        }
    }

    private static class ToolbarItemJMesa extends AbstractItemRenderer {

        public ToolbarItemJMesa(ToolbarItem item, CoreContext coreContext) {
            setToolbarItem(item);
            setCoreContext(coreContext);
        }

        public String render() {

            ToolbarItem item = getToolbarItem();
            StringBuilder action = new StringBuilder("javascript:");
            action.append("alert('Hello World')");
            item.setAction(action.toString());
            return item.enabled();
        }
    }
}

I hope you can help me. I'going to kill me computer... in one or two hours :D

Original issue reported on code.google.com by andyned...@gmail.com on 12 Nov 2009 at 11:58

GoogleCodeExporter commented 9 years ago
Sorry for the late reply...I have been away for a few days.

The problem is your browser is not recognizing the response headers as a file
download. If you look in the AbstractViewExporter.responseHeaders() method you 
can
see how I set things in the response.

This is the first I have heard about it not working  with JMesa, but I have 
seen this
problem in general on projects before.

What I would do is try a test without using JMesa and see what works for you. I 
do
not know what framework you use, but just create a simple Controller (or 
Action) and
see if you can just export some simple text string. 

That, or you could get the source code working on your computer and play around 
with
the response headers.

http://code.google.com/p/jmesa/wiki/ProjectBuild

You will probably have to spend some time on Google to figure out what you need.

If you figure it out let me know and I can work with you to get the api more 
generic
so it works for you.

Original comment by jeff.johnston.mn@gmail.com on 18 Nov 2009 at 10:07

GoogleCodeExporter commented 9 years ago
Thanks for you response. At the end I get the solution after trying some 
methods.
I've used API code and tags. This is "the code":

====================================

<%@page buffer="none" session="true" import="java.util.*, org.opencms.jsp.*,
org.opencms.file.*,org.opencms.xml.content.*, org.jmesa.view.editor.*,
com.lowagie.text.pdf.*, jxl.write.biff.*" %>
<%@page import="javax.servlet.*"%>
<%@page import="org.jmesa.facade.*, org.jmesa.limit.*, 
org.jmesa.view.component.*,
org.jmesa.view.html.component.*" %>

<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%
    CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);

    pageContext.setAttribute("cms", cms);

    CmsRequestContext cmsRequest = cms.getRequestContext();

    CmsJspNavBuilder navigation = cms.getNavigation();
    CmsJspNavElement navElement = (CmsJspNavElement)
navigation.getNavigationBreadCrumb(3, 3).get(0);
    String rutaInicio = navElement.getParentFolderName();
    String rutaActual = cmsRequest.getFolderUri();
    Boolean esFilaImpar = true;

    CmsObject cmsObj = cms.getCmsObject();
    CmsProject curProj = cmsObj.getRequestContext().currentProject();
    List listadoNoticias = null;

    cmsObj.loginUser("Admin", "admin");

    listadoNoticias = cmsObj.getResourcesInFolder("/ruta_noticias/",
CmsResourceFilter.requireType(3060)); // Elegi en el xml el Id de noticias 3060
    List<Noticia> listado = new ArrayList<Noticia>();

    if (listadoNoticias != null) {
        for (int i = 0; i < listadoNoticias.size(); i++) {
            Noticia noticia= new Noticia();
            CmsFile ficheroNoticia = (CmsFile) listadoNoticias.get(i);

            CmsJspXmlContentBean cjxc = new CmsJspXmlContentBean(pageContext,
request, response);

            I_CmsXmlContentContainer icms = cjxc.contentload("singleFile",
ficheroNoticia.getRootPath().substring(14), false);
            Iterator it = icms.getCollectorResult().iterator();

            while (it.hasNext()) {
                CmsResource recurso = (CmsResource)it.next();
                CmsFile arquivo = new CmsFile(recurso);
                String cadcontido = new String(arquivo.getContents());
                CmsXmlContent content = CmsXmlContentFactory.unmarshal(cmsObj,arquivo);
                String desde = content.getValue("FechaDesde[1]",
cmsRequest.getLocale()).getStringValue(cmsObj);
                String visible = content.getValue("Visible[1]",
cmsRequest.getLocale()).getStringValue(cmsObj);
                navElement =
navigation.getNavigationForResource(ficheroNoticia.getRootPath().substring(14));
                String titular = content.getValue("Titular[1]",
cmsRequest.getLocale()).getStringValue(cmsObj);

                noticia.setNombreFichero(ficheroNoticia.getRootPath().substring(14));
                noticia.setId(i);
                noticia.setFecha(new Date(Long.parseLong(desde)));
                noticia.setTitular(titular);
                noticia.setPrioridad(navElement.getNavPosition());
                noticia.setVisible(Boolean.parseBoolean(visible));
                listado.add(noticia);
            }
        }
    }

    if (request.getSession().getAttribute("listado") == null){
        request.getSession().setAttribute("listado", listado);
    }

    TableFacade tablefacade = TableFacadeFactory.createTableFacade("jmesa_noticias",
request);
    tablefacade.setColumnProperties("fecha", "titular", "prioridad");
    tablefacade.setItems(listado);
    tablefacade.setExportTypes(response, ExportType.CSV, ExportType.JEXCEL,
ExportType.PDFP);
    tablefacade.setStateAttr("restore");

    Limit limit = tablefacade.getLimit();

    if(limit.isExported())
    {
        Table table = tablefacade.getTable();
        Row row = table.getRow();

        Column colFecha = row.getColumn("fecha");
        colFecha.setTitle("Fecha");
        colFecha.getCellRenderer().setCellEditor(new DateCellEditor("dd/MM/yyyy"));
        Column colTitular = row.getColumn("titular");
        colTitular.setTitle("Titular");
        Column colPrioridad = row.getColumn("prioridad");
        colPrioridad.setTitle("Prioridad");

        tablefacade.render();
    }
    else{
%>
        <%@ taglib prefix="jmesa" uri="http://code.google.com/p/jmesa" %>

        <%@page import="mi_libreria.Noticia"%>

        ......

                --[ head ]--

        ......

        <div id="listadoContenido">

        <form name="displ2" action="index.html">
            <jmesa:tableFacade toolbar="mi_libreria.ToolbarJMesa" id="jmesa_noticias"
stateAttr="restore" items="${listado}" 
            var="noticias" editable="true" exportTypes="jexcel,pdfp,csv" >
                <jmesa:htmlTable>
                    <jmesa:htmlRow uniqueProperty="id">
                        <jmesa:htmlColumn property="checkbox" title=" "
worksheetEditor="org.jmesa.worksheet.editor.CheckboxWorksheetEditor"
filterable="false" sortable="false" width="2%"/>
                        <jmesa:htmlColumn property="fecha" title="Fecha"
pattern="dd/MM/yyyy" editable="false"
cellEditor="org.jmesa.view.editor.DateCellEditor" sortable="true" width="10%"
headerClass="sortable" sortOrder="asc,desc,none"  />
                        <jmesa:htmlColumn property="titular" title="Titular"
sortable="true" filterEditor="org.jmesa.view.html.editor.DroplistFilterEditor"
editable="false" headerClass="sortable" sortOrder="asc,desc,none" width="70%"/>
                        <jmesa:htmlColumn property="prioridad" filterable="false"
title="Prioridad" editable="false" sortable="true" headerClass="sortable"
sortOrder="desc,asc,none"  width="8%"/>
                        <jmesa:htmlColumn property="visible" title="Visible"
editable="false" sortable="true" width="10%"><img
src="<cms:link>${rutaImagen}dibujos/${noticias.mostrarVisible}</cms:link>"
/></jmesa:htmlColumn> 
                    </jmesa:htmlRow>
                </jmesa:htmlTable>
            </jmesa:tableFacade> 
        </form>

        <c:set var="rutaPrincipal"><%=rutaActual%></c:set>

        <script type="text/javascript">
            function onInvokeAction(id,action)
            {
                setExportToLimit(id, '');

                //Con este IF hacemos que sólo se pueda ordenar las columnas de una
en una, 
                //no manteniendo ordenaciones de columnas ya pinchadas
                if (action == 'sort') {
                    var limit = jQuery.jmesa.getTableFacade(id).limit;
                    var sortSet = limit.getSortSet();
                    if (sortSet) {
                        var sort = limit.sortSet[limit.sortSet.length - 1];
                        removeAllSortsFromLimit(id)
                        limit.sortSet[0] = sort;
                    }
                }

                createHiddenInputFieldsForLimitAndSubmit(id);
            }

            function onInvokeExportAction(id,action) {
                var parameterString = createParameterStringForLimit(id);
                location.href = 'index.html?' + parameterString;
            }
        </script>

        </div>

        ....

        --[ foot ]--
<%
}
%>
==================================

Really I use 1 IF: WAY "A" to generate de export file (it works peferctly with 
PDF,
Excel and CSV), and WAY "B" to show the list with the information, the personal
toolbar, etc. I don't know if the code could be more optimize (sure yes!!) but 
by the
moment my first procupation were it works. Thanks for you response and I hope 
than my
solution helps to anybody with the same problem i had.

Original comment by andyned...@gmail.com on 19 Nov 2009 at 7:40