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