001/*
002 * (C) Copyright 2006-20011 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.ecm.platform.reporting.listener;
020
021import static org.nuxeo.ecm.platform.reporting.api.Constants.BIRT_REPORT_INSTANCE_SCHEMA;
022import static org.nuxeo.ecm.platform.reporting.api.Constants.BIRT_REPORT_MODEL_SCHEMA;
023
024import java.io.IOException;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.PropertyException;
030import org.nuxeo.ecm.core.event.Event;
031import org.nuxeo.ecm.core.event.EventContext;
032import org.nuxeo.ecm.core.event.EventListener;
033import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
034import org.nuxeo.ecm.platform.reporting.api.ReportInstance;
035import org.nuxeo.ecm.platform.reporting.api.ReportModel;
036
037/**
038 * Synchronous {@link EventListener} used to extract ReportParameters from the Birt ReportDesign file
039 *
040 * @author Tiry (tdelprat@nuxeo.com)
041 */
042public class ReportParserListener implements EventListener {
043
044    protected static final Log log = LogFactory.getLog(ReportParserListener.class);
045
046    @Override
047    public void handleEvent(Event event) {
048
049        EventContext ctx = event.getContext();
050        if (ctx instanceof DocumentEventContext) {
051            DocumentEventContext docCtx = (DocumentEventContext) ctx;
052            DocumentModel doc = docCtx.getSourceDocument();
053
054            if (doc.hasSchema(BIRT_REPORT_MODEL_SCHEMA)) {
055                ReportModel reportModel = doc.getAdapter(ReportModel.class);
056                if (reportModel != null) {
057                    try {
058                        if (doc.getProperty("file:content").isDirty()) {
059                            reportModel.parseParametersDefinition();
060                            reportModel.updateMetadata();
061                        }
062                    } catch (PropertyException | IOException e) {
063                        log.error("Error while parsing report model parameters", e);
064                    }
065                }
066            } else if (doc.hasSchema(BIRT_REPORT_INSTANCE_SCHEMA)) {
067                ReportInstance reportInstance = doc.getAdapter(ReportInstance.class);
068                if (reportInstance != null) {
069                    try {
070                        reportInstance.initParameterList();
071                    } catch (IOException e) {
072                        log.error("Error initializing report parameters", e);
073                    }
074                }
075            }
076        }
077    }
078
079}