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.List; |
20 |
|
import java.util.Map; |
21 |
|
|
22 |
|
import org.apache.commons.logging.Log; |
23 |
|
import org.apache.commons.logging.LogFactory; |
24 |
|
import org.apache.jetspeed.JetspeedActions; |
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 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
public class AddPortletAction |
50 |
|
extends MovePortletAction |
51 |
|
implements AjaxAction, AjaxBuilder, Constants |
52 |
|
{ |
53 |
0 |
protected Log log = LogFactory.getLog(AddPortletAction.class); |
54 |
0 |
protected GetPortletsAction getPortletsAction = null; |
55 |
0 |
protected boolean allowDuplicatePortlets = true; |
56 |
|
|
57 |
|
public AddPortletAction(String template, String errorTemplate, GetPortletsAction getPortletsAction) |
58 |
|
throws AJAXException |
59 |
|
{ |
60 |
0 |
this(template, errorTemplate, null, class="keyword">null, getPortletsAction, true); |
61 |
0 |
} |
62 |
|
|
63 |
|
public AddPortletAction(String template, |
64 |
|
String errorTemplate, |
65 |
|
PageManager pageManager, |
66 |
|
PortletActionSecurityBehavior securityBehavior, |
67 |
|
GetPortletsAction getPortletsAction) |
68 |
|
throws AJAXException |
69 |
|
{ |
70 |
0 |
this(template, errorTemplate, pageManager, securityBehavior, getPortletsAction, true); |
71 |
0 |
} |
72 |
|
|
73 |
|
public AddPortletAction(String template, |
74 |
|
String errorTemplate, |
75 |
|
PageManager pageManager, |
76 |
|
PortletActionSecurityBehavior securityBehavior, |
77 |
|
GetPortletsAction getPortletsAction, |
78 |
|
boolean allowDuplicatePortlets) |
79 |
|
throws AJAXException |
80 |
|
{ |
81 |
0 |
super(template, errorTemplate, pageManager, securityBehavior); |
82 |
0 |
this.getPortletsAction = getPortletsAction; |
83 |
0 |
this.allowDuplicatePortlets = allowDuplicatePortlets; |
84 |
0 |
} |
85 |
|
|
86 |
|
protected boolean runAction( RequestContext requestContext, Map resultMap, class="keyword">boolean batch ) throws AJAXException |
87 |
|
{ |
88 |
0 |
boolean success = true; |
89 |
0 |
String status = "success"; |
90 |
|
try |
91 |
|
{ |
92 |
0 |
resultMap.put(ACTION, "add"); |
93 |
|
|
94 |
0 |
String portletId = getActionParameter(requestContext, PORTLETID); |
95 |
0 |
if (portletId == null) |
96 |
|
{ |
97 |
0 |
throw new RuntimeException("portlet id not provided"); |
98 |
|
} |
99 |
0 |
resultMap.put(PORTLETID, portletId); |
100 |
|
|
101 |
|
|
102 |
|
|
103 |
0 |
verifyPortletId(requestContext, portletId); |
104 |
|
|
105 |
0 |
if(allowDuplicatePortlets == false) { |
106 |
|
|
107 |
0 |
checkForDuplicatePortlet(requestContext, resultMap, portletId); |
108 |
|
} |
109 |
|
|
110 |
0 |
if (false == checkAccess(requestContext, JetspeedActions.EDIT)) |
111 |
|
{ |
112 |
0 |
if (!createNewPageOnEdit(requestContext)) |
113 |
|
{ |
114 |
0 |
success = false; |
115 |
0 |
resultMap.put(REASON, "Insufficient access to edit page"); |
116 |
0 |
return success; |
117 |
|
} |
118 |
0 |
status = "refresh"; |
119 |
|
} |
120 |
|
|
121 |
0 |
Page page = requestContext.getPage(); |
122 |
0 |
String layoutId = getActionParameter(requestContext, LAYOUTID); |
123 |
0 |
Fragment fragment = pageManager.newFragment(); |
124 |
0 |
fragment.setType(Fragment.PORTLET); |
125 |
0 |
fragment.setName(portletId); |
126 |
|
|
127 |
|
|
128 |
|
|
129 |
0 |
Fragment placeInLayoutFragment = null; |
130 |
0 |
if ( layoutId != null && layoutId.length() > 0 ) |
131 |
|
{ |
132 |
0 |
placeInLayoutFragment = page.getFragmentById( layoutId ); |
133 |
0 |
if ( placeInLayoutFragment == null ) |
134 |
|
{ |
135 |
0 |
throw new Exception( "layout id not found: " + layoutId ); |
136 |
|
} |
137 |
|
} |
138 |
|
else |
139 |
|
{ |
140 |
0 |
placeInLayoutFragment = page.getRootFragment(); |
141 |
|
} |
142 |
|
|
143 |
0 |
success = placeFragment( requestContext, |
144 |
|
pageManager, |
145 |
|
batch, |
146 |
|
resultMap, |
147 |
|
fragment, |
148 |
|
placeInLayoutFragment ) ; |
149 |
|
|
150 |
0 |
resultMap.put(STATUS, status); |
151 |
0 |
resultMap.put(PORTLETENTITY, fragment.getId()); |
152 |
|
} |
153 |
0 |
catch (Exception e) |
154 |
|
{ |
155 |
|
|
156 |
0 |
log.error("exception while adding a portlet", e); |
157 |
0 |
resultMap.put(REASON, e.toString()); |
158 |
|
|
159 |
|
|
160 |
0 |
success = false; |
161 |
0 |
} |
162 |
|
|
163 |
0 |
return success; |
164 |
|
} |
165 |
|
|
166 |
|
protected void verifyPortletId(RequestContext requestContext, String portletId) throws Exception |
167 |
|
{ |
168 |
|
|
169 |
0 |
List portletList = getPortletsAction.retrievePortlets(requestContext, null); |
170 |
0 |
if(portletList != null) { |
171 |
0 |
for(int i = 0; i < portletList.size(); i++) { |
172 |
0 |
PortletInfo portletInfo = (PortletInfo)portletList.get(i); |
173 |
0 |
if(portletInfo != null) { |
174 |
0 |
if(portletInfo.getName().equalsIgnoreCase(portletId)) { |
175 |
|
|
176 |
0 |
return; |
177 |
|
} |
178 |
|
} |
179 |
|
} |
180 |
|
} |
181 |
|
|
182 |
0 |
throw new Exception(portletId + " is not a valid portlet or not allowed for this user"); |
183 |
|
} |
184 |
|
|
185 |
|
protected void checkForDuplicatePortlet(RequestContext requestContext, Map resultMap, String portletId) |
186 |
|
throws AJAXException |
187 |
|
{ |
188 |
|
|
189 |
0 |
Page page = requestContext.getPage(); |
190 |
|
|
191 |
0 |
boolean duplicateFound = isDuplicateFragment(page.getRootFragment(), portletId); |
192 |
|
|
193 |
|
|
194 |
0 |
if(duplicateFound == true) { |
195 |
0 |
throw new AJAXException(portletId + " is already on the page, duplicates are not allowed"); |
196 |
|
} |
197 |
0 |
} |
198 |
|
|
199 |
|
protected boolean isDuplicateFragment(Fragment fragment, String portletId) { |
200 |
0 |
if(fragment != null) { |
201 |
|
|
202 |
0 |
String fragmentName = fragment.getName(); |
203 |
0 |
if(fragmentName.equals(portletId)) { |
204 |
|
|
205 |
0 |
return true; |
206 |
|
} else { |
207 |
|
|
208 |
0 |
List childFragments = fragment.getFragments(); |
209 |
0 |
if(childFragments != null) { |
210 |
0 |
for(int i = 0; i < childFragments.size(); i++) { |
211 |
|
|
212 |
0 |
if(isDuplicateFragment((Fragment)childFragments.get(i),portletId) == true) { |
213 |
|
|
214 |
0 |
return true; |
215 |
|
} |
216 |
|
} |
217 |
|
} |
218 |
|
} |
219 |
|
} |
220 |
|
|
221 |
0 |
return false; |
222 |
|
} |
223 |
|
} |