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 7, 2005
014: * @author James Dixon
015: */
016:
017: package org.pentaho.ui.component;
018:
019: import java.io.File;
020: import java.util.List;
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.dom4j.Document;
024: import org.dom4j.DocumentHelper;
025: import org.dom4j.Node;
026: import org.pentaho.ui.XmlComponent;
027: import org.pentaho.core.repository.ISolutionRepository;
028: import org.pentaho.core.solution.IActionDefinition;
029: import org.pentaho.core.solution.IActionSequence;
030: import org.pentaho.core.system.PentahoSystem;
031: import org.pentaho.core.ui.IPentahoUrlFactory;
032: import org.pentaho.core.util.XForm;
033: import org.pentaho.messages.Messages;
034:
035: public class InputFormComponent extends XmlComponent {
036:
037: /**
038: *
039: */
040: private static final long serialVersionUID = -6106477602576378538L;
041:
042: String templateName;
043:
044: String stylesheetName;
045:
046: String solution;
047:
048: String path;
049:
050: String actionName;
051:
052: String instanceId;
053:
054: public InputFormComponent(IPentahoUrlFactory urlFactory,
055: String instanceId, String templateName,
056: String stylesheetName, String solution, String path,
057: String actionName, List messages) {
058: super (urlFactory, messages, solution + File.separator + path);
059: this .instanceId = instanceId;
060: this .templateName = templateName;
061: this .stylesheetName = stylesheetName;
062: this .solution = solution;
063: this .path = path;
064: this .actionName = actionName;
065: }
066:
067: private static final Log logger = LogFactory
068: .getLog(InputFormComponent.class);
069:
070: public Log getLogger() {
071: return logger;
072: }
073:
074: public boolean validate() {
075: boolean ok = true;
076: if (instanceId == null) {
077: // error( "Instance id not specified" );
078: // out.write( "instance id not provided" );
079: // ok = false;
080: }
081:
082: if (solution == null) {
083: error(Messages
084: .getString("InputForm.ERROR_0001_SOLUTION_NOT_SPECIFIED")); //$NON-NLS-1$
085: ok = false;
086: }
087:
088: if (path == null) {
089: error(Messages
090: .getString("InputForm.ERROR_0002_ACTION_NAME_NOT_SPECIFIED")); //$NON-NLS-1$
091: ok = false;
092: }
093:
094: if (actionName == null) {
095: // TODO log this
096: error(Messages
097: .getString("InputForm.ERROR_0003_ACTION_PATH_NOT_SPECIFIED")); //$NON-NLS-1$
098: ok = false;
099: }
100:
101: return ok;
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see org.pentaho.core.ui.component.BaseUIComponent#getXmlContent()
108: */
109: public Document getXmlContent() {
110:
111: ISolutionRepository repository = PentahoSystem
112: .getSolutionRepository(getSession());
113:
114: IActionSequence actionSequence = repository.getActionSequence(
115: solution, path, actionName, getLoggingLevel(),
116: ISolutionRepository.ACTION_EXECUTE);
117:
118: if (actionSequence == null) {
119: // TODO log this
120: error(Messages
121: .getString("InputForm.ERROR_0004_ACTION_NOT_FOUND") + solution + path + actionName); //$NON-NLS-1$
122: return null;
123: }
124:
125: List actions = actionSequence
126: .getActionDefinitionsAndSequences();
127: IActionDefinition action = (IActionDefinition) actions.get(0);
128:
129: Node node = action.getComponentSection();
130: if (node == null) {
131: error(Messages
132: .getString("InputForm.ERROR_0005_INBOX_DEFINITION_MISSING") + solution + path + actionName); //$NON-NLS-1$
133: return null;
134: }
135:
136: if (templateName == null) {
137:
138: // see if the template is specified in the action document
139: Node templateNode = node.selectSingleNode("//template"); //$NON-NLS-1$
140: if (templateNode != null) {
141: templateName = templateNode.getText();
142: }
143: if (templateName == null) {
144: error(Messages
145: .getString("InputForm.ERROR_0006_TEMPLATE_NOT_SPECIFIED")); //$NON-NLS-1$
146: return null;
147: }
148: }
149: Node xFormNode = node.selectSingleNode("//xForm"); //$NON-NLS-1$
150:
151: try {
152:
153: String actionTitle = actionSequence.getTitle();
154: if (actionTitle != null) {
155: setXslProperty("title", actionTitle); //$NON-NLS-1$
156: }
157:
158: String description = actionSequence.getDescription();
159: if (description != null) {
160: setXslProperty("description", description); //$NON-NLS-1$
161: }
162:
163: String xFormHtml = XForm.transformSnippet(xFormNode,
164: getSession());
165: if (xFormHtml == null) {
166: error(Messages
167: .getString("InputForm.ERROR_0007_INBOX_DEFINITION_INVALID") + solution + path + actionName); //$NON-NLS-1$
168: return null;
169: }
170: Document document = DocumentHelper.parseText(xFormHtml);
171: Node xFormHtmlNode = document.selectSingleNode("//xForm"); //$NON-NLS-1$
172:
173: setXslProperty("xForm", xFormHtmlNode.asXML()); //$NON-NLS-1$
174:
175: if (stylesheetName != null && !"".equals(stylesheetName)) { //$NON-NLS-1$
176: setXslProperty("css", stylesheetName); //$NON-NLS-1$
177: }
178: setXsl("text/html", templateName); //$NON-NLS-1$
179:
180: return document;
181:
182: } catch (Exception e) {
183: return null;
184: }
185:
186: }
187:
188: }
|