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.engine;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentModelList;
029import org.nuxeo.ecm.platform.reporting.api.ReportInstance;
030import org.nuxeo.ecm.platform.reporting.api.ReportModel;
031import org.nuxeo.ecm.platform.reporting.api.ReportService;
032import org.nuxeo.runtime.model.ComponentContext;
033import org.nuxeo.runtime.model.DefaultComponent;
034
035/**
036 * Component Implementation of the {@link ReportService} interface. Mainly encapsulate NXQL queries.
037 *
038 * @author Tiry (tdelprat@nuxeo.com)
039 */
040public class ReportComponent extends DefaultComponent implements ReportService {
041
042    protected static final Log log = LogFactory.getLog(ReportComponent.class);
043
044    public static final String BIRT_REPORTS_CONTAINER_PATH = "/report-models";
045
046    @Override
047    public void activate(ComponentContext context) {
048    }
049
050    @Override
051    public void deactivate(ComponentContext context) {
052        BirtEngine.destroyBirtEngine();
053    }
054
055    @Override
056    public List<ReportInstance> getReportInstanceByModelName(CoreSession session, String reportModelName)
057            {
058        String uuid = getReportModelByName(session, reportModelName).getId();
059        String query = "SELECT * FROM BirtReport WHERE birt:modelRef='" + uuid + "'";
060        DocumentModelList reports = session.query(query);
061        List<ReportInstance> result = new ArrayList<ReportInstance>();
062        for (DocumentModel doc : reports) {
063            ReportInstance report = doc.getAdapter(ReportInstance.class);
064            if (report != null) {
065                result.add(report);
066            }
067        }
068        return result;
069    }
070
071    @Override
072    public ReportInstance getReportInstanceByKey(CoreSession session, String key) {
073        String query = "SELECT * FROM BirtReport WHERE birt:reportKey='" + key + "'";
074        DocumentModelList reports = session.query(query);
075        if (reports.isEmpty()) {
076            return null;
077        }
078        return reports.get(0).getAdapter(ReportInstance.class);
079    }
080
081    @Override
082    public String getReportModelsContainer() {
083        return BIRT_REPORTS_CONTAINER_PATH;
084    }
085
086    @Override
087    public ReportModel getReportModelByName(CoreSession session, String reportModelName) {
088        String query = "SELECT * FROM BirtReportModel WHERE birtmodel:reportName='" + reportModelName + "'";
089        DocumentModelList reports = session.query(query);
090        if (reports.isEmpty()) {
091            return null;
092        }
093        return reports.get(0).getAdapter(ReportModel.class);
094    }
095
096    public List<ReportModel> getReportAvailableModels(CoreSession session) {
097        String query = "SELECT * FROM BirtReportModel WHERE ecm:currentLifeCycleState != 'deleted'";
098        DocumentModelList reports = session.query(query);
099        List<ReportModel> result = new ArrayList<ReportModel>();
100        for (DocumentModel doc : reports) {
101            ReportModel report = doc.getAdapter(ReportModel.class);
102            if (report != null) {
103                result.add(report);
104            }
105        }
106        return result;
107    }
108
109}