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.exception.NoSuchLayoutException;
018    import com.liferay.portal.exception.RequiredLayoutException;
019    import com.liferay.portal.exception.SitemapChangeFrequencyException;
020    import com.liferay.portal.exception.SitemapIncludeException;
021    import com.liferay.portal.exception.SitemapPagePriorityException;
022    import com.liferay.portal.kernel.bean.BeanReference;
023    import com.liferay.portal.kernel.dao.orm.QueryUtil;
024    import com.liferay.portal.kernel.exception.PortalException;
025    import com.liferay.portal.kernel.exception.SystemException;
026    import com.liferay.portal.kernel.systemevent.SystemEvent;
027    import com.liferay.portal.kernel.systemevent.SystemEventHierarchyEntry;
028    import com.liferay.portal.kernel.systemevent.SystemEventHierarchyEntryThreadLocal;
029    import com.liferay.portal.kernel.util.ContentTypes;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.ListUtil;
032    import com.liferay.portal.kernel.util.LocaleUtil;
033    import com.liferay.portal.kernel.util.ParamUtil;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.kernel.util.UnicodeProperties;
038    import com.liferay.portal.kernel.util.Validator;
039    import com.liferay.portal.model.Group;
040    import com.liferay.portal.model.Layout;
041    import com.liferay.portal.model.LayoutConstants;
042    import com.liferay.portal.model.LayoutFriendlyURL;
043    import com.liferay.portal.model.LayoutPrototype;
044    import com.liferay.portal.model.LayoutReference;
045    import com.liferay.portal.model.LayoutSet;
046    import com.liferay.portal.model.LayoutSetPrototype;
047    import com.liferay.portal.model.LayoutType;
048    import com.liferay.portal.model.LayoutTypePortlet;
049    import com.liferay.portal.model.PortletConstants;
050    import com.liferay.portal.model.ResourceConstants;
051    import com.liferay.portal.model.ResourcePermission;
052    import com.liferay.portal.model.SystemEventConstants;
053    import com.liferay.portal.model.User;
054    import com.liferay.portal.model.UserGroup;
055    import com.liferay.portal.model.impl.VirtualLayout;
056    import com.liferay.portal.service.ServiceContext;
057    import com.liferay.portal.service.base.LayoutLocalServiceBaseImpl;
058    import com.liferay.portal.util.PortalUtil;
059    import com.liferay.portal.util.PropsValues;
060    import com.liferay.portal.util.comparator.LayoutComparator;
061    import com.liferay.portal.util.comparator.LayoutPriorityComparator;
062    import com.liferay.portlet.exportimport.lar.MissingReferences;
063    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
064    import com.liferay.sites.kernel.util.Sites;
065    import com.liferay.sites.kernel.util.SitesUtil;
066    
067    import java.io.File;
068    import java.io.InputStream;
069    
070    import java.util.ArrayList;
071    import java.util.Date;
072    import java.util.HashMap;
073    import java.util.HashSet;
074    import java.util.LinkedHashSet;
075    import java.util.List;
076    import java.util.Locale;
077    import java.util.Map;
078    import java.util.Set;
079    
080    /**
081     * Provides the local service for accessing, adding, deleting, exporting,
082     * importing, and updating layouts.
083     *
084     * @author Brian Wing Shun Chan
085     * @author Jorge Ferrer
086     * @author Joel Kozikowski
087     * @author Charles May
088     * @author Raymond Augé
089     * @author Jorge Ferrer
090     * @author Bruno Farache
091     * @author Vilmos Papp
092     * @author James Lefeu
093     * @author Tibor Lipusz
094     */
095    public class LayoutLocalServiceImpl extends LayoutLocalServiceBaseImpl {
096    
097            /**
098             * Returns the object counter's name.
099             *
100             * @param  groupId the primary key of the group
101             * @param  privateLayout whether layout is private to the group
102             * @return the object counter's name
103             */
104            public static String getCounterName(long groupId, boolean privateLayout) {
105                    StringBundler sb = new StringBundler(5);
106    
107                    sb.append(Layout.class.getName());
108                    sb.append(StringPool.POUND);
109                    sb.append(groupId);
110                    sb.append(StringPool.POUND);
111                    sb.append(privateLayout);
112    
113                    return sb.toString();
114            }
115    
116            /**
117             * Adds a layout with additional parameters.
118             *
119             * <p>
120             * This method handles the creation of the layout including its resources,
121             * metadata, and internal data structures. It is not necessary to make
122             * subsequent calls to any methods to setup default groups, resources, ...
123             * etc.
124             * </p>
125             *
126             * @param  userId the primary key of the user
127             * @param  groupId the primary key of the group
128             * @param  privateLayout whether the layout is private to the group
129             * @param  parentLayoutId the primary key of the parent layout (optionally
130             *         {@link LayoutConstants#DEFAULT_PARENT_LAYOUT_ID})
131             * @param  nameMap the layout's locales and localized names
132             * @param  titleMap the layout's locales and localized titles
133             * @param  descriptionMap the layout's locales and localized descriptions
134             * @param  keywordsMap the layout's locales and localized keywords
135             * @param  robotsMap the layout's locales and localized robots
136             * @param  type the layout's type (optionally {@link
137             *         LayoutConstants#TYPE_PORTLET}). The possible types can be found
138             *         in {@link LayoutConstants}.
139             * @param  typeSettings the settings to load the unicode properties object.
140             *         See {@link UnicodeProperties #fastLoad(String)}.
141             * @param  hidden whether the layout is hidden
142             * @param  friendlyURLMap the layout's locales and localized friendly URLs.
143             *         To see how the URL is normalized when accessed, see {@link
144             *         com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil#normalize(
145             *         String)}.
146             * @param  serviceContext the service context to be applied. Must set the
147             *         UUID for the layout. Can set the creation date, modification
148             *         date, and expando bridge attributes for the layout. For layouts
149             *         that belong to a layout set prototype, an attribute named
150             *         <code>layoutUpdateable</code> can be set to specify whether site
151             *         administrators can modify this page within their site. For
152             *         layouts that are created from a layout prototype, attributes
153             *         named <code>layoutPrototypeUuid</code> and
154             *         <code>layoutPrototypeLinkedEnabled</code> can be specified to
155             *         provide the unique identifier of the source prototype and a
156             *         boolean to determine whether a link to it should be enabled to
157             *         activate propagation of changes made to the linked page in the
158             *         prototype.
159             * @return the layout
160             */
161            @Override
162            public Layout addLayout(
163                            long userId, long groupId, boolean privateLayout,
164                            long parentLayoutId, Map<Locale, String> nameMap,
165                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
166                            Map<Locale, String> keywordsMap, Map<Locale, String> robotsMap,
167                            String type, String typeSettings, boolean hidden,
168                            Map<Locale, String> friendlyURLMap, ServiceContext serviceContext)
169                    throws PortalException {
170    
171                    // Layout
172    
173                    User user = userPersistence.findByPrimaryKey(userId);
174                    long layoutId = getNextLayoutId(groupId, privateLayout);
175                    parentLayoutId = layoutLocalServiceHelper.getParentLayoutId(
176                            groupId, privateLayout, parentLayoutId);
177                    String name = nameMap.get(LocaleUtil.getSiteDefault());
178                    friendlyURLMap = layoutLocalServiceHelper.getFriendlyURLMap(
179                            groupId, privateLayout, layoutId, name, friendlyURLMap);
180    
181                    String friendlyURL = friendlyURLMap.get(LocaleUtil.getSiteDefault());
182    
183                    int priority = layoutLocalServiceHelper.getNextPriority(
184                            groupId, privateLayout, parentLayoutId, null, -1);
185    
186                    layoutLocalServiceHelper.validate(
187                            groupId, privateLayout, layoutId, parentLayoutId, name, type,
188                            hidden, friendlyURLMap, serviceContext);
189    
190                    Date now = new Date();
191    
192                    long plid = counterLocalService.increment();
193    
194                    Layout layout = layoutPersistence.create(plid);
195    
196                    layout.setUuid(serviceContext.getUuid());
197                    layout.setGroupId(groupId);
198                    layout.setCompanyId(user.getCompanyId());
199                    layout.setUserId(user.getUserId());
200                    layout.setUserName(user.getFullName());
201                    layout.setCreateDate(serviceContext.getCreateDate(now));
202                    layout.setModifiedDate(serviceContext.getModifiedDate(now));
203                    layout.setPrivateLayout(privateLayout);
204                    layout.setLayoutId(layoutId);
205                    layout.setParentLayoutId(parentLayoutId);
206                    layout.setNameMap(nameMap);
207                    layout.setTitleMap(titleMap);
208                    layout.setDescriptionMap(descriptionMap);
209                    layout.setKeywordsMap(keywordsMap);
210                    layout.setRobotsMap(robotsMap);
211                    layout.setType(type);
212                    layout.setHidden(hidden);
213                    layout.setFriendlyURL(friendlyURL);
214                    layout.setPriority(priority);
215    
216                    boolean layoutUpdateable = ParamUtil.getBoolean(
217                            serviceContext, Sites.LAYOUT_UPDATEABLE, true);
218    
219                    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
220    
221                    typeSettingsProperties.fastLoad(typeSettings);
222    
223                    if (!layoutUpdateable) {
224                            typeSettingsProperties.put(
225                                    Sites.LAYOUT_UPDATEABLE, String.valueOf(layoutUpdateable));
226                    }
227    
228                    if (privateLayout) {
229                            typeSettingsProperties.put(
230                                    "privateLayout", String.valueOf(privateLayout));
231                    }
232    
233                    validateTypeSettingsProperties(typeSettingsProperties);
234    
235                    layout.setTypeSettingsProperties(typeSettingsProperties);
236    
237                    String layoutPrototypeUuid = ParamUtil.getString(
238                            serviceContext, "layoutPrototypeUuid");
239                    boolean layoutPrototypeLinkEnabled = ParamUtil.getBoolean(
240                            serviceContext, "layoutPrototypeLinkEnabled",
241                            PropsValues.LAYOUT_PROTOTYPE_LINK_ENABLED_DEFAULT);
242    
243                    if (Validator.isNotNull(layoutPrototypeUuid)) {
244                            layout.setLayoutPrototypeUuid(layoutPrototypeUuid);
245                            layout.setLayoutPrototypeLinkEnabled(layoutPrototypeLinkEnabled);
246                    }
247    
248                    if (type.equals(LayoutConstants.TYPE_PORTLET)) {
249                            LayoutTypePortlet layoutTypePortlet =
250                                    (LayoutTypePortlet)layout.getLayoutType();
251    
252                            if (Validator.isNull(layoutTypePortlet.getLayoutTemplateId())) {
253                                    layoutTypePortlet.setLayoutTemplateId(
254                                            0, PropsValues.LAYOUT_DEFAULT_TEMPLATE_ID, false);
255                            }
256                    }
257    
258                    layout.setExpandoBridgeAttributes(serviceContext);
259    
260                    layoutPersistence.update(layout);
261    
262                    // Layout friendly URLs
263    
264                    layoutFriendlyURLLocalService.updateLayoutFriendlyURLs(
265                            user.getUserId(), user.getCompanyId(), groupId, plid, privateLayout,
266                            friendlyURLMap, serviceContext);
267    
268                    // Layout prototype
269    
270                    if (Validator.isNotNull(layoutPrototypeUuid) &&
271                            !layoutPrototypeLinkEnabled) {
272    
273                            LayoutPrototype layoutPrototype =
274                                    layoutPrototypeLocalService.
275                                            getLayoutPrototypeByUuidAndCompanyId(
276                                                    layoutPrototypeUuid, layout.getCompanyId());
277    
278                            try {
279                                    SitesUtil.applyLayoutPrototype(
280                                            layoutPrototype, layout, layoutPrototypeLinkEnabled);
281                            }
282                            catch (PortalException pe) {
283                                    throw pe;
284                            }
285                            catch (SystemException se) {
286                                    throw se;
287                            }
288                            catch (Exception e) {
289                                    throw new SystemException(e);
290                            }
291                    }
292    
293                    // Resources
294    
295                    boolean addGroupPermissions = true;
296    
297                    Group group = groupLocalService.getGroup(groupId);
298    
299                    if (privateLayout && (group.isUser() || group.isUserGroup())) {
300                            addGroupPermissions = false;
301                    }
302    
303                    boolean addGuestPermissions = false;
304    
305                    if (!privateLayout || type.equals(LayoutConstants.TYPE_CONTROL_PANEL) ||
306                            group.isLayoutSetPrototype()) {
307    
308                            addGuestPermissions = true;
309                    }
310    
311                    resourceLocalService.addResources(
312                            user.getCompanyId(), groupId, user.getUserId(),
313                            Layout.class.getName(), layout.getPlid(), false,
314                            addGroupPermissions, addGuestPermissions);
315    
316                    // Group
317    
318                    groupLocalService.updateSite(groupId, true);
319    
320                    // Layout set
321    
322                    layoutSetLocalService.updatePageCount(groupId, privateLayout);
323    
324                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
325                            groupId, privateLayout);
326    
327                    layout.setLayoutSet(layoutSet);
328    
329                    // Asset
330    
331                    updateAsset(
332                            userId, layout, serviceContext.getAssetCategoryIds(),
333                            serviceContext.getAssetTagNames());
334    
335                    return layout;
336            }
337    
338            /**
339             * Adds a layout with single entry maps for name, title, and description to
340             * the default locale.
341             *
342             * <p>
343             * This method handles the creation of the layout including its resources,
344             * metadata, and internal data structures. It is not necessary to make
345             * subsequent calls to any methods to setup default groups, resources, ...
346             * etc.
347             * </p>
348             *
349             * @param  userId the primary key of the user
350             * @param  groupId the primary key of the group
351             * @param  privateLayout whether the layout is private to the group
352             * @param  parentLayoutId the primary key of the parent layout (optionally
353             *         {@link LayoutConstants#DEFAULT_PARENT_LAYOUT_ID}). The possible
354             *         values can be found in {@link LayoutConstants}.
355             * @param  name the layout's name (optionally {@link
356             *         PropsValues#DEFAULT_USER_PRIVATE_LAYOUT_NAME} or {@link
357             *         PropsValues#DEFAULT_USER_PUBLIC_LAYOUT_NAME}). The default values
358             *         can be overridden in <code>portal-ext.properties</code> by
359             *         specifying new values for the corresponding properties defined in
360             *         {@link PropsValues}
361             * @param  title the layout's title
362             * @param  description the layout's description
363             * @param  type the layout's type (optionally {@link
364             *         LayoutConstants#TYPE_PORTLET}). The possible types can be found
365             *         in {@link LayoutConstants}.
366             * @param  hidden whether the layout is hidden
367             * @param  friendlyURL the friendly URL of the layout (optionally {@link
368             *         PropsValues#DEFAULT_USER_PRIVATE_LAYOUT_FRIENDLY_URL} or {@link
369             *         PropsValues#DEFAULT_USER_PUBLIC_LAYOUT_FRIENDLY_URL}). The
370             *         default values can be overridden in
371             *         <code>portal-ext.properties</code> by specifying new values for
372             *         the corresponding properties defined in {@link PropsValues}. To
373             *         see how the URL is normalized when accessed, see {@link
374             *         com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil#normalize(
375             *         String)}.
376             * @param  serviceContext the service context to be applied. Must set the
377             *         UUID for the layout. Can set the creation date and modification
378             *         date for the layout. For layouts that belong to a layout set
379             *         prototype, an attribute named <code>layoutUpdateable</code> can
380             *         be set to specify whether site administrators can modify this
381             *         page within their site.
382             * @return the layout
383             */
384            @Override
385            public Layout addLayout(
386                            long userId, long groupId, boolean privateLayout,
387                            long parentLayoutId, String name, String title, String description,
388                            String type, boolean hidden, String friendlyURL,
389                            ServiceContext serviceContext)
390                    throws PortalException {
391    
392                    Locale locale = LocaleUtil.getSiteDefault();
393    
394                    Map<Locale, String> nameMap = new HashMap<>();
395    
396                    nameMap.put(locale, name);
397    
398                    Map<Locale, String> titleMap = new HashMap<>();
399    
400                    titleMap.put(locale, title);
401    
402                    Map<Locale, String> descriptionMap = new HashMap<>();
403    
404                    descriptionMap.put(locale, description);
405    
406                    Map<Locale, String> friendlyURLMap = new HashMap<>();
407    
408                    friendlyURLMap.put(LocaleUtil.getSiteDefault(), friendlyURL);
409    
410                    return addLayout(
411                            userId, groupId, privateLayout, parentLayoutId, nameMap, titleMap,
412                            descriptionMap, new HashMap<Locale, String>(),
413                            new HashMap<Locale, String>(), type, StringPool.BLANK, hidden,
414                            friendlyURLMap, serviceContext);
415            }
416    
417            /**
418             * Deletes the layout, its child layouts, and its associated resources.
419             *
420             * @param layout the layout
421             * @param updateLayoutSet whether the layout set's page counter needs to be
422             *        updated
423             * @param serviceContext the service context to be applied
424             */
425            @Override
426            @SystemEvent(
427                    action = SystemEventConstants.ACTION_SKIP,
428                    type = SystemEventConstants.TYPE_DELETE
429            )
430            public void deleteLayout(
431                            Layout layout, boolean updateLayoutSet,
432                            ServiceContext serviceContext)
433                    throws PortalException {
434    
435                    // First layout validation
436    
437                    if (layout.getParentLayoutId() ==
438                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
439    
440                            List<Layout> rootLayouts = layoutPersistence.findByG_P_P(
441                                    layout.getGroupId(), layout.isPrivateLayout(),
442                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0, 2);
443    
444                            if (rootLayouts.size() > 1) {
445                                    Layout firstLayout = rootLayouts.get(0);
446    
447                                    if (firstLayout.getLayoutId() == layout.getLayoutId()) {
448                                            Layout secondLayout = rootLayouts.get(1);
449    
450                                            layoutLocalServiceHelper.validateFirstLayout(secondLayout);
451                                    }
452                            }
453                    }
454    
455                    // Child layouts
456    
457                    List<Layout> childLayouts = layoutPersistence.findByG_P_P(
458                            layout.getGroupId(), layout.isPrivateLayout(),
459                            layout.getLayoutId());
460    
461                    for (Layout childLayout : childLayouts) {
462                            layoutLocalService.deleteLayout(
463                                    childLayout, updateLayoutSet, serviceContext);
464                    }
465    
466                    // Layout friendly URLs
467    
468                    layoutFriendlyURLLocalService.deleteLayoutFriendlyURLs(
469                            layout.getPlid());
470    
471                    // Portlet preferences
472    
473                    portletPreferencesLocalService.deletePortletPreferencesByPlid(
474                            layout.getPlid());
475    
476                    // Subscriptions
477    
478                    subscriptionLocalService.deleteSubscriptions(
479                            layout.getCompanyId(), Layout.class.getName(), layout.getPlid());
480    
481                    // Asset
482    
483                    assetEntryLocalService.deleteEntry(
484                            Layout.class.getName(), layout.getPlid());
485    
486                    // Ratings
487    
488                    ratingsStatsLocalService.deleteStats(
489                            Layout.class.getName(), layout.getPlid());
490    
491                    // Expando
492    
493                    expandoRowLocalService.deleteRows(layout.getPlid());
494    
495                    // Icon
496    
497                    imageLocalService.deleteImage(layout.getIconImageId());
498    
499                    // Scope group
500    
501                    Group scopeGroup = layout.getScopeGroup();
502    
503                    if (scopeGroup != null) {
504                            groupLocalService.deleteGroup(scopeGroup.getGroupId());
505                    }
506    
507                    // Resources
508    
509                    String primKey =
510                            layout.getPlid() + PortletConstants.LAYOUT_SEPARATOR + "%";
511    
512                    List<ResourcePermission> resourcePermissions =
513                            resourcePermissionPersistence.findByC_LikeP(
514                                    layout.getCompanyId(), primKey);
515    
516                    for (ResourcePermission resourcePermission : resourcePermissions) {
517                            resourcePermissionLocalService.deleteResourcePermission(
518                                    resourcePermission);
519                    }
520    
521                    resourceLocalService.deleteResource(
522                            layout.getCompanyId(), Layout.class.getName(),
523                            ResourceConstants.SCOPE_INDIVIDUAL, layout.getPlid());
524    
525                    // Layout
526    
527                    layoutPersistence.remove(layout);
528    
529                    // Layout set
530    
531                    if (updateLayoutSet) {
532                            layoutSetLocalService.updatePageCount(
533                                    layout.getGroupId(), layout.isPrivateLayout());
534                    }
535    
536                    // System event
537    
538                    SystemEventHierarchyEntry systemEventHierarchyEntry =
539                            SystemEventHierarchyEntryThreadLocal.peek();
540    
541                    if ((systemEventHierarchyEntry != null) &&
542                            systemEventHierarchyEntry.hasTypedModel(
543                                    Layout.class.getName(), layout.getPlid())) {
544    
545                            systemEventHierarchyEntry.setExtraDataValue(
546                                    "privateLayout", StringUtil.valueOf(layout.isPrivateLayout()));
547                    }
548            }
549    
550            /**
551             * Deletes the layout with the primary key, also deleting the layout's child
552             * layouts, and associated resources.
553             *
554             * @param groupId the primary key of the group
555             * @param privateLayout whether the layout is private to the group
556             * @param layoutId the primary key of the layout
557             * @param serviceContext the service context to be applied
558             */
559            @Override
560            public void deleteLayout(
561                            long groupId, boolean privateLayout, long layoutId,
562                            ServiceContext serviceContext)
563                    throws PortalException {
564    
565                    Layout layout = layoutPersistence.findByG_P_L(
566                            groupId, privateLayout, layoutId);
567    
568                    layoutLocalService.deleteLayout(layout, true, serviceContext);
569            }
570    
571            /**
572             * Deletes the layout with the plid, also deleting the layout's child
573             * layouts, and associated resources.
574             *
575             * @param plid the primary key of the layout
576             * @param serviceContext the service context to be applied
577             */
578            @Override
579            public void deleteLayout(long plid, ServiceContext serviceContext)
580                    throws PortalException {
581    
582                    Layout layout = layoutPersistence.findByPrimaryKey(plid);
583    
584                    layoutLocalService.deleteLayout(layout, true, serviceContext);
585            }
586    
587            /**
588             * Deletes the group's private or non-private layouts, also deleting the
589             * layouts' child layouts, and associated resources.
590             *
591             * @param groupId the primary key of the group
592             * @param privateLayout whether the layout is private to the group
593             * @param serviceContext the service context to be applied. The parent
594             *        layout set's page count will be updated by default, unless an
595             *        attribute named <code>updatePageCount</code> is set to
596             *        <code>false</code>.
597             */
598            @Override
599            public void deleteLayouts(
600                            long groupId, boolean privateLayout, ServiceContext serviceContext)
601                    throws PortalException {
602    
603                    // Layouts
604    
605                    List<Layout> layouts = layoutPersistence.findByG_P_P(
606                            groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
607                            QueryUtil.ALL_POS, QueryUtil.ALL_POS,
608                            new LayoutPriorityComparator(false));
609    
610                    for (Layout layout : layouts) {
611                            try {
612                                    layoutLocalService.deleteLayout(layout, false, serviceContext);
613                            }
614                            catch (NoSuchLayoutException nsle) {
615                            }
616                    }
617    
618                    // Layout set
619    
620                    if (GetterUtil.getBoolean(
621                                    serviceContext.getAttribute("updatePageCount"), true)) {
622    
623                            layoutSetLocalService.updatePageCount(groupId, privateLayout);
624                    }
625    
626                    // Counter
627    
628                    counterLocalService.reset(getCounterName(groupId, privateLayout));
629            }
630    
631            /**
632             * Exports layouts with the primary keys and criteria as a byte array.
633             *
634             * @param      groupId the primary key of the group
635             * @param      privateLayout whether the layout is private to the group
636             * @param      layoutIds the primary keys of the layouts to be exported
637             * @param      parameterMap the mapping of parameters indicating which
638             *             information to export. For information on the keys used in
639             *             the map see {@link
640             *             com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys}.
641             * @param      startDate the export's start date
642             * @param      endDate the export's end date
643             * @return     the layouts as a byte array
644             * @throws     PortalException
645             * @deprecated As of 7.0.0, with no direct replacement
646             */
647            @Deprecated
648            @Override
649            public byte[] exportLayouts(
650                            long groupId, boolean privateLayout, long[] layoutIds,
651                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
652                    throws PortalException {
653    
654                    throw new UnsupportedOperationException();
655            }
656    
657            /**
658             * Exports all layouts that match the criteria as a byte array.
659             *
660             * @param      groupId the primary key of the group
661             * @param      privateLayout whether the layout is private to the group
662             * @param      parameterMap the mapping of parameters indicating which
663             *             information to export. For information on the keys used in
664             *             the map see {@link
665             *             com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys}.
666             * @param      startDate the export's start date
667             * @param      endDate the export's end date
668             * @return     the layout as a byte array
669             * @throws     PortalException
670             * @deprecated As of 7.0.0, with no direct replacement
671             */
672            @Deprecated
673            @Override
674            public byte[] exportLayouts(
675                            long groupId, boolean privateLayout,
676                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
677                    throws PortalException {
678    
679                    throw new UnsupportedOperationException();
680            }
681    
682            /**
683             * @throws     PortalException
684             * @deprecated As of 7.0.0, replaced by {@link
685             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#exportLayoutsAsFile(
686             *             ExportImportConfiguration)}
687             */
688            @Deprecated
689            @Override
690            public File exportLayoutsAsFile(
691                            ExportImportConfiguration exportImportConfiguration)
692                    throws PortalException {
693    
694                    throw new UnsupportedOperationException();
695            }
696    
697            /**
698             * Exports the layouts that match the primary keys and criteria as a file.
699             *
700             * @param      groupId the primary key of the group
701             * @param      privateLayout whether the layout is private to the group
702             * @param      layoutIds the primary keys of the layouts to be exported
703             *             (optionally <code>null</code>)
704             * @param      parameterMap the mapping of parameters indicating which
705             *             information to export. For information on the keys used in
706             *             the map see {@link
707             *             com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys}.
708             * @param      startDate the export's start date
709             * @param      endDate the export's end date
710             * @return     the layouts as a File
711             * @throws     PortalException
712             * @deprecated As of 7.0.0, with no direct replacement
713             */
714            @Deprecated
715            @Override
716            public File exportLayoutsAsFile(
717                            long groupId, boolean privateLayout, long[] layoutIds,
718                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
719                    throws PortalException {
720    
721                    throw new UnsupportedOperationException();
722            }
723    
724            /**
725             * @throws     PortalException
726             * @deprecated As of 7.0.0, replaced by {@link
727             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#exportLayoutsAsFileInBackground(
728             *             long, ExportImportConfiguration)}
729             */
730            @Deprecated
731            @Override
732            public long exportLayoutsAsFileInBackground(
733                            long userId, ExportImportConfiguration exportImportConfiguration)
734                    throws PortalException {
735    
736                    throw new UnsupportedOperationException();
737            }
738    
739            /**
740             * @throws     PortalException
741             * @deprecated As of 7.0.0, replaced by {@link
742             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#exportLayoutsAsFileInBackground(
743             *             long, long)}
744             */
745            @Deprecated
746            @Override
747            public long exportLayoutsAsFileInBackground(
748                            long userId, long exportImportConfigurationId)
749                    throws PortalException {
750    
751                    throw new UnsupportedOperationException();
752            }
753    
754            /**
755             * @throws     PortalException
756             * @deprecated As of 7.0.0, with no direct replacement
757             */
758            @Deprecated
759            @Override
760            public long exportLayoutsAsFileInBackground(
761                            long userId, String taskName, long groupId, boolean privateLayout,
762                            long[] layoutIds, Map<String, String[]> parameterMap,
763                            Date startDate, Date endDate)
764                    throws PortalException {
765    
766                    throw new UnsupportedOperationException();
767            }
768    
769            /**
770             * @throws     PortalException
771             * @deprecated As of 7.0.0, with no direct replacement
772             */
773            @Deprecated
774            @Override
775            public long exportLayoutsAsFileInBackground(
776                            long userId, String taskName, long groupId, boolean privateLayout,
777                            long[] layoutIds, Map<String, String[]> parameterMap,
778                            Date startDate, Date endDate, String fileName)
779                    throws PortalException {
780    
781                    throw new UnsupportedOperationException();
782            }
783    
784            /**
785             * Exports the portlet information (categories, permissions, ... etc.) as a
786             * byte array.
787             *
788             * @param      plid the primary key of the layout
789             * @param      groupId the primary key of the group
790             * @param      portletId the primary key of the portlet
791             * @param      parameterMap the mapping of parameters indicating which
792             *             information to export. For information on the keys used in
793             *             the map see {@link
794             *             com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys}.
795             * @param      startDate the export's start date
796             * @param      endDate the export's end date
797             * @return     the portlet information as a byte array
798             * @throws     PortalException
799             * @deprecated As of 7.0.0, with no direct replacement
800             */
801            @Deprecated
802            @Override
803            public byte[] exportPortletInfo(
804                            long plid, long groupId, String portletId,
805                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
806                    throws PortalException {
807    
808                    throw new UnsupportedOperationException();
809            }
810    
811            /**
812             * @throws     PortalException
813             * @deprecated As of 7.0.0, with no direct replacement
814             */
815            @Deprecated
816            @Override
817            public byte[] exportPortletInfo(
818                            long companyId, String portletId,
819                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
820                    throws PortalException {
821    
822                    throw new UnsupportedOperationException();
823            }
824    
825            /**
826             * @throws     PortalException
827             * @deprecated As of 7.0.0, replaced by {@link
828             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#exportPortletInfoAsFile(
829             *             ExportImportConfiguration)}}
830             */
831            @Deprecated
832            @Override
833            public File exportPortletInfoAsFile(
834                            ExportImportConfiguration exportImportConfiguration)
835                    throws PortalException {
836    
837                    throw new UnsupportedOperationException();
838            }
839    
840            /**
841             * Exports the portlet information (categories, permissions, ... etc.) as a
842             * file.
843             *
844             * @param      plid the primary key of the layout
845             * @param      groupId the primary key of the group
846             * @param      portletId the primary key of the portlet
847             * @param      parameterMap the mapping of parameters indicating which
848             *             information to export. For information on the keys used in
849             *             the map see {@link
850             *             com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys}.
851             * @param      startDate the export's start date
852             * @param      endDate the export's end date
853             * @return     the portlet information as a file
854             * @throws     PortalException
855             * @deprecated As of 7.0.0, with no direct replacement
856             */
857            @Deprecated
858            @Override
859            public File exportPortletInfoAsFile(
860                            long plid, long groupId, String portletId,
861                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
862                    throws PortalException {
863    
864                    throw new UnsupportedOperationException();
865            }
866    
867            /**
868             * @throws     PortalException
869             * @deprecated As of 7.0.0, with no direct replacement
870             */
871            @Deprecated
872            @Override
873            public File exportPortletInfoAsFile(
874                            long companyId, String portletId,
875                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
876                    throws PortalException {
877    
878                    throw new UnsupportedOperationException();
879            }
880    
881            /**
882             * @throws     PortalException
883             * @deprecated As of 7.0.0, replaced by {@link
884             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#exportPortletInfoAsFileInBackground(
885             *             long, ExportImportConfiguration)}}
886             */
887            @Deprecated
888            @Override
889            public long exportPortletInfoAsFileInBackground(
890                            long userId, ExportImportConfiguration exportImportConfiguration)
891                    throws PortalException {
892    
893                    throw new UnsupportedOperationException();
894            }
895    
896            /**
897             * @throws     PortalException
898             * @deprecated As of 7.0.0, replaced by {@link
899             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#exportPortletInfoAsFileInBackground(
900             *             long, long)}}
901             */
902            @Deprecated
903            @Override
904            public long exportPortletInfoAsFileInBackground(
905                            long userId, long exportImportConfigurationId)
906                    throws PortalException {
907    
908                    throw new UnsupportedOperationException();
909            }
910    
911            /**
912             * @throws     PortalException
913             * @deprecated As of 7.0.0, with no direct replacement
914             */
915            @Deprecated
916            @Override
917            public long exportPortletInfoAsFileInBackground(
918                            long userId, String taskName, long plid, long groupId,
919                            String portletId, Map<String, String[]> parameterMap,
920                            Date startDate, Date endDate, String fileName)
921                    throws PortalException {
922    
923                    throw new UnsupportedOperationException();
924            }
925    
926            /**
927             * @throws     PortalException
928             * @deprecated As of 7.0.0, with no direct replacement
929             */
930            @Deprecated
931            @Override
932            public long exportPortletInfoAsFileInBackground(
933                            long userId, String taskName, String portletId,
934                            Map<String, String[]> parameterMap, Date startDate, Date endDate,
935                            String fileName)
936                    throws PortalException {
937    
938                    throw new UnsupportedOperationException();
939            }
940    
941            @Override
942            public Layout fetchFirstLayout(
943                    long groupId, boolean privateLayout, long parentLayoutId) {
944    
945                    return layoutPersistence.fetchByG_P_P_First(
946                            groupId, privateLayout, parentLayoutId,
947                            new LayoutPriorityComparator());
948            }
949    
950            @Override
951            public Layout fetchLayout(
952                    long groupId, boolean privateLayout, long layoutId) {
953    
954                    return layoutPersistence.fetchByG_P_L(groupId, privateLayout, layoutId);
955            }
956    
957            @Override
958            public Layout fetchLayoutByFriendlyURL(
959                    long groupId, boolean privateLayout, String friendlyURL) {
960    
961                    return layoutPersistence.fetchByG_P_F(
962                            groupId, privateLayout, friendlyURL);
963            }
964    
965            /**
966             * Returns the primary key of the default layout for the group
967             *
968             * @param  groupId the primary key of the group
969             * @return the primary key of the default layout for the group (optionally
970             *         {@link LayoutConstants#DEFAULT_PLID})
971             */
972            @Override
973            public long getDefaultPlid(long groupId) {
974                    if (groupId > 0) {
975                            List<Layout> layouts = layoutPersistence.findByGroupId(
976                                    groupId, 0, 1);
977    
978                            if (!layouts.isEmpty()) {
979                                    Layout layout = layouts.get(0);
980    
981                                    return layout.getPlid();
982                            }
983                    }
984    
985                    return LayoutConstants.DEFAULT_PLID;
986            }
987    
988            /**
989             * Returns primary key of the matching default layout for the group
990             *
991             * @param  groupId the primary key of the group
992             * @param  privateLayout whether the layout is private to the group
993             * @return the primary key of the default layout for the group; {@link
994             *         LayoutConstants#DEFAULT_PLID}) otherwise
995             */
996            @Override
997            public long getDefaultPlid(long groupId, boolean privateLayout) {
998                    if (groupId > 0) {
999                            List<Layout> layouts = layoutPersistence.findByG_P(
1000                                    groupId, privateLayout, 0, 1);
1001    
1002                            if (!layouts.isEmpty()) {
1003                                    Layout layout = layouts.get(0);
1004    
1005                                    return layout.getPlid();
1006                            }
1007                    }
1008    
1009                    return LayoutConstants.DEFAULT_PLID;
1010            }
1011    
1012            /**
1013             * Returns primary key of the default portlet layout for the group
1014             *
1015             * @param  groupId the primary key of the group
1016             * @param  privateLayout whether the layout is private to the group
1017             * @param  portletId the primary key of the portlet
1018             * @return the primary key of the default portlet layout for the group;
1019             *         {@link LayoutConstants#DEFAULT_PLID} otherwise
1020             * @throws PortalException
1021             */
1022            @Override
1023            public long getDefaultPlid(
1024                            long groupId, boolean privateLayout, String portletId)
1025                    throws PortalException {
1026    
1027                    if (groupId > 0) {
1028                            List<Layout> layouts = layoutPersistence.findByG_P(
1029                                    groupId, privateLayout);
1030    
1031                            for (Layout layout : layouts) {
1032                                    if (layout.isTypePortlet()) {
1033                                            LayoutTypePortlet layoutTypePortlet =
1034                                                    (LayoutTypePortlet)layout.getLayoutType();
1035    
1036                                            if (layoutTypePortlet.hasPortletId(portletId)) {
1037                                                    return layout.getPlid();
1038                                            }
1039                                    }
1040                            }
1041                    }
1042    
1043                    return LayoutConstants.DEFAULT_PLID;
1044            }
1045    
1046            /**
1047             * Returns the layout for the friendly URL
1048             *
1049             * @param  groupId the primary key of the group
1050             * @param  privateLayout whether the layout is private to the group
1051             * @param  friendlyURL the friendly URL of the layout
1052             * @return the layout for the friendly URL
1053             */
1054            @Override
1055            public Layout getFriendlyURLLayout(
1056                            long groupId, boolean privateLayout, String friendlyURL)
1057                    throws PortalException {
1058    
1059                    if (Validator.isNull(friendlyURL)) {
1060                            StringBundler sb = new StringBundler(5);
1061    
1062                            sb.append("{groupId=");
1063                            sb.append(groupId);
1064                            sb.append(", privateLayout=");
1065                            sb.append(privateLayout);
1066                            sb.append("}");
1067    
1068                            throw new NoSuchLayoutException(sb.toString());
1069                    }
1070    
1071                    friendlyURL = layoutLocalServiceHelper.getFriendlyURL(friendlyURL);
1072    
1073                    Layout layout = null;
1074    
1075                    List<LayoutFriendlyURL> layoutFriendlyURLs =
1076                            layoutFriendlyURLPersistence.findByG_P_F(
1077                                    groupId, privateLayout, friendlyURL, 0, 1);
1078    
1079                    if (!layoutFriendlyURLs.isEmpty()) {
1080                            LayoutFriendlyURL layoutFriendlyURL = layoutFriendlyURLs.get(0);
1081    
1082                            layout = layoutPersistence.findByPrimaryKey(
1083                                    layoutFriendlyURL.getPlid());
1084                    }
1085    
1086                    if ((layout == null) && friendlyURL.startsWith(StringPool.SLASH) &&
1087                            Validator.isNumber(friendlyURL.substring(1))) {
1088    
1089                            long layoutId = GetterUtil.getLong(friendlyURL.substring(1));
1090    
1091                            layout = layoutPersistence.fetchByG_P_L(
1092                                    groupId, privateLayout, layoutId);
1093                    }
1094    
1095                    if (layout == null) {
1096                            StringBundler sb = new StringBundler(7);
1097    
1098                            sb.append("{groupId=");
1099                            sb.append(groupId);
1100                            sb.append(", privateLayout=");
1101                            sb.append(privateLayout);
1102                            sb.append(", friendlyURL=");
1103                            sb.append(friendlyURL);
1104                            sb.append("}");
1105    
1106                            throw new NoSuchLayoutException(sb.toString());
1107                    }
1108    
1109                    return layout;
1110            }
1111    
1112            /**
1113             * Returns the layout matching the primary key, group, and privacy; throws a
1114             * {@link NoSuchLayoutException} otherwise.
1115             *
1116             * @param  groupId the primary key of the group
1117             * @param  privateLayout whether the layout is private to the group
1118             * @param  layoutId the primary key of the layout
1119             * @return the matching layout
1120             */
1121            @Override
1122            public Layout getLayout(long groupId, boolean privateLayout, long layoutId)
1123                    throws PortalException {
1124    
1125                    return layoutPersistence.findByG_P_L(groupId, privateLayout, layoutId);
1126            }
1127    
1128            /**
1129             * Returns the layout for the icon image; throws a {@link
1130             * NoSuchLayoutException} otherwise.
1131             *
1132             * @param  iconImageId the primary key of the icon image
1133             * @return Returns the layout for the icon image
1134             */
1135            @Override
1136            public Layout getLayoutByIconImageId(long iconImageId)
1137                    throws PortalException {
1138    
1139                    return layoutPersistence.findByIconImageId(iconImageId);
1140            }
1141    
1142            /**
1143             * Returns all the layouts belonging to the group.
1144             *
1145             * @param  groupId the primary key of the group
1146             * @param  privateLayout whether the layout is private to the group
1147             * @return the matching layouts, or <code>null</code> if no matches were
1148             *         found
1149             */
1150            @Override
1151            public List<Layout> getLayouts(long groupId, boolean privateLayout) {
1152                    return layoutPersistence.findByG_P(groupId, privateLayout);
1153            }
1154    
1155            /**
1156             * Returns all the layouts belonging to the group that are children of the
1157             * parent layout.
1158             *
1159             * @param  groupId the primary key of the group
1160             * @param  privateLayout whether the layout is private to the group
1161             * @param  parentLayoutId the primary key of the parent layout
1162             * @return the matching layouts, or <code>null</code> if no matches were
1163             *         found
1164             */
1165            @Override
1166            public List<Layout> getLayouts(
1167                    long groupId, boolean privateLayout, long parentLayoutId) {
1168    
1169                    return layoutPersistence.findByG_P_P(
1170                            groupId, privateLayout, parentLayoutId);
1171            }
1172    
1173            /**
1174             * Returns a range of all the layouts belonging to the group that are
1175             * children of the parent layout.
1176             *
1177             * <p>
1178             * Useful when paginating results. Returns a maximum of <code>end -
1179             * start</code> instances. <code>start</code> and <code>end</code> are not
1180             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1181             * refers to the first result in the set. Setting both <code>start</code>
1182             * and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full
1183             * result set.
1184             * </p>
1185             *
1186             * @param  groupId the primary key of the group
1187             * @param  privateLayout whether the layout is private to the group
1188             * @param  parentLayoutId the primary key of the parent layout
1189             * @param  incomplete whether the layout is incomplete
1190             * @param  start the lower bound of the range of layouts
1191             * @param  end the upper bound of the range of layouts (not inclusive)
1192             * @return the matching layouts, or <code>null</code> if no matches were
1193             *         found
1194             */
1195            @Override
1196            public List<Layout> getLayouts(
1197                    long groupId, boolean privateLayout, long parentLayoutId,
1198                    boolean incomplete, int start, int end) {
1199    
1200                    return layoutPersistence.findByG_P_P(
1201                            groupId, privateLayout, parentLayoutId, start, end);
1202            }
1203    
1204            /**
1205             * Returns all the layouts that match the layout IDs and belong to the
1206             * group.
1207             *
1208             * @param  groupId the primary key of the group
1209             * @param  privateLayout whether the layout is private to the group
1210             * @param  layoutIds the primary keys of the layouts
1211             * @return the matching layouts, or an empty list if no matches were found
1212             */
1213            @Override
1214            public List<Layout> getLayouts(
1215                            long groupId, boolean privateLayout, long[] layoutIds)
1216                    throws PortalException {
1217    
1218                    List<Layout> layouts = new ArrayList<>();
1219    
1220                    for (long layoutId : layoutIds) {
1221                            Layout layout = getLayout(groupId, privateLayout, layoutId);
1222    
1223                            layouts.add(layout);
1224                    }
1225    
1226                    return layouts;
1227            }
1228    
1229            /**
1230             * Returns all the layouts that match the type and belong to the group.
1231             *
1232             * @param  groupId the primary key of the group
1233             * @param  privateLayout whether the layout is private to the group
1234             * @param  type the type of the layouts (optionally {@link
1235             *         LayoutConstants#TYPE_PORTLET})
1236             * @return the matching layouts, or <code>null</code> if no matches were
1237             *         found
1238             */
1239            @Override
1240            public List<Layout> getLayouts(
1241                    long groupId, boolean privateLayout, String type) {
1242    
1243                    return layoutPersistence.findByG_P_T(groupId, privateLayout, type);
1244            }
1245    
1246            /**
1247             * Returns the layout references for all the layouts that belong to the
1248             * company and belong to the portlet that matches the preferences.
1249             *
1250             * @param  companyId the primary key of the company
1251             * @param  portletId the primary key of the portlet
1252             * @param  preferencesKey the portlet's preference key
1253             * @param  preferencesValue the portlet's preference value
1254             * @return the layout references of the matching layouts
1255             */
1256            @Override
1257            public LayoutReference[] getLayouts(
1258                    long companyId, String portletId, String preferencesKey,
1259                    String preferencesValue) {
1260    
1261                    List<LayoutReference> layoutReferences = layoutFinder.findByC_P_P(
1262                            companyId, portletId, preferencesKey, preferencesValue);
1263    
1264                    return layoutReferences.toArray(
1265                            new LayoutReference[layoutReferences.size()]);
1266            }
1267    
1268            @Override
1269            public List<Layout> getLayoutsByLayoutPrototypeUuid(
1270                    String layoutPrototypeUuid) {
1271    
1272                    return layoutPersistence.findByLayoutPrototypeUuid(layoutPrototypeUuid);
1273            }
1274    
1275            @Override
1276            public int getLayoutsByLayoutPrototypeUuidCount(
1277                    String layoutPrototypeUuid) {
1278    
1279                    return layoutPersistence.countByLayoutPrototypeUuid(
1280                            layoutPrototypeUuid);
1281            }
1282    
1283            @Override
1284            public int getLayoutsCount(Group group, boolean privateLayout)
1285                    throws PortalException {
1286    
1287                    return getLayoutsCount(group, privateLayout, true);
1288            }
1289    
1290            @Override
1291            public int getLayoutsCount(
1292                            Group group, boolean privateLayout, boolean includeUserGroups)
1293                    throws PortalException {
1294    
1295                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
1296                            group.getGroupId(), privateLayout);
1297    
1298                    int count = layoutSet.getPageCount();
1299    
1300                    if (!group.isUser() || !includeUserGroups) {
1301                            return count;
1302                    }
1303    
1304                    long[] userGroupIds = userPersistence.getUserGroupPrimaryKeys(
1305                            group.getClassPK());
1306    
1307                    if (userGroupIds.length != 0) {
1308                            long userGroupClassNameId = classNameLocalService.getClassNameId(
1309                                    UserGroup.class);
1310    
1311                            for (long userGroupId : userGroupIds) {
1312                                    Group userGroupGroup = groupPersistence.findByC_C_C(
1313                                            group.getCompanyId(), userGroupClassNameId, userGroupId);
1314    
1315                                    layoutSet = layoutSetPersistence.findByG_P(
1316                                            userGroupGroup.getGroupId(), privateLayout);
1317    
1318                                    count += layoutSet.getPageCount();
1319                            }
1320                    }
1321    
1322                    return count;
1323            }
1324    
1325            @Override
1326            public int getLayoutsCount(
1327                    Group group, boolean privateLayout, long parentLayoutId) {
1328    
1329                    return layoutPersistence.countByG_P_P(
1330                            group.getGroupId(), privateLayout, parentLayoutId);
1331            }
1332    
1333            @Override
1334            public int getLayoutsCount(User user, boolean privateLayout)
1335                    throws PortalException {
1336    
1337                    return getLayoutsCount(user, privateLayout, true);
1338            }
1339    
1340            @Override
1341            public int getLayoutsCount(
1342                            User user, boolean privateLayout, boolean includeUserGroups)
1343                    throws PortalException {
1344    
1345                    long classNameId = classNameLocalService.getClassNameId(User.class);
1346    
1347                    Group group = groupPersistence.findByC_C_C(
1348                            user.getCompanyId(), classNameId, user.getUserId());
1349    
1350                    return getLayoutsCount(group, privateLayout, includeUserGroups);
1351            }
1352    
1353            /**
1354             * Returns the primary key to use for the next layout.
1355             *
1356             * @param  groupId the primary key of the group
1357             * @param  privateLayout whether the layout is private to the group
1358             * @return the primary key to use for the next layout
1359             */
1360            @Override
1361            public long getNextLayoutId(long groupId, boolean privateLayout) {
1362                    long nextLayoutId = counterLocalService.increment(
1363                            getCounterName(groupId, privateLayout));
1364    
1365                    if (nextLayoutId == 1) {
1366                            List<Layout> layouts = layoutPersistence.findByG_P(
1367                                    groupId, privateLayout, 0, 1, new LayoutComparator());
1368    
1369                            if (!layouts.isEmpty()) {
1370                                    Layout layout = layouts.get(0);
1371    
1372                                    nextLayoutId = layout.getLayoutId() + 1;
1373    
1374                                    counterLocalService.reset(
1375                                            getCounterName(groupId, privateLayout), nextLayoutId);
1376                            }
1377                    }
1378    
1379                    return nextLayoutId;
1380            }
1381    
1382            /**
1383             * Returns all the layouts without resource permissions
1384             *
1385             * @param  roleId the primary key of the role
1386             * @return all the layouts without resource permissions
1387             */
1388            @Override
1389            public List<Layout> getNoPermissionLayouts(long roleId) {
1390                    return layoutFinder.findByNoPermissions(roleId);
1391            }
1392    
1393            /**
1394             * Returns all the layouts whose friendly URLs are <code>null</code>
1395             *
1396             * @return all the layouts whose friendly URLs are <code>null</code>
1397             */
1398            @Override
1399            public List<Layout> getNullFriendlyURLLayouts() {
1400                    return layoutFinder.findByNullFriendlyURL();
1401            }
1402    
1403            @Override
1404            public Layout getParentLayout(Layout layout) throws PortalException {
1405                    Layout parentLayout = null;
1406    
1407                    if (layout instanceof VirtualLayout) {
1408                            VirtualLayout virtualLayout = (VirtualLayout)layout;
1409    
1410                            Layout sourceLayout = virtualLayout.getSourceLayout();
1411    
1412                            parentLayout = getLayout(
1413                                    sourceLayout.getGroupId(), sourceLayout.isPrivateLayout(),
1414                                    sourceLayout.getParentLayoutId());
1415    
1416                            parentLayout = new VirtualLayout(parentLayout, layout.getGroup());
1417                    }
1418                    else {
1419                            parentLayout = getLayout(
1420                                    layout.getGroupId(), layout.isPrivateLayout(),
1421                                    layout.getParentLayoutId());
1422                    }
1423    
1424                    return parentLayout;
1425            }
1426    
1427            @Override
1428            public List<Layout> getScopeGroupLayouts(long parentGroupId)
1429                    throws PortalException {
1430    
1431                    Group parentGroup = groupPersistence.findByPrimaryKey(parentGroupId);
1432    
1433                    List<Group> groups = groupLocalService.getGroups(
1434                            parentGroup.getCompanyId(), Layout.class.getName(), parentGroupId);
1435    
1436                    List<Layout> layouts = new ArrayList<>(groups.size());
1437    
1438                    for (Group group : groups) {
1439                            layouts.add(layoutPersistence.findByPrimaryKey(group.getClassPK()));
1440                    }
1441    
1442                    return layouts;
1443            }
1444    
1445            /**
1446             * Returns all the layouts within scope of the group
1447             *
1448             * @param  privateLayout whether the layout is private to the group
1449             * @return the layouts within scope of the group
1450             */
1451            @Override
1452            public List<Layout> getScopeGroupLayouts(
1453                            long parentGroupId, boolean privateLayout)
1454                    throws PortalException {
1455    
1456                    Group parentGroup = groupPersistence.findByPrimaryKey(parentGroupId);
1457    
1458                    List<Group> groups = groupLocalService.getGroups(
1459                            parentGroup.getCompanyId(), Layout.class.getName(), parentGroupId);
1460    
1461                    List<Layout> layouts = new ArrayList<>(groups.size());
1462    
1463                    for (Group group : groups) {
1464                            Layout layout = layoutPersistence.findByPrimaryKey(
1465                                    group.getClassPK());
1466    
1467                            if (layout.getPrivateLayout() == privateLayout) {
1468                                    layouts.add(layout);
1469                            }
1470                    }
1471    
1472                    return layouts;
1473            }
1474    
1475            @Override
1476            public boolean hasLayouts(Group group) throws PortalException {
1477                    List<LayoutSet> groupLayoutSets = layoutSetPersistence.findByGroupId(
1478                            group.getGroupId());
1479    
1480                    for (LayoutSet layoutSet : groupLayoutSets) {
1481                            if (layoutSet.getPageCount() > 0) {
1482                                    return true;
1483                            }
1484                    }
1485    
1486                    if (!group.isUser()) {
1487                            return false;
1488                    }
1489    
1490                    long[] userGroupIds = userPersistence.getUserGroupPrimaryKeys(
1491                            group.getClassPK());
1492    
1493                    if (userGroupIds.length != 0) {
1494                            long userGroupClassNameId = classNameLocalService.getClassNameId(
1495                                    UserGroup.class);
1496    
1497                            for (long userGroupId : userGroupIds) {
1498                                    Group userGroupGroup = groupPersistence.findByC_C_C(
1499                                            group.getCompanyId(), userGroupClassNameId, userGroupId);
1500    
1501                                    List<LayoutSet> userGroupGroupLayoutSets =
1502                                            layoutSetPersistence.findByGroupId(
1503                                                    userGroupGroup.getGroupId());
1504    
1505                                    for (LayoutSet layoutSet : userGroupGroupLayoutSets) {
1506                                            if (layoutSet.getPageCount() > 0) {
1507                                                    return true;
1508                                            }
1509                                    }
1510                            }
1511                    }
1512    
1513                    return false;
1514            }
1515    
1516            @Override
1517            public boolean hasLayouts(Group group, boolean privateLayout)
1518                    throws PortalException {
1519    
1520                    return hasLayouts(group, privateLayout, true);
1521            }
1522    
1523            @Override
1524            public boolean hasLayouts(
1525                            Group group, boolean privateLayout, boolean includeUserGroups)
1526                    throws PortalException {
1527    
1528                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
1529                            group.getGroupId(), privateLayout);
1530    
1531                    if (layoutSet.getPageCount() > 0) {
1532                            return true;
1533                    }
1534    
1535                    if (!group.isUser() || !includeUserGroups) {
1536                            return false;
1537                    }
1538    
1539                    long[] userGroupIds = userPersistence.getUserGroupPrimaryKeys(
1540                            group.getClassPK());
1541    
1542                    if (userGroupIds.length != 0) {
1543                            long userGroupClassNameId = classNameLocalService.getClassNameId(
1544                                    UserGroup.class);
1545    
1546                            for (long userGroupId : userGroupIds) {
1547                                    Group userGroupGroup = groupPersistence.findByC_C_C(
1548                                            group.getCompanyId(), userGroupClassNameId, userGroupId);
1549    
1550                                    layoutSet = layoutSetPersistence.findByG_P(
1551                                            userGroupGroup.getGroupId(), privateLayout);
1552    
1553                                    if (layoutSet.getPageCount() > 0) {
1554                                            return true;
1555                                    }
1556                            }
1557                    }
1558    
1559                    return false;
1560            }
1561    
1562            /**
1563             * Returns <code>true</code> if the group has any layouts;
1564             * <code>false</code> otherwise.
1565             *
1566             * @param  groupId the primary key of the group
1567             * @param  privateLayout whether the layout is private to the group
1568             * @param  parentLayoutId the primary key of the parent layout
1569             * @return <code>true</code> if the group has any layouts;
1570             *         <code>false</code> otherwise
1571             */
1572            @Override
1573            public boolean hasLayouts(
1574                    long groupId, boolean privateLayout, long parentLayoutId) {
1575    
1576                    return layoutPersistence.countByG_P_P(
1577                            groupId, privateLayout, parentLayoutId) > 0;
1578            }
1579    
1580            @Override
1581            public boolean hasLayouts(User user, boolean privateLayout)
1582                    throws PortalException {
1583    
1584                    return hasLayouts(user, privateLayout, true);
1585            }
1586    
1587            @Override
1588            public boolean hasLayouts(
1589                            User user, boolean privateLayout, boolean includeUserGroups)
1590                    throws PortalException {
1591    
1592                    long classNameId = classNameLocalService.getClassNameId(User.class);
1593    
1594                    Group group = groupPersistence.findByC_C_C(
1595                            user.getCompanyId(), classNameId, user.getUserId());
1596    
1597                    return hasLayouts(group, privateLayout, includeUserGroups);
1598            }
1599    
1600            @Override
1601            public boolean hasLayoutSetPrototypeLayout(
1602                            long layoutSetPrototypeId, String layoutUuid)
1603                    throws PortalException {
1604    
1605                    LayoutSetPrototype layoutSetPrototype =
1606                            layoutSetPrototypeLocalService.getLayoutSetPrototype(
1607                                    layoutSetPrototypeId);
1608    
1609                    return layoutLocalServiceHelper.hasLayoutSetPrototypeLayout(
1610                            layoutSetPrototype, layoutUuid);
1611            }
1612    
1613            @Override
1614            public boolean hasLayoutSetPrototypeLayout(
1615                            String layoutSetPrototypeUuid, long companyId, String layoutUuid)
1616                    throws PortalException {
1617    
1618                    LayoutSetPrototype layoutSetPrototype =
1619                            layoutSetPrototypeLocalService.
1620                                    getLayoutSetPrototypeByUuidAndCompanyId(
1621                                            layoutSetPrototypeUuid, companyId);
1622    
1623                    return layoutLocalServiceHelper.hasLayoutSetPrototypeLayout(
1624                            layoutSetPrototype, layoutUuid);
1625            }
1626    
1627            /**
1628             * @throws     PortalException
1629             * @deprecated As of 7.0.0, replaced by {@link
1630             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#importLayouts(
1631             *             ExportImportConfiguration, File)}}
1632             */
1633            @Deprecated
1634            @Override
1635            public void importLayouts(
1636                            ExportImportConfiguration exportImportConfiguration, File file)
1637                    throws PortalException {
1638    
1639                    throw new UnsupportedOperationException();
1640            }
1641    
1642            /**
1643             * @throws     PortalException
1644             * @deprecated As of 7.0.0, replaced by {@link
1645             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#importLayouts(
1646             *             ExportImportConfiguration, InputStream)}}
1647             */
1648            @Deprecated
1649            @Override
1650            public void importLayouts(
1651                            ExportImportConfiguration exportImportConfiguration, InputStream is)
1652                    throws PortalException {
1653    
1654                    throw new UnsupportedOperationException();
1655            }
1656    
1657            /**
1658             * Imports the layouts from the byte array.
1659             *
1660             * @param      userId the primary key of the user
1661             * @param      groupId the primary key of the group
1662             * @param      privateLayout whether the layout is private to the group
1663             * @param      parameterMap the mapping of parameters indicating which
1664             *             information will be imported. For information on the keys
1665             *             used in the map see {@link
1666             *             com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys}.
1667             * @param      bytes the byte array with the data
1668             * @throws     PortalException
1669             * @see        com.liferay.portlet.exportimport.lar.LayoutImporter
1670             * @deprecated As of 7.0.0, with no direct replacement
1671             */
1672            @Deprecated
1673            @Override
1674            public void importLayouts(
1675                            long userId, long groupId, boolean privateLayout,
1676                            Map<String, String[]> parameterMap, byte[] bytes)
1677                    throws PortalException {
1678    
1679                    throw new UnsupportedOperationException();
1680            }
1681    
1682            /**
1683             * Imports the layouts from the file.
1684             *
1685             * @param      userId the primary key of the user
1686             * @param      groupId the primary key of the group
1687             * @param      privateLayout whether the layout is private to the group
1688             * @param      parameterMap the mapping of parameters indicating which
1689             *             information will be imported. For information on the keys
1690             *             used in the map see {@link
1691             *             com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys}.
1692             * @param      file the LAR file with the data
1693             * @throws     PortalException
1694             * @see        com.liferay.portlet.exportimport.lar.LayoutImporter
1695             * @deprecated As of 7.0.0, with no direct replacement
1696             */
1697            @Deprecated
1698            @Override
1699            public void importLayouts(
1700                            long userId, long groupId, boolean privateLayout,
1701                            Map<String, String[]> parameterMap, File file)
1702                    throws PortalException {
1703    
1704                    throw new UnsupportedOperationException();
1705            }
1706    
1707            /**
1708             * Imports the layouts from the input stream.
1709             *
1710             * @param      userId the primary key of the user
1711             * @param      groupId the primary key of the group
1712             * @param      privateLayout whether the layout is private to the group
1713             * @param      parameterMap the mapping of parameters indicating which
1714             *             information will be imported. For information on the keys
1715             *             used in the map see {@link
1716             *             com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys}.
1717             * @param      is the input stream
1718             * @throws     PortalException
1719             * @see        com.liferay.portlet.exportimport.lar.LayoutImporter
1720             * @deprecated As of 7.0.0, with no direct replacement
1721             */
1722            @Deprecated
1723            @Override
1724            public void importLayouts(
1725                            long userId, long groupId, boolean privateLayout,
1726                            Map<String, String[]> parameterMap, InputStream is)
1727                    throws PortalException {
1728    
1729                    throw new UnsupportedOperationException();
1730            }
1731    
1732            /**
1733             * @throws     PortalException
1734             * @deprecated As of 7.0.0, replaced by {@link
1735             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#importLayoutsDataDeletions(
1736             *             ExportImportConfiguration, File)}
1737             */
1738            @Deprecated
1739            @Override
1740            public void importLayoutsDataDeletions(
1741                            ExportImportConfiguration exportImportConfiguration, File file)
1742                    throws PortalException {
1743    
1744                    throw new UnsupportedOperationException();
1745            }
1746    
1747            /**
1748             * @throws     PortalException
1749             * @deprecated As of 7.0.0, replaced by {@link
1750             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#importLayoutsInBackground(
1751             *             long, ExportImportConfiguration, File)}
1752             */
1753            @Deprecated
1754            @Override
1755            public long importLayoutsInBackground(
1756                            long userId, ExportImportConfiguration exportImportConfiguration,
1757                            File file)
1758                    throws PortalException {
1759    
1760                    throw new UnsupportedOperationException();
1761            }
1762    
1763            /**
1764             * @throws     PortalException
1765             * @deprecated As of 7.0.0, replaced by {@link
1766             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#importLayoutsInBackground(
1767             *             long, long, File)}
1768             */
1769            @Deprecated
1770            @Override
1771            public long importLayoutsInBackground(
1772                            long userId, long exportImportConfigurationId, File file)
1773                    throws PortalException {
1774    
1775                    throw new UnsupportedOperationException();
1776            }
1777    
1778            /**
1779             * @throws     PortalException
1780             * @deprecated As of 7.0.0, with no direct replacement
1781             */
1782            @Deprecated
1783            @Override
1784            public long importLayoutsInBackground(
1785                            long userId, String taskName, long groupId, boolean privateLayout,
1786                            Map<String, String[]> parameterMap, File file)
1787                    throws PortalException {
1788    
1789                    throw new UnsupportedOperationException();
1790            }
1791    
1792            /**
1793             * @throws     PortalException
1794             * @deprecated As of 7.0.0, with no direct replacement
1795             */
1796            @Deprecated
1797            @Override
1798            public long importLayoutsInBackground(
1799                            long userId, String taskName, long groupId, boolean privateLayout,
1800                            Map<String, String[]> parameterMap, InputStream is)
1801                    throws PortalException {
1802    
1803                    throw new UnsupportedOperationException();
1804            }
1805    
1806            /**
1807             * @throws     PortalException
1808             * @deprecated As of 7.0.0, replaced by {@link
1809             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#importPortletDataDeletions(
1810             *             ExportImportConfiguration, File)}
1811             */
1812            @Deprecated
1813            @Override
1814            public void importPortletDataDeletions(
1815                            ExportImportConfiguration exportImportConfiguration, File file)
1816                    throws PortalException {
1817    
1818                    throw new UnsupportedOperationException();
1819            }
1820    
1821            /**
1822             * @throws     PortalException
1823             * @deprecated As of 7.0.0, replaced by {@link
1824             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#importPortletInfo(
1825             *             ExportImportConfiguration, File)}
1826             */
1827            @Deprecated
1828            @Override
1829            public void importPortletInfo(
1830                            ExportImportConfiguration exportImportConfiguration, File file)
1831                    throws PortalException {
1832    
1833                    throw new UnsupportedOperationException();
1834            }
1835    
1836            /**
1837             * @throws     PortalException
1838             * @deprecated As of 7.0.0, replaced by {@link
1839             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#importPortletInfo(
1840             *             ExportImportConfiguration, InputStream)}
1841             */
1842            @Deprecated
1843            @Override
1844            public void importPortletInfo(
1845                            ExportImportConfiguration exportImportConfiguration, InputStream is)
1846                    throws PortalException {
1847    
1848                    throw new UnsupportedOperationException();
1849            }
1850    
1851            /**
1852             * Imports the portlet information (categories, permissions, ... etc.) from
1853             * the file.
1854             *
1855             * @param      userId the primary key of the user
1856             * @param      plid the primary key of the target layout
1857             * @param      groupId the primary key of the target group
1858             * @param      portletId the primary key of the portlet
1859             * @param      parameterMap the mapping of parameters indicating which
1860             *             information will be imported. For information on the keys
1861             *             used in the map see {@link
1862             *             com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys}.
1863             * @param      file the LAR file with the data
1864             * @throws     PortalException
1865             * @deprecated As of 7.0.0, with no direct replacement
1866             */
1867            @Deprecated
1868            @Override
1869            public void importPortletInfo(
1870                            long userId, long plid, long groupId, String portletId,
1871                            Map<String, String[]> parameterMap, File file)
1872                    throws PortalException {
1873    
1874                    throw new UnsupportedOperationException();
1875            }
1876    
1877            /**
1878             * Imports the portlet information (categories, permissions, ... etc.) from
1879             * the input stream.
1880             *
1881             * @param      userId the primary key of the user
1882             * @param      plid the primary key of the layout
1883             * @param      groupId the primary key of the group
1884             * @param      portletId the primary key of the portlet
1885             * @param      parameterMap the mapping of parameters indicating which
1886             *             information will be imported. For information on the keys
1887             *             used in the map see {@link
1888             *             com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys}.
1889             * @param      is the input stream
1890             * @throws     PortalException
1891             * @deprecated As of 7.0.0, with no direct replacement
1892             */
1893            @Deprecated
1894            @Override
1895            public void importPortletInfo(
1896                            long userId, long plid, long groupId, String portletId,
1897                            Map<String, String[]> parameterMap, InputStream is)
1898                    throws PortalException {
1899    
1900                    throw new UnsupportedOperationException();
1901            }
1902    
1903            /**
1904             * @throws     PortalException
1905             * @deprecated As of 7.0.0, with no direct replacement
1906             */
1907            @Deprecated
1908            @Override
1909            public void importPortletInfo(
1910                            long userId, String portletId, Map<String, String[]> parameterMap,
1911                            File file)
1912                    throws PortalException {
1913    
1914                    throw new UnsupportedOperationException();
1915            }
1916    
1917            /**
1918             * @throws     PortalException
1919             * @deprecated As of 7.0.0, with no direct replacement
1920             */
1921            @Deprecated
1922            @Override
1923            public void importPortletInfo(
1924                            long userId, String portletId, Map<String, String[]> parameterMap,
1925                            InputStream is)
1926                    throws PortalException {
1927    
1928                    throw new UnsupportedOperationException();
1929            }
1930    
1931            /**
1932             * @throws     PortalException
1933             * @deprecated As of 7.0.0, replaced by {@link
1934             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#importPortletInfoInBackground(
1935             *             long, ExportImportConfiguration, File)}
1936             */
1937            @Deprecated
1938            @Override
1939            public long importPortletInfoInBackground(
1940                            long userId, ExportImportConfiguration exportImportConfiguration,
1941                            File file)
1942                    throws PortalException {
1943    
1944                    throw new UnsupportedOperationException();
1945            }
1946    
1947            /**
1948             * @throws     PortalException
1949             * @deprecated As of 7.0.0, replaced by {@link
1950             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#importPortletInfoInBackground(
1951             *             long, long, File)}
1952             */
1953            @Deprecated
1954            @Override
1955            public long importPortletInfoInBackground(
1956                            long userId, long exportImportConfigurationId, File file)
1957                    throws PortalException {
1958    
1959                    throw new UnsupportedOperationException();
1960            }
1961    
1962            /**
1963             * @throws     PortalException
1964             * @deprecated As of 7.0.0, with no direct replacement
1965             */
1966            @Deprecated
1967            @Override
1968            public long importPortletInfoInBackground(
1969                            long userId, String taskName, long plid, long groupId,
1970                            String portletId, Map<String, String[]> parameterMap, File file)
1971                    throws PortalException {
1972    
1973                    throw new UnsupportedOperationException();
1974            }
1975    
1976            /**
1977             * @throws     PortalException
1978             * @deprecated As of 7.0.0, with no direct replacement
1979             */
1980            @Deprecated
1981            @Override
1982            public long importPortletInfoInBackground(
1983                            long userId, String taskName, long plid, long groupId,
1984                            String portletId, Map<String, String[]> parameterMap,
1985                            InputStream is)
1986                    throws PortalException {
1987    
1988                    throw new UnsupportedOperationException();
1989            }
1990    
1991            /**
1992             * @throws     PortalException
1993             * @deprecated As of 7.0.0, with no direct replacement
1994             */
1995            @Deprecated
1996            @Override
1997            public long importPortletInfoInBackground(
1998                            long userId, String taskName, String portletId,
1999                            Map<String, String[]> parameterMap, File file)
2000                    throws PortalException {
2001    
2002                    throw new UnsupportedOperationException();
2003            }
2004    
2005            /**
2006             * @throws     PortalException
2007             * @deprecated As of 7.0.0, with no direct replacement
2008             */
2009            @Deprecated
2010            @Override
2011            public long importPortletInfoInBackground(
2012                            long userId, String taskName, String portletId,
2013                            Map<String, String[]> parameterMap, InputStream is)
2014                    throws PortalException {
2015    
2016                    throw new UnsupportedOperationException();
2017            }
2018    
2019            /**
2020             * Sets the layouts for the group, replacing and prioritizing all layouts of
2021             * the parent layout.
2022             *
2023             * @param groupId the primary key of the group
2024             * @param privateLayout whether the layout is private to the group
2025             * @param parentLayoutId the primary key of the parent layout
2026             * @param layoutIds the primary keys of the layouts
2027             * @param serviceContext the service context to be applied
2028             */
2029            @Override
2030            public void setLayouts(
2031                            long groupId, boolean privateLayout, long parentLayoutId,
2032                            long[] layoutIds, ServiceContext serviceContext)
2033                    throws PortalException {
2034    
2035                    if (layoutIds == null) {
2036                            return;
2037                    }
2038    
2039                    if (parentLayoutId == LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
2040                            if (layoutIds.length < 1) {
2041                                    throw new RequiredLayoutException(
2042                                            RequiredLayoutException.AT_LEAST_ONE);
2043                            }
2044    
2045                            Layout layout = layoutPersistence.findByG_P_L(
2046                                    groupId, privateLayout, layoutIds[0]);
2047    
2048                            LayoutType layoutType = layout.getLayoutType();
2049    
2050                            if (!layoutType.isFirstPageable()) {
2051                                    throw new RequiredLayoutException(
2052                                            RequiredLayoutException.FIRST_LAYOUT_TYPE);
2053                            }
2054                    }
2055    
2056                    Set<Long> layoutIdsSet = new LinkedHashSet<>();
2057    
2058                    for (long layoutId : layoutIds) {
2059                            layoutIdsSet.add(layoutId);
2060                    }
2061    
2062                    Set<Long> newLayoutIdsSet = new HashSet<>();
2063    
2064                    List<Layout> layouts = layoutPersistence.findByG_P_P(
2065                            groupId, privateLayout, parentLayoutId);
2066    
2067                    for (Layout layout : layouts) {
2068                            if (!layoutIdsSet.contains(layout.getLayoutId())) {
2069                                    deleteLayout(layout, true, serviceContext);
2070                            }
2071                            else {
2072                                    newLayoutIdsSet.add(layout.getLayoutId());
2073                            }
2074                    }
2075    
2076                    int priority = 0;
2077    
2078                    for (long layoutId : layoutIdsSet) {
2079                            Layout layout = layoutPersistence.findByG_P_L(
2080                                    groupId, privateLayout, layoutId);
2081    
2082                            layout.setPriority(priority++);
2083    
2084                            layoutPersistence.update(layout);
2085                    }
2086    
2087                    layoutSetLocalService.updatePageCount(groupId, privateLayout);
2088            }
2089    
2090            @Override
2091            public void updateAsset(
2092                            long userId, Layout layout, long[] assetCategoryIds,
2093                            String[] assetTagNames)
2094                    throws PortalException {
2095    
2096                    assetEntryLocalService.updateEntry(
2097                            userId, layout.getGroupId(), layout.getCreateDate(),
2098                            layout.getModifiedDate(), Layout.class.getName(), layout.getPlid(),
2099                            layout.getUuid(), 0, assetCategoryIds, assetTagNames, false, null,
2100                            null, null, ContentTypes.TEXT_HTML,
2101                            layout.getName(LocaleUtil.getDefault()), null, null, null, null, 0,
2102                            0, null);
2103            }
2104    
2105            /**
2106             * Updates the friendly URL of the layout.
2107             *
2108             * @param  userId the primary key of the user
2109             * @param  plid the primary key of the layout
2110             * @param  friendlyURL the friendly URL to be assigned
2111             * @param  languageId the primary key of the language
2112             * @return the updated layout
2113             */
2114            @Override
2115            public Layout updateFriendlyURL(
2116                            long userId, long plid, String friendlyURL, String languageId)
2117                    throws PortalException {
2118    
2119                    Date now = new Date();
2120    
2121                    Layout layout = layoutPersistence.findByPrimaryKey(plid);
2122    
2123                    friendlyURL = layoutLocalServiceHelper.getFriendlyURL(
2124                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
2125                            StringPool.BLANK, friendlyURL);
2126    
2127                    layoutLocalServiceHelper.validateFriendlyURL(
2128                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
2129                            friendlyURL);
2130    
2131                    layoutFriendlyURLLocalService.updateLayoutFriendlyURL(
2132                            userId, layout.getCompanyId(), layout.getGroupId(),
2133                            layout.getPlid(), layout.isPrivateLayout(), friendlyURL, languageId,
2134                            new ServiceContext());
2135    
2136                    layout.setModifiedDate(now);
2137    
2138                    String defaultLanguageId = LocaleUtil.toLanguageId(
2139                            LocaleUtil.getSiteDefault());
2140    
2141                    if (languageId.equals(defaultLanguageId)) {
2142                            layout.setFriendlyURL(friendlyURL);
2143                    }
2144    
2145                    layoutPersistence.update(layout);
2146    
2147                    return layout;
2148            }
2149    
2150            /**
2151             * Updates the friendly URL of the layout.
2152             *
2153             * @param      plid the primary key of the layout
2154             * @param      friendlyURL the friendly URL to be assigned
2155             * @param      languageId the primary key of the language
2156             * @return     the updated layout
2157             * @deprecated As of 7.0.0, replaced by {@link #updateFriendlyURL(long,
2158             *             long, String, String)}
2159             */
2160            @Deprecated
2161            @Override
2162            public Layout updateFriendlyURL(
2163                            long plid, String friendlyURL, String languageId)
2164                    throws PortalException {
2165    
2166                    Layout layout = layoutPersistence.findByPrimaryKey(plid);
2167    
2168                    return updateFriendlyURL(
2169                            layout.getUserId(), plid, friendlyURL, languageId);
2170            }
2171    
2172            @Override
2173            public Layout updateIconImage(long plid, byte[] bytes)
2174                    throws PortalException {
2175    
2176                    Layout layout = layoutPersistence.fetchByPrimaryKey(plid);
2177    
2178                    if (layout == null) {
2179                            return null;
2180                    }
2181    
2182                    PortalUtil.updateImageId(layout, true, bytes, "iconImageId", 0, 0, 0);
2183    
2184                    layoutPersistence.update(layout);
2185    
2186                    return layout;
2187            }
2188    
2189            /**
2190             * Updates the layout.
2191             *
2192             * @param  groupId the primary key of the group
2193             * @param  privateLayout whether the layout is private to the group
2194             * @param  layoutId the primary key of the layout
2195             * @param  parentLayoutId the primary key of the layout's new parent layout
2196             * @param  nameMap the locales and localized names to merge (optionally
2197             *         <code>null</code>)
2198             * @param  titleMap the locales and localized titles to merge (optionally
2199             *         <code>null</code>)
2200             * @param  descriptionMap the locales and localized descriptions to merge
2201             *         (optionally <code>null</code>)
2202             * @param  keywordsMap the locales and localized keywords to merge
2203             *         (optionally <code>null</code>)
2204             * @param  robotsMap the locales and localized robots to merge (optionally
2205             *         <code>null</code>)
2206             * @param  type the layout's new type (optionally {@link
2207             *         LayoutConstants#TYPE_PORTLET})
2208             * @param  hidden whether the layout is hidden
2209             * @param  friendlyURLMap the layout's locales and localized friendly URLs.
2210             *         To see how the URL is normalized when accessed, see {@link
2211             *         com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil#normalize(
2212             *         String)}.
2213             * @param  iconImage whether the icon image will be updated
2214             * @param  iconBytes the byte array of the layout's new icon image
2215             * @param  serviceContext the service context to be applied. Can set the
2216             *         modification date and expando bridge attributes for the layout.
2217             *         For layouts that are linked to a layout prototype, attributes
2218             *         named <code>layoutPrototypeUuid</code> and
2219             *         <code>layoutPrototypeLinkedEnabled</code> can be specified to
2220             *         provide the unique identifier of the source prototype and a
2221             *         boolean to determine whether a link to it should be enabled to
2222             *         activate propagation of changes made to the linked page in the
2223             *         prototype.
2224             * @return the updated layout
2225             */
2226            @Override
2227            public Layout updateLayout(
2228                            long groupId, boolean privateLayout, long layoutId,
2229                            long parentLayoutId, Map<Locale, String> nameMap,
2230                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
2231                            Map<Locale, String> keywordsMap, Map<Locale, String> robotsMap,
2232                            String type, boolean hidden, Map<Locale, String> friendlyURLMap,
2233                            boolean iconImage, byte[] iconBytes, ServiceContext serviceContext)
2234                    throws PortalException {
2235    
2236                    // Layout
2237    
2238                    parentLayoutId = layoutLocalServiceHelper.getParentLayoutId(
2239                            groupId, privateLayout, parentLayoutId);
2240                    String name = nameMap.get(LocaleUtil.getSiteDefault());
2241                    friendlyURLMap = layoutLocalServiceHelper.getFriendlyURLMap(
2242                            groupId, privateLayout, layoutId, name, friendlyURLMap);
2243    
2244                    String friendlyURL = friendlyURLMap.get(LocaleUtil.getSiteDefault());
2245    
2246                    layoutLocalServiceHelper.validate(
2247                            groupId, privateLayout, layoutId, parentLayoutId, name, type,
2248                            hidden, friendlyURLMap, serviceContext);
2249    
2250                    layoutLocalServiceHelper.validateParentLayoutId(
2251                            groupId, privateLayout, layoutId, parentLayoutId);
2252    
2253                    Date now = new Date();
2254    
2255                    Layout layout = layoutPersistence.findByG_P_L(
2256                            groupId, privateLayout, layoutId);
2257    
2258                    if (parentLayoutId != layout.getParentLayoutId()) {
2259                            int priority = layoutLocalServiceHelper.getNextPriority(
2260                                    groupId, privateLayout, parentLayoutId,
2261                                    layout.getSourcePrototypeLayoutUuid(), -1);
2262    
2263                            layout.setPriority(priority);
2264                    }
2265    
2266                    layout.setModifiedDate(serviceContext.getModifiedDate(now));
2267                    layout.setParentLayoutId(parentLayoutId);
2268                    layout.setNameMap(nameMap);
2269                    layout.setTitleMap(titleMap);
2270                    layout.setDescriptionMap(descriptionMap);
2271                    layout.setKeywordsMap(keywordsMap);
2272                    layout.setRobotsMap(robotsMap);
2273                    layout.setType(type);
2274                    layout.setHidden(hidden);
2275                    layout.setFriendlyURL(friendlyURL);
2276    
2277                    PortalUtil.updateImageId(
2278                            layout, iconImage, iconBytes, "iconImageId", 0, 0, 0);
2279    
2280                    boolean layoutUpdateable = ParamUtil.getBoolean(
2281                            serviceContext, Sites.LAYOUT_UPDATEABLE, true);
2282    
2283                    UnicodeProperties typeSettingsProperties =
2284                            layout.getTypeSettingsProperties();
2285    
2286                    typeSettingsProperties.put(
2287                            Sites.LAYOUT_UPDATEABLE, String.valueOf(layoutUpdateable));
2288    
2289                    if (privateLayout) {
2290                            typeSettingsProperties.put(
2291                                    "privateLayout", String.valueOf(privateLayout));
2292                    }
2293    
2294                    layout.setTypeSettingsProperties(typeSettingsProperties);
2295    
2296                    String layoutPrototypeUuid = ParamUtil.getString(
2297                            serviceContext, "layoutPrototypeUuid");
2298                    boolean layoutPrototypeLinkEnabled = ParamUtil.getBoolean(
2299                            serviceContext, "layoutPrototypeLinkEnabled");
2300    
2301                    if (Validator.isNotNull(layoutPrototypeUuid)) {
2302                            layout.setLayoutPrototypeUuid(layoutPrototypeUuid);
2303                            layout.setLayoutPrototypeLinkEnabled(layoutPrototypeLinkEnabled);
2304                    }
2305    
2306                    layout.setExpandoBridgeAttributes(serviceContext);
2307    
2308                    layoutPersistence.update(layout);
2309    
2310                    // Layout friendly URLs
2311    
2312                    layoutFriendlyURLLocalService.updateLayoutFriendlyURLs(
2313                            serviceContext.getUserId(), layout.getCompanyId(),
2314                            layout.getGroupId(), layout.getPlid(), layout.isPrivateLayout(),
2315                            friendlyURLMap, serviceContext);
2316    
2317                    // Asset
2318    
2319                    updateAsset(
2320                            serviceContext.getUserId(), layout,
2321                            serviceContext.getAssetCategoryIds(),
2322                            serviceContext.getAssetTagNames());
2323    
2324                    return layout;
2325            }
2326    
2327            /**
2328             * Updates the layout replacing its type settings.
2329             *
2330             * @param  groupId the primary key of the group
2331             * @param  privateLayout whether the layout is private to the group
2332             * @param  layoutId the primary key of the layout
2333             * @param  typeSettings the settings to load the unicode properties object.
2334             *         See {@link UnicodeProperties #fastLoad(String)}.
2335             * @return the updated layout
2336             */
2337            @Override
2338            public Layout updateLayout(
2339                            long groupId, boolean privateLayout, long layoutId,
2340                            String typeSettings)
2341                    throws PortalException {
2342    
2343                    Date now = new Date();
2344    
2345                    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
2346    
2347                    typeSettingsProperties.fastLoad(typeSettings);
2348    
2349                    validateTypeSettingsProperties(typeSettingsProperties);
2350    
2351                    Layout layout = layoutPersistence.findByG_P_L(
2352                            groupId, privateLayout, layoutId);
2353    
2354                    layout.setModifiedDate(now);
2355                    layout.setTypeSettings(typeSettingsProperties.toString());
2356    
2357                    layoutPersistence.update(layout);
2358    
2359                    return layout;
2360            }
2361    
2362            /**
2363             * Updates the look and feel of the layout.
2364             *
2365             * @param  groupId the primary key of the group
2366             * @param  privateLayout whether the layout is private to the group
2367             * @param  layoutId the primary key of the layout
2368             * @param  themeId the primary key of the layout's new theme
2369             * @param  colorSchemeId the primary key of the layout's new color scheme
2370             * @param  css the layout's new CSS
2371             * @param  wapTheme whether the theme is for WAP browsers
2372             * @return the updated layout
2373             */
2374            @Override
2375            public Layout updateLookAndFeel(
2376                            long groupId, boolean privateLayout, long layoutId, String themeId,
2377                            String colorSchemeId, String css, boolean wapTheme)
2378                    throws PortalException {
2379    
2380                    Date now = new Date();
2381    
2382                    Layout layout = layoutPersistence.findByG_P_L(
2383                            groupId, privateLayout, layoutId);
2384    
2385                    layout.setModifiedDate(now);
2386    
2387                    if (wapTheme) {
2388                            layout.setWapThemeId(themeId);
2389                            layout.setWapColorSchemeId(colorSchemeId);
2390                    }
2391                    else {
2392                            layout.setThemeId(themeId);
2393                            layout.setColorSchemeId(colorSchemeId);
2394                            layout.setCss(css);
2395                    }
2396    
2397                    layoutPersistence.update(layout);
2398    
2399                    return layout;
2400            }
2401    
2402            /**
2403             * Updates the name of the layout.
2404             *
2405             * @param  layout the layout to be updated
2406             * @param  name the layout's new name
2407             * @param  languageId the primary key of the language. For more information
2408             *         see {@link Locale}.
2409             * @return the updated layout
2410             */
2411            @Override
2412            public Layout updateName(Layout layout, String name, String languageId)
2413                    throws PortalException {
2414    
2415                    Date now = new Date();
2416    
2417                    layoutLocalServiceHelper.validateName(name, languageId);
2418    
2419                    layout.setModifiedDate(now);
2420                    layout.setName(name, LocaleUtil.fromLanguageId(languageId));
2421    
2422                    layoutPersistence.update(layout);
2423    
2424                    Group group = layout.getGroup();
2425    
2426                    if (group.isLayoutPrototype()) {
2427                            LayoutPrototype layoutPrototype =
2428                                    layoutPrototypeLocalService.getLayoutPrototype(
2429                                            group.getClassPK());
2430    
2431                            layoutPrototype.setModifiedDate(now);
2432                            layoutPrototype.setName(
2433                                    name, LocaleUtil.fromLanguageId(languageId));
2434    
2435                            layoutPrototypePersistence.update(layoutPrototype);
2436                    }
2437    
2438                    return layout;
2439            }
2440    
2441            /**
2442             * Updates the name of the layout matching the group, layout ID, and
2443             * privacy.
2444             *
2445             * @param  groupId the primary key of the group
2446             * @param  privateLayout whether the layout is private to the group
2447             * @param  layoutId the primary key of the layout
2448             * @param  name the layout's new name
2449             * @param  languageId the primary key of the language. For more information
2450             *         see {@link Locale}.
2451             * @return the updated layout
2452             */
2453            @Override
2454            public Layout updateName(
2455                            long groupId, boolean privateLayout, long layoutId, String name,
2456                            String languageId)
2457                    throws PortalException {
2458    
2459                    Layout layout = layoutPersistence.findByG_P_L(
2460                            groupId, privateLayout, layoutId);
2461    
2462                    return updateName(layout, name, languageId);
2463            }
2464    
2465            /**
2466             * Updates the name of the layout matching the primary key.
2467             *
2468             * @param  plid the primary key of the layout
2469             * @param  name the name to be assigned
2470             * @param  languageId the primary key of the language. For more information
2471             *         see {@link Locale}.
2472             * @return the updated layout
2473             */
2474            @Override
2475            public Layout updateName(long plid, String name, String languageId)
2476                    throws PortalException {
2477    
2478                    Layout layout = layoutPersistence.findByPrimaryKey(plid);
2479    
2480                    return updateName(layout, name, languageId);
2481            }
2482    
2483            /**
2484             * Updates the parent layout ID of the layout matching the group, layout ID,
2485             * and privacy.
2486             *
2487             * @param  groupId the primary key of the group
2488             * @param  privateLayout whether the layout is private to the group
2489             * @param  layoutId the primary key of the layout
2490             * @param  parentLayoutId the primary key to be assigned to the parent
2491             *         layout
2492             * @return the matching layout
2493             */
2494            @Override
2495            public Layout updateParentLayoutId(
2496                            long groupId, boolean privateLayout, long layoutId,
2497                            long parentLayoutId)
2498                    throws PortalException {
2499    
2500                    parentLayoutId = layoutLocalServiceHelper.getParentLayoutId(
2501                            groupId, privateLayout, parentLayoutId);
2502    
2503                    layoutLocalServiceHelper.validateParentLayoutId(
2504                            groupId, privateLayout, layoutId, parentLayoutId);
2505    
2506                    Date now = new Date();
2507    
2508                    Layout layout = layoutPersistence.findByG_P_L(
2509                            groupId, privateLayout, layoutId);
2510    
2511                    if (parentLayoutId != layout.getParentLayoutId()) {
2512                            int priority = layoutLocalServiceHelper.getNextPriority(
2513                                    groupId, privateLayout, parentLayoutId,
2514                                    layout.getSourcePrototypeLayoutUuid(), -1);
2515    
2516                            layout.setPriority(priority);
2517                    }
2518    
2519                    layout.setModifiedDate(now);
2520                    layout.setParentLayoutId(parentLayoutId);
2521    
2522                    layoutPersistence.update(layout);
2523    
2524                    return layout;
2525            }
2526    
2527            /**
2528             * Updates the parent layout ID of the layout matching the primary key. If a
2529             * layout matching the parent primary key is found, the layout ID of that
2530             * layout is assigned, otherwise {@link
2531             * LayoutConstants#DEFAULT_PARENT_LAYOUT_ID} is assigned.
2532             *
2533             * @param  plid the primary key of the layout
2534             * @param  parentPlid the primary key of the parent layout
2535             * @return the layout matching the primary key
2536             */
2537            @Override
2538            public Layout updateParentLayoutId(long plid, long parentPlid)
2539                    throws PortalException {
2540    
2541                    Date now = new Date();
2542    
2543                    Layout layout = layoutPersistence.findByPrimaryKey(plid);
2544    
2545                    long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
2546    
2547                    if (parentPlid > 0) {
2548                            Layout parentLayout = layoutPersistence.fetchByPrimaryKey(
2549                                    parentPlid);
2550    
2551                            if (parentLayout != null) {
2552                                    parentLayoutId = parentLayout.getLayoutId();
2553                            }
2554                    }
2555    
2556                    parentLayoutId = layoutLocalServiceHelper.getParentLayoutId(
2557                            layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId);
2558    
2559                    layoutLocalServiceHelper.validateParentLayoutId(
2560                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
2561                            parentLayoutId);
2562    
2563                    if (parentLayoutId != layout.getParentLayoutId()) {
2564                            int priority = layoutLocalServiceHelper.getNextPriority(
2565                                    layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId,
2566                                    layout.getSourcePrototypeLayoutUuid(), -1);
2567    
2568                            layout.setPriority(priority);
2569                    }
2570    
2571                    layout.setModifiedDate(now);
2572                    layout.setParentLayoutId(parentLayoutId);
2573    
2574                    layoutPersistence.update(layout);
2575    
2576                    return layout;
2577            }
2578    
2579            /**
2580             * Updates the parent layout ID and priority of the layout.
2581             *
2582             * @param  plid the primary key of the layout
2583             * @param  parentPlid the primary key of the parent layout
2584             * @param  priority the layout's new priority
2585             * @return the layout matching the primary key
2586             */
2587            @Override
2588            public Layout updateParentLayoutIdAndPriority(
2589                            long plid, long parentPlid, int priority)
2590                    throws PortalException {
2591    
2592                    Layout layout = updateParentLayoutId(plid, parentPlid);
2593    
2594                    return updatePriority(layout, priority);
2595            }
2596    
2597            /**
2598             * Updates the priorities of the layouts.
2599             *
2600             * @param  groupId the primary key of the group
2601             * @param  privateLayout whether the layout is private to the group
2602             * @throws PortalException
2603             */
2604            @Override
2605            public void updatePriorities(long groupId, boolean privateLayout)
2606                    throws PortalException {
2607    
2608                    List<Layout> layouts = layoutPersistence.findByG_P(
2609                            groupId, privateLayout);
2610    
2611                    for (Layout layout : layouts) {
2612                            int nextPriority = layoutLocalServiceHelper.getNextPriority(
2613                                    layout.getGroupId(), layout.isPrivateLayout(),
2614                                    layout.getParentLayoutId(),
2615                                    layout.getSourcePrototypeLayoutUuid(), layout.getPriority());
2616    
2617                            layout.setPriority(nextPriority);
2618    
2619                            layoutPersistence.update(layout);
2620                    }
2621            }
2622    
2623            /**
2624             * Updates the priority of the layout.
2625             *
2626             * @param  layout the layout to be updated
2627             * @param  priority the layout's new priority
2628             * @return the updated layout
2629             */
2630            @Override
2631            public Layout updatePriority(Layout layout, int priority)
2632                    throws PortalException {
2633    
2634                    if (layout.getPriority() == priority) {
2635                            return layout;
2636                    }
2637    
2638                    int oldPriority = layout.getPriority();
2639    
2640                    int nextPriority = layoutLocalServiceHelper.getNextPriority(
2641                            layout.getGroupId(), layout.isPrivateLayout(),
2642                            layout.getParentLayoutId(), layout.getSourcePrototypeLayoutUuid(),
2643                            priority);
2644    
2645                    if (oldPriority == nextPriority) {
2646                            return layout;
2647                    }
2648    
2649                    layout.setModifiedDate(new Date());
2650                    layout.setPriority(nextPriority);
2651    
2652                    layoutPersistence.update(layout);
2653    
2654                    List<Layout> layouts = layoutPersistence.findByG_P_P(
2655                            layout.getGroupId(), layout.isPrivateLayout(),
2656                            layout.getParentLayoutId());
2657    
2658                    boolean lessThan = false;
2659    
2660                    if (oldPriority < nextPriority) {
2661                            lessThan = true;
2662                    }
2663    
2664                    layouts = ListUtil.sort(
2665                            layouts, new LayoutPriorityComparator(layout, lessThan));
2666    
2667                    if (layout.getParentLayoutId() ==
2668                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
2669    
2670                            Layout firstLayout = layouts.get(0);
2671    
2672                            layoutLocalServiceHelper.validateFirstLayout(firstLayout);
2673                    }
2674    
2675                    int newPriority = LayoutConstants.FIRST_PRIORITY;
2676    
2677                    for (Layout curLayout : layouts) {
2678                            int curNextPriority = layoutLocalServiceHelper.getNextPriority(
2679                                    layout.getGroupId(), layout.isPrivateLayout(),
2680                                    layout.getParentLayoutId(),
2681                                    curLayout.getSourcePrototypeLayoutUuid(), newPriority++);
2682    
2683                            if (curLayout.getPriority() == curNextPriority) {
2684                                    continue;
2685                            }
2686    
2687                            curLayout.setModifiedDate(layout.getModifiedDate());
2688                            curLayout.setPriority(curNextPriority);
2689    
2690                            layoutPersistence.update(curLayout);
2691    
2692                            if (curLayout.equals(layout)) {
2693                                    layout = curLayout;
2694                            }
2695                    }
2696    
2697                    return layout;
2698            }
2699    
2700            /**
2701             * Updates the priority of the layout matching the group, layout ID, and
2702             * privacy.
2703             *
2704             * @param  groupId the primary key of the group
2705             * @param  privateLayout whether the layout is private to the group
2706             * @param  layoutId the primary key of the layout
2707             * @param  priority the layout's new priority
2708             * @return the updated layout
2709             */
2710            @Override
2711            public Layout updatePriority(
2712                            long groupId, boolean privateLayout, long layoutId, int priority)
2713                    throws PortalException {
2714    
2715                    Layout layout = layoutPersistence.findByG_P_L(
2716                            groupId, privateLayout, layoutId);
2717    
2718                    return updatePriority(layout, priority);
2719            }
2720    
2721            /**
2722             * Updates the priority of the layout matching the group, layout ID, and
2723             * privacy, setting the layout's priority based on the priorities of the
2724             * next and previous layouts.
2725             *
2726             * @param  groupId the primary key of the group
2727             * @param  privateLayout whether the layout is private to the group
2728             * @param  layoutId the primary key of the layout
2729             * @param  nextLayoutId the primary key of the next layout
2730             * @param  previousLayoutId the primary key of the previous layout
2731             * @return the updated layout
2732             */
2733            @Override
2734            public Layout updatePriority(
2735                            long groupId, boolean privateLayout, long layoutId,
2736                            long nextLayoutId, long previousLayoutId)
2737                    throws PortalException {
2738    
2739                    Layout layout = getLayout(groupId, privateLayout, layoutId);
2740    
2741                    int priority = layout.getPriority();
2742    
2743                    Layout nextLayout = null;
2744    
2745                    if (nextLayoutId > 0) {
2746                            nextLayout = getLayout(groupId, privateLayout, nextLayoutId);
2747                    }
2748    
2749                    Layout previousLayout = null;
2750    
2751                    if (previousLayoutId > 0) {
2752                            previousLayout = getLayout(
2753                                    groupId, privateLayout, previousLayoutId);
2754                    }
2755    
2756                    if ((nextLayout != null) && (priority > nextLayout.getPriority())) {
2757                            priority = nextLayout.getPriority();
2758                    }
2759                    else if ((previousLayout != null) &&
2760                                     (priority < previousLayout.getPriority())) {
2761    
2762                            priority = previousLayout.getPriority();
2763                    }
2764    
2765                    return updatePriority(layout, priority);
2766            }
2767    
2768            /**
2769             * Updates the priority of the layout matching the primary key.
2770             *
2771             * @param  plid the primary key of the layout
2772             * @param  priority the layout's new priority
2773             * @return the updated layout
2774             */
2775            @Override
2776            public Layout updatePriority(long plid, int priority)
2777                    throws PortalException {
2778    
2779                    Layout layout = layoutPersistence.findByPrimaryKey(plid);
2780    
2781                    return updatePriority(layout, priority);
2782            }
2783    
2784            /**
2785             * @throws     PortalException
2786             * @deprecated As of 7.0.0, replaced by {@link
2787             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#validateImportLayoutsFile(
2788             *             ExportImportConfiguration, File)}
2789             */
2790            @Deprecated
2791            @Override
2792            public MissingReferences validateImportLayoutsFile(
2793                            ExportImportConfiguration exportImportConfiguration, File file)
2794                    throws PortalException {
2795    
2796                    throw new UnsupportedOperationException();
2797            }
2798    
2799            /**
2800             * @throws     PortalException
2801             * @deprecated As of 7.0.0, replaced by {@link
2802             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#validateImportLayoutsFile(
2803             *             ExportImportConfiguration, InputStream)}
2804             */
2805            @Deprecated
2806            @Override
2807            public MissingReferences validateImportLayoutsFile(
2808                            ExportImportConfiguration exportImportConfiguration,
2809                            InputStream inputStream)
2810                    throws PortalException {
2811    
2812                    throw new UnsupportedOperationException();
2813            }
2814    
2815            /**
2816             * @throws     PortalException
2817             * @deprecated As of 7.0.0, with no direct replacement
2818             */
2819            @Deprecated
2820            @Override
2821            public MissingReferences validateImportLayoutsFile(
2822                            long userId, long groupId, boolean privateLayout,
2823                            Map<String, String[]> parameterMap, File file)
2824                    throws PortalException {
2825    
2826                    throw new UnsupportedOperationException();
2827            }
2828    
2829            /**
2830             * @throws     PortalException
2831             * @deprecated As of 7.0.0, with no direct replacement
2832             */
2833            @Deprecated
2834            @Override
2835            public MissingReferences validateImportLayoutsFile(
2836                            long userId, long groupId, boolean privateLayout,
2837                            Map<String, String[]> parameterMap, InputStream inputStream)
2838                    throws PortalException {
2839    
2840                    throw new UnsupportedOperationException();
2841            }
2842    
2843            /**
2844             * @throws     PortalException
2845             * @deprecated As of 7.0.0, replaced by {@link
2846             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#validateImportPortletInfo(
2847             *             ExportImportConfiguration, File)}
2848             */
2849            @Deprecated
2850            @Override
2851            public MissingReferences validateImportPortletInfo(
2852                            ExportImportConfiguration exportImportConfiguration, File file)
2853                    throws PortalException {
2854    
2855                    throw new UnsupportedOperationException();
2856            }
2857    
2858            /**
2859             * @throws     PortalException
2860             * @deprecated As of 7.0.0, replaced by {@link
2861             *             com.liferay.portlet.exportimport.service.ExportImportLocalService#validateImportPortletInfo(
2862             *             ExportImportConfiguration, InputStream)}
2863             */
2864            @Deprecated
2865            @Override
2866            public MissingReferences validateImportPortletInfo(
2867                            ExportImportConfiguration exportImportConfiguration,
2868                            InputStream inputStream)
2869                    throws PortalException {
2870    
2871                    throw new UnsupportedOperationException();
2872            }
2873    
2874            /**
2875             * @throws     PortalException
2876             * @deprecated As of 7.0.0, with no direct replacement
2877             */
2878            @Deprecated
2879            @Override
2880            public MissingReferences validateImportPortletInfo(
2881                            long userId, long plid, long groupId, String portletId,
2882                            Map<String, String[]> parameterMap, File file)
2883                    throws PortalException {
2884    
2885                    throw new UnsupportedOperationException();
2886            }
2887    
2888            /**
2889             * @throws     PortalException
2890             * @deprecated As of 7.0.0, with no direct replacement
2891             */
2892            @Deprecated
2893            @Override
2894            public MissingReferences validateImportPortletInfo(
2895                            long userId, long plid, long groupId, String portletId,
2896                            Map<String, String[]> parameterMap, InputStream inputStream)
2897                    throws PortalException {
2898    
2899                    throw new UnsupportedOperationException();
2900            }
2901    
2902            protected void validateTypeSettingsProperties(
2903                            UnicodeProperties typeSettingsProperties)
2904                    throws PortalException {
2905    
2906                    String sitemapChangeFrequency = typeSettingsProperties.getProperty(
2907                            "sitemap-changefreq");
2908    
2909                    if (Validator.isNotNull(sitemapChangeFrequency) &&
2910                            !sitemapChangeFrequency.equals("always") &&
2911                            !sitemapChangeFrequency.equals("hourly") &&
2912                            !sitemapChangeFrequency.equals("daily") &&
2913                            !sitemapChangeFrequency.equals("weekly") &&
2914                            !sitemapChangeFrequency.equals("monthly") &&
2915                            !sitemapChangeFrequency.equals("yearly") &&
2916                            !sitemapChangeFrequency.equals("never")) {
2917    
2918                            throw new SitemapChangeFrequencyException();
2919                    }
2920    
2921                    String sitemapInclude = typeSettingsProperties.getProperty(
2922                            "sitemap-include");
2923    
2924                    if (Validator.isNotNull(sitemapInclude) &&
2925                            !sitemapInclude.equals("0") && !sitemapInclude.equals("1")) {
2926    
2927                            throw new SitemapIncludeException();
2928                    }
2929    
2930                    String sitemapPriority = typeSettingsProperties.getProperty(
2931                            "sitemap-priority");
2932    
2933                    if (Validator.isNotNull(sitemapPriority)) {
2934                            try {
2935                                    double priority = Double.parseDouble(sitemapPriority);
2936    
2937                                    if ((priority < 0) || (priority > 1)) {
2938                                            throw new SitemapPagePriorityException();
2939                                    }
2940                            }
2941                            catch (NumberFormatException nfe) {
2942                                    throw new SitemapPagePriorityException();
2943                            }
2944                    }
2945            }
2946    
2947            @BeanReference(type = LayoutLocalServiceHelper.class)
2948            protected LayoutLocalServiceHelper layoutLocalServiceHelper;
2949    
2950    }