001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.counter.service.CounterLocalServiceUtil;
018    import com.liferay.portal.kernel.bean.BeanReference;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.staging.LayoutStagingUtil;
024    import com.liferay.portal.kernel.staging.MergeLayoutPrototypesThreadLocal;
025    import com.liferay.portal.kernel.staging.StagingUtil;
026    import com.liferay.portal.kernel.systemevent.SystemEventHierarchyEntry;
027    import com.liferay.portal.kernel.systemevent.SystemEventHierarchyEntryThreadLocal;
028    import com.liferay.portal.kernel.util.ArrayUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.LocaleUtil;
031    import com.liferay.portal.kernel.util.ParamUtil;
032    import com.liferay.portal.kernel.util.ProxyUtil;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.workflow.WorkflowConstants;
035    import com.liferay.portal.model.Layout;
036    import com.liferay.portal.model.LayoutRevision;
037    import com.liferay.portal.model.LayoutSet;
038    import com.liferay.portal.model.LayoutStagingHandler;
039    import com.liferay.portal.model.SystemEventConstants;
040    import com.liferay.portal.model.User;
041    import com.liferay.portal.security.auth.PrincipalThreadLocal;
042    import com.liferay.portal.service.ImageLocalServiceUtil;
043    import com.liferay.portal.service.LayoutFriendlyURLLocalServiceUtil;
044    import com.liferay.portal.service.LayoutLocalService;
045    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
046    import com.liferay.portal.service.ServiceContext;
047    import com.liferay.portal.service.ServiceContextThreadLocal;
048    import com.liferay.portal.service.SystemEventLocalServiceUtil;
049    import com.liferay.portal.service.UserLocalServiceUtil;
050    import com.liferay.portal.service.persistence.LayoutRevisionUtil;
051    import com.liferay.portal.service.persistence.LayoutUtil;
052    import com.liferay.portal.staging.ProxiedLayoutsThreadLocal;
053    import com.liferay.portal.staging.StagingAdvicesThreadLocal;
054    import com.liferay.portal.util.ClassLoaderUtil;
055    
056    import java.lang.reflect.InvocationTargetException;
057    import java.lang.reflect.Method;
058    
059    import java.util.ArrayList;
060    import java.util.Arrays;
061    import java.util.HashMap;
062    import java.util.HashSet;
063    import java.util.List;
064    import java.util.Locale;
065    import java.util.Map;
066    import java.util.Set;
067    
068    import org.aopalliance.intercept.MethodInterceptor;
069    import org.aopalliance.intercept.MethodInvocation;
070    
071    import org.springframework.core.annotation.Order;
072    
073    /**
074     * @author Raymond Aug??
075     * @author Brian Wing Shun Chan
076     */
077    @Order(1)
078    public class LayoutLocalServiceStagingAdvice implements MethodInterceptor {
079    
080            public LayoutLocalServiceStagingAdvice() {
081                    if (_log.isDebugEnabled()) {
082                            _log.debug("Instantiating " + hashCode());
083                    }
084            }
085    
086            public void deleteLayout(
087                            LayoutLocalService layoutLocalService, Layout layout,
088                            boolean updateLayoutSet, ServiceContext serviceContext)
089                    throws PortalException, SystemException {
090    
091                    long layoutSetBranchId = ParamUtil.getLong(
092                            serviceContext, "layoutSetBranchId");
093    
094                    if (layoutSetBranchId > 0) {
095                            LayoutRevisionLocalServiceUtil.deleteLayoutRevisions(
096                                    layoutSetBranchId, layout.getPlid());
097    
098                            List<LayoutRevision> notIncompleteLayoutRevisions =
099                                    LayoutRevisionUtil.findByP_NotS(
100                                            layout.getPlid(), WorkflowConstants.STATUS_INCOMPLETE);
101    
102                            if (notIncompleteLayoutRevisions.isEmpty()) {
103                                    LayoutRevisionLocalServiceUtil.deleteLayoutLayoutRevisions(
104                                            layout.getPlid());
105    
106                                    doDeleteLayout(
107                                            layoutLocalService, layout, updateLayoutSet,
108                                            serviceContext);
109                            }
110                    }
111                    else {
112                            doDeleteLayout(
113                                    layoutLocalService, layout, updateLayoutSet, serviceContext);
114                    }
115            }
116    
117            public void deleteLayout(
118                            LayoutLocalService layoutLocalService, long groupId,
119                            boolean privateLayout, long layoutId, ServiceContext serviceContext)
120                    throws PortalException, SystemException {
121    
122                    Layout layout = layoutLocalService.getLayout(
123                            groupId, privateLayout, layoutId);
124    
125                    deleteLayout(layoutLocalService, layout, true, serviceContext);
126            }
127    
128            @Override
129            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
130                    if (!StagingAdvicesThreadLocal.isEnabled()) {
131                            return methodInvocation.proceed();
132                    }
133    
134                    Method method = methodInvocation.getMethod();
135    
136                    String methodName = method.getName();
137    
138                    boolean showIncomplete = false;
139    
140                    if (!_layoutLocalServiceStagingAdviceMethodNames.contains(methodName)) {
141                            return wrapReturnValue(methodInvocation.proceed(), showIncomplete);
142                    }
143    
144                    Object returnValue = null;
145    
146                    Object thisObject = methodInvocation.getThis();
147                    Object[] arguments = methodInvocation.getArguments();
148    
149                    if (methodName.equals("createLayout")) {
150                            return methodInvocation.proceed();
151                    }
152                    else if (methodName.equals("deleteLayout")) {
153                            if (arguments.length == 3) {
154                                    deleteLayout(
155                                            (LayoutLocalService)thisObject, (Layout)arguments[0],
156                                            (Boolean)arguments[1], (ServiceContext)arguments[2]);
157                            }
158                            else if (arguments.length == 4) {
159                                    deleteLayout(
160                                            (LayoutLocalService)thisObject, (Long)arguments[0],
161                                            (Boolean)arguments[1], (Long)arguments[2],
162                                            (ServiceContext)arguments[3]);
163                            }
164                            else {
165                                    return wrapReturnValue(
166                                            methodInvocation.proceed(), showIncomplete);
167                            }
168                    }
169                    else if (methodName.equals("getLayouts")) {
170                            if (arguments.length == 6) {
171                                    showIncomplete = (Boolean)arguments[3];
172                            }
173    
174                            return wrapReturnValue(methodInvocation.proceed(), showIncomplete);
175                    }
176                    else if (methodName.equals("updateLayout") &&
177                                     (arguments.length == 15)) {
178    
179                            Map<Locale, String> friendlyURLMap = null;
180    
181                            if (Arrays.equals(
182                                            method.getParameterTypes(),
183                                            _UPDATE_LAYOUT_PARAMETER_TYPES)) {
184    
185                                    friendlyURLMap = new HashMap<Locale, String>();
186    
187                                    friendlyURLMap.put(
188                                            LocaleUtil.getSiteDefault(), (String)arguments[11]);
189                            }
190                            else {
191                                    friendlyURLMap = (Map<Locale, String>)arguments[11];
192                            }
193    
194                            returnValue = updateLayout(
195                                    (LayoutLocalService)thisObject, (Long)arguments[0],
196                                    (Boolean)arguments[1], (Long)arguments[2], (Long)arguments[3],
197                                    (Map<Locale, String>)arguments[4],
198                                    (Map<Locale, String>)arguments[5],
199                                    (Map<Locale, String>)arguments[6],
200                                    (Map<Locale, String>)arguments[7],
201                                    (Map<Locale, String>)arguments[8], (String)arguments[9],
202                                    (Boolean)arguments[10], friendlyURLMap, (Boolean)arguments[12],
203                                    (byte[])arguments[13], (ServiceContext)arguments[14]);
204                    }
205                    else {
206                            try {
207                                    Class<?> clazz = getClass();
208    
209                                    Class<?>[] parameterTypes = ArrayUtil.append(
210                                            new Class<?>[] {LayoutLocalService.class},
211                                            method.getParameterTypes());
212    
213                                    Method layoutLocalServiceStagingAdviceMethod = clazz.getMethod(
214                                            methodName, parameterTypes);
215    
216                                    arguments = ArrayUtil.append(
217                                            new Object[] {thisObject}, arguments);
218    
219                                    returnValue = layoutLocalServiceStagingAdviceMethod.invoke(
220                                            this, arguments);
221                            }
222                            catch (InvocationTargetException ite) {
223                                    throw ite.getTargetException();
224                            }
225                            catch (NoSuchMethodException nsme) {
226                                    returnValue = methodInvocation.proceed();
227                            }
228                    }
229    
230                    returnValue = wrapReturnValue(returnValue, showIncomplete);
231    
232                    return returnValue;
233            }
234    
235            public Layout updateLayout(
236                            LayoutLocalService layoutLocalService, long groupId,
237                            boolean privateLayout, long layoutId, long parentLayoutId,
238                            Map<Locale, String> nameMap, Map<Locale, String> titleMap,
239                            Map<Locale, String> descriptionMap, Map<Locale, String> keywordsMap,
240                            Map<Locale, String> robotsMap, String type, boolean hidden,
241                            Map<Locale, String> friendlyURLMap, Boolean iconImage,
242                            byte[] iconBytes, ServiceContext serviceContext)
243                    throws PortalException, SystemException {
244    
245                    // Layout
246    
247                    parentLayoutId = layoutLocalServiceHelper.getParentLayoutId(
248                            groupId, privateLayout, parentLayoutId);
249                    String name = nameMap.get(LocaleUtil.getSiteDefault());
250                    friendlyURLMap = layoutLocalServiceHelper.getFriendlyURLMap(
251                            groupId, privateLayout, layoutId, StringPool.BLANK, friendlyURLMap);
252                    String friendlyURL = friendlyURLMap.get(LocaleUtil.getSiteDefault());
253    
254                    layoutLocalServiceHelper.validate(
255                            groupId, privateLayout, layoutId, parentLayoutId, name, type,
256                            hidden, friendlyURLMap);
257    
258                    layoutLocalServiceHelper.validateParentLayoutId(
259                            groupId, privateLayout, layoutId, parentLayoutId);
260    
261                    Layout originalLayout = LayoutUtil.findByG_P_L(
262                            groupId, privateLayout, layoutId);
263    
264                    Layout layout = wrapLayout(originalLayout);
265    
266                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
267                            layout);
268    
269                    if (layoutRevision == null) {
270                            return layoutLocalService.updateLayout(
271                                    groupId, privateLayout, layoutId, parentLayoutId, nameMap,
272                                    titleMap, descriptionMap, keywordsMap, robotsMap, type, hidden,
273                                    friendlyURLMap, iconImage, iconBytes, serviceContext);
274                    }
275    
276                    if (parentLayoutId != originalLayout.getParentLayoutId()) {
277                            int priority = layoutLocalServiceHelper.getNextPriority(
278                                    groupId, privateLayout, parentLayoutId,
279                                    originalLayout.getSourcePrototypeLayoutUuid(), -1);
280    
281                            originalLayout.setPriority(priority);
282                    }
283    
284                    originalLayout.setParentLayoutId(parentLayoutId);
285                    layoutRevision.setNameMap(nameMap);
286                    layoutRevision.setTitleMap(titleMap);
287                    layoutRevision.setDescriptionMap(descriptionMap);
288                    layoutRevision.setKeywordsMap(keywordsMap);
289                    layoutRevision.setRobotsMap(robotsMap);
290                    originalLayout.setType(type);
291                    originalLayout.setHidden(hidden);
292                    originalLayout.setFriendlyURL(friendlyURL);
293    
294                    if (iconImage != null) {
295                            layoutRevision.setIconImage(iconImage.booleanValue());
296    
297                            if (iconImage.booleanValue()) {
298                                    long iconImageId = layoutRevision.getIconImageId();
299    
300                                    if (iconImageId <= 0) {
301                                            iconImageId = CounterLocalServiceUtil.increment();
302    
303                                            layoutRevision.setIconImageId(iconImageId);
304                                    }
305                            }
306                    }
307    
308                    boolean layoutPrototypeLinkEnabled = ParamUtil.getBoolean(
309                            serviceContext, "layoutPrototypeLinkEnabled", true);
310    
311                    originalLayout.setLayoutPrototypeLinkEnabled(
312                            layoutPrototypeLinkEnabled);
313    
314                    originalLayout.setExpandoBridgeAttributes(serviceContext);
315    
316                    LayoutUtil.update(originalLayout);
317    
318                    LayoutFriendlyURLLocalServiceUtil.updateLayoutFriendlyURLs(
319                            originalLayout.getUserId(), originalLayout.getCompanyId(),
320                            originalLayout.getGroupId(), originalLayout.getPlid(),
321                            originalLayout.isPrivateLayout(), friendlyURLMap, serviceContext);
322    
323                    boolean hasWorkflowTask = StagingUtil.hasWorkflowTask(
324                            serviceContext.getUserId(), layoutRevision);
325    
326                    serviceContext.setAttribute("revisionInProgress", hasWorkflowTask);
327    
328                    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
329    
330                    LayoutRevisionLocalServiceUtil.updateLayoutRevision(
331                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
332                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
333                            layoutRevision.getTitle(), layoutRevision.getDescription(),
334                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
335                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
336                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
337                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
338                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
339                            serviceContext);
340    
341                    // Icon
342    
343                    if (iconImage != null) {
344                            if (ArrayUtil.isNotEmpty(iconBytes)) {
345                                    ImageLocalServiceUtil.updateImage(
346                                            layoutRevision.getIconImageId(), iconBytes);
347                            }
348                    }
349    
350                    return layout;
351            }
352    
353            public Layout updateLayout(
354                            LayoutLocalService layoutLocalService, long groupId,
355                            boolean privateLayout, long layoutId, String typeSettings)
356                    throws PortalException, SystemException {
357    
358                    Layout layout = LayoutUtil.findByG_P_L(
359                            groupId, privateLayout, layoutId);
360    
361                    layout = wrapLayout(layout);
362    
363                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
364                            layout);
365    
366                    if (layoutRevision == null) {
367                            return layoutLocalService.updateLayout(
368                                    groupId, privateLayout, layoutId, typeSettings);
369                    }
370    
371                    layout.setTypeSettings(typeSettings);
372    
373                    ServiceContext serviceContext =
374                            ServiceContextThreadLocal.getServiceContext();
375    
376                    boolean hasWorkflowTask = StagingUtil.hasWorkflowTask(
377                            serviceContext.getUserId(), layoutRevision);
378    
379                    serviceContext.setAttribute("revisionInProgress", hasWorkflowTask);
380    
381                    if (!MergeLayoutPrototypesThreadLocal.isInProgress()) {
382                            serviceContext.setWorkflowAction(
383                                    WorkflowConstants.ACTION_SAVE_DRAFT);
384                    }
385    
386                    LayoutRevisionLocalServiceUtil.updateLayoutRevision(
387                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
388                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
389                            layoutRevision.getTitle(), layoutRevision.getDescription(),
390                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
391                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
392                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
393                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
394                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
395                            serviceContext);
396    
397                    return layout;
398            }
399    
400            public Layout updateLookAndFeel(
401                            LayoutLocalService layoutLocalService, long groupId,
402                            boolean privateLayout, long layoutId, String themeId,
403                            String colorSchemeId, String css, boolean wapTheme)
404                    throws PortalException, SystemException {
405    
406                    Layout layout = LayoutUtil.findByG_P_L(
407                            groupId, privateLayout, layoutId);
408    
409                    layout = wrapLayout(layout);
410    
411                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
412                            layout);
413    
414                    if (layoutRevision == null) {
415                            return layoutLocalService.updateLookAndFeel(
416                                    groupId, privateLayout, layoutId, themeId, colorSchemeId, css,
417                                    wapTheme);
418                    }
419    
420                    if (wapTheme) {
421                            layout.setWapThemeId(themeId);
422                            layout.setWapColorSchemeId(colorSchemeId);
423                    }
424                    else {
425                            layout.setThemeId(themeId);
426                            layout.setColorSchemeId(colorSchemeId);
427                            layout.setCss(css);
428                    }
429    
430                    ServiceContext serviceContext =
431                            ServiceContextThreadLocal.getServiceContext();
432    
433                    boolean hasWorkflowTask = StagingUtil.hasWorkflowTask(
434                            serviceContext.getUserId(), layoutRevision);
435    
436                    serviceContext.setAttribute("revisionInProgress", hasWorkflowTask);
437    
438                    if (!MergeLayoutPrototypesThreadLocal.isInProgress()) {
439                            serviceContext.setWorkflowAction(
440                                    WorkflowConstants.ACTION_SAVE_DRAFT);
441                    }
442    
443                    LayoutRevisionLocalServiceUtil.updateLayoutRevision(
444                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
445                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
446                            layoutRevision.getTitle(), layoutRevision.getDescription(),
447                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
448                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
449                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
450                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
451                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
452                            serviceContext);
453    
454                    return layout;
455            }
456    
457            public Layout updateName(
458                            LayoutLocalService layoutLocalService, Layout layout, String name,
459                            String languageId)
460                    throws PortalException, SystemException {
461    
462                    layout = wrapLayout(layout);
463    
464                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
465                            layout);
466    
467                    if (layoutRevision == null) {
468                            return layoutLocalService.updateName(layout, name, languageId);
469                    }
470    
471                    layoutLocalServiceHelper.validateName(name, languageId);
472    
473                    layout.setName(name, LocaleUtil.fromLanguageId(languageId));
474    
475                    ServiceContext serviceContext =
476                            ServiceContextThreadLocal.getServiceContext();
477    
478                    boolean hasWorkflowTask = StagingUtil.hasWorkflowTask(
479                            serviceContext.getUserId(), layoutRevision);
480    
481                    serviceContext.setAttribute("revisionInProgress", hasWorkflowTask);
482    
483                    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
484    
485                    LayoutRevisionLocalServiceUtil.updateLayoutRevision(
486                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
487                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
488                            layoutRevision.getTitle(), layoutRevision.getDescription(),
489                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
490                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
491                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
492                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
493                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
494                            serviceContext);
495    
496                    return layout;
497            }
498    
499            protected void doDeleteLayout(
500                            LayoutLocalService layoutLocalService, Layout layout,
501                            boolean updateLayoutSet, ServiceContext serviceContext)
502                    throws PortalException, SystemException {
503    
504                    if (SystemEventHierarchyEntryThreadLocal.push(
505                                    Layout.class, layout.getPlid()) == null) {
506    
507                            layoutLocalService.deleteLayout(
508                                    layout, updateLayoutSet, serviceContext);
509                    }
510                    else {
511                            try {
512                                    layoutLocalService.deleteLayout(
513                                            layout, updateLayoutSet, serviceContext);
514    
515                                    SystemEventHierarchyEntry systemEventHierarchyEntry =
516                                            SystemEventHierarchyEntryThreadLocal.peek();
517    
518                                    SystemEventLocalServiceUtil.addSystemEvent(
519                                            0, layout.getGroupId(), Layout.class.getName(),
520                                            layout.getPlid(), layout.getUuid(), null,
521                                            SystemEventConstants.TYPE_DELETE,
522                                            systemEventHierarchyEntry.getExtraData());
523                            }
524                            finally {
525                                    SystemEventHierarchyEntryThreadLocal.pop(
526                                            Layout.class, layout.getPlid());
527                            }
528                    }
529            }
530    
531            protected Layout getProxiedLayout(Layout layout) {
532                    Map<Layout, Object> proxiedLayouts =
533                            ProxiedLayoutsThreadLocal.getProxiedLayouts();
534    
535                    Object proxiedLayout = proxiedLayouts.get(layout);
536    
537                    if (proxiedLayout != null) {
538                            return (Layout)proxiedLayout;
539                    }
540    
541                    proxiedLayout = ProxyUtil.newProxyInstance(
542                            ClassLoaderUtil.getPortalClassLoader(), new Class[] {Layout.class},
543                            new LayoutStagingHandler(layout));
544    
545                    proxiedLayouts.put(layout, proxiedLayout);
546    
547                    return (Layout)proxiedLayout;
548            }
549    
550            protected Layout unwrapLayout(Layout layout) {
551                    LayoutStagingHandler layoutStagingHandler =
552                            LayoutStagingUtil.getLayoutStagingHandler(layout);
553    
554                    if (layoutStagingHandler == null) {
555                            return layout;
556                    }
557    
558                    return layoutStagingHandler.getLayout();
559            }
560    
561            protected Layout wrapLayout(Layout layout) {
562                    LayoutStagingHandler layoutStagingHandler =
563                            LayoutStagingUtil.getLayoutStagingHandler(layout);
564    
565                    if (layoutStagingHandler != null) {
566                            return layout;
567                    }
568    
569                    if (!LayoutStagingUtil.isBranchingLayout(layout)) {
570                            return layout;
571                    }
572    
573                    return getProxiedLayout(layout);
574            }
575    
576            protected List<Layout> wrapLayouts(
577                    List<Layout> layouts, boolean showIncomplete) {
578    
579                    if (layouts.isEmpty()) {
580                            return layouts;
581                    }
582    
583                    Layout firstLayout = layouts.get(0);
584    
585                    Layout wrappedFirstLayout = wrapLayout(firstLayout);
586    
587                    if (wrappedFirstLayout == firstLayout) {
588                            return layouts;
589                    }
590    
591                    long layoutSetBranchId = 0;
592    
593                    if (!showIncomplete) {
594                            long userId = 0;
595    
596                            try {
597                                    userId = GetterUtil.getLong(PrincipalThreadLocal.getName());
598    
599                                    if (userId > 0) {
600                                            User user = UserLocalServiceUtil.getUser(userId);
601    
602                                            LayoutSet layoutSet = firstLayout.getLayoutSet();
603    
604                                            layoutSetBranchId = StagingUtil.getRecentLayoutSetBranchId(
605                                                    user, layoutSet.getLayoutSetId());
606                                    }
607                            }
608                            catch (Exception e) {
609                                    if (_log.isDebugEnabled()) {
610                                            _log.debug("No layout set branch found for user " + userId);
611                                    }
612                            }
613                    }
614    
615                    List<Layout> wrappedLayouts = new ArrayList<Layout>(layouts.size());
616    
617                    for (int i = 0; i < layouts.size(); i++) {
618                            Layout wrappedLayout = wrapLayout(layouts.get(i));
619    
620                            if (showIncomplete ||
621                                    !StagingUtil.isIncomplete(wrappedLayout, layoutSetBranchId)) {
622    
623                                    wrappedLayouts.add(wrappedLayout);
624                            }
625                    }
626    
627                    return wrappedLayouts;
628            }
629    
630            protected Object wrapReturnValue(
631                    Object returnValue, boolean showIncomplete) {
632    
633                    if (returnValue instanceof Layout) {
634                            returnValue = wrapLayout((Layout)returnValue);
635                    }
636                    else if (returnValue instanceof List<?>) {
637                            List<?> list = (List<?>)returnValue;
638    
639                            if (!list.isEmpty()) {
640                                    Object object = list.get(0);
641    
642                                    if (object instanceof Layout) {
643                                            returnValue = wrapLayouts(
644                                                    (List<Layout>)returnValue, showIncomplete);
645                                    }
646                            }
647                    }
648    
649                    return returnValue;
650            }
651    
652            @BeanReference(type = LayoutLocalServiceHelper.class)
653            protected LayoutLocalServiceHelper layoutLocalServiceHelper;
654    
655            private static final Class<?>[] _UPDATE_LAYOUT_PARAMETER_TYPES = {
656                    long.class, boolean.class, long.class, long.class, Map.class, Map.class,
657                    Map.class, Map.class, Map.class, String.class, boolean.class,
658                    String.class, Boolean.class, byte[].class, ServiceContext.class
659            };
660    
661            private static Log _log = LogFactoryUtil.getLog(
662                    LayoutLocalServiceStagingAdvice.class);
663    
664            private static Set<String> _layoutLocalServiceStagingAdviceMethodNames =
665                    new HashSet<String>();
666    
667            static {
668                    _layoutLocalServiceStagingAdviceMethodNames.add("createLayout");
669                    _layoutLocalServiceStagingAdviceMethodNames.add("deleteLayout");
670                    _layoutLocalServiceStagingAdviceMethodNames.add("getLayouts");
671                    _layoutLocalServiceStagingAdviceMethodNames.add("updateLayout");
672                    _layoutLocalServiceStagingAdviceMethodNames.add("updateLookAndFeel");
673                    _layoutLocalServiceStagingAdviceMethodNames.add("updateName");
674            }
675    
676    }