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                    }
165                    else if (methodName.equals("getLayouts")) {
166                            if (arguments.length == 6) {
167                                    showIncomplete = (Boolean)arguments[3];
168                            }
169    
170                            return wrapReturnValue(methodInvocation.proceed(), showIncomplete);
171                    }
172                    else if (methodName.equals("updateLayout") &&
173                                     (arguments.length == 15)) {
174    
175                            Map<Locale, String> friendlyURLMap = null;
176    
177                            if (Arrays.equals(
178                                            method.getParameterTypes(),
179                                            _UPDATE_LAYOUT_PARAMETER_TYPES)) {
180    
181                                    friendlyURLMap = new HashMap<Locale, String>();
182    
183                                    friendlyURLMap.put(
184                                            LocaleUtil.getSiteDefault(), (String)arguments[11]);
185                            }
186                            else {
187                                    friendlyURLMap = (Map<Locale, String>)arguments[11];
188                            }
189    
190                            returnValue = updateLayout(
191                                    (LayoutLocalService)thisObject, (Long)arguments[0],
192                                    (Boolean)arguments[1], (Long)arguments[2], (Long)arguments[3],
193                                    (Map<Locale, String>)arguments[4],
194                                    (Map<Locale, String>)arguments[5],
195                                    (Map<Locale, String>)arguments[6],
196                                    (Map<Locale, String>)arguments[7],
197                                    (Map<Locale, String>)arguments[8], (String)arguments[9],
198                                    (Boolean)arguments[10], friendlyURLMap, (Boolean)arguments[12],
199                                    (byte[])arguments[13], (ServiceContext)arguments[14]);
200                    }
201                    else {
202                            try {
203                                    Class<?> clazz = getClass();
204    
205                                    Class<?>[] parameterTypes = ArrayUtil.append(
206                                            new Class<?>[] {LayoutLocalService.class},
207                                            method.getParameterTypes());
208    
209                                    Method layoutLocalServiceStagingAdviceMethod = clazz.getMethod(
210                                            methodName, parameterTypes);
211    
212                                    arguments = ArrayUtil.append(
213                                            new Object[] {thisObject}, arguments);
214    
215                                    returnValue = layoutLocalServiceStagingAdviceMethod.invoke(
216                                            this, arguments);
217                            }
218                            catch (InvocationTargetException ite) {
219                                    throw ite.getTargetException();
220                            }
221                            catch (NoSuchMethodException nsme) {
222                                    returnValue = methodInvocation.proceed();
223                            }
224                    }
225    
226                    returnValue = wrapReturnValue(returnValue, showIncomplete);
227    
228                    return returnValue;
229            }
230    
231            public Layout updateLayout(
232                            LayoutLocalService layoutLocalService, long groupId,
233                            boolean privateLayout, long layoutId, long parentLayoutId,
234                            Map<Locale, String> nameMap, Map<Locale, String> titleMap,
235                            Map<Locale, String> descriptionMap, Map<Locale, String> keywordsMap,
236                            Map<Locale, String> robotsMap, String type, boolean hidden,
237                            Map<Locale, String> friendlyURLMap, Boolean iconImage,
238                            byte[] iconBytes, ServiceContext serviceContext)
239                    throws PortalException, SystemException {
240    
241                    // Layout
242    
243                    parentLayoutId = layoutLocalServiceHelper.getParentLayoutId(
244                            groupId, privateLayout, parentLayoutId);
245                    String name = nameMap.get(LocaleUtil.getSiteDefault());
246                    friendlyURLMap = layoutLocalServiceHelper.getFriendlyURLMap(
247                            groupId, privateLayout, layoutId, StringPool.BLANK, friendlyURLMap);
248                    String friendlyURL = friendlyURLMap.get(LocaleUtil.getSiteDefault());
249    
250                    layoutLocalServiceHelper.validate(
251                            groupId, privateLayout, layoutId, parentLayoutId, name, type,
252                            hidden, friendlyURLMap);
253    
254                    layoutLocalServiceHelper.validateParentLayoutId(
255                            groupId, privateLayout, layoutId, parentLayoutId);
256    
257                    Layout originalLayout = LayoutUtil.findByG_P_L(
258                            groupId, privateLayout, layoutId);
259    
260                    Layout layout = wrapLayout(originalLayout);
261    
262                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
263                            layout);
264    
265                    if (layoutRevision == null) {
266                            return layoutLocalService.updateLayout(
267                                    groupId, privateLayout, layoutId, parentLayoutId, nameMap,
268                                    titleMap, descriptionMap, keywordsMap, robotsMap, type, hidden,
269                                    friendlyURLMap, iconImage, iconBytes, serviceContext);
270                    }
271    
272                    if (parentLayoutId != originalLayout.getParentLayoutId()) {
273                            int priority = layoutLocalServiceHelper.getNextPriority(
274                                    groupId, privateLayout, parentLayoutId,
275                                    originalLayout.getSourcePrototypeLayoutUuid(), -1);
276    
277                            originalLayout.setPriority(priority);
278                    }
279    
280                    originalLayout.setParentLayoutId(parentLayoutId);
281                    layoutRevision.setNameMap(nameMap);
282                    layoutRevision.setTitleMap(titleMap);
283                    layoutRevision.setDescriptionMap(descriptionMap);
284                    layoutRevision.setKeywordsMap(keywordsMap);
285                    layoutRevision.setRobotsMap(robotsMap);
286                    originalLayout.setType(type);
287                    originalLayout.setHidden(hidden);
288                    originalLayout.setFriendlyURL(friendlyURL);
289    
290                    if (iconImage != null) {
291                            layoutRevision.setIconImage(iconImage.booleanValue());
292    
293                            if (iconImage.booleanValue()) {
294                                    long iconImageId = layoutRevision.getIconImageId();
295    
296                                    if (iconImageId <= 0) {
297                                            iconImageId = CounterLocalServiceUtil.increment();
298    
299                                            layoutRevision.setIconImageId(iconImageId);
300                                    }
301                            }
302                    }
303    
304                    boolean layoutPrototypeLinkEnabled = ParamUtil.getBoolean(
305                            serviceContext, "layoutPrototypeLinkEnabled", true);
306    
307                    originalLayout.setLayoutPrototypeLinkEnabled(
308                            layoutPrototypeLinkEnabled);
309    
310                    originalLayout.setExpandoBridgeAttributes(serviceContext);
311    
312                    LayoutUtil.update(originalLayout);
313    
314                    LayoutFriendlyURLLocalServiceUtil.updateLayoutFriendlyURLs(
315                            originalLayout.getUserId(), originalLayout.getCompanyId(),
316                            originalLayout.getGroupId(), originalLayout.getPlid(),
317                            originalLayout.isPrivateLayout(), friendlyURLMap, serviceContext);
318    
319                    boolean hasWorkflowTask = StagingUtil.hasWorkflowTask(
320                            serviceContext.getUserId(), layoutRevision);
321    
322                    serviceContext.setAttribute("revisionInProgress", hasWorkflowTask);
323    
324                    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
325    
326                    LayoutRevisionLocalServiceUtil.updateLayoutRevision(
327                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
328                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
329                            layoutRevision.getTitle(), layoutRevision.getDescription(),
330                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
331                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
332                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
333                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
334                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
335                            serviceContext);
336    
337                    // Icon
338    
339                    if (iconImage != null) {
340                            if (ArrayUtil.isNotEmpty(iconBytes)) {
341                                    ImageLocalServiceUtil.updateImage(
342                                            layoutRevision.getIconImageId(), iconBytes);
343                            }
344                    }
345    
346                    return layout;
347            }
348    
349            public Layout updateLayout(
350                            LayoutLocalService layoutLocalService, long groupId,
351                            boolean privateLayout, long layoutId, String typeSettings)
352                    throws PortalException, SystemException {
353    
354                    Layout layout = LayoutUtil.findByG_P_L(
355                            groupId, privateLayout, layoutId);
356    
357                    layout = wrapLayout(layout);
358    
359                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
360                            layout);
361    
362                    if (layoutRevision == null) {
363                            return layoutLocalService.updateLayout(
364                                    groupId, privateLayout, layoutId, typeSettings);
365                    }
366    
367                    layout.setTypeSettings(typeSettings);
368    
369                    ServiceContext serviceContext =
370                            ServiceContextThreadLocal.getServiceContext();
371    
372                    boolean hasWorkflowTask = StagingUtil.hasWorkflowTask(
373                            serviceContext.getUserId(), layoutRevision);
374    
375                    serviceContext.setAttribute("revisionInProgress", hasWorkflowTask);
376    
377                    if (!MergeLayoutPrototypesThreadLocal.isInProgress()) {
378                            serviceContext.setWorkflowAction(
379                                    WorkflowConstants.ACTION_SAVE_DRAFT);
380                    }
381    
382                    LayoutRevisionLocalServiceUtil.updateLayoutRevision(
383                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
384                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
385                            layoutRevision.getTitle(), layoutRevision.getDescription(),
386                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
387                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
388                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
389                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
390                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
391                            serviceContext);
392    
393                    return layout;
394            }
395    
396            public Layout updateLookAndFeel(
397                            LayoutLocalService layoutLocalService, long groupId,
398                            boolean privateLayout, long layoutId, String themeId,
399                            String colorSchemeId, String css, boolean wapTheme)
400                    throws PortalException, SystemException {
401    
402                    Layout layout = LayoutUtil.findByG_P_L(
403                            groupId, privateLayout, layoutId);
404    
405                    layout = wrapLayout(layout);
406    
407                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
408                            layout);
409    
410                    if (layoutRevision == null) {
411                            return layoutLocalService.updateLookAndFeel(
412                                    groupId, privateLayout, layoutId, themeId, colorSchemeId, css,
413                                    wapTheme);
414                    }
415    
416                    if (wapTheme) {
417                            layout.setWapThemeId(themeId);
418                            layout.setWapColorSchemeId(colorSchemeId);
419                    }
420                    else {
421                            layout.setThemeId(themeId);
422                            layout.setColorSchemeId(colorSchemeId);
423                            layout.setCss(css);
424                    }
425    
426                    ServiceContext serviceContext =
427                            ServiceContextThreadLocal.getServiceContext();
428    
429                    boolean hasWorkflowTask = StagingUtil.hasWorkflowTask(
430                            serviceContext.getUserId(), layoutRevision);
431    
432                    serviceContext.setAttribute("revisionInProgress", hasWorkflowTask);
433    
434                    if (!MergeLayoutPrototypesThreadLocal.isInProgress()) {
435                            serviceContext.setWorkflowAction(
436                                    WorkflowConstants.ACTION_SAVE_DRAFT);
437                    }
438    
439                    LayoutRevisionLocalServiceUtil.updateLayoutRevision(
440                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
441                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
442                            layoutRevision.getTitle(), layoutRevision.getDescription(),
443                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
444                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
445                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
446                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
447                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
448                            serviceContext);
449    
450                    return layout;
451            }
452    
453            public Layout updateName(
454                            LayoutLocalService layoutLocalService, Layout layout, String name,
455                            String languageId)
456                    throws PortalException, SystemException {
457    
458                    layout = wrapLayout(layout);
459    
460                    LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
461                            layout);
462    
463                    if (layoutRevision == null) {
464                            return layoutLocalService.updateName(layout, name, languageId);
465                    }
466    
467                    layoutLocalServiceHelper.validateName(name, languageId);
468    
469                    layout.setName(name, LocaleUtil.fromLanguageId(languageId));
470    
471                    ServiceContext serviceContext =
472                            ServiceContextThreadLocal.getServiceContext();
473    
474                    boolean hasWorkflowTask = StagingUtil.hasWorkflowTask(
475                            serviceContext.getUserId(), layoutRevision);
476    
477                    serviceContext.setAttribute("revisionInProgress", hasWorkflowTask);
478    
479                    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
480    
481                    LayoutRevisionLocalServiceUtil.updateLayoutRevision(
482                            serviceContext.getUserId(), layoutRevision.getLayoutRevisionId(),
483                            layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
484                            layoutRevision.getTitle(), layoutRevision.getDescription(),
485                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
486                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
487                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
488                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
489                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
490                            serviceContext);
491    
492                    return layout;
493            }
494    
495            protected void doDeleteLayout(
496                            LayoutLocalService layoutLocalService, Layout layout,
497                            boolean updateLayoutSet, ServiceContext serviceContext)
498                    throws PortalException, SystemException {
499    
500                    if (SystemEventHierarchyEntryThreadLocal.push(
501                                    Layout.class, layout.getPlid()) == null) {
502    
503                            layoutLocalService.deleteLayout(
504                                    layout, updateLayoutSet, serviceContext);
505                    }
506                    else {
507                            try {
508                                    layoutLocalService.deleteLayout(
509                                            layout, updateLayoutSet, serviceContext);
510    
511                                    SystemEventHierarchyEntry systemEventHierarchyEntry =
512                                            SystemEventHierarchyEntryThreadLocal.peek();
513    
514                                    SystemEventLocalServiceUtil.addSystemEvent(
515                                            0, layout.getGroupId(), Layout.class.getName(),
516                                            layout.getPlid(), layout.getUuid(), null,
517                                            SystemEventConstants.TYPE_DELETE,
518                                            systemEventHierarchyEntry.getExtraData());
519                            }
520                            finally {
521                                    SystemEventHierarchyEntryThreadLocal.pop(
522                                            Layout.class, layout.getPlid());
523                            }
524                    }
525            }
526    
527            protected Layout getProxiedLayout(Layout layout) {
528                    Map<Layout, Object> proxiedLayouts =
529                            ProxiedLayoutsThreadLocal.getProxiedLayouts();
530    
531                    Object proxiedLayout = proxiedLayouts.get(layout);
532    
533                    if (proxiedLayout != null) {
534                            return (Layout)proxiedLayout;
535                    }
536    
537                    proxiedLayout = ProxyUtil.newProxyInstance(
538                            ClassLoaderUtil.getPortalClassLoader(), new Class[] {Layout.class},
539                            new LayoutStagingHandler(layout));
540    
541                    proxiedLayouts.put(layout, proxiedLayout);
542    
543                    return (Layout)proxiedLayout;
544            }
545    
546            protected Layout unwrapLayout(Layout layout) {
547                    LayoutStagingHandler layoutStagingHandler =
548                            LayoutStagingUtil.getLayoutStagingHandler(layout);
549    
550                    if (layoutStagingHandler == null) {
551                            return layout;
552                    }
553    
554                    return layoutStagingHandler.getLayout();
555            }
556    
557            protected Layout wrapLayout(Layout layout) {
558                    LayoutStagingHandler layoutStagingHandler =
559                            LayoutStagingUtil.getLayoutStagingHandler(layout);
560    
561                    if (layoutStagingHandler != null) {
562                            return layout;
563                    }
564    
565                    if (!LayoutStagingUtil.isBranchingLayout(layout)) {
566                            return layout;
567                    }
568    
569                    return getProxiedLayout(layout);
570            }
571    
572            protected List<Layout> wrapLayouts(
573                    List<Layout> layouts, boolean showIncomplete) {
574    
575                    if (layouts.isEmpty()) {
576                            return layouts;
577                    }
578    
579                    Layout firstLayout = layouts.get(0);
580    
581                    Layout wrappedFirstLayout = wrapLayout(firstLayout);
582    
583                    if (wrappedFirstLayout == firstLayout) {
584                            return layouts;
585                    }
586    
587                    long layoutSetBranchId = 0;
588    
589                    if (!showIncomplete) {
590                            try {
591                                    long userId = GetterUtil.getLong(
592                                            PrincipalThreadLocal.getName());
593    
594                                    if (userId > 0) {
595                                            User user = UserLocalServiceUtil.getUser(userId);
596    
597                                            LayoutSet layoutSet = firstLayout.getLayoutSet();
598    
599                                            layoutSetBranchId = StagingUtil.getRecentLayoutSetBranchId(
600                                                    user, layoutSet.getLayoutSetId());
601                                    }
602                            }
603                            catch (Exception e) {
604                            }
605                    }
606    
607                    List<Layout> wrappedLayouts = new ArrayList<Layout>(layouts.size());
608    
609                    for (int i = 0; i < layouts.size(); i++) {
610                            Layout wrappedLayout = wrapLayout(layouts.get(i));
611    
612                            if (showIncomplete ||
613                                    !StagingUtil.isIncomplete(wrappedLayout, layoutSetBranchId)) {
614    
615                                    wrappedLayouts.add(wrappedLayout);
616                            }
617                    }
618    
619                    return wrappedLayouts;
620            }
621    
622            protected Object wrapReturnValue(
623                    Object returnValue, boolean showIncomplete) {
624    
625                    if (returnValue instanceof Layout) {
626                            returnValue = wrapLayout((Layout)returnValue);
627                    }
628                    else if (returnValue instanceof List<?>) {
629                            List<?> list = (List<?>)returnValue;
630    
631                            if (!list.isEmpty()) {
632                                    Object object = list.get(0);
633    
634                                    if (object instanceof Layout) {
635                                            returnValue = wrapLayouts(
636                                                    (List<Layout>)returnValue, showIncomplete);
637                                    }
638                            }
639                    }
640    
641                    return returnValue;
642            }
643    
644            @BeanReference(type = LayoutLocalServiceHelper.class)
645            protected LayoutLocalServiceHelper layoutLocalServiceHelper;
646    
647            private static final Class<?>[] _UPDATE_LAYOUT_PARAMETER_TYPES = {
648                    long.class, boolean.class, long.class, long.class, Map.class, Map.class,
649                    Map.class, Map.class, Map.class, String.class, boolean.class,
650                    String.class, Boolean.class, byte[].class, ServiceContext.class
651            };
652    
653            private static Log _log = LogFactoryUtil.getLog(
654                    LayoutLocalServiceStagingAdvice.class);
655    
656            private static Set<String> _layoutLocalServiceStagingAdviceMethodNames =
657                    new HashSet<String>();
658    
659            static {
660                    _layoutLocalServiceStagingAdviceMethodNames.add("createLayout");
661                    _layoutLocalServiceStagingAdviceMethodNames.add("deleteLayout");
662                    _layoutLocalServiceStagingAdviceMethodNames.add("getLayouts");
663                    _layoutLocalServiceStagingAdviceMethodNames.add("updateLayout");
664                    _layoutLocalServiceStagingAdviceMethodNames.add("updateLookAndFeel");
665                    _layoutLocalServiceStagingAdviceMethodNames.add("updateName");
666            }
667    
668    }