001: package org.theospi.portfolio.reports.model;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005: import org.sakaiproject.content.api.ContentHostingService;
006: import org.sakaiproject.content.api.ContentResource;
007: import org.sakaiproject.exception.IdUnusedException;
008: import org.sakaiproject.exception.PermissionException;
009: import org.sakaiproject.exception.TypeException;
010: import org.sakaiproject.metaobj.shared.model.Id;
011: import org.theospi.portfolio.shared.model.OspException;
012:
013: import java.io.ByteArrayOutputStream;
014: import java.io.IOException;
015: import java.io.InputStream;
016:
017: public class ReportXslFile {
018:
019: protected final transient Log logger = LogFactory
020: .getLog(getClass());
021: /** the link to the report definition */
022: private ReportDefinitionXmlFile reportDef;
023: private String reportXslFileRef = null;
024: private String reportDefId;
025: private Id reportXslFileId;
026: private byte[] xslFile;
027:
028: /**
029: * the getter for the reportId property
030: */
031: public ReportXslFile() {
032:
033: }
034:
035: public String getReportDefId() {
036: return reportDefId;
037: }
038:
039: public void setReportDefId(String reportDefId) {
040: this .reportDefId = reportDefId;
041: }
042:
043: public ReportXslFile(ReportXsl reportXsl,
044: ContentHostingService contentHosting) {
045: try {
046: String id = reportXsl.getXslLink();
047: ContentResource resource = contentHosting.getResource(id);
048: setXslFile(readStreamToBytes(resource.streamContent()));
049: setReportXslFileRef(reportXsl.getXslLink());
050: } catch (PermissionException pe) {
051: logger
052: .warn(
053: "Failed loading content: no permission to view file",
054: pe);
055: throw new OspException(
056: "Permission Error loading the following xsl file:"
057: + reportXsl.getXslLink());
058: } catch (TypeException te) {
059: logger.warn("Wrong type", te);
060: throw new OspException(
061: "Error loading the following xsl file:"
062: + reportXsl.getXslLink());
063: } catch (IdUnusedException iue) {
064: logger.warn("UnusedId: ", iue);
065: throw new OspException(
066: "Error loading the following xsl file:"
067: + reportXsl.getXslLink());
068: } catch (Exception e) {
069: e.printStackTrace();
070: throw new OspException(
071: "Error loading the following xsl file:"
072: + reportXsl.getXslLink());
073: }
074: }
075:
076: public String getReportXslFileRef() {
077: return reportXslFileRef;
078: }
079:
080: public void setReportXslFileRef(String reportXslFileRef) {
081: this .reportXslFileRef = reportXslFileRef;
082: }
083:
084: public byte[] getXslFile() {
085: return xslFile;
086: }
087:
088: public void setXslFile(byte[] xslFile) {
089: this .xslFile = xslFile;
090: }
091:
092: private byte[] readStreamToBytes(InputStream inStream)
093: throws IOException {
094: ByteArrayOutputStream bytes = new ByteArrayOutputStream();
095: byte data[] = new byte[10 * 1024];
096:
097: int count;
098: while ((count = inStream.read(data, 0, 10 * 1024)) != -1) {
099: bytes.write(data, 0, count);
100: }
101: byte[] tmp = bytes.toByteArray();
102: bytes.close();
103: return tmp;
104: }
105:
106: public ReportDefinitionXmlFile getReportDef() {
107: return reportDef;
108: }
109:
110: public void setReportDef(ReportDefinitionXmlFile reportDef) {
111: this .reportDef = reportDef;
112: }
113:
114: public Id getReportXslFileId() {
115: return reportXslFileId;
116: }
117:
118: public void setReportXslFileId(Id reportXslFileId) {
119: this.reportXslFileId = reportXslFileId;
120: }
121:
122: }
|