001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
020 import com.liferay.portal.kernel.portlet.LiferayPortletMode;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.JavaConstants;
023 import com.liferay.portal.kernel.util.ParamUtil;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.kernel.xml.simple.Element;
026 import com.liferay.portal.model.Layout;
027 import com.liferay.portal.model.LayoutConstants;
028 import com.liferay.portal.model.LayoutTypePortlet;
029 import com.liferay.portal.model.Portlet;
030 import com.liferay.portal.model.PortletConstants;
031 import com.liferay.portal.model.PortletPreferencesIds;
032 import com.liferay.portal.security.auth.PrincipalException;
033 import com.liferay.portal.security.permission.ActionKeys;
034 import com.liferay.portal.security.permission.PermissionChecker;
035 import com.liferay.portal.security.permission.PermissionThreadLocal;
036 import com.liferay.portal.service.PortalPreferencesLocalServiceUtil;
037 import com.liferay.portal.service.PortletLocalServiceUtil;
038 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
039 import com.liferay.portal.service.UserLocalServiceUtil;
040 import com.liferay.portal.service.permission.LayoutPermissionUtil;
041 import com.liferay.portal.theme.ThemeDisplay;
042 import com.liferay.portal.util.PortalUtil;
043 import com.liferay.portal.util.PortletKeys;
044 import com.liferay.portal.util.WebKeys;
045 import com.liferay.portal.xml.StAXReaderUtil;
046
047 import java.util.ArrayList;
048 import java.util.HashMap;
049 import java.util.List;
050 import java.util.Map;
051
052 import javax.portlet.PortletPreferences;
053 import javax.portlet.PortletRequest;
054 import javax.portlet.PreferencesValidator;
055
056 import javax.servlet.http.HttpServletRequest;
057 import javax.servlet.http.HttpSession;
058
059 import javax.xml.stream.XMLEventReader;
060 import javax.xml.stream.XMLInputFactory;
061 import javax.xml.stream.XMLStreamException;
062 import javax.xml.stream.events.EndElement;
063 import javax.xml.stream.events.StartElement;
064 import javax.xml.stream.events.XMLEvent;
065
066
071 public class PortletPreferencesFactoryImpl
072 implements PortletPreferencesFactory {
073
074 public PortletPreferences fromDefaultXML(String xml)
075 throws SystemException {
076
077 PortletPreferencesImpl portletPreferencesImpl =
078 new PortletPreferencesImpl();
079
080 Map<String, Preference> preferencesMap =
081 portletPreferencesImpl.getPreferences();
082
083 populateMap(xml, preferencesMap);
084
085 return portletPreferencesImpl;
086 }
087
088 public PortletPreferencesImpl fromXML(
089 long companyId, long ownerId, int ownerType, long plid,
090 String portletId, String xml)
091 throws SystemException {
092
093 try {
094 Map<String, Preference> preferencesMap =
095 new HashMap<String, Preference>();
096
097 populateMap(xml, preferencesMap);
098
099 return new PortletPreferencesImpl(
100 companyId, ownerId, ownerType, plid, portletId,
101 preferencesMap);
102 }
103 catch (SystemException se) {
104 throw se;
105 }
106 }
107
108 public PortalPreferencesImpl fromXML(
109 long companyId, long ownerId, int ownerType, String xml)
110 throws SystemException {
111
112 try {
113 Map<String, Preference> preferencesMap =
114 new HashMap<String, Preference>();
115
116 populateMap(xml, preferencesMap);
117
118 return new PortalPreferencesImpl(
119 companyId, ownerId, ownerType, preferencesMap, false);
120 }
121 catch (SystemException se) {
122 throw se;
123 }
124 }
125
126 public PortletPreferences getLayoutPortletSetup(
127 Layout layout, String portletId)
128 throws SystemException {
129
130 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
131 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
132
133 return PortletPreferencesLocalServiceUtil.getPreferences(
134 layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
135 portletId);
136 }
137
138 public PortalPreferences getPortalPreferences(HttpServletRequest request)
139 throws SystemException {
140
141 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
142 WebKeys.THEME_DISPLAY);
143
144 return getPortalPreferences(
145 request.getSession(), themeDisplay.getCompanyId(),
146 themeDisplay.getUserId(), themeDisplay.isSignedIn());
147 }
148
149 public PortalPreferences getPortalPreferences(
150 HttpSession session, long companyId, long userId, boolean signedIn)
151 throws SystemException {
152
153 long ownerId = userId;
154 int ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
155
156 PortalPreferences portalPreferences = null;
157
158 if (signedIn) {
159 PortalPreferencesWrapper portalPreferencesWrapper =
160 (PortalPreferencesWrapper)
161 PortalPreferencesLocalServiceUtil.getPreferences(
162 companyId, ownerId, ownerType);
163
164 portalPreferences =
165 portalPreferencesWrapper.getPortalPreferencesImpl();
166 }
167 else {
168 if (session != null) {
169 portalPreferences = (PortalPreferences)session.getAttribute(
170 WebKeys.PORTAL_PREFERENCES);
171 }
172
173 if (portalPreferences == null) {
174 PortalPreferencesWrapper portalPreferencesWrapper =
175 (PortalPreferencesWrapper)
176 PortalPreferencesLocalServiceUtil.getPreferences(
177 companyId, ownerId, ownerType);
178
179 PortalPreferencesImpl portalPreferencesImpl =
180 portalPreferencesWrapper.getPortalPreferencesImpl();
181
182 portalPreferences =
183 (PortalPreferences)portalPreferencesImpl.clone();
184
185 if (session != null) {
186 session.setAttribute(
187 WebKeys.PORTAL_PREFERENCES, portalPreferences);
188 }
189 }
190 }
191
192 portalPreferences.setSignedIn(signedIn);
193
194 return portalPreferences;
195 }
196
197 public PortalPreferences getPortalPreferences(
198 long companyId, long userId, boolean signedIn)
199 throws SystemException {
200
201 return getPortalPreferences(null, companyId, userId, signedIn);
202 }
203
204 public PortalPreferences getPortalPreferences(PortletRequest portletRequest)
205 throws SystemException {
206
207 HttpServletRequest request = PortalUtil.getHttpServletRequest(
208 portletRequest);
209
210 return getPortalPreferences(request);
211 }
212
213 public PortletPreferences getPortletPreferences(
214 HttpServletRequest request, String portletId)
215 throws PortalException, SystemException {
216
217 PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
218 request, portletId);
219
220 return PortletPreferencesLocalServiceUtil.getPreferences(
221 portletPreferencesIds);
222 }
223
224 public PortletPreferencesIds getPortletPreferencesIds(
225 HttpServletRequest request, Layout layout, String portletId)
226 throws PortalException, SystemException {
227
228 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
229 WebKeys.THEME_DISPLAY);
230
231 long scopeGroupId = PortalUtil.getScopeGroupId(
232 request, portletId);
233 long userId = PortalUtil.getUserId(request);
234 LayoutTypePortlet layoutTypePortlet =
235 themeDisplay.getLayoutTypePortlet();
236
237 boolean modeEditGuest = false;
238
239 String portletMode = ParamUtil.getString(request, "p_p_mode");
240
241 if (portletMode.equals(LiferayPortletMode.EDIT_GUEST.toString()) ||
242 ((layoutTypePortlet != null) &&
243 (layoutTypePortlet.hasModeEditGuestPortletId(portletId)))) {
244
245 modeEditGuest = true;
246 }
247
248 return getPortletPreferencesIds(
249 scopeGroupId, userId, layout, portletId, modeEditGuest);
250 }
251
252 public PortletPreferencesIds getPortletPreferencesIds(
253 HttpServletRequest request, String portletId)
254 throws PortalException, SystemException {
255
256 Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
257
258 return getPortletPreferencesIds(request, layout, portletId);
259 }
260
261 public PortletPreferencesIds getPortletPreferencesIds(
262 long scopeGroupId, long userId, Layout layout, String portletId,
263 boolean modeEditGuest)
264 throws PortalException, SystemException {
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287 PermissionChecker permissionChecker =
288 PermissionThreadLocal.getPermissionChecker();
289
290 Portlet portlet = PortletLocalServiceUtil.getPortletById(
291 layout.getCompanyId(), portletId);
292
293 long ownerId = 0;
294 int ownerType = 0;
295 long plid = 0;
296
297 if (modeEditGuest) {
298 boolean hasUpdateLayoutPermission = LayoutPermissionUtil.contains(
299 permissionChecker, layout, ActionKeys.UPDATE);
300
301 if (!layout.isPrivateLayout() && hasUpdateLayoutPermission) {
302 }
303 else {
304
305
306
307
308 throw new PrincipalException();
309 }
310 }
311
312 if (portlet.isPreferencesCompanyWide()) {
313 ownerId = layout.getCompanyId();
314 ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
315 plid = PortletKeys.PREFS_PLID_SHARED;
316 portletId = PortletConstants.getRootPortletId(portletId);
317 }
318 else {
319 if (portlet.isPreferencesUniquePerLayout()) {
320 ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
321 ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
322 plid = layout.getPlid();
323
324 if (portlet.isPreferencesOwnedByGroup()) {
325 }
326 else {
327 if ((userId <= 0) || modeEditGuest) {
328 userId = UserLocalServiceUtil.getDefaultUserId(
329 layout.getCompanyId());
330 }
331
332 ownerId = userId;
333 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
334 }
335 }
336 else {
337 plid = PortletKeys.PREFS_PLID_SHARED;
338
339 if (portlet.isPreferencesOwnedByGroup()) {
340 ownerId = scopeGroupId;
341 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
342 portletId = PortletConstants.getRootPortletId(portletId);
343 }
344 else {
345 if ((userId <= 0) || modeEditGuest) {
346 userId = UserLocalServiceUtil.getDefaultUserId(
347 layout.getCompanyId());
348 }
349
350 ownerId = userId;
351 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
352 }
353 }
354 }
355
356 return new PortletPreferencesIds(
357 layout.getCompanyId(), ownerId, ownerType, plid, portletId);
358 }
359
360 public PortletPreferences getPortletSetup(
361 HttpServletRequest request, String portletId)
362 throws PortalException, SystemException {
363
364 return getPortletSetup(request, portletId, null);
365 }
366
367 public PortletPreferences getPortletSetup(
368 HttpServletRequest request, String portletId,
369 String defaultPreferences)
370 throws PortalException, SystemException {
371
372 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
373 WebKeys.THEME_DISPLAY);
374
375 long scopeGroupId = PortalUtil.getScopeGroupId(request, portletId);
376
377 return getPortletSetup(
378 scopeGroupId, themeDisplay.getLayout(), portletId,
379 defaultPreferences);
380 }
381
382 public PortletPreferences getPortletSetup(
383 Layout layout, String portletId, String defaultPreferences)
384 throws SystemException {
385
386 return getPortletSetup(
387 LayoutConstants.DEFAULT_PLID, layout, portletId,
388 defaultPreferences);
389 }
390
391 public PortletPreferences getPortletSetup(
392 long scopeGroupId, Layout layout, String portletId,
393 String defaultPreferences)
394 throws SystemException {
395
396 Portlet portlet = PortletLocalServiceUtil.getPortletById(
397 layout.getCompanyId(), portletId);
398
399 boolean uniquePerLayout = false;
400 boolean uniquePerGroup = false;
401
402 if (portlet.isPreferencesCompanyWide()) {
403 portletId = PortletConstants.getRootPortletId(portletId);
404 }
405 else {
406 if (portlet.isPreferencesUniquePerLayout()) {
407 uniquePerLayout = true;
408
409 if (portlet.isPreferencesOwnedByGroup()) {
410 uniquePerGroup = true;
411 }
412 }
413 else {
414 if (portlet.isPreferencesOwnedByGroup()) {
415 uniquePerGroup = true;
416 portletId = PortletConstants.getRootPortletId(portletId);
417 }
418 }
419 }
420
421 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
422 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
423 long plid = layout.getPlid();
424
425 if (!uniquePerLayout) {
426 plid = PortletKeys.PREFS_PLID_SHARED;
427
428 if (uniquePerGroup) {
429 if (scopeGroupId > LayoutConstants.DEFAULT_PLID) {
430 ownerId = scopeGroupId;
431 }
432 else {
433 ownerId = layout.getGroupId();
434 }
435
436 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
437 }
438 else {
439 ownerId = layout.getCompanyId();
440 ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
441 }
442 }
443
444 return PortletPreferencesLocalServiceUtil.getPreferences(
445 layout.getCompanyId(), ownerId, ownerType, plid, portletId,
446 defaultPreferences);
447 }
448
449 public PortletPreferences getPortletSetup(PortletRequest portletRequest)
450 throws PortalException, SystemException {
451
452 HttpServletRequest request = PortalUtil.getHttpServletRequest(
453 portletRequest);
454 String portletId = PortalUtil.getPortletId(portletRequest);
455
456 return getPortletSetup(request, portletId);
457 }
458
459 public PortletPreferences getPortletSetup(
460 PortletRequest portletRequest, String portletId)
461 throws PortalException, SystemException {
462
463 HttpServletRequest request = PortalUtil.getHttpServletRequest(
464 portletRequest);
465
466 return getPortletSetup(request, portletId);
467 }
468
469 public PortletPreferences getPreferences(HttpServletRequest request) {
470 PortletRequest portletRequest = (PortletRequest)request.getAttribute(
471 JavaConstants.JAVAX_PORTLET_REQUEST);
472
473 PortletPreferences portletPreferences = null;
474
475 if (portletRequest != null) {
476 PortletPreferencesWrapper portletPreferencesWrapper =
477 (PortletPreferencesWrapper)portletRequest.getPreferences();
478
479 portletPreferences =
480 portletPreferencesWrapper.getPortletPreferencesImpl();
481 }
482
483 return portletPreferences;
484 }
485
486 public PreferencesValidator getPreferencesValidator(Portlet portlet) {
487 return PortalUtil.getPreferencesValidator(portlet);
488 }
489
490 public PortletPreferences getStrictLayoutPortletSetup(
491 Layout layout, String portletId)
492 throws SystemException {
493
494 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
495 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
496
497 return PortletPreferencesLocalServiceUtil.getStrictPreferences(
498 layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
499 portletId);
500 }
501
502 public String toXML(PortalPreferences portalPreferences) {
503 PortalPreferencesImpl portalPreferencesImpl =
504 (PortalPreferencesImpl)portalPreferences;
505
506 Map<String, Preference> preferencesMap =
507 portalPreferencesImpl.getPreferences();
508
509 return toXML(preferencesMap);
510 }
511
512 public String toXML(PortletPreferences portletPreferences) {
513 PortletPreferencesImpl portletPreferencesImpl =
514 (PortletPreferencesImpl)portletPreferences;
515
516 Map<String, Preference> preferencesMap =
517 portletPreferencesImpl.getPreferences();
518
519 return toXML(preferencesMap);
520 }
521
522 protected void populateMap(
523 String xml, Map<String, Preference> preferencesMap)
524 throws SystemException {
525
526 if (Validator.isNull(xml)) {
527 return;
528 }
529
530 XMLEventReader xmlEventReader = null;
531
532 try {
533 XMLInputFactory xmlInputFactory =
534 StAXReaderUtil.getXMLInputFactory();
535
536 xmlEventReader = xmlInputFactory.createXMLEventReader(
537 new UnsyncStringReader(xml));
538
539 while (xmlEventReader.hasNext()) {
540 XMLEvent xmlEvent = xmlEventReader.nextEvent();
541
542 if (xmlEvent.isStartElement()) {
543 StartElement startElement = xmlEvent.asStartElement();
544
545 String elementName = startElement.getName().getLocalPart();
546
547 if (elementName.equals("preference")) {
548 Preference preference = readPreference(xmlEventReader);
549
550 preferencesMap.put(preference.getName(), preference);
551 }
552 }
553 }
554 }
555 catch (XMLStreamException xse) {
556 throw new SystemException(xse);
557 }
558 finally {
559 if (xmlEventReader != null) {
560 try {
561 xmlEventReader.close();
562 }
563 catch (XMLStreamException xse) {
564 }
565 }
566 }
567 }
568
569 protected Preference readPreference(XMLEventReader xmlEventReader)
570 throws XMLStreamException {
571
572 String name = null;
573 List<String> values = new ArrayList<String>();
574 boolean readOnly = false;
575
576 while (xmlEventReader.hasNext()) {
577 XMLEvent xmlEvent = xmlEventReader.nextEvent();
578
579 if (xmlEvent.isStartElement()) {
580 StartElement startElement = xmlEvent.asStartElement();
581
582 String elementName = startElement.getName().getLocalPart();
583
584 if (elementName.equals("name")) {
585 name = StAXReaderUtil.read(xmlEventReader);
586 }
587 else if (elementName.equals("value")) {
588 String value = StAXReaderUtil.read(xmlEventReader);
589
590 values.add(value);
591 }
592 else if (elementName.equals("read-only")) {
593 String value = StAXReaderUtil.read(xmlEventReader);
594
595 readOnly = GetterUtil.getBoolean(value);
596 }
597 }
598 else if (xmlEvent.isEndElement()) {
599 EndElement endElement = xmlEvent.asEndElement();
600
601 String elementName = endElement.getName().getLocalPart();
602
603 if (elementName.equals("preference")) {
604 break;
605 }
606 }
607 }
608
609 return new Preference(
610 name, values.toArray(new String[values.size()]), readOnly);
611 }
612
613 protected String toXML(Map<String, Preference> preferencesMap) {
614 Element portletPreferencesElement = new Element(
615 "portlet-preferences", false);
616
617 for (Map.Entry<String, Preference> entry : preferencesMap.entrySet()) {
618 Preference preference = entry.getValue();
619
620 Element preferenceElement = portletPreferencesElement.addElement(
621 "preference");
622
623 preferenceElement.addElement("name", preference.getName());
624
625 for (String value : preference.getValues()) {
626 preferenceElement.addElement("value", value);
627 }
628
629 if (preference.isReadOnly()) {
630 preferenceElement.addElement("read-only", Boolean.TRUE);
631 }
632 }
633
634 return portletPreferencesElement.toXMLString();
635 }
636
637 }