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