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