1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.components.portletentity; |
18 |
|
|
19 |
|
import java.io.IOException; |
20 |
|
import java.rmi.server.UID; |
21 |
|
import java.util.Collection; |
22 |
|
import java.util.Iterator; |
23 |
|
import java.util.prefs.BackingStoreException; |
24 |
|
|
25 |
|
import org.apache.jetspeed.components.portletregistry.PortletRegistry; |
26 |
|
import org.apache.jetspeed.container.window.PortletWindowAccessor; |
27 |
|
import org.apache.jetspeed.om.common.portlet.MutablePortletApplication; |
28 |
|
import org.apache.jetspeed.om.common.portlet.MutablePortletEntity; |
29 |
|
import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite; |
30 |
|
import org.apache.jetspeed.om.page.ContentFragment; |
31 |
|
import org.apache.jetspeed.om.page.Fragment; |
32 |
|
import org.apache.jetspeed.om.preference.impl.PrefsPreferenceSetImpl; |
33 |
|
import org.apache.jetspeed.request.RequestContextComponent; |
34 |
|
import org.apache.jetspeed.util.JetspeedObjectID; |
35 |
|
import org.apache.ojb.broker.query.Criteria; |
36 |
|
import org.apache.ojb.broker.query.Query; |
37 |
|
import org.apache.ojb.broker.query.QueryFactory; |
38 |
|
import org.apache.pluto.om.common.ObjectID; |
39 |
|
import org.apache.pluto.om.common.PreferenceSet; |
40 |
|
import org.apache.pluto.om.entity.PortletEntity; |
41 |
|
import org.apache.pluto.om.entity.PortletEntityCtrl; |
42 |
|
import org.apache.pluto.om.portlet.PortletDefinition; |
43 |
|
import org.apache.pluto.om.window.PortletWindow; |
44 |
|
import org.springframework.orm.ojb.support.PersistenceBrokerDaoSupport; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
public class PersistenceBrokerPortletEntityAccess extends PersistenceBrokerDaoSupport |
59 |
|
implements |
60 |
|
PortletEntityAccessComponent |
61 |
|
{ |
62 |
|
private PortletRegistry registry; |
63 |
0 |
private PortletWindowAccessor windowAccessor = null; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
0 |
boolean mergeSharedPreferences = false; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
public PersistenceBrokerPortletEntityAccess( PortletRegistry registry ) |
77 |
|
{ |
78 |
0 |
super(); |
79 |
0 |
this.registry = registry; |
80 |
0 |
PortletEntityImpl.registry = registry; |
81 |
0 |
} |
82 |
|
|
83 |
|
public PersistenceBrokerPortletEntityAccess(PortletRegistry registry, RequestContextComponent rcc) |
84 |
|
{ |
85 |
0 |
super(); |
86 |
0 |
this.registry = registry; |
87 |
0 |
PortletEntityImpl.registry = registry; |
88 |
0 |
PortletEntityImpl.rcc = rcc; |
89 |
0 |
} |
90 |
|
|
91 |
|
public PersistenceBrokerPortletEntityAccess(PortletRegistry registry, RequestContextComponent rcc, boolean mergeSharedPreferences) |
92 |
|
{ |
93 |
0 |
super(); |
94 |
0 |
this.registry = registry; |
95 |
0 |
PortletEntityImpl.registry = registry; |
96 |
0 |
PortletEntityImpl.rcc = rcc; |
97 |
0 |
this.mergeSharedPreferences = mergeSharedPreferences; |
98 |
0 |
} |
99 |
|
|
100 |
|
public void setEntityAccessProxy(PortletEntityAccessComponent proxy) |
101 |
|
{ |
102 |
0 |
PortletEntityImpl.pac = proxy; |
103 |
0 |
} |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
public MutablePortletEntity generateEntityFromFragment( ContentFragment fragment, String principal ) |
118 |
|
throws PortletEntityNotGeneratedException |
119 |
|
{ |
120 |
0 |
PortletDefinition pd = registry.getPortletDefinitionByUniqueName(fragment.getName()); |
121 |
0 |
ObjectID entityKey = generateEntityKey(fragment, principal); |
122 |
0 |
MutablePortletEntity portletEntity = null; |
123 |
|
|
124 |
0 |
if (pd != null) |
125 |
|
{ |
126 |
0 |
portletEntity = newPortletEntityInstance(pd); |
127 |
0 |
if (portletEntity == null) |
128 |
|
{ |
129 |
0 |
throw new PortletEntityNotGeneratedException("Failed to create Portlet Entity for " |
130 |
|
+ fragment.getName()); |
131 |
|
} |
132 |
|
} |
133 |
|
else |
134 |
|
{ |
135 |
0 |
String msg = "Failed to retrieve Portlet Definition for " + fragment.getName(); |
136 |
0 |
logger.warn(msg); |
137 |
0 |
portletEntity = new PortletEntityImpl(fragment); |
138 |
0 |
fragment.overrideRenderedContent(msg); |
139 |
|
} |
140 |
|
|
141 |
0 |
portletEntity.setId(entityKey.toString()); |
142 |
|
|
143 |
0 |
return portletEntity; |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
public MutablePortletEntity generateEntityFromFragment( ContentFragment fragment ) |
158 |
|
throws PortletEntityNotGeneratedException |
159 |
|
{ |
160 |
0 |
return generateEntityFromFragment(fragment, null); |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
public ObjectID generateEntityKey( Fragment fragment, String principal ) |
176 |
|
{ |
177 |
0 |
StringBuffer key = new StringBuffer(); |
178 |
0 |
if (principal != null && principal.length() > 0) |
179 |
|
{ |
180 |
0 |
key.append(principal); |
181 |
0 |
key.append("/"); |
182 |
|
} |
183 |
0 |
key.append(fragment.getId()); |
184 |
0 |
return JetspeedObjectID.createFromString(key.toString()); |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
public Collection getPortletEntities( PortletDefinition portletDefinition ) |
198 |
|
{ |
199 |
0 |
Criteria c = new Criteria(); |
200 |
0 |
String appName = ((MutablePortletApplication) portletDefinition.getPortletApplicationDefinition()).getName(); |
201 |
0 |
String portletName = portletDefinition.getName(); |
202 |
0 |
c.addEqualTo("appName", appName); |
203 |
0 |
c.addEqualTo("portletName", portletName); |
204 |
|
|
205 |
0 |
return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(PortletEntityImpl.class, c)); |
206 |
|
} |
207 |
|
|
208 |
|
public Collection getPortletEntities( String portletUniqueName ) |
209 |
|
{ |
210 |
0 |
String[] split = portletUniqueName.split("::"); |
211 |
0 |
String appName = split[0]; |
212 |
0 |
String portletName = split[1]; |
213 |
0 |
Criteria c = new Criteria(); |
214 |
0 |
c.addEqualTo("appName", appName); |
215 |
0 |
c.addEqualTo("portletName", portletName); |
216 |
|
|
217 |
0 |
return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(PortletEntityImpl.class, c)); |
218 |
|
} |
219 |
|
|
220 |
|
public MutablePortletEntity getPortletEntity( ObjectID id ) |
221 |
|
{ |
222 |
|
try |
223 |
|
{ |
224 |
0 |
return getPortletEntity(id, null); |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
0 |
catch (PortletEntityNotStoredException e) |
229 |
|
{ |
230 |
0 |
IllegalStateException ise = new IllegalStateException("Unexepected error while retrieving portlet entity "+id); |
231 |
0 |
ise.initCause(e); |
232 |
0 |
throw ise; |
233 |
|
} |
234 |
|
} |
235 |
|
|
236 |
|
protected MutablePortletEntity getPortletEntity(ObjectID id, ContentFragment fragment) throws PortletEntityNotStoredException |
237 |
|
{ |
238 |
0 |
Criteria c = new Criteria(); |
239 |
0 |
c.addEqualTo("id", id.toString()); |
240 |
0 |
Query q = QueryFactory.newQuery(PortletEntityImpl.class, c); |
241 |
0 |
MutablePortletEntity portletEntity = (MutablePortletEntity) getPersistenceBrokerTemplate().getObjectByQuery(q); |
242 |
0 |
if (portletEntity == null) |
243 |
|
{ |
244 |
0 |
return null; |
245 |
|
} |
246 |
|
else |
247 |
|
{ |
248 |
0 |
String portletUniqueName = portletEntity.getPortletUniqueName(); |
249 |
0 |
PortletDefinitionComposite parentPortletDef = registry.getPortletDefinitionByUniqueName(portletUniqueName); |
250 |
0 |
if(parentPortletDef != null) |
251 |
|
{ |
252 |
|
|
253 |
0 |
if(fragment != null && !portletUniqueName.equals(fragment.getName())) |
254 |
|
{ |
255 |
0 |
parentPortletDef = registry.getPortletDefinitionByUniqueName(fragment.getName()); |
256 |
0 |
((PortletEntityCtrl)portletEntity).setPortletDefinition(parentPortletDef); |
257 |
0 |
storePortletEntity(portletEntity); |
258 |
|
} |
259 |
|
else |
260 |
|
{ |
261 |
0 |
((PortletEntityCtrl)portletEntity).setPortletDefinition(parentPortletDef); |
262 |
|
} |
263 |
|
} |
264 |
0 |
else if(fragment != null && parentPortletDef == class="keyword">null) |
265 |
|
{ |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
0 |
parentPortletDef = registry.getPortletDefinitionByUniqueName(fragment.getName()); |
270 |
0 |
if ( parentPortletDef != null) |
271 |
|
{ |
272 |
0 |
((PortletEntityCtrl)portletEntity).setPortletDefinition(parentPortletDef); |
273 |
0 |
storePortletEntity(portletEntity); |
274 |
|
} |
275 |
|
} |
276 |
|
|
277 |
0 |
if(parentPortletDef == null) |
278 |
|
{ |
279 |
0 |
final String msg = "Portlet "+portletUniqueName+" not found"; |
280 |
0 |
String content = fragment.getOverriddenContent(); |
281 |
0 |
if (content == null || !content.equals(msg)) |
282 |
|
{ |
283 |
0 |
fragment.overrideRenderedContent(msg); |
284 |
0 |
logger.error(msg); |
285 |
|
} |
286 |
|
} |
287 |
|
|
288 |
0 |
return portletEntity; |
289 |
|
} |
290 |
|
} |
291 |
|
|
292 |
|
public MutablePortletEntity getPortletEntity( String id ) |
293 |
|
{ |
294 |
0 |
ObjectID oid = JetspeedObjectID.createFromString(id); |
295 |
0 |
return getPortletEntity(oid); |
296 |
|
} |
297 |
|
|
298 |
|
public MutablePortletEntity getPortletEntityForFragment( ContentFragment fragment, String principal ) throws PortletEntityNotStoredException |
299 |
|
{ |
300 |
0 |
return getPortletEntity(generateEntityKey(fragment, principal), fragment); |
301 |
|
} |
302 |
|
|
303 |
|
public MutablePortletEntity getPortletEntityForFragment( ContentFragment fragment ) throws PortletEntityNotStoredException |
304 |
|
{ |
305 |
0 |
return getPortletEntity(generateEntityKey(fragment, null), fragment); |
306 |
|
} |
307 |
|
|
308 |
|
public MutablePortletEntity newPortletEntityInstance( PortletDefinition portletDefinition ) |
309 |
|
{ |
310 |
0 |
return newPortletEntityInstance(portletDefinition, autoGenerateID(portletDefinition)); |
311 |
|
} |
312 |
|
|
313 |
|
public MutablePortletEntity newPortletEntityInstance(PortletDefinition portletDefinition, String id) |
314 |
|
{ |
315 |
0 |
PortletEntityImpl portletEntity = new PortletEntityImpl(); |
316 |
0 |
portletEntity.setPortletDefinition(portletDefinition); |
317 |
0 |
portletEntity.setId(id); |
318 |
0 |
return portletEntity; |
319 |
|
} |
320 |
|
|
321 |
|
|
322 |
|
public void removeFromCache(PortletEntity entity) |
323 |
|
{ |
324 |
0 |
if (windowAccessor != null) |
325 |
|
{ |
326 |
0 |
String windowId = entity.getId().toString(); |
327 |
0 |
PortletWindow window = windowAccessor.getPortletWindow(windowId); |
328 |
0 |
if (window != null) |
329 |
|
{ |
330 |
0 |
windowAccessor.removeWindow(window); |
331 |
|
} |
332 |
|
} |
333 |
0 |
} |
334 |
|
|
335 |
|
public void removePortletEntities( PortletDefinition portletDefinition ) throws PortletEntityNotDeletedException |
336 |
|
{ |
337 |
0 |
Iterator entities = getPortletEntities(portletDefinition).iterator(); |
338 |
0 |
while (entities.hasNext()) |
339 |
|
{ |
340 |
0 |
PortletEntity entity = (PortletEntity) entities.next(); |
341 |
0 |
removePortletEntity(entity); |
342 |
0 |
} |
343 |
|
|
344 |
0 |
} |
345 |
|
|
346 |
|
public void removePortletEntity( PortletEntity portletEntity ) throws PortletEntityNotDeletedException |
347 |
|
{ |
348 |
0 |
PreferenceSet prefsSet = portletEntity.getPreferenceSet(); |
349 |
0 |
getPersistenceBrokerTemplate().delete(portletEntity); |
350 |
|
|
351 |
0 |
if(prefsSet instanceof PrefsPreferenceSetImpl) |
352 |
|
{ |
353 |
|
try |
354 |
|
{ |
355 |
0 |
((PrefsPreferenceSetImpl)prefsSet).clear(); |
356 |
0 |
removeFromCache(portletEntity); |
357 |
|
} |
358 |
0 |
catch (BackingStoreException e) |
359 |
|
{ |
360 |
0 |
throw new PortletEntityNotDeletedException("Failed to remove preferences for portlet entity "+portletEntity.getId()+". "+e.getMessage(), e); |
361 |
0 |
} |
362 |
|
} |
363 |
0 |
} |
364 |
|
|
365 |
|
|
366 |
|
|
367 |
|
|
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
|
375 |
|
|
376 |
|
|
377 |
|
public void updatePortletEntity(PortletEntity portletEntity, ContentFragment fragment) throws PortletEntityNotStoredException |
378 |
|
{ |
379 |
|
|
380 |
0 |
if (!fragment.getId().equals(portletEntity.getId().toString())) |
381 |
|
{ |
382 |
0 |
throw new PortletEntityNotStoredException("Fragment and PortletEntity ids do not match, update skipped: " + fragment.getId() + " != " + portletEntity.getId() ); |
383 |
|
} |
384 |
|
|
385 |
|
|
386 |
0 |
PortletDefinition pd = registry.getPortletDefinitionByUniqueName(fragment.getName()); |
387 |
0 |
if (pd != null) |
388 |
|
{ |
389 |
0 |
((PortletEntityImpl)portletEntity).setPortletDefinition(pd); |
390 |
|
} |
391 |
|
else |
392 |
|
{ |
393 |
0 |
throw new PortletEntityNotStoredException("Fragment PortletDefinition not found: " + fragment.getName() ); |
394 |
|
} |
395 |
0 |
} |
396 |
|
|
397 |
|
public void storePortletEntity( PortletEntity portletEntity ) throws PortletEntityNotStoredException |
398 |
|
{ |
399 |
|
try |
400 |
|
{ |
401 |
0 |
((PortletEntityCtrl) portletEntity).store(); |
402 |
|
} |
403 |
0 |
catch (Exception e) |
404 |
|
{ |
405 |
0 |
throw new PortletEntityNotStoredException(e.toString(), e); |
406 |
0 |
} |
407 |
|
|
408 |
0 |
} |
409 |
|
|
410 |
|
|
411 |
|
|
412 |
|
|
413 |
|
|
414 |
|
|
415 |
|
|
416 |
|
|
417 |
|
|
418 |
|
|
419 |
|
public void storePreferenceSet( PreferenceSet prefSet, PortletEntity entity ) throws IOException |
420 |
|
{ |
421 |
|
try |
422 |
|
{ |
423 |
0 |
getPersistenceBrokerTemplate().store(entity); |
424 |
0 |
if (prefSet != null && prefSet instanceof PrefsPreferenceSetImpl) |
425 |
|
{ |
426 |
0 |
((PrefsPreferenceSetImpl)prefSet).flush(); |
427 |
|
} |
428 |
|
|
429 |
|
} |
430 |
0 |
catch (Exception e) |
431 |
|
{ |
432 |
0 |
String msg = "Failed to store portlet entity:" + e.toString(); |
433 |
0 |
IOException ioe = new IOException(msg); |
434 |
0 |
ioe.initCause(e); |
435 |
0 |
throw ioe; |
436 |
0 |
} |
437 |
|
|
438 |
0 |
} |
439 |
|
|
440 |
|
protected String autoGenerateID(PortletDefinition pd) |
441 |
|
{ |
442 |
0 |
String appName = ((MutablePortletApplication)pd.getPortletApplicationDefinition()).getName(); |
443 |
0 |
String portletName = pd.getName(); |
444 |
0 |
return appName+"::"+portletName+"::"+new UID().toString(); |
445 |
|
} |
446 |
|
|
447 |
|
|
448 |
|
|
449 |
|
|
450 |
|
public boolean isMergeSharedPreferences() |
451 |
|
{ |
452 |
0 |
return this.mergeSharedPreferences; |
453 |
|
} |
454 |
|
|
455 |
|
|
456 |
|
} |