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.Serializable;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.platform.reporting.report.ReportParameter;
031
032/**
033 * Base class for the {@link ReportModel} and {@link ReportInstance} adapters Contains common code for Parameters
034 * management.
035 *
036 * @author Tiry (tdelprat@nuxeo.com)
037 */
038public abstract class BaseBirtReportAdapter {
039
040    protected DocumentModel doc;
041
042    public BaseBirtReportAdapter(DocumentModel doc) {
043        this.doc = doc;
044    }
045
046    protected CoreSession getSession() {
047        return doc.getCoreSession();
048    }
049
050    protected abstract String getPrefix();
051
052    public abstract List<ReportParameter> getReportParameters() throws IOException;
053
054    public void setParameter(ReportParameter param) throws IOException {
055        setParameter(param, true);
056    }
057
058    public void setParameter(ReportParameter param, boolean save) throws IOException {
059        setParameter(param.getName(), param.getStringValue(), save);
060    }
061
062    public void setParameter(String name, Object value) throws IOException {
063        setParameter(name, value, true);
064    }
065
066    @SuppressWarnings("unchecked")
067    public void setParameter(String name, Object value, boolean save) throws IOException {
068        List<ReportParameter> reportParams = getReportParameters();
069        ReportParameter targetParameter = null;
070        for (ReportParameter reportParam : reportParams) {
071            if (reportParam.getName().equals(name)) {
072                targetParameter = reportParam;
073                break;
074            }
075        }
076
077        if (targetParameter == null) {
078            // parameter does not exist !
079            return;
080        }
081
082        targetParameter.setObjectValue(value);
083        String stringValue = targetParameter.getStringValue();
084        List<Map<String, Serializable>> localParams = (List<Map<String, Serializable>>) doc.getPropertyValue(getPrefix()
085                + ":parameters");
086        if (localParams == null) {
087            localParams = new ArrayList<Map<String, Serializable>>();
088        }
089
090        boolean addParam = true;
091        for (Map<String, Serializable> localParam : localParams) {
092            String pName = (String) localParam.get("pName");
093            if (name.equals(pName)) {
094                localParam.put("pValue", stringValue);
095                addParam = false;
096                break;
097            }
098        }
099
100        if (addParam) {
101            Map<String, Serializable> newEntry = new HashMap<String, Serializable>();
102            newEntry.put("pName", name);
103            newEntry.put("pValue", stringValue);
104            localParams.add(newEntry);
105        }
106
107        doc.setPropertyValue(getPrefix() + ":parameters", (Serializable) localParams);
108        if (save) {
109            doc = getSession().saveDocument(doc);
110        }
111    }
112
113    public DocumentModel getDoc() {
114        return doc;
115    }
116
117}