001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.layout.impl;
018:
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.jetspeed.ajax.AJAXException;
026: import org.apache.jetspeed.ajax.AjaxAction;
027: import org.apache.jetspeed.ajax.AjaxBuilder;
028: import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
029: import org.apache.jetspeed.om.page.Fragment;
030: import org.apache.jetspeed.om.page.Page;
031: import org.apache.jetspeed.page.PageManager;
032: import org.apache.jetspeed.request.RequestContext;
033:
034: /**
035: * Abstract portlet placement action
036: *
037: * @author <a>David Gurney</a>
038: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
039: * @version $Id: $
040: */
041: public abstract class BasePortletAction implements AjaxAction,
042: AjaxBuilder, Constants {
043: protected static final Log log = LogFactory
044: .getLog(BasePortletAction.class);
045: protected String template = null;
046: protected PageManager pageManager = null;
047: protected String errorTemplate = null;
048: protected PortletActionSecurityBehavior securityBehavior;
049:
050: public BasePortletAction(String template, String errorTemplate,
051: PortletActionSecurityBehavior securityBehavior) {
052: this .template = template;
053: this .errorTemplate = errorTemplate;
054: this .securityBehavior = securityBehavior;
055: }
056:
057: public BasePortletAction(String template, String errorTemplate,
058: PageManager pageManager) {
059: this .template = template;
060: this .errorTemplate = errorTemplate;
061: this .pageManager = pageManager;
062: this .securityBehavior = null;
063: }
064:
065: public BasePortletAction(String template, String errorTemplate,
066: PageManager pageManager,
067: PortletActionSecurityBehavior securityBehavior) {
068: this (template, errorTemplate, securityBehavior);
069: this .pageManager = pageManager;
070: }
071:
072: public boolean buildContext(RequestContext requestContext,
073: Map responseContext) {
074: return true;
075: }
076:
077: public boolean buildErrorContext(RequestContext requestContext,
078: Map responseContext) {
079: responseContext.put(STATUS, "failure");
080:
081: // Check for the case where we don't know basic information
082: if (responseContext.get(ACTION) == null) {
083: responseContext.put(ACTION, "unknown");
084: }
085:
086: if (responseContext.get(PORTLETID) == null) {
087: responseContext.put(PORTLETID, "unknown");
088: }
089:
090: return true;
091: }
092:
093: public String getErrorTemplate() {
094: return errorTemplate;
095: }
096:
097: public String getTemplate() {
098: return template;
099: }
100:
101: public boolean checkAccess(RequestContext context, String action) {
102: boolean access = true;
103: if (null != securityBehavior) {
104: access = securityBehavior.checkAccess(context, action);
105: }
106: return access;
107: }
108:
109: public boolean isCreateNewPageOnEditEnabled() {
110: if (securityBehavior == null)
111: return false;
112: return securityBehavior.isCreateNewPageOnEditEnabled();
113: }
114:
115: public boolean isPageQualifiedForCreateNewPageOnEdit(
116: RequestContext context) {
117: if (securityBehavior == null)
118: return false;
119: return securityBehavior
120: .isPageQualifiedForCreateNewPageOnEdit(context);
121: }
122:
123: public boolean createNewPageOnEdit(RequestContext context) {
124: if (securityBehavior == null)
125: return false;
126:
127: return securityBehavior.createNewPageOnEdit(context);
128: }
129:
130: public Fragment getFragmentIdFromLocation(int row, int column,
131: Page page) {
132: return getFragmentIdFromLocation(row, column, page
133: .getRootFragment());
134: }
135:
136: public Fragment getFragmentIdFromLocation(int row, int column,
137: Fragment parentFragment) {
138: Iterator fragments = parentFragment.getFragments().iterator();
139: while (fragments.hasNext()) {
140: Fragment fragment = (Fragment) fragments.next();
141: if (fragment.getLayoutColumn() == column
142: && fragment.getLayoutRow() == row) {
143: return fragment;
144: }
145: }
146: return null;
147: }
148:
149: public boolean runBatch(RequestContext requestContext, Map resultMap)
150: throws AJAXException {
151: return run(requestContext, resultMap);
152: }
153:
154: public String getActionParameter(RequestContext requestContext,
155: String name) {
156: String parameter = requestContext.getRequestParameter(name);
157: if (parameter == null) {
158: Object o = requestContext.getAttribute(name);
159: if (o != null) {
160: if (o instanceof String)
161: return (String) o;
162: }
163: }
164: return parameter;
165: }
166:
167: public String getNonNullActionParameter(
168: RequestContext requestContext, String name) {
169: String result = getActionParameter(requestContext, name);
170: if (result == null)
171: return "";
172: return result;
173: }
174:
175: public Fragment getParentFragmentById(String id, Fragment root) {
176: return NestedFragmentContext.getParentFragmentById(id, root);
177: }
178: }
|