/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright 2006 - 2009 Pentaho Corporation. All rights reserved.
*
*/
package org.pentaho.platform.plugin.action.jfreereport.helper;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.pentaho.actionsequence.dom.IActionResource;
import org.pentaho.platform.api.engine.IActionSequenceResource;
import org.pentaho.platform.api.engine.ISolutionFile;
import org.pentaho.platform.api.repository.ISolutionRepository;
import org.pentaho.platform.engine.core.system.PentahoSystem;
import org.pentaho.platform.engine.services.actionsequence.ActionSequenceResource;
import org.pentaho.reporting.libraries.resourceloader.ResourceData;
import org.pentaho.reporting.libraries.resourceloader.ResourceKey;
import org.pentaho.reporting.libraries.resourceloader.ResourceLoadingException;
import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
import org.pentaho.reporting.libraries.resourceloader.loader.AbstractResourceData;
/**
* This class is implemented to support loading solution files
* from the pentaho repository into JFreeReport
*
* @author Will Gorman
*/
public class PentahoResourceData extends AbstractResourceData {
private static final long serialVersionUID = 1806026106310340013L;
private String filename;
private ResourceKey key;
/**
* constructor which takes a resource key for data loading specifics
*
* @param key resource key
*/
public PentahoResourceData(final ResourceKey key) throws ResourceLoadingException {
if (key == null) {
throw new NullPointerException();
}
this.key = key;
this.filename = (String) key.getIdentifier();
}
/**
* gets a resource stream from the runtime context.
*
* @param caller resource manager
* @return input stream
*/
public InputStream getResourceAsStream(final ResourceManager caller) throws ResourceLoadingException {
int resourceType = IActionResource.SOLUTION_FILE_RESOURCE;
if (filename.contains("://")) {
resourceType = IActionResource.URL_RESOURCE;
}
final IActionSequenceResource resource = new ActionSequenceResource(
"", resourceType, "application/binary", (String) key.getIdentifier()); //$NON-NLS-1$ //$NON-NLS-2$
try {
ISolutionRepository solutionRepository = PentahoSystem.get(ISolutionRepository.class);
return solutionRepository.getResourceInputStream(resource, true, ISolutionRepository.ACTION_EXECUTE);
} catch (FileNotFoundException e) {
throw new ResourceLoadingException(e.getLocalizedMessage(), e);
}
}
/**
* returns a requested attribute, currently only supporting filename.
*
* @param key attribute requested
* @return attribute value
*/
public Object getAttribute(final String lookupKey) {
if (lookupKey.equals(ResourceData.FILENAME)) {
return filename;
}
return null;
}
/**
* return the version number. We don't have access to file dates or versions
* so return 0
*
* @param caller resource manager
*
* @return version
*/
public long getVersion(final ResourceManager caller) throws ResourceLoadingException {
final IActionSequenceResource resource = new ActionSequenceResource(
"", IActionResource.SOLUTION_FILE_RESOURCE, "application/binary", (String) key.getIdentifier()); //$NON-NLS-1$ //$NON-NLS-2$
ISolutionRepository solutionRepository = PentahoSystem.get(ISolutionRepository.class);
final ISolutionFile file = solutionRepository.getSolutionFile(resource, ISolutionRepository.ACTION_EXECUTE);
long version = -1L;
if (file != null) {
version = file.getLastModified();
}
return version;
}
/**
* get the resource key
*
* @return resource key
*/
public ResourceKey getKey() {
return key;
}
}