1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.layout.impl;
18
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.jetspeed.ajax.AJAXException;
26 import org.apache.jetspeed.ajax.AjaxAction;
27 import org.apache.jetspeed.ajax.AjaxBuilder;
28 import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
29 import org.apache.jetspeed.om.page.Fragment;
30 import org.apache.jetspeed.om.page.Page;
31 import org.apache.jetspeed.page.PageManager;
32 import org.apache.jetspeed.request.RequestContext;
33
34 /***
35 * Abstract portlet placement action
36 *
37 * @author <a>David Gurney</a>
38 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
39 * @version $Id: $
40 */
41 public abstract class BasePortletAction
42 implements AjaxAction, AjaxBuilder, Constants
43 {
44 protected static final Log log = LogFactory.getLog(BasePortletAction.class);
45 protected String template = null;
46 protected PageManager pageManager = null;
47 protected String errorTemplate = null;
48 protected PortletActionSecurityBehavior securityBehavior;
49
50 public BasePortletAction(String template,
51 String errorTemplate,
52 PortletActionSecurityBehavior securityBehavior)
53 {
54 this.template = template;
55 this.errorTemplate = errorTemplate;
56 this.securityBehavior = securityBehavior;
57 }
58
59 public BasePortletAction(String template,
60 String errorTemplate,
61 PageManager pageManager)
62 {
63 this.template = template;
64 this.errorTemplate = errorTemplate;
65 this.pageManager = pageManager;
66 this.securityBehavior = null;
67 }
68
69 public BasePortletAction(String template,
70 String errorTemplate,
71 PageManager pageManager,
72 PortletActionSecurityBehavior securityBehavior)
73 {
74 this(template, errorTemplate, securityBehavior);
75 this.pageManager = pageManager;
76 }
77
78 public boolean buildContext(RequestContext requestContext, Map responseContext)
79 {
80 return true;
81 }
82
83 public boolean buildErrorContext(RequestContext requestContext,
84 Map responseContext)
85 {
86 responseContext.put(STATUS, "failure");
87
88
89 if (responseContext.get(ACTION) == null)
90 {
91 responseContext.put(ACTION, "unknown");
92 }
93
94 if (responseContext.get(PORTLETID) == null)
95 {
96 responseContext.put(PORTLETID, "unknown");
97 }
98
99 return true;
100 }
101
102 public String getErrorTemplate()
103 {
104 return errorTemplate;
105 }
106
107 public String getTemplate()
108 {
109 return template;
110 }
111
112 public boolean checkAccess(RequestContext context, String action)
113 {
114 boolean access = true;
115 if (null != securityBehavior)
116 {
117 access = securityBehavior.checkAccess(context, action);
118 }
119 return access;
120 }
121
122 public boolean isCreateNewPageOnEditEnabled()
123 {
124 if (securityBehavior == null)
125 return false;
126 return securityBehavior.isCreateNewPageOnEditEnabled();
127 }
128 public boolean isPageQualifiedForCreateNewPageOnEdit(RequestContext context)
129 {
130 if (securityBehavior == null)
131 return false;
132 return securityBehavior.isPageQualifiedForCreateNewPageOnEdit(context);
133 }
134 public boolean createNewPageOnEdit(RequestContext context)
135 {
136 if (securityBehavior == null)
137 return false;
138
139 return securityBehavior.createNewPageOnEdit(context);
140 }
141
142 public Fragment getFragmentIdFromLocation( int row, int column, Page page )
143 {
144 return getFragmentIdFromLocation( row, column, page.getRootFragment() );
145 }
146 public Fragment getFragmentIdFromLocation( int row, int column, Fragment parentFragment )
147 {
148 Iterator fragments = parentFragment.getFragments().iterator();
149 while ( fragments.hasNext() )
150 {
151 Fragment fragment = (Fragment)fragments.next();
152 if ( fragment.getLayoutColumn() == column && fragment.getLayoutRow() == row )
153 {
154 return fragment;
155 }
156 }
157 return null;
158 }
159
160 public boolean runBatch(RequestContext requestContext, Map resultMap) throws AJAXException
161 {
162 return run(requestContext, resultMap);
163 }
164
165 public String getActionParameter(RequestContext requestContext, String name)
166 {
167 String parameter = requestContext.getRequestParameter(name);
168 if (parameter == null)
169 {
170 Object o = requestContext.getAttribute(name);
171 if (o != null)
172 {
173 if (o instanceof String)
174 return (String)o;
175 }
176 }
177 return parameter;
178 }
179
180 public String getNonNullActionParameter(RequestContext requestContext, String name)
181 {
182 String result = getActionParameter(requestContext, name);
183 if (result == null)
184 return "";
185 return result;
186 }
187
188 public Fragment getParentFragmentById(String id, Fragment root)
189 {
190 return NestedFragmentContext.getParentFragmentById( id, root );
191 }
192 }