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