/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
*
* This file is part of the OpenWGA server platform.
*
* OpenWGA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, a special exception is granted by the copyright holders
* of OpenWGA called "OpenWGA plugin exception". You should have received
* a copy of this exception along with OpenWGA in file COPYING.
* If not, see <http://www.openwga.com/gpl-plugin-exception>.
*
* OpenWGA 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenWGA in file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package de.innovationgate.wgpublisher.webtml;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.jsp.PageContext;
import org.dom4j.Element;
import de.innovationgate.webgate.api.WGAPIException;
import de.innovationgate.wgpublisher.WGACore;
import de.innovationgate.wgpublisher.webtml.FormBase.FormStatus;
import de.innovationgate.wgpublisher.webtml.utils.TMLContext;
import de.innovationgate.wgpublisher.webtml.utils.TMLForm;
import de.innovationgate.wgpublisher.webtml.utils.TMLOption;
import de.innovationgate.wgpublisher.webtml.utils.Warning;
public class BaseTagStatus {
/**
* The class of the tag whose status we are
*/
public Class tagClass = null;
/**
* The status of our tags parent tag
*/
public BaseTagStatus parentTag = null;
/**
* The cascadation level of this tag to the absolute root tag
*/
protected Integer level = null;
/**
* All WebTML options for this tag
*/
protected Map tagOptions = null;
/**
* All options declared as dynamic attributes on this tag
*/
protected Map dynamicOptions = new HashMap();
/**
* Options set explicitly on this tag (excluding inherited ones)
*/
protected Map localTagOptions;
/**
* This tags own WebTML context
*/
public TMLContext tmlContext;
/**
* The WebTML context inherited to child tags
*/
protected TMLContext childTMLContext;
/**
* Flag if the tag should evaluate its body content
*/
public boolean evalBody = false;
/**
* Flag if the tag should put out its result to the page
*/
boolean resultOutput = true;
/**
* Iteration counter
*/
public int iteration = 0;
/**
* Flag showing if a context change failed on this tag
*/
boolean subContextError = false;
/**
* Flag showing if the tags execution was canceled
*/
public boolean cancelTag = false;
/**
* Flag showing if the tag should keep its result after it's execution finishes (for other tags/functionalities to reference on)
*/
protected boolean keepResult = false;
/**
* The tags result in native data type
*/
public Object result = null;
/**
* A suffix for the tag result, put out after it in unencoded form
*/
protected String suffix = "";
/**
* A suffix for the tag result, put out after it in unencoded form
*/
protected String prefix = "";
/**
* Attribute delegate: Encodings to put out the tags result in. Initialized with the tags "encode" attribute content.
*/
protected String encode;
/**
* Attribute delegate: Divider to put out between multiple result values. Initialized with the tags "divider" attribute content.
*/
protected String divider;
/**
* Attribute delegate: Output formatting for date and number values. Initialized with the tags "format" attribute content.
*/
protected String format;
/**
* WebTML debugger iteration nmde
*/
protected Element iterationDebugNode = null;
/**
* Number of subtags on this tag (WebTML debugger)
*/
protected int subtags = 0;
/**
* Starting time of the tag execution (WebTML debugger)
*/
protected long starttime = 0;
/**
* Duration of the tag execution in milliseconds (WebTML Debugger)
*/
protected long duration = 0;
/**
* Real processing time of the tags own functionality (excluding subtag duration) (WebTML debugger)
*/
protected long processingTime = 0;
/**
* The tags unique id
*/
public String id;
/**
* The line in the WebTML module which defines this tag
*/
public int sourceLine = 0;
public BaseTagStatus() {
}
public Object getTagInfo(String name) throws WGAPIException {
name = name.toLowerCase().trim();
if (name.equals("contentkey")) {
return this.tmlContext.content().getContentKey().toString();
}
else if (name.equals("iteration")) {
return iteration;
}
else if (name.equals("taglevel")) {
return level;
}
else if (name.equals("result")) {
return result;
}
else {
return null;
}
}
public void initAttributeDelegates(Base tag) {
this.encode = tag.getEncode();
this.divider = tag.getDivider();
this.format = tag.getFormat();
}
public BaseTagStatus getAncestorTag(Class parentClass) {
BaseTagStatus parent = this.getParentTag();
while (parent != null) {
if (parentClass.isAssignableFrom(parent.tagClass) || parentClass.isAssignableFrom(parent.getClass())) {
return parent;
}
else {
parent = parent.getParentTag();
}
}
return null;
}
/**
* search recursive for a parent includeTag with the given type
*
* @param type
* @return includeTag or null if not found
*/
public BaseTagStatus getAncestorIncludeTag(String type) {
Include.Status includeTag = (Include.Status) getAncestorTag(Include.class);
if (includeTag != null) {
if (includeTag.type.equals(type)) {
return includeTag;
}
else {
return includeTag.getAncestorIncludeTag(type);
}
}
else {
return null;
}
}
public BaseTagStatus getParentTag() {
if (parentTag == this) {
return null;
}
else {
return parentTag;
}
}
public void retrieveParentTag(Base tag) {
// Special functionality for tml:root searching its tml:include
// parent
if (this instanceof Root.Status) {
PageContext pageContext = tag.getPageContext();
Include.Status includeTag = (Include.Status) pageContext.getAttribute(Include.ATTRIB_INCLUDEPARENT, PageContext.REQUEST_SCOPE);
if (includeTag != null) {
pageContext.setAttribute(Include.ATTRIB_INCLUDEPARENT, null, PageContext.REQUEST_SCOPE);
parentTag = includeTag;
}
}
else {
javax.servlet.jsp.tagext.Tag parent = tag.getParent();
while (parent != null) {
if (parent instanceof Base) {
parentTag = ((Base) parent).getStatus();
break;
}
parent = parent.getParent();
}
}
}
public BaseTagStatus getParentTag(Class tagClass) {
BaseTagStatus tag = this.getParentTag();
if (tagClass.isAssignableFrom(tag.tagClass)) {
return tag;
}
else {
return null;
}
}
public Root.Status getRootTag() {
Root.Status tag = (Root.Status) tmlContext.getrequest().getAttribute(WGACore.ATTRIB_ROOT_TAG);
if (tag != null) {
return tag;
}
else if (this instanceof Root.Status) {
return (Root.Status) this;
}
else {
tmlContext.getlog().error("Tag " + id + " cannot identify root tag!");
return null;
}
}
public void addSubtags(int number) {
subtags += number;
}
public BaseTagStatus getTagStatusById(String id) {
return this.getTagStatusById(id, Base.class);
}
public BaseTagStatus getTagStatusById(String id, Class tagClass) {
BaseTagStatus tag = (BaseTagStatus) this.getTagIds().get(id);
if (tag != null && (tagClass.isAssignableFrom(tag.getClass()) || tagClass.isAssignableFrom(tag.tagClass))) {
return tag;
}
else {
return null;
}
}
public Map getTagIds() {
return (Map) this.tmlContext.getEnvironment().getPageContext().getAttribute(WGACore.ATTRIB_TAGIDS, PageContext.REQUEST_SCOPE);
}
public String getDesignDBKey() {
return (String) this.getOption(Base.OPTION_DESIGNDB);
}
public Object getOption(String optionName) {
TMLOption option = (TMLOption) tagOptions.get(optionName);
if (option != null) {
return option.getValue();
}
else {
return null;
}
}
public TMLOption getOptionObject(String optionName) {
TMLOption option = (TMLOption) tagOptions.get(optionName);
if (option != null) {
return option;
}
else {
return null;
}
}
public void addWarning(String msg, boolean severe) {
WGACore core = tmlContext.getwgacore();
if (!core.getWgaConfiguration().isWarningsEnabled()) {
return;
}
PageContext pageContext = tmlContext.getEnvironment().getPageContext();
ArrayList warnings = (ArrayList) pageContext.getAttribute(Base.class.getName() + ":Warnings", PageContext.REQUEST_SCOPE);
if (warnings == null) {
warnings = new ArrayList();
pageContext.setAttribute(Base.class.getName() + ":Warnings", warnings, PageContext.REQUEST_SCOPE);
}
Warning warning = new Warning(this, tmlContext, msg, severe);
warnings.add(warning);
if (core.getWgaConfiguration().isWarningsOutputOnConsole()) {
core.getLog().warn(warning.getConsoleText());
}
if (severe == true) {
cancelTag = true;
}
}
public boolean isBrowserInterface3() {
Boolean bi = (Boolean) tmlContext.gethttpsession().getAttribute(WGACore.ATTRIB_BI_VERSION3);
if (bi != null && bi.booleanValue() == true) {
return true;
}
else {
return false;
}
}
public boolean isBrowserInterface() {
return tmlContext.getwgacore().getDispatcher().isBrowserInterface(tmlContext.gethttpsession());
}
public boolean isBrowserInterface4() {
Boolean bi = (Boolean) tmlContext.gethttpsession().getAttribute(WGACore.ATTRIB_BI_VERSION4);
if (bi != null && bi.booleanValue() == true) {
return true;
}
else {
return false;
}
}
public String getRequestURL() {
return tmlContext.getrequest().getAttribute(WGACore.ATTRIB_REQUESTURL).toString();
}
public void setOption(String name, Object value, String scope) {
TMLOption option = new TMLOption(name, value, scope);
tagOptions.put(name, option);
localTagOptions.put(name, option.getValue());
}
public void removeOption(String option) {
tagOptions.remove(option);
}
public String getMainMediaKey() {
String mediaKey = (String) this.getOption(Base.OPTION_DEFAULT_MEDIAKEY);
if (mediaKey == null) {
mediaKey = (String) this.tmlContext.getrequest().getAttribute(WGACore.ATTRIB_MEDIAKEY);
}
return mediaKey;
}
public String getRelevantForm() {
FormStatus form = (FormStatus) getAncestorTag(Form.class);
//if (form != null && form.isFormEditable()) {
//--> none editable forms should be submitted to ensure access in tml:action
if (form != null) {
return form.id;
}
/*
* No automatic form detection. Form must be specified by
* form-Attribute, if it should be conserved on action call TMLForm
* tmlForm = (TMLForm)
* getTMLContext().getrequest().getAttribute(WGACore.ATTRIB_LASTFORM);
* if (tmlForm != null && tmlForm.isEditable()) { return
* tmlForm.getformid(); }
*/
Boolean subForm = (Boolean) tmlContext.getrequest().getAttribute(Root.REQUEST_ATTRIB_IS_AJAX_SUBFORM);
if (subForm != null && subForm) {
TMLForm tmlform = tmlContext.gettmlform();
if (tmlform != null) {
return tmlform.getformid();
}
}
return null;
}
public String getTMLModuleName() {
return (String) getOption(Base.OPTION_TMLMODULE_NAME);
}
public String getTMLModuleMediaKey() {
return (String) getOption(Base.OPTION_TMLMODULE_MEDIAKEY);
}
/**
* Returns a map of all WebTML options that were defined directly for the current WebTML tag.
* WebTML options that were inherited from the parent tag are omitted here.
* @return Map containing WebTML options, option names as key, option values as content
*/
public Map getLocalOptions() {
return new HashMap(localTagOptions);
}
public String getWGPPath() {
return tmlContext.getEnvironment().getPublisherURL();
}
public List<Warning> getWarnings() {
return tmlContext.getEnvironment().getWarnings();
}
public Map getTagOptions() {
return tagOptions;
}
public WGACore getCore() {
return tmlContext.getwgacore();
}
public String getScopedString(String str) {
String scope = (String) getOption(Base.OPTION_WEBTML_SCOPE);
if (scope == null) {
return str;
}
return this.tmlContext.getScopedString(str, scope);
}
}