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 java.util.Random; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.nuxeo.common.utils.IdUtils; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.PropertyException; 028import org.nuxeo.ecm.core.event.Event; 029import org.nuxeo.ecm.core.event.EventContext; 030import org.nuxeo.ecm.core.event.EventListener; 031import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 032import org.nuxeo.ecm.platform.reporting.api.Constants; 033import org.nuxeo.ecm.platform.reporting.api.ReportInstance; 034 035/** 036 * Synchronous {@link EventListener} used to compute unique {@link ReportInstance} keys 037 * 038 * @author Tiry (tdelprat@nuxeo.com) 039 */ 040public class ReportKeyListener implements EventListener { 041 042 protected static final Log log = LogFactory.getLog(ReportKeyListener.class); 043 044 @Override 045 public void handleEvent(Event event) { 046 EventContext ctx = event.getContext(); 047 if (ctx instanceof DocumentEventContext) { 048 DocumentEventContext docCtx = (DocumentEventContext) ctx; 049 DocumentModel doc = docCtx.getSourceDocument(); 050 051 if (doc.hasSchema(Constants.BIRT_REPORT_INSTANCE_SCHEMA)) { 052 ReportInstance reportInstance = doc.getAdapter(ReportInstance.class); 053 if (reportInstance != null) { 054 try { 055 // give default title if needed 056 String title = doc.getTitle(); 057 if (title == null || title.isEmpty()) { 058 title = reportInstance.getModel().getDoc().getTitle(); 059 doc.setPropertyValue("dc:title", title); 060 } 061 // compute report key if needed 062 String key = reportInstance.getReportKey(); 063 if (key == null || key.isEmpty()) { 064 reportInstance.setReportKey(generateReportKey(reportInstance)); 065 } 066 } catch (PropertyException e) { 067 log.error("Error while parsing report parameters", e); 068 } 069 } 070 } 071 } 072 } 073 074 protected String generateReportKey(ReportInstance reportInstance) { 075 String name = reportInstance.getModel().getReportName(); 076 if (name == null) { 077 name = reportInstance.getModel().getDoc().getName(); 078 } 079 080 StringBuffer key = new StringBuffer(); 081 key.append(IdUtils.generateId(name, "_", true, 20)); 082 key.append("-"); 083 084 Random rnd = new Random(System.currentTimeMillis()); 085 key.append(rnd.nextInt(1000)); 086 087 return key.toString(); 088 } 089 090}