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.api; 020 021import java.io.IOException; 022import java.io.InputStream; 023import java.io.Serializable; 024import java.util.ArrayList; 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028 029import org.eclipse.birt.report.engine.api.IParameterDefn; 030import org.eclipse.birt.report.engine.api.IReportRunnable; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 033import org.nuxeo.ecm.platform.reporting.report.ReportHelper; 034import org.nuxeo.ecm.platform.reporting.report.ReportParameter; 035 036/** 037 * Implementation class for the {@link ReportModel} adapter interface 038 * 039 * @author Tiry (tdelprat@nuxeo.com) 040 */ 041public class BirtReportModel extends BaseBirtReportAdapter implements ReportModel { 042 043 protected static final String PREFIX = "birtmodel"; 044 045 protected transient List<IParameterDefn> cachedParamsDef = null; 046 047 public BirtReportModel(DocumentModel doc) { 048 super(doc); 049 } 050 051 @Override 052 public String getId() { 053 return doc.getId(); 054 } 055 056 @Override 057 public String getReportName() { 058 return (String) doc.getPropertyValue(PREFIX + ":reportName"); 059 } 060 061 @Override 062 public InputStream getReportFileAsStream() throws IOException { 063 BlobHolder bh = doc.getAdapter(BlobHolder.class); 064 return bh.getBlob().getStream(); 065 } 066 067 @Override 068 public void parseParametersDefinition() throws IOException { 069 List<IParameterDefn> paramsDef = getParameterDef(); 070 for (IParameterDefn def : paramsDef) { 071 ReportParameter param = new ReportParameter(def); 072 setParameter(param, false); 073 } 074 } 075 076 @Override 077 public void updateMetadata() throws IOException { 078 Map<String, String> meta = ReportHelper.getReportMetaData(getReportFileAsStream()); 079 080 String name = meta.get("displayName"); 081 if (name == null) { 082 BlobHolder bh = doc.getAdapter(BlobHolder.class); 083 name = bh.getBlob().getFilename(); 084 } 085 doc.setPropertyValue(PREFIX + ":reportName", name); 086 087 if (meta.get("title") != null) { 088 doc.setPropertyValue("dc:title", meta.get("title")); 089 } 090 if (meta.get("description") != null) { 091 doc.setPropertyValue("dc:description", meta.get("description")); 092 } 093 } 094 095 protected List<IParameterDefn> getParameterDef() throws IOException { 096 if (cachedParamsDef == null) { 097 IReportRunnable report = ReportHelper.getReport(getReportFileAsStream()); 098 cachedParamsDef = ReportHelper.getReportParameter(report); 099 } 100 return cachedParamsDef; 101 } 102 103 @Override 104 public List<ReportParameter> getReportParameters() throws IOException { 105 Map<String, String> storedParams = getStoredParameters(); 106 List<IParameterDefn> paramsDef = getParameterDef(); 107 108 List<ReportParameter> result = new ArrayList<ReportParameter>(); 109 for (IParameterDefn def : paramsDef) { 110 ReportParameter param = new ReportParameter(def, storedParams.get(def.getName())); 111 result.add(param); 112 } 113 return result; 114 } 115 116 @Override 117 @SuppressWarnings("unchecked") 118 public Map<String, String> getStoredParameters() { 119 List<Map<String, Serializable>> localParams = (List<Map<String, Serializable>>) doc.getPropertyValue(PREFIX 120 + ":parameters"); 121 Map<String, String> params = new HashMap<String, String>(); 122 if (localParams != null) { 123 for (Map<String, Serializable> localParam : localParams) { 124 String name = (String) localParam.get("pName"); 125 String value = (String) localParam.get("pValue"); 126 if (value != null) { 127 params.put(name, value); 128 } 129 } 130 } 131 return params; 132 } 133 134 @Override 135 protected String getPrefix() { 136 return PREFIX; 137 } 138 139}