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: import org.apache.jetspeed.security.UserManager;
034:
035: /**
036: * Abstract portlet placement action
037: *
038: * @author <a>David Gurney</a>
039: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
040: * @author <a href="mailto:mikko.wuokko@evtek.fi">Mikko Wuokko</a>
041: * @version $Id: $
042: */
043: public abstract class BaseUserAction implements AjaxAction,
044: AjaxBuilder, Constants {
045: protected Log log = LogFactory.getLog(BaseUserAction.class);
046: protected String template = null;
047: protected UserManager userManager = null;
048: protected String errorTemplate = null;
049: protected RolesSecurityBehavior securityBehavior;
050:
051: public BaseUserAction(String template, String errorTemplate,
052: RolesSecurityBehavior securityBehavior) {
053: this .template = template;
054: this .errorTemplate = errorTemplate;
055: this .securityBehavior = securityBehavior;
056: }
057:
058: public BaseUserAction(String template, String errorTemplate,
059: UserManager userManager) {
060: this .template = template;
061: this .errorTemplate = errorTemplate;
062: this .userManager = userManager;
063: this .securityBehavior = null;
064: }
065:
066: public BaseUserAction(String template, String errorTemplate,
067: UserManager userManager,
068: RolesSecurityBehavior securityBehavior) {
069: this (template, errorTemplate, securityBehavior);
070: this .userManager = userManager;
071: }
072:
073: public boolean buildContext(RequestContext requestContext,
074: Map responseContext) {
075: return true;
076: }
077:
078: public boolean buildErrorContext(RequestContext requestContext,
079: Map responseContext) {
080: responseContext.put(STATUS, "failure");
081:
082: // Check for the case where we don't know basic information
083: if (responseContext.get(ACTION) == null) {
084: responseContext.put(ACTION, "unknown");
085: }
086:
087: if (responseContext.get(PORTLETID) == null) {
088: responseContext.put(PORTLETID, "unknown");
089: }
090:
091: return true;
092: }
093:
094: public String getErrorTemplate() {
095: return errorTemplate;
096: }
097:
098: public String getTemplate() {
099: return template;
100: }
101:
102: public boolean checkAccess(RequestContext context, String action) {
103: boolean access = true;
104: if (null != securityBehavior) {
105: access = securityBehavior.checkAccess(context, action);
106: }
107: return access;
108: }
109:
110: public boolean createNewPageOnEdit(RequestContext context) {
111: if (securityBehavior == null)
112: return false;
113:
114: return securityBehavior.createNewPageOnEdit(context);
115: }
116:
117: // TODO: support nested fragments
118: public Fragment getFragmentIdFromLocation(int row, int column,
119: Page page) {
120: Fragment root = page.getRootFragment();
121: Iterator fragments = root.getFragments().iterator();
122: while (fragments.hasNext()) {
123: Fragment fragment = (Fragment) fragments.next();
124: if (fragment.getLayoutColumn() == column
125: && fragment.getLayoutRow() == row) {
126: return fragment;
127: }
128: }
129: return null;
130: }
131:
132: public boolean runBatch(RequestContext requestContext, Map resultMap)
133: throws AJAXException {
134: return run(requestContext, resultMap);
135: }
136:
137: public String getActionParameter(RequestContext requestContext,
138: String name) {
139: String parameter = requestContext.getRequestParameter(name);
140: if (parameter == null) {
141: Object o = requestContext.getAttribute(name);
142: if (o != null) {
143: if (o instanceof String)
144: return (String) o;
145: }
146: }
147: return parameter;
148: }
149:
150: public Fragment getParentFragmentById(String id, Fragment root) {
151: if (id == null) {
152: return null;
153: }
154: return searchForParentFragmentById(id, root);
155: }
156:
157: protected Fragment searchForParentFragmentById(String id,
158: Fragment parent) {
159: // find fragment by id, tracking fragment parent
160: Fragment matchedParent = null;
161: if (parent != null) {
162: // process the children
163: List children = parent.getFragments();
164: for (int i = 0, cSize = children.size(); i < cSize; i++) {
165: Fragment childFrag = (Fragment) children.get(i);
166: if (childFrag != null) {
167: if (id.equals(childFrag.getId())) {
168: matchedParent = parent;
169: break;
170: } else {
171: matchedParent = searchForParentFragmentById(id,
172: childFrag);
173: if (matchedParent != null) {
174: break;
175: }
176: }
177: }
178: }
179: }
180: return matchedParent;
181: }
182:
183: /**
184: * Helper method to determine if a parameter is true. Prevents
185: * accidental NullPointerExceptions when comparing or or using
186: * the parameter value.
187: * @param parameter The value to be determined as boolean true or false.
188: * @return boolean true or false according to the @param value.
189: */
190: public boolean isTrue(String parameter) {
191: boolean isTrue = false;
192: if (parameter != null) {
193: if (parameter.equalsIgnoreCase("true")) {
194: isTrue = true;
195: }
196: }
197: return isTrue;
198: }
199:
200: }
|