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