001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Aug 17, 2005
014: * @author James Dixon
015: */
016:
017: package org.pentaho.plugin.core;
018:
019: import java.io.OutputStream;
020: import java.util.Set;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.pentaho.actionsequence.dom.actions.TemplateMsgAction;
025: import org.pentaho.core.repository.IContentItem;
026: import org.pentaho.core.runtime.IActionParameter;
027: import org.pentaho.core.solution.IActionResource;
028: import org.pentaho.messages.Messages;
029: import org.pentaho.messages.util.LocaleHelper;
030: import org.pentaho.plugin.ComponentBase;
031:
032: public class TemplateComponent extends ComponentBase {
033:
034: private final static String TEMPLATE = "template"; //$NON-NLS-1$
035:
036: /**
037: *
038: */
039: private static final long serialVersionUID = 4383466190328580251L;
040:
041: public Log getLogger() {
042: return LogFactory.getLog(TemplateComponent.class);
043: }
044:
045: protected boolean validateAction() {
046:
047: // see if we have a template defined
048: TemplateMsgAction actionDefinition = (TemplateMsgAction) getActionDefinition();
049:
050: boolean templateOk = false;
051: if (null != actionDefinition.getTemplate()) {
052: templateOk = true;
053: } else if (isDefinedResource(TEMPLATE)) {
054: templateOk = true;
055: }
056:
057: if (!templateOk) {
058: error(Messages
059: .getString("Template.ERROR_0001_TEMPLATE_NOT_DEFINED")); //$NON-NLS-1$
060: return false;
061: }
062: Set outputs = getOutputNames();
063: if ((outputs == null) || (outputs.size() == 0)
064: || (outputs.size() > 1)) {
065: error(Messages
066: .getString("Template.ERROR_0002_OUTPUT_COUNT_WRONG")); //$NON-NLS-1$
067: return false;
068: }
069: return true;
070: }
071:
072: protected boolean validateSystemSettings() {
073: // nothing to do here
074: return true;
075: }
076:
077: public void done() {
078: // nothing to do here
079: }
080:
081: protected boolean executeAction() {
082:
083: try {
084:
085: TemplateMsgAction actionDefinition = (TemplateMsgAction) getActionDefinition();
086: String template = null;
087:
088: template = actionDefinition.getTemplate().getStringValue();
089: if (null == template && isDefinedResource(TEMPLATE)) {
090: IActionResource resource = getResource("template"); //$NON-NLS-1$
091: template = getResourceAsString(resource);
092: }
093:
094: String outputName = (String) getOutputNames().iterator()
095: .next();
096: IActionParameter outputParam = getOutputItem(outputName);
097:
098: if (outputParam.getType().equals(
099: IActionParameter.TYPE_CONTENT)) {
100:
101: String mimeType = actionDefinition.getMimeType()
102: .getStringValue(); //$NON-NLS-1$
103: String extension = actionDefinition.getExtension()
104: .getStringValue(); //$NON-NLS-1$
105:
106: IContentItem outputItem = getOutputItem(outputName,
107: mimeType, extension);
108: OutputStream outputStream = outputItem
109: .getOutputStream(getActionName());
110:
111: outputStream.write(applyInputsToFormat(template)
112: .getBytes(LocaleHelper.getSystemEncoding()));
113: outputItem.closeOutputStream();
114: return true;
115: } else {
116: setOutputValue(outputName,
117: applyInputsToFormat(template));
118: }
119:
120: return true;
121: } catch (Exception e) {
122: error(
123: Messages
124: .getString("Template.ERROR_0004_COULD_NOT_FORMAT_TEMPLATE"), e); //$NON-NLS-1$
125: return false;
126: }
127:
128: }
129:
130: public boolean init() {
131: // nothing to do here
132: return true;
133: }
134:
135: }
|