001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.model.impl;
016    
017    import com.liferay.portal.kernel.configuration.Filter;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.portlet.PortletLayoutListener;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
025    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.ListUtil;
028    import com.liferay.portal.kernel.util.PropsKeys;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.StringUtil;
031    import com.liferay.portal.kernel.util.UnicodeProperties;
032    import com.liferay.portal.kernel.util.UniqueList;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.model.CustomizedPages;
035    import com.liferay.portal.model.Group;
036    import com.liferay.portal.model.Layout;
037    import com.liferay.portal.model.LayoutSet;
038    import com.liferay.portal.model.LayoutTemplate;
039    import com.liferay.portal.model.LayoutTypePortlet;
040    import com.liferay.portal.model.LayoutTypePortletConstants;
041    import com.liferay.portal.model.Plugin;
042    import com.liferay.portal.model.Portlet;
043    import com.liferay.portal.model.PortletConstants;
044    import com.liferay.portal.model.PortletPreferences;
045    import com.liferay.portal.model.PortletPreferencesIds;
046    import com.liferay.portal.model.Theme;
047    import com.liferay.portal.security.permission.ActionKeys;
048    import com.liferay.portal.security.permission.PermissionChecker;
049    import com.liferay.portal.security.permission.PermissionThreadLocal;
050    import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
051    import com.liferay.portal.service.PluginSettingLocalServiceUtil;
052    import com.liferay.portal.service.PortletLocalServiceUtil;
053    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
054    import com.liferay.portal.service.permission.LayoutPermissionUtil;
055    import com.liferay.portal.service.permission.PortletPermissionUtil;
056    import com.liferay.portal.util.PortalUtil;
057    import com.liferay.portal.util.PortletKeys;
058    import com.liferay.portal.util.PropsUtil;
059    import com.liferay.portal.util.PropsValues;
060    import com.liferay.portlet.PortalPreferences;
061    import com.liferay.portlet.PortletPreferencesFactoryUtil;
062    import com.liferay.portlet.sites.util.SitesUtil;
063    import com.liferay.util.JS;
064    import com.liferay.util.PwdGenerator;
065    
066    import java.text.DateFormat;
067    import java.text.Format;
068    
069    import java.util.ArrayList;
070    import java.util.Date;
071    import java.util.List;
072    import java.util.Map;
073    import java.util.Set;
074    
075    /**
076     * @author Brian Wing Shun Chan
077     * @author Berentey Zsolt
078     * @author Jorge Ferrer
079     * @author Raymond Augé
080     */
081    public class LayoutTypePortletImpl
082            extends LayoutTypeImpl implements LayoutTypePortlet {
083    
084            public static String generateInstanceId() {
085                    return PwdGenerator.getPassword(
086                            PwdGenerator.KEY1 + PwdGenerator.KEY2 + PwdGenerator.KEY3, 12);
087            }
088    
089            public LayoutTypePortletImpl(Layout layout) {
090                    super(layout);
091    
092                    if (_nestedPortletsNamespace == null) {
093                            _nestedPortletsNamespace = PortalUtil.getPortletNamespace(
094                                    PortletKeys.NESTED_PORTLETS);
095                    }
096    
097                    _layoutSetPrototypeLayout = SitesUtil.getLayoutSetPrototypeLayout(
098                            layout);
099            }
100    
101            public void addModeAboutPortletId(String portletId) {
102                    removeModesPortletId(portletId);
103                    setModeAbout(StringUtil.add(getModeAbout(), portletId));
104            }
105    
106            public void addModeConfigPortletId(String portletId) {
107                    removeModesPortletId(portletId);
108                    setModeConfig(StringUtil.add(getModeConfig(), portletId));
109            }
110    
111            public void addModeEditDefaultsPortletId(String portletId) {
112                    removeModesPortletId(portletId);
113                    setModeEditDefaults(StringUtil.add(getModeEditDefaults(), portletId));
114            }
115    
116            public void addModeEditGuestPortletId(String portletId) {
117                    removeModesPortletId(portletId);
118                    setModeEditGuest(StringUtil.add(getModeEditGuest(), portletId));
119            }
120    
121            public void addModeEditPortletId(String portletId) {
122                    removeModesPortletId(portletId);
123                    setModeEdit(StringUtil.add(getModeEdit(), portletId));
124            }
125    
126            public void addModeHelpPortletId(String portletId) {
127                    removeModesPortletId(portletId);
128                    setModeHelp(StringUtil.add(getModeHelp(), portletId));
129            }
130    
131            public void addModePreviewPortletId(String portletId) {
132                    removeModesPortletId(portletId);
133                    setModePreview(StringUtil.add(getModePreview(), portletId));
134            }
135    
136            public void addModePrintPortletId(String portletId) {
137                    removeModesPortletId(portletId);
138                    setModePrint(StringUtil.add(getModePrint(), portletId));
139            }
140    
141            public String addPortletId(long userId, String portletId)
142                    throws PortalException, SystemException {
143    
144                    return addPortletId(userId, portletId, true);
145            }
146    
147            public String addPortletId(
148                            long userId, String portletId, boolean checkPermission)
149                    throws PortalException, SystemException {
150    
151                    return addPortletId(userId, portletId, null, -1, checkPermission);
152            }
153    
154            public String addPortletId(
155                            long userId, String portletId, String columnId, int columnPos)
156                    throws PortalException, SystemException {
157    
158                    return addPortletId(userId, portletId, columnId, columnPos, true);
159            }
160    
161            public String addPortletId(
162                            long userId, String portletId, String columnId, int columnPos,
163                            boolean checkPermission)
164                    throws PortalException, SystemException {
165    
166                    return addPortletId(
167                            userId, portletId, columnId, columnPos, checkPermission, false);
168            }
169    
170            public void addPortletIds(
171                            long userId, String[] portletIds, boolean checkPermission)
172                    throws PortalException, SystemException {
173    
174                    for (String portletId : portletIds) {
175                            addPortletId(userId, portletId, checkPermission);
176                    }
177            }
178    
179            public void addPortletIds(
180                            long userId, String[] portletIds, String columnId,
181                            boolean checkPermission)
182                    throws PortalException, SystemException {
183    
184                    for (String portletId : portletIds) {
185                            addPortletId(userId, portletId, columnId, -1, checkPermission);
186                    }
187            }
188    
189            public void addStateMaxPortletId(String portletId) {
190                    removeStatesPortletId(portletId);
191                    //setStateMax(StringUtil.add(getStateMax(), portletId));
192                    setStateMax(StringUtil.add(StringPool.BLANK, portletId));
193            }
194    
195            public void addStateMinPortletId(String portletId) {
196                    removeStateMaxPortletId(portletId);
197                    setStateMin(StringUtil.add(getStateMin(), portletId));
198            }
199    
200            public List<Portlet> addStaticPortlets(
201                    List<Portlet> portlets, List<Portlet> startPortlets,
202                    List<Portlet> endPortlets) {
203    
204                    // Return the original array of portlets if no static portlets are
205                    // specified
206    
207                    if (startPortlets == null) {
208                            startPortlets = new ArrayList<Portlet>();
209                    }
210    
211                    if (endPortlets == null) {
212                            endPortlets = new ArrayList<Portlet>();
213                    }
214    
215                    if (startPortlets.isEmpty() && endPortlets.isEmpty()) {
216                            return portlets;
217                    }
218    
219                    // New array of portlets that contain the static portlets
220    
221                    List<Portlet> list = new ArrayList<Portlet>(
222                            portlets.size() + startPortlets.size() + endPortlets.size());
223    
224                    if (!startPortlets.isEmpty()) {
225                            list.addAll(startPortlets);
226                    }
227    
228                    for (int i = 0; i < portlets.size(); i++) {
229                            Portlet portlet = portlets.get(i);
230    
231                            // Add the portlet if and only if it is not also a static portlet
232    
233                            if (!startPortlets.contains(portlet) &&
234                                    !endPortlets.contains(portlet)) {
235    
236                                    list.add(portlet);
237                            }
238                    }
239    
240                    if (!endPortlets.isEmpty()) {
241                            list.addAll(endPortlets);
242                    }
243    
244                    return list;
245            }
246    
247            public List<Portlet> getAllPortlets()
248                    throws PortalException, SystemException {
249    
250                    List<Portlet> portlets = new ArrayList<Portlet>();
251    
252                    List<String> columns = getColumns();
253    
254                    for (int i = 0; i < columns.size(); i++) {
255                            String columnId = columns.get(i);
256    
257                            portlets.addAll(getAllPortlets(columnId));
258                    }
259    
260                    List<Portlet> staticPortlets = getStaticPortlets(
261                            PropsKeys.LAYOUT_STATIC_PORTLETS_ALL);
262    
263                    List<Portlet> embeddedPortlets = getEmbeddedPortlets(
264                            portlets, staticPortlets);
265    
266                    return addStaticPortlets(portlets, staticPortlets, embeddedPortlets);
267            }
268    
269            public List<Portlet> getAllPortlets(String columnId)
270                    throws PortalException, SystemException {
271    
272                    String columnValue = getColumnValue(columnId);
273    
274                    String[] portletIds = StringUtil.split(columnValue);
275    
276                    List<Portlet> portlets = new ArrayList<Portlet>(portletIds.length);
277    
278                    for (String portletId : portletIds) {
279                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
280                                    getCompanyId(), portletId);
281    
282                            if (portlet != null) {
283                                    portlets.add(portlet);
284                            }
285                    }
286    
287                    List<Portlet> startPortlets = getStaticPortlets(
288                            PropsKeys.LAYOUT_STATIC_PORTLETS_START + columnId);
289    
290                    List<Portlet> endPortlets = getStaticPortlets(
291                            PropsKeys.LAYOUT_STATIC_PORTLETS_END + columnId);
292    
293                    return addStaticPortlets(portlets, startPortlets, endPortlets);
294            }
295    
296            public Layout getLayoutSetPrototypeLayout() {
297                    return _layoutSetPrototypeLayout;
298            }
299    
300            public String getLayoutSetPrototypeLayoutProperty(String key) {
301                    if (_layoutSetPrototypeLayout== null) {
302                            return StringPool.BLANK;
303                    }
304    
305                    UnicodeProperties typeSettingsProperties =
306                            _layoutSetPrototypeLayout.getTypeSettingsProperties();
307    
308                    return typeSettingsProperties.getProperty(key);
309            }
310    
311            public LayoutTemplate getLayoutTemplate() {
312                    String themeId = getThemeId();
313    
314                    LayoutTemplate layoutTemplate =
315                            LayoutTemplateLocalServiceUtil.getLayoutTemplate(
316                                    getLayoutTemplateId(), false, themeId);
317    
318                    if (layoutTemplate == null) {
319                            layoutTemplate = new LayoutTemplateImpl(
320                                    StringPool.BLANK, StringPool.BLANK);
321    
322                            List<String> columns = new ArrayList<String>();
323    
324                            for (int i = 1; i <= 10; i++) {
325                                    columns.add(LayoutTypePortletConstants.COLUMN_PREFIX + i);
326                            }
327    
328                            layoutTemplate.setColumns(columns);
329                    }
330    
331                    return layoutTemplate;
332            }
333    
334            public String getLayoutTemplateId() {
335                    return GetterUtil.getString(
336                            getTypeSettingsProperty(
337                                    LayoutTypePortletConstants.LAYOUT_TEMPLATE_ID));
338            }
339    
340            public String getModeAbout() {
341                    return getTypeSettingsProperty(LayoutTypePortletConstants.MODE_ABOUT);
342            }
343    
344            public String getModeConfig() {
345                    return getTypeSettingsProperty(LayoutTypePortletConstants.MODE_CONFIG);
346            }
347    
348            public String getModeEdit() {
349                    return getTypeSettingsProperty(LayoutTypePortletConstants.MODE_EDIT);
350            }
351    
352            public String getModeEditDefaults() {
353                    return getTypeSettingsProperty(
354                            LayoutTypePortletConstants.MODE_EDIT_DEFAULTS);
355            }
356    
357            public String getModeEditGuest() {
358                    return getTypeSettingsProperty(
359                            LayoutTypePortletConstants.MODE_EDIT_GUEST);
360            }
361    
362            public String getModeHelp() {
363                    return getTypeSettingsProperty(LayoutTypePortletConstants.MODE_HELP);
364            }
365    
366            public String getModePreview() {
367                    return getTypeSettingsProperty(LayoutTypePortletConstants.MODE_PREVIEW);
368            }
369    
370            public String getModePrint() {
371                    return getTypeSettingsProperty(LayoutTypePortletConstants.MODE_PRINT);
372            }
373    
374            public int getNumOfColumns() {
375                    return getLayoutTemplate().getColumns().size();
376            }
377    
378            public PortalPreferences getPortalPreferences() {
379                    return _portalPreferences;
380            }
381    
382            public List<String> getPortletIds() {
383                    List<String> portletIds = new ArrayList<String>();
384    
385                    List<String> columns = getColumns();
386    
387                    for (int i = 0; i < columns.size(); i++) {
388                            String columnId = columns.get(i);
389    
390                            String columnValue = getColumnValue(columnId);
391    
392                            portletIds.addAll(
393                                    ListUtil.fromArray(StringUtil.split(columnValue)));
394                    }
395    
396                    return portletIds;
397            }
398    
399            public List<Portlet> getPortlets() throws SystemException {
400                    List<String> portletIds = getPortletIds();
401    
402                    List<Portlet> portlets = new ArrayList<Portlet>(portletIds.size());
403    
404                    for (int i = 0; i < portletIds.size(); i++) {
405                            String portletId = portletIds.get(i);
406    
407                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
408                                    getCompanyId(), portletId);
409    
410                            if (portlet != null) {
411                                    portlets.add(portlet);
412                            }
413                    }
414    
415                    return portlets;
416            }
417    
418            public String getStateMax() {
419                    return getTypeSettingsProperty(LayoutTypePortletConstants.STATE_MAX);
420            }
421    
422            public String getStateMaxPortletId() {
423                    String[] stateMax = StringUtil.split(getStateMax());
424    
425                    if (stateMax.length > 0) {
426                            return stateMax[0];
427                    }
428                    else {
429                            return StringPool.BLANK;
430                    }
431            }
432    
433            public String getStateMin() {
434                    return getTypeSettingsProperty(LayoutTypePortletConstants.STATE_MIN);
435            }
436    
437            public boolean hasDefaultScopePortletId(long groupId, String portletId)
438                    throws PortalException, SystemException {
439    
440                    if (hasPortletId(portletId)) {
441                            long scopeGroupId = PortalUtil.getScopeGroupId(
442                                    getLayout(), portletId);
443    
444                            if (groupId == scopeGroupId) {
445                                    return true;
446                            }
447                    }
448    
449                    return false;
450            }
451    
452            public boolean hasModeAboutPortletId(String portletId) {
453                    return StringUtil.contains(getModeAbout(), portletId);
454            }
455    
456            public boolean hasModeConfigPortletId(String portletId) {
457                    return StringUtil.contains(getModeConfig(), portletId);
458            }
459    
460            public boolean hasModeEditDefaultsPortletId(String portletId) {
461                    return StringUtil.contains(getModeEditDefaults(), portletId);
462            }
463    
464            public boolean hasModeEditGuestPortletId(String portletId) {
465                    return StringUtil.contains(getModeEditGuest(), portletId);
466            }
467    
468            public boolean hasModeEditPortletId(String portletId) {
469                    return StringUtil.contains(getModeEdit(), portletId);
470            }
471    
472            public boolean hasModeHelpPortletId(String portletId) {
473                    return StringUtil.contains(getModeHelp(), portletId);
474            }
475    
476            public boolean hasModePreviewPortletId(String portletId) {
477                    return StringUtil.contains(getModePreview(), portletId);
478            }
479    
480            public boolean hasModePrintPortletId(String portletId) {
481                    return StringUtil.contains(getModePrint(), portletId);
482            }
483    
484            public boolean hasModeViewPortletId(String portletId) {
485                    if (hasModeAboutPortletId(portletId) ||
486                            hasModeConfigPortletId(portletId) ||
487                            hasModeEditPortletId(portletId) ||
488                            hasModeEditDefaultsPortletId(portletId) ||
489                            hasModeEditGuestPortletId(portletId) ||
490                            hasModeHelpPortletId(portletId) ||
491                            hasModePreviewPortletId(portletId) ||
492                            hasModePrintPortletId(portletId)) {
493    
494                            return false;
495                    }
496                    else {
497                            return true;
498                    }
499            }
500    
501            public boolean hasPortletId(String portletId)
502                    throws PortalException, SystemException {
503    
504                    return hasPortletId(portletId, false);
505            }
506    
507            public boolean hasStateMax() {
508                    String[] stateMax = StringUtil.split(getStateMax());
509    
510                    if (stateMax.length > 0) {
511                            return true;
512                    }
513                    else {
514                            return false;
515                    }
516            }
517    
518            public boolean hasStateMaxPortletId(String portletId) {
519                    if (StringUtil.contains(getStateMax(), portletId)) {
520                            return true;
521                    }
522                    else {
523                            return false;
524                    }
525            }
526    
527            public boolean hasStateMin() {
528                    String[] stateMin = StringUtil.split(getStateMin());
529    
530                    if (stateMin.length > 0) {
531                            return true;
532                    }
533                    else {
534                            return false;
535                    }
536            }
537    
538            public boolean hasStateMinPortletId(String portletId) {
539                    if (StringUtil.contains(getStateMin(), portletId)) {
540                            return true;
541                    }
542                    else {
543                            return false;
544                    }
545            }
546    
547            public boolean hasStateNormalPortletId(String portletId) {
548                    if (hasStateMaxPortletId(portletId) ||
549                            hasStateMinPortletId(portletId)) {
550    
551                            return false;
552                    }
553                    else {
554                            return true;
555                    }
556            }
557    
558            public boolean hasUpdatePermission() {
559                    return _updatePermission;
560            }
561    
562            public boolean isColumnCustomizable(String columnId) {
563                    if (!isLayoutSetPrototype()) {
564                            String customizableString = getTypeSettingsProperty(
565                                    CustomizedPages.namespaceColumnId(columnId));
566    
567                            boolean customizable = GetterUtil.getBoolean(customizableString);
568    
569                            if (!customizable && hasUserPreferences()) {
570                                    String columnValue = _portalPreferences.getValue(
571                                            CustomizedPages.namespacePlid(getPlid()), columnId,
572                                            StringPool.NULL);
573    
574                                    if (!Validator.equals(columnValue, StringPool.NULL)) {
575                                            setUserPreference(columnId, null);
576                                    }
577                            }
578    
579                            return customizable;
580                    }
581    
582                    return false;
583            }
584    
585            public boolean isColumnDisabled(String columnId) {
586                    if ((isCustomizedView() && !isColumnCustomizable(columnId)) ||
587                            (!isCustomizedView() && !hasUpdatePermission())) {
588    
589                            return true;
590                    }
591    
592                    return false;
593            }
594    
595            public boolean isCustomizable() {
596                    for (String columnId : getColumns()) {
597                            if (isColumnCustomizable(columnId)) {
598                                    return true;
599                            }
600                    }
601    
602                    return false;
603            }
604    
605            public boolean isCustomizedView() {
606                    return _customizedView;
607            }
608    
609            public boolean isDefaultUpdated() {
610                    if (!isCustomizedView() || !hasUserPreferences()) {
611                            return false;
612                    }
613    
614                    String preferencesModifiedDateString = _portalPreferences.getValue(
615                            CustomizedPages.namespacePlid(getPlid()), _MODIFIED_DATE,
616                            _NULL_DATE);
617    
618                    DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
619                            PropsValues.INDEX_DATE_FORMAT_PATTERN);
620    
621                    try {
622                            Date preferencesModifiedDate = dateFormat.parse(
623                                    preferencesModifiedDateString);
624    
625                            Layout layout = getLayout();
626    
627                            String propertiesModifiedDateString =
628                                    layout.getTypeSettingsProperty(_MODIFIED_DATE, _NULL_DATE);
629    
630                            Date propertiesModifiedDate = dateFormat.parse(
631                                    propertiesModifiedDateString);
632    
633                            return propertiesModifiedDate.after(preferencesModifiedDate);
634                    }
635                    catch (Exception e) {
636                            _log.error(e, e);
637                    }
638    
639                    return false;
640            }
641    
642            public boolean isPortletCustomizable(String portletId) {
643                    return isColumnCustomizable(getColumn(portletId));
644            }
645    
646            public void movePortletId(
647                            long userId, String portletId, String columnId, int columnPos)
648                    throws PortalException, SystemException {
649    
650                    _enablePortletLayoutListener = false;
651    
652                    try {
653                            removePortletId(userId, portletId, false);
654                            addPortletId(userId, portletId, columnId, columnPos, false, true);
655                    }
656                    finally {
657                            _enablePortletLayoutListener = true;
658                    }
659    
660                    Layout layout = getLayout();
661    
662                    try {
663                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
664                                    layout.getCompanyId(), portletId);
665    
666                            if (portlet != null) {
667                                    PortletLayoutListener portletLayoutListener =
668                                            portlet.getPortletLayoutListenerInstance();
669    
670                                    if (portletLayoutListener != null) {
671                                            portletLayoutListener.onMoveInLayout(
672                                                    portletId, layout.getPlid());
673                                    }
674                            }
675                    }
676                    catch (Exception e) {
677                            _log.error("Unable to fire portlet layout listener event", e);
678                    }
679            }
680    
681            public void removeModeAboutPortletId(String portletId) {
682                    setModeAbout(StringUtil.remove(getModeAbout(), portletId));
683            }
684    
685            public void removeModeConfigPortletId(String portletId) {
686                    setModeConfig(StringUtil.remove(getModeConfig(), portletId));
687            }
688    
689            public void removeModeEditDefaultsPortletId(String portletId) {
690                    setModeEditDefaults(
691                            StringUtil.remove(getModeEditDefaults(), portletId));
692            }
693    
694            public void removeModeEditGuestPortletId(String portletId) {
695                    setModeEditGuest(StringUtil.remove(getModeEditGuest(), portletId));
696            }
697    
698            public void removeModeEditPortletId(String portletId) {
699                    setModeEdit(StringUtil.remove(getModeEdit(), portletId));
700            }
701    
702            public void removeModeHelpPortletId(String portletId) {
703                    setModeHelp(StringUtil.remove(getModeHelp(), portletId));
704            }
705    
706            public void removeModePreviewPortletId(String portletId) {
707                    setModePreview(StringUtil.remove(getModePreview(), portletId));
708            }
709    
710            public void removeModePrintPortletId(String portletId) {
711                    setModePrint(StringUtil.remove(getModePrint(), portletId));
712            }
713    
714            public void removeModesPortletId(String portletId) {
715                    removeModeAboutPortletId(portletId);
716                    removeModeConfigPortletId(portletId);
717                    removeModeEditPortletId(portletId);
718                    removeModeEditDefaultsPortletId(portletId);
719                    removeModeEditGuestPortletId(portletId);
720                    removeModeHelpPortletId(portletId);
721                    removeModePreviewPortletId(portletId);
722                    removeModePrintPortletId(portletId);
723            }
724    
725            public void removeNestedColumns(String portletNamespace) {
726                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
727    
728                    UnicodeProperties newTypeSettingsProperties = new UnicodeProperties();
729    
730                    for (Map.Entry<String, String> entry :
731                                    typeSettingsProperties.entrySet()) {
732    
733                            String key = entry.getKey();
734    
735                            if (!key.startsWith(portletNamespace)) {
736                                    newTypeSettingsProperties.setProperty(key, entry.getValue());
737                            }
738                    }
739    
740                    Layout layout = getLayout();
741    
742                    layout.setTypeSettingsProperties(newTypeSettingsProperties);
743    
744                    String nestedColumnIds = GetterUtil.getString(
745                            getTypeSettingsProperty(
746                                    LayoutTypePortletConstants.NESTED_COLUMN_IDS));
747    
748                    String[] nestedColumnIdsArray = ArrayUtil.removeByPrefix(
749                            StringUtil.split(nestedColumnIds), portletNamespace);
750    
751                    setTypeSettingsProperty(
752                            LayoutTypePortletConstants.NESTED_COLUMN_IDS,
753                            StringUtil.merge(nestedColumnIdsArray));
754            }
755    
756            public void removePortletId(long userId, String portletId) {
757                    removePortletId(userId, portletId, true);
758            }
759    
760            public void removePortletId(
761                    long userId, String portletId, boolean cleanUp) {
762    
763                    try {
764                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
765                                    getCompanyId(), portletId);
766    
767                            if (portlet == null) {
768                                    _log.error(
769                                            "Portlet " + portletId +
770                                                    " cannot be removed because it is not registered");
771    
772                                    return;
773                            }
774    
775                            PermissionChecker permissionChecker =
776                                    PermissionThreadLocal.getPermissionChecker();
777    
778                            if (!LayoutPermissionUtil.contains(
779                                            permissionChecker, getLayout(), ActionKeys.UPDATE) &&
780                                    !isCustomizable()) {
781    
782                                    return;
783                            }
784                    }
785                    catch (Exception e) {
786                            _log.error(e, e);
787    
788                            return;
789                    }
790    
791                    List<String> columns = getColumns();
792    
793                    for (int i = 0; i < columns.size(); i++) {
794                            String columnId = columns.get(i);
795    
796                            if (isCustomizable() && isColumnDisabled(columnId)) {
797                                    continue;
798                            }
799    
800                            String columnValue = StringPool.BLANK;
801    
802                            if (hasUserPreferences()) {
803                                    columnValue = getUserPreference(columnId);
804                            }
805                            else {
806                                    columnValue = getTypeSettingsProperty(columnId);
807                            }
808    
809                            columnValue = StringUtil.remove(columnValue, portletId);
810    
811                            if (hasUserPreferences()) {
812                                    setUserPreference(columnId, columnValue);
813                            }
814                            else {
815                                    setTypeSettingsProperty(columnId, columnValue);
816                            }
817                    }
818    
819                    if (cleanUp) {
820                            try {
821                                    onRemoveFromLayout(new String[] {portletId});
822                            }
823                            catch (Exception e) {
824                                    _log.error(e, e);
825                            }
826                    }
827            }
828    
829            public void removeStateMaxPortletId(String portletId) {
830                    setStateMax(StringUtil.remove(getStateMax(), portletId));
831            }
832    
833            public void removeStateMinPortletId(String portletId) {
834                    setStateMin(StringUtil.remove(getStateMin(), portletId));
835            }
836    
837            public void removeStatesPortletId(String portletId) {
838                    removeStateMaxPortletId(portletId);
839                    removeStateMinPortletId(portletId);
840            }
841    
842            public void reorganizePortlets(
843                    List<String> newColumns, List<String> oldColumns) {
844    
845                    String lastNewColumnId = newColumns.get(newColumns.size() - 1);
846                    String lastNewColumnValue = getTypeSettingsProperty(lastNewColumnId);
847    
848                    for (String oldColumnId : oldColumns) {
849                            if (!newColumns.contains(oldColumnId)) {
850                                    String oldColumnValue = getTypeSettingsProperties().remove(
851                                            oldColumnId);
852    
853                                    String[] portletIds = StringUtil.split(oldColumnValue);
854    
855                                    for (String portletId : portletIds) {
856                                            lastNewColumnValue = StringUtil.add(
857                                                    lastNewColumnValue, portletId);
858                                    }
859                            }
860                    }
861    
862                    setTypeSettingsProperty(lastNewColumnId, lastNewColumnValue);
863            }
864    
865            public void resetModes() {
866                    setModeAbout(StringPool.BLANK);
867                    setModeConfig(StringPool.BLANK);
868                    setModeEdit(StringPool.BLANK);
869                    setModeEditDefaults(StringPool.BLANK);
870                    setModeEditGuest(StringPool.BLANK);
871                    setModeHelp(StringPool.BLANK);
872                    setModePreview(StringPool.BLANK);
873                    setModePrint(StringPool.BLANK);
874            }
875    
876            public void resetStates() {
877                    setStateMax(StringPool.BLANK);
878                    setStateMin(StringPool.BLANK);
879            }
880    
881            public void resetUserPreferences() {
882                    if (!hasUserPreferences()) {
883                            return;
884                    }
885    
886                    long plid = getPlid();
887    
888                    List<String> customPortletIds = new UniqueList<String>();
889    
890                    for (String columnId : getColumns()) {
891                            String value = _portalPreferences.getValue(
892                                    CustomizedPages.namespacePlid(plid), columnId);
893    
894                            for (String customPortletId : StringUtil.split(value)) {
895                                    customPortletIds.add(customPortletId);
896                            }
897                    }
898    
899                    try {
900                            onRemoveFromLayout(
901                                    customPortletIds.toArray(new String[customPortletIds.size()]));
902                    }
903                    catch (Exception e) {
904                            _log.error(e, e);
905                    }
906    
907                    _portalPreferences.resetValues(CustomizedPages.namespacePlid(plid));
908    
909                    _portalPreferences.setValue(
910                            CustomizedPages.namespacePlid(plid), _MODIFIED_DATE,
911                            _dateFormat.format(new Date()));
912            }
913    
914            public void setCustomizedView(boolean customizedView) {
915                    _customizedView = customizedView;
916            }
917    
918            public void setLayoutTemplateId(long userId, String newLayoutTemplateId) {
919                    setLayoutTemplateId(userId, newLayoutTemplateId, true);
920            }
921    
922            public void setLayoutTemplateId(
923                    long userId, String newLayoutTemplateId, boolean checkPermission) {
924    
925                    if (checkPermission &&
926                            !PluginSettingLocalServiceUtil.hasPermission(
927                                    userId, newLayoutTemplateId, Plugin.TYPE_LAYOUT_TEMPLATE)) {
928    
929                            return;
930                    }
931    
932                    String oldLayoutTemplateId = getLayoutTemplateId();
933    
934                    if (Validator.isNull(oldLayoutTemplateId)) {
935                            oldLayoutTemplateId = PropsValues.DEFAULT_LAYOUT_TEMPLATE_ID;
936                    }
937    
938                    String themeId = getThemeId();
939    
940                    LayoutTemplate oldLayoutTemplate =
941                            LayoutTemplateLocalServiceUtil.getLayoutTemplate(
942                                    oldLayoutTemplateId, false, themeId);
943    
944                    if (oldLayoutTemplate == null) {
945                            if (_log.isWarnEnabled()) {
946                                    _log.warn(
947                                            "Unable to find layout template " + oldLayoutTemplateId);
948                            }
949    
950                            return;
951                    }
952    
953                    LayoutTemplate newLayoutTemplate =
954                            LayoutTemplateLocalServiceUtil.getLayoutTemplate(
955                                    newLayoutTemplateId, false, themeId);
956    
957                    if (newLayoutTemplate == null) {
958                            if (_log.isWarnEnabled()) {
959                                    _log.warn(
960                                            "Unable to find layout template " + newLayoutTemplateId);
961                            }
962    
963                            return;
964                    }
965    
966                    setTypeSettingsProperty(
967                            LayoutTypePortletConstants.LAYOUT_TEMPLATE_ID, newLayoutTemplateId);
968    
969                    List<String> oldColumns = oldLayoutTemplate.getColumns();
970                    List<String> newColumns = newLayoutTemplate.getColumns();
971    
972                    reorganizePortlets(newColumns, oldColumns);
973            }
974    
975            public void setModeAbout(String modeAbout) {
976                    setTypeSettingsProperty(
977                            LayoutTypePortletConstants.MODE_ABOUT, modeAbout);
978            }
979    
980            public void setModeConfig(String modeConfig) {
981                    setTypeSettingsProperty(
982                            LayoutTypePortletConstants.MODE_CONFIG, modeConfig);
983            }
984    
985            public void setModeEdit(String modeEdit) {
986                    setTypeSettingsProperty(LayoutTypePortletConstants.MODE_EDIT, modeEdit);
987            }
988    
989            public void setModeEditDefaults(String modeEditDefaults) {
990                    setTypeSettingsProperty(
991                            LayoutTypePortletConstants.MODE_EDIT_DEFAULTS, modeEditDefaults);
992            }
993    
994            public void setModeEditGuest(String modeEditGuest) {
995                    setTypeSettingsProperty(
996                            LayoutTypePortletConstants.MODE_EDIT_GUEST, modeEditGuest);
997            }
998    
999            public void setModeHelp(String modeHelp) {
1000                    setTypeSettingsProperty(LayoutTypePortletConstants.MODE_HELP, modeHelp);
1001            }
1002    
1003            public void setModePreview(String modePreview) {
1004                    setTypeSettingsProperty(
1005                            LayoutTypePortletConstants.MODE_PREVIEW, modePreview);
1006            }
1007    
1008            public void setModePrint(String modePrint) {
1009                    setTypeSettingsProperty(
1010                            LayoutTypePortletConstants.MODE_PRINT, modePrint);
1011            }
1012    
1013            public void setPortalPreferences(PortalPreferences portalPreferences) {
1014                    _portalPreferences = portalPreferences;
1015            }
1016    
1017            public void setStateMax(String stateMax) {
1018                    setTypeSettingsProperty(LayoutTypePortletConstants.STATE_MAX, stateMax);
1019            }
1020    
1021            public void setStateMin(String stateMin) {
1022                    setTypeSettingsProperty(LayoutTypePortletConstants.STATE_MIN, stateMin);
1023            }
1024    
1025            public void setUpdatePermission(boolean updatePermission) {
1026                    _updatePermission = updatePermission;
1027            }
1028    
1029            protected void addNestedColumn(String columnId) {
1030                    String nestedColumnIds = getTypeSettingsProperty(
1031                            LayoutTypePortletConstants.NESTED_COLUMN_IDS, StringPool.BLANK);
1032    
1033                    if (!nestedColumnIds.contains(columnId)) {
1034                            nestedColumnIds = StringUtil.add(nestedColumnIds, columnId);
1035    
1036                            setTypeSettingsProperty(
1037                                    LayoutTypePortletConstants.NESTED_COLUMN_IDS, nestedColumnIds);
1038                    }
1039            }
1040    
1041            protected String addPortletId(
1042                            long userId, String portletId, String columnId, int columnPos,
1043                            boolean checkPermission, boolean strictHasPortlet)
1044                    throws PortalException, SystemException {
1045    
1046                    portletId = JS.getSafeName(portletId);
1047    
1048                    Layout layout = getLayout();
1049    
1050                    Portlet portlet = null;
1051    
1052                    try {
1053                            portlet = PortletLocalServiceUtil.getPortletById(
1054                                    layout.getCompanyId(), portletId);
1055    
1056                            if (portlet == null) {
1057                                    if (_log.isWarnEnabled()) {
1058                                            _log.warn(
1059                                                    "Portlet " + portletId +
1060                                                            " cannot be added because it is not registered");
1061                                    }
1062    
1063                                    return null;
1064                            }
1065    
1066                            PermissionChecker permissionChecker =
1067                                    PermissionThreadLocal.getPermissionChecker();
1068    
1069                            if (checkPermission &&
1070                                    !PortletPermissionUtil.contains(
1071                                            permissionChecker, layout, portlet,
1072                                            ActionKeys.ADD_TO_PAGE)) {
1073    
1074                                    return null;
1075                            }
1076                    }
1077                    catch (Exception e) {
1078                            _log.error(e, e);
1079                    }
1080    
1081                    if (portlet.isSystem()) {
1082                            return null;
1083                    }
1084    
1085                    if (portlet.isInstanceable() &&
1086                            !PortletConstants.hasInstanceId(portletId)) {
1087    
1088                            portletId = PortletConstants.assemblePortletId(
1089                                    portletId, generateInstanceId());
1090                    }
1091    
1092                    if (hasPortletId(portletId, strictHasPortlet)) {
1093                            return null;
1094                    }
1095    
1096                    if (columnId == null) {
1097                            LayoutTemplate layoutTemplate = getLayoutTemplate();
1098    
1099                            List<String> columns = layoutTemplate.getColumns();
1100    
1101                            if (columns.size() > 0) {
1102                                    columnId = columns.get(0);
1103                            }
1104                    }
1105    
1106                    if (columnId == null) {
1107                            return null;
1108                    }
1109    
1110                    if (isCustomizable()) {
1111                            if (isColumnDisabled(columnId)) {
1112                                    return null;
1113                            }
1114    
1115                            if (PortletConstants.hasInstanceId(portletId) &&
1116                                    hasUserPreferences()) {
1117    
1118                                    portletId = PortletConstants.assemblePortletId(
1119                                            portletId, userId);
1120                            }
1121                    }
1122    
1123                    String columnValue = StringPool.BLANK;
1124    
1125                    if (hasUserPreferences()) {
1126                            columnValue = getUserPreference(columnId);
1127                    }
1128                    else {
1129                            columnValue = getTypeSettingsProperty(columnId);
1130                    }
1131    
1132                    if ((columnValue == null) &&
1133                            columnId.startsWith(_nestedPortletsNamespace)) {
1134    
1135                            addNestedColumn(columnId);
1136                    }
1137    
1138                    if (columnPos >= 0) {
1139                            List<String> portletIds = ListUtil.fromArray(
1140                                    StringUtil.split(columnValue));
1141    
1142                            if (columnPos <= portletIds.size()) {
1143                                    portletIds.add(columnPos, portletId);
1144                            }
1145                            else {
1146                                    portletIds.add(portletId);
1147                            }
1148    
1149                            columnValue = StringUtil.merge(portletIds);
1150                    }
1151                    else {
1152                            columnValue = StringUtil.add(columnValue, portletId);
1153                    }
1154    
1155                    if (hasUserPreferences()) {
1156                            setUserPreference(columnId, columnValue);
1157                    }
1158                    else {
1159                            setTypeSettingsProperty(columnId, columnValue);
1160                    }
1161    
1162                    try {
1163                            if (_enablePortletLayoutListener) {
1164                                    PortletLayoutListener portletLayoutListener =
1165                                            portlet.getPortletLayoutListenerInstance();
1166    
1167                                    if (portletLayoutListener != null) {
1168                                            portletLayoutListener.onAddToLayout(
1169                                                    portletId, layout.getPlid());
1170                                    }
1171                            }
1172                    }
1173                    catch (Exception e) {
1174                            _log.error("Unable to fire portlet layout listener event", e);
1175                    }
1176    
1177                    return portletId;
1178            }
1179    
1180            protected void copyPreferences(
1181                    long userId, String sourcePortletId, String targetPortletId) {
1182    
1183                    Layout layout = getLayout();
1184    
1185                    try {
1186                            PortletPreferencesIds portletPreferencesIds =
1187                                    PortletPreferencesFactoryUtil.getPortletPreferencesIds(
1188                                            layout.getGroupId(), 0, layout, sourcePortletId, false);
1189    
1190                            javax.portlet.PortletPreferences sourcePortletPreferences =
1191                                    PortletPreferencesLocalServiceUtil.getStrictPreferences(
1192                                            portletPreferencesIds);
1193    
1194                            portletPreferencesIds =
1195                                    PortletPreferencesFactoryUtil.getPortletPreferencesIds(
1196                                            layout.getGroupId(), userId, layout, targetPortletId,
1197                                            false);
1198    
1199                            PortletPreferencesLocalServiceUtil.updatePreferences(
1200                                    portletPreferencesIds.getOwnerId(),
1201                                    portletPreferencesIds.getOwnerType(),
1202                                    portletPreferencesIds.getPlid(),
1203                                    portletPreferencesIds.getPortletId(), sourcePortletPreferences);
1204                    }
1205                    catch (Exception e) {
1206                    }
1207            }
1208    
1209            protected String getColumn(String portletId) {
1210                    List<String> columns = getColumns();
1211    
1212                    for (String columnId : columns) {
1213                            String columnValue = getColumnValue(columnId);
1214    
1215                            String[] portletIds = StringUtil.split(columnValue);
1216    
1217                            for (String columnPortletId : portletIds) {
1218                                    if (PortletConstants.hasIdenticalRootPortletId(
1219                                                    columnPortletId, portletId)) {
1220    
1221                                            return columnId;
1222                                    }
1223                            }
1224                    }
1225    
1226                    return StringPool.BLANK;
1227            }
1228    
1229            protected List<String> getColumns() {
1230                    List<String> columns = new ArrayList<String>();
1231    
1232                    Layout layout = getLayout();
1233    
1234                    if (layout.isTypePortlet()) {
1235                            LayoutTemplate layoutTemplate = getLayoutTemplate();
1236    
1237                            columns.addAll(layoutTemplate.getColumns());
1238    
1239                            columns.addAll(getNestedColumns());
1240                    }
1241                    else if (layout.isTypePanel()) {
1242                            columns.add("panelSelectedPortlets");
1243                    }
1244    
1245                    return columns;
1246            }
1247    
1248            protected String getColumnValue(String columnId) {
1249                    if (hasUserPreferences() && isCustomizable() &&
1250                            !isColumnDisabled(columnId)) {
1251    
1252                            return getUserPreference(columnId);
1253                    }
1254    
1255                    return getTypeSettingsProperty(columnId);
1256            }
1257    
1258            protected long getCompanyId() {
1259                    Layout layout = getLayout();
1260    
1261                    return layout.getCompanyId();
1262            }
1263    
1264            protected List<Portlet> getEmbeddedPortlets(
1265                            List<Portlet> columnPortlets, List<Portlet> staticPortlets)
1266                    throws SystemException {
1267    
1268                    if (_embeddedPortlets != null) {
1269                            return _embeddedPortlets;
1270                    }
1271    
1272                    List<Portlet> portlets = new ArrayList<Portlet>();
1273    
1274                    Layout layout = getLayout();
1275    
1276                    List<PortletPreferences> portletPreferences =
1277                            PortletPreferencesLocalServiceUtil.getPortletPreferences(
1278                                    PortletKeys.PREFS_OWNER_ID_DEFAULT,
1279                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, layout.getPlid());
1280    
1281                    if (isCustomizable() && hasUserPreferences()) {
1282                            portletPreferences = ListUtil.copy(portletPreferences);
1283    
1284                            portletPreferences.addAll(
1285                                    PortletPreferencesLocalServiceUtil.getPortletPreferences(
1286                                            _portalPreferences.getUserId(),
1287                                            PortletKeys.PREFS_OWNER_TYPE_USER, layout.getPlid()));
1288                    }
1289    
1290                    Set<String> portletAddDefaultResourceCheckWhiteList =
1291                            PortalUtil.getPortletAddDefaultResourceCheckWhitelist();
1292    
1293                    for (PortletPreferences portletPreference : portletPreferences) {
1294                            String portletId = portletPreference.getPortletId();
1295    
1296                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
1297                                    getCompanyId(), portletId);
1298    
1299                            if (Validator.isNull(portletId) ||
1300                                    columnPortlets.contains(portlet) ||
1301                                    staticPortlets.contains(portlet) ||
1302                                    portlet.isSystem() || portlet.isUndeployedPortlet() ||
1303                                    !portlet.isActive() ||
1304                                    portletAddDefaultResourceCheckWhiteList.contains(portletId)) {
1305    
1306                                    continue;
1307                            }
1308    
1309                            if (portlet != null) {
1310                                    Portlet embeddedPortlet = portlet;
1311    
1312                                    if (portlet.isInstanceable()) {
1313    
1314                                            // Instanceable portlets do not need to be cloned because
1315                                            // they are already cloned. See the method getPortletById in
1316                                            // the class PortletLocalServiceImpl and how it references
1317                                            // the method getClonedInstance in the class PortletImpl.
1318    
1319                                    }
1320                                    else {
1321                                            embeddedPortlet = (Portlet)embeddedPortlet.clone();
1322                                    }
1323    
1324                                    // We set embedded portlets as static on order to avoid adding
1325                                    // the close and/or move icons.
1326    
1327                                    embeddedPortlet.setStatic(true);
1328    
1329                                    portlets.add(embeddedPortlet);
1330                            }
1331                    }
1332    
1333                    _embeddedPortlets = portlets;
1334    
1335                    return _embeddedPortlets;
1336            }
1337    
1338            protected List<String> getNestedColumns() {
1339                    String nestedColumnIds = getTypeSettingsProperty(
1340                            LayoutTypePortletConstants.NESTED_COLUMN_IDS);
1341    
1342                    return ListUtil.fromArray(StringUtil.split(nestedColumnIds));
1343            }
1344    
1345            protected long getPlid() {
1346                    Layout layout = getLayout();
1347    
1348                    return layout.getPlid();
1349            }
1350    
1351            protected String[] getStaticPortletIds(String position)
1352                    throws PortalException, SystemException {
1353    
1354                    Layout layout = getLayout();
1355    
1356                    String selector1 = StringPool.BLANK;
1357    
1358                    Group group = layout.getGroup();
1359    
1360                    if (group.isUser()) {
1361                            selector1 = LayoutTypePortletConstants.STATIC_PORTLET_USER_SELECTOR;
1362                    }
1363                    else if (group.isOrganization()) {
1364                            selector1 =
1365                                    LayoutTypePortletConstants.STATIC_PORTLET_ORGANIZATION_SELECTOR;
1366                    }
1367                    else if (group.isRegularSite()) {
1368                            selector1 =
1369                                    LayoutTypePortletConstants.STATIC_PORTLET_REGULAR_SITE_SELECTOR;
1370                    }
1371    
1372                    String selector2 = layout.getFriendlyURL();
1373    
1374                    String[] portletIds = PropsUtil.getArray(
1375                            position, new Filter(selector1, selector2));
1376    
1377                    for (int i = 0; i < portletIds.length; i++) {
1378                            portletIds[i] = JS.getSafeName(portletIds[i]);
1379                    }
1380    
1381                    return portletIds;
1382            }
1383    
1384            protected List<Portlet> getStaticPortlets(String position)
1385                    throws PortalException, SystemException {
1386    
1387                    String[] portletIds = getStaticPortletIds(position);
1388    
1389                    List<Portlet> portlets = new ArrayList<Portlet>();
1390    
1391                    for (String portletId : portletIds) {
1392                            if (Validator.isNull(portletId) ||
1393                                    hasNonstaticPortletId(portletId)) {
1394    
1395                                    continue;
1396                            }
1397    
1398                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
1399                                    getCompanyId(), portletId);
1400    
1401                            if (portlet != null) {
1402                                    Portlet staticPortlet = portlet;
1403    
1404                                    if (portlet.isInstanceable()) {
1405    
1406                                            // Instanceable portlets do not need to be cloned because
1407                                            // they are already cloned. See the method getPortletById in
1408                                            // the class PortletLocalServiceImpl and how it references
1409                                            // the method getClonedInstance in the class PortletImpl.
1410    
1411                                    }
1412                                    else {
1413                                            staticPortlet = (Portlet)staticPortlet.clone();
1414                                    }
1415    
1416                                    staticPortlet.setStatic(true);
1417    
1418                                    if (position.startsWith("layout.static.portlets.start")) {
1419                                            staticPortlet.setStaticStart(true);
1420                                    }
1421    
1422                                    portlets.add(staticPortlet);
1423                            }
1424                    }
1425    
1426                    return portlets;
1427            }
1428    
1429            protected String getThemeId() {
1430                    String themeId = null;
1431    
1432                    try {
1433                            Layout layout = getLayout();
1434    
1435                            Theme theme = layout.getTheme();
1436    
1437                            if (theme != null) {
1438                                    themeId = theme.getThemeId();
1439                            }
1440                            else {
1441                                    themeId = layout.getThemeId();
1442                            }
1443                    }
1444                    catch (Exception e) {
1445                            _log.error(e, e);
1446                    }
1447    
1448                    return themeId;
1449            }
1450    
1451            protected String getUserPreference(String key) {
1452                    String value = StringPool.BLANK;
1453    
1454                    if (!hasUserPreferences()) {
1455                            return value;
1456                    }
1457    
1458                    value = _portalPreferences.getValue(
1459                            CustomizedPages.namespacePlid(getPlid()), key, StringPool.NULL);
1460    
1461                    if (!value.equals(StringPool.NULL)) {
1462                            return value;
1463                    }
1464    
1465                    value = getTypeSettingsProperty(key);
1466    
1467                    if (Validator.isNull(value)) {
1468                            return value;
1469                    }
1470    
1471                    String[] portletIds = StringUtil.split(value);
1472    
1473                    String[] newPortletIds = new String[portletIds.length];
1474    
1475                    for (int i = 0; i < portletIds.length; i++) {
1476                            if (PortletConstants.hasInstanceId(portletIds[i])) {
1477                                    newPortletIds[i] = PortletConstants.assemblePortletId(
1478                                            portletIds[i], _portalPreferences.getUserId(),
1479                                            generateInstanceId());
1480    
1481                                    copyPreferences(
1482                                            _portalPreferences.getUserId(), portletIds[i],
1483                                            newPortletIds[i]);
1484                            }
1485                            else {
1486                                    newPortletIds[i] = portletIds[i];
1487                            }
1488                    }
1489    
1490                    value = StringUtil.merge(newPortletIds);
1491    
1492                    setUserPreference(key, value);
1493    
1494                    return value;
1495            }
1496    
1497            protected boolean hasNonstaticPortletId(String portletId) {
1498                    LayoutTemplate layoutTemplate = getLayoutTemplate();
1499    
1500                    List<String> columns = layoutTemplate.getColumns();
1501    
1502                    for (int i = 0; i < columns.size(); i++) {
1503                            String columnId = columns.get(i);
1504    
1505                            if (hasNonstaticPortletId(columnId, portletId)) {
1506                                    return true;
1507                            }
1508                    }
1509    
1510                    return false;
1511            }
1512    
1513            protected boolean hasNonstaticPortletId(String columnId, String portletId) {
1514                    String columnValue = getColumnValue(columnId);
1515    
1516                    String[] columnValues = StringUtil.split(columnValue);
1517    
1518                    for (String nonstaticPortletId : columnValues) {
1519                            if (nonstaticPortletId.equals(portletId) ||
1520                                    PortletConstants.getRootPortletId(
1521                                            nonstaticPortletId).equals(portletId)) {
1522    
1523                                    return true;
1524                            }
1525                    }
1526    
1527                    return false;
1528            }
1529    
1530            protected boolean hasPortletId(String portletId, boolean strict)
1531                    throws PortalException, SystemException {
1532    
1533                    List<String> columns = getColumns();
1534    
1535                    for (String columnId : columns) {
1536                            if (hasNonstaticPortletId(columnId, portletId)) {
1537                                    return true;
1538                            }
1539    
1540                            if (hasStaticPortletId(columnId, portletId)) {
1541                                    return true;
1542                            }
1543                    }
1544    
1545                    Layout layout = getLayout();
1546    
1547                    if (layout.isTypeControlPanel()) {
1548                            return false;
1549                    }
1550    
1551                    if (!strict &&
1552                            ((PortletPreferencesLocalServiceUtil.getPortletPreferencesCount(
1553                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, layout.getPlid(),
1554                                    portletId) > 0) ||
1555                             (PortletPreferencesLocalServiceUtil.getPortletPreferencesCount(
1556                                    PortletKeys.PREFS_OWNER_TYPE_USER, layout.getPlid(),
1557                                    portletId) > 0))) {
1558    
1559                            return true;
1560                    }
1561    
1562                    return false;
1563            }
1564    
1565            protected boolean hasStaticPortletId(String columnId, String portletId)
1566                    throws PortalException, SystemException {
1567    
1568                    String[] staticPortletIdsStart = getStaticPortletIds(
1569                            PropsKeys.LAYOUT_STATIC_PORTLETS_START + columnId);
1570    
1571                    String[] staticPortletIdsEnd = getStaticPortletIds(
1572                            PropsKeys.LAYOUT_STATIC_PORTLETS_END + columnId);
1573    
1574                    for (String staticPortletId : staticPortletIdsStart) {
1575                            if (staticPortletId.equals(portletId) ||
1576                                    PortletConstants.getRootPortletId(
1577                                            staticPortletId).equals(portletId)) {
1578    
1579                                    return true;
1580                            }
1581                    }
1582    
1583                    for (String staticPortletId : staticPortletIdsEnd) {
1584                            if (staticPortletId.equals(portletId) ||
1585                                    PortletConstants.getRootPortletId(
1586                                            staticPortletId).equals(portletId)) {
1587    
1588                                    return true;
1589                            }
1590                    }
1591    
1592                    return false;
1593            }
1594    
1595            protected boolean hasUserPreferences() {
1596                    if (_portalPreferences != null) {
1597                            return true;
1598                    }
1599    
1600                    return false;
1601            }
1602    
1603            protected boolean isLayoutSetPrototype() {
1604                    try {
1605                            Layout layout = getLayout();
1606    
1607                            LayoutSet layoutSet = layout.getLayoutSet();
1608    
1609                            Group group = layoutSet.getGroup();
1610    
1611                            return group.isLayoutSetPrototype();
1612                    }
1613                    catch (Exception e) {
1614                            _log.error(e, e);
1615                    }
1616    
1617                    return false;
1618            }
1619    
1620            protected void onRemoveFromLayout(String[] portletIds)
1621                    throws SystemException {
1622    
1623                    List<String> portletIdList = new UniqueList<String>();
1624    
1625                    for (String portletId : portletIds) {
1626                            removeModesPortletId(portletId);
1627                            removeStatesPortletId(portletId);
1628    
1629                            portletIdList.add(portletId);
1630    
1631                            String rootPortletId = PortletConstants.getRootPortletId(portletId);
1632    
1633                            if (rootPortletId.equals(PortletKeys.NESTED_PORTLETS)) {
1634                                    String portletNamespace = PortalUtil.getPortletNamespace(
1635                                            portletId);
1636    
1637                                    UnicodeProperties typeSettingsProperties =
1638                                            getTypeSettingsProperties();
1639    
1640                                    for (Map.Entry<String, String> entry :
1641                                                    typeSettingsProperties.entrySet()) {
1642    
1643                                            String key = entry.getKey();
1644    
1645                                            if (!key.startsWith(portletNamespace)) {
1646                                                    continue;
1647                                            }
1648    
1649                                            String nestedPortletIds = entry.getValue();
1650    
1651                                            for (String nestedPortletId :
1652                                                            StringUtil.split(nestedPortletIds)) {
1653    
1654                                                    removeModesPortletId(nestedPortletId);
1655                                                    removeStatesPortletId(nestedPortletId);
1656    
1657                                                    portletIdList.add(nestedPortletId);
1658                                            }
1659                                    }
1660    
1661                                    removeNestedColumns(portletNamespace);
1662                            }
1663                    }
1664    
1665                    try {
1666                            PortletLocalServiceUtil.deletePortlets(
1667                                    getCompanyId(),
1668                                    portletIdList.toArray(new String[portletIdList.size()]),
1669                                    getPlid());
1670                    }
1671                    catch (PortalException pe) {
1672                            _log.error(pe, pe);
1673                    }
1674            }
1675    
1676            protected void setUserPreference(String key, String value) {
1677                    if (!hasUserPreferences()) {
1678                            return;
1679                    }
1680    
1681                    _portalPreferences.setValue(
1682                            CustomizedPages.namespacePlid(getPlid()), key, value);
1683    
1684                    _portalPreferences.setValue(
1685                            CustomizedPages.namespacePlid(getPlid()), _MODIFIED_DATE,
1686                            _dateFormat.format(new Date()));
1687            }
1688    
1689            private static final String _MODIFIED_DATE = "modifiedDate";
1690    
1691            private static final String _NULL_DATE = "00000000000000";
1692    
1693            private static Log _log = LogFactoryUtil.getLog(
1694                    LayoutTypePortletImpl.class);
1695    
1696            private static String _nestedPortletsNamespace;
1697    
1698            private boolean _customizedView;
1699            private Format _dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
1700                    PropsValues.INDEX_DATE_FORMAT_PATTERN);
1701            private List<Portlet> _embeddedPortlets;
1702            private boolean _enablePortletLayoutListener = true;
1703            private Layout _layoutSetPrototypeLayout;
1704            private PortalPreferences _portalPreferences;
1705            private boolean _updatePermission;
1706    
1707    }