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