001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.RequiredLayoutPrototypeException;
019    import com.liferay.portal.kernel.model.Group;
020    import com.liferay.portal.kernel.model.GroupConstants;
021    import com.liferay.portal.kernel.model.Layout;
022    import com.liferay.portal.kernel.model.LayoutConstants;
023    import com.liferay.portal.kernel.model.LayoutPrototype;
024    import com.liferay.portal.kernel.model.ResourceConstants;
025    import com.liferay.portal.kernel.model.SystemEventConstants;
026    import com.liferay.portal.kernel.model.User;
027    import com.liferay.portal.kernel.service.ServiceContext;
028    import com.liferay.portal.kernel.systemevent.SystemEvent;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.LocaleUtil;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.StringPool;
033    import com.liferay.portal.security.permission.PermissionCacheUtil;
034    import com.liferay.portal.service.base.LayoutPrototypeLocalServiceBaseImpl;
035    
036    import java.util.Date;
037    import java.util.HashMap;
038    import java.util.List;
039    import java.util.Locale;
040    import java.util.Map;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Jorge Ferrer
045     * @author Vilmos Papp
046     */
047    public class LayoutPrototypeLocalServiceImpl
048            extends LayoutPrototypeLocalServiceBaseImpl {
049    
050            @Override
051            public LayoutPrototype addLayoutPrototype(
052                            long userId, long companyId, Map<Locale, String> nameMap,
053                            Map<Locale, String> descriptionMap, boolean active,
054                            ServiceContext serviceContext)
055                    throws PortalException {
056    
057                    // Layout prototype
058    
059                    User user = userPersistence.findByPrimaryKey(userId);
060                    Date now = new Date();
061    
062                    long layoutPrototypeId = counterLocalService.increment();
063    
064                    LayoutPrototype layoutPrototype = layoutPrototypePersistence.create(
065                            layoutPrototypeId);
066    
067                    layoutPrototype.setUuid(serviceContext.getUuid());
068                    layoutPrototype.setCompanyId(companyId);
069                    layoutPrototype.setUserId(userId);
070                    layoutPrototype.setUserName(user.getFullName());
071                    layoutPrototype.setCreateDate(serviceContext.getCreateDate(now));
072                    layoutPrototype.setModifiedDate(serviceContext.getModifiedDate(now));
073                    layoutPrototype.setNameMap(nameMap);
074                    layoutPrototype.setDescriptionMap(descriptionMap);
075                    layoutPrototype.setActive(active);
076    
077                    layoutPrototypePersistence.update(layoutPrototype);
078    
079                    // Resources
080    
081                    resourceLocalService.addResources(
082                            companyId, 0, userId, LayoutPrototype.class.getName(),
083                            layoutPrototype.getLayoutPrototypeId(), false, true, false);
084    
085                    // Group
086    
087                    String friendlyURL =
088                            "/template-" + layoutPrototype.getLayoutPrototypeId();
089    
090                    Group group = groupLocalService.addGroup(
091                            userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
092                            LayoutPrototype.class.getName(),
093                            layoutPrototype.getLayoutPrototypeId(),
094                            GroupConstants.DEFAULT_LIVE_GROUP_ID, layoutPrototype.getNameMap(),
095                            null, 0, true, GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
096                            friendlyURL, false, true, null);
097    
098                    if (GetterUtil.getBoolean(
099                                    serviceContext.getAttribute("addDefaultLayout"), true)) {
100    
101                            Map<Locale, String> friendlyURLMap = new HashMap<>();
102    
103                            friendlyURLMap.put(LocaleUtil.getSiteDefault(), "/layout");
104    
105                            layoutLocalService.addLayout(
106                                    userId, group.getGroupId(), true,
107                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
108                                    layoutPrototype.getNameMap(), null, null, null, null,
109                                    LayoutConstants.TYPE_PORTLET, StringPool.BLANK, false,
110                                    friendlyURLMap, serviceContext);
111                    }
112    
113                    return layoutPrototype;
114            }
115    
116            /**
117             * @deprecated As of 7.0.0, replaced by {@link #addLayoutPrototype(long,
118             *             long, Map, Map, boolean, ServiceContext)}
119             */
120            @Deprecated
121            @Override
122            public LayoutPrototype addLayoutPrototype(
123                            long userId, long companyId, Map<Locale, String> nameMap,
124                            String description, boolean active, ServiceContext serviceContext)
125                    throws PortalException {
126    
127                    Map<Locale, String> descriptionMap = new HashMap<>();
128    
129                    descriptionMap.put(LocaleUtil.getDefault(), description);
130    
131                    return addLayoutPrototype(
132                            userId, companyId, nameMap, descriptionMap, active, serviceContext);
133            }
134    
135            @Override
136            @SystemEvent(
137                    action = SystemEventConstants.ACTION_SKIP,
138                    type = SystemEventConstants.TYPE_DELETE
139            )
140            public LayoutPrototype deleteLayoutPrototype(
141                            LayoutPrototype layoutPrototype)
142                    throws PortalException {
143    
144                    // Group
145    
146                    if (layoutPersistence.countByLayoutPrototypeUuid(
147                                    layoutPrototype.getUuid()) > 0) {
148    
149                            throw new RequiredLayoutPrototypeException();
150                    }
151    
152                    Group group = layoutPrototype.getGroup();
153    
154                    groupLocalService.deleteGroup(group);
155    
156                    // Resources
157    
158                    resourceLocalService.deleteResource(
159                            layoutPrototype.getCompanyId(), LayoutPrototype.class.getName(),
160                            ResourceConstants.SCOPE_INDIVIDUAL,
161                            layoutPrototype.getLayoutPrototypeId());
162    
163                    // Layout Prototype
164    
165                    layoutPrototypePersistence.remove(layoutPrototype);
166    
167                    // Permission cache
168    
169                    PermissionCacheUtil.clearCache();
170    
171                    return layoutPrototype;
172            }
173    
174            @Override
175            public LayoutPrototype deleteLayoutPrototype(long layoutPrototypeId)
176                    throws PortalException {
177    
178                    LayoutPrototype layoutPrototype =
179                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
180    
181                    return layoutPrototypeLocalService.deleteLayoutPrototype(
182                            layoutPrototype);
183            }
184    
185            @Override
186            public void deleteNondefaultLayoutPrototypes(long companyId)
187                    throws PortalException {
188    
189                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
190    
191                    List<LayoutPrototype> layoutPrototypes =
192                            layoutPrototypePersistence.findByCompanyId(companyId);
193    
194                    for (LayoutPrototype layoutPrototype : layoutPrototypes) {
195                            if (layoutPrototype.getUserId() != defaultUserId) {
196                                    layoutPrototypeLocalService.deleteLayoutPrototype(
197                                            layoutPrototype);
198                            }
199                    }
200            }
201    
202            @Override
203            public LayoutPrototype getLayoutPrototypeByUuidAndCompanyId(
204                            String uuid, long companyId)
205                    throws PortalException {
206    
207                    return layoutPrototypePersistence.findByUuid_C_First(
208                            uuid, companyId, null);
209            }
210    
211            @Override
212            public List<LayoutPrototype> search(
213                    long companyId, Boolean active, int start, int end,
214                    OrderByComparator<LayoutPrototype> obc) {
215    
216                    if (active != null) {
217                            return layoutPrototypePersistence.findByC_A(
218                                    companyId, active, start, end, obc);
219                    }
220                    else {
221                            return layoutPrototypePersistence.findByCompanyId(
222                                    companyId, start, end, obc);
223                    }
224            }
225    
226            @Override
227            public int searchCount(long companyId, Boolean active) {
228                    if (active != null) {
229                            return layoutPrototypePersistence.countByC_A(companyId, active);
230                    }
231                    else {
232                            return layoutPrototypePersistence.countByCompanyId(companyId);
233                    }
234            }
235    
236            @Override
237            public LayoutPrototype updateLayoutPrototype(
238                            long layoutPrototypeId, Map<Locale, String> nameMap,
239                            Map<Locale, String> descriptionMap, boolean active,
240                            ServiceContext serviceContext)
241                    throws PortalException {
242    
243                    // Layout prototype
244    
245                    LayoutPrototype layoutPrototype =
246                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
247    
248                    layoutPrototype.setModifiedDate(
249                            serviceContext.getModifiedDate(new Date()));
250                    layoutPrototype.setNameMap(nameMap);
251                    layoutPrototype.setDescriptionMap(descriptionMap);
252                    layoutPrototype.setActive(active);
253    
254                    layoutPrototypePersistence.update(layoutPrototype);
255    
256                    // Layout
257    
258                    Layout layout = layoutPrototype.getLayout();
259    
260                    layout.setModifiedDate(layoutPrototype.getModifiedDate());
261                    layout.setNameMap(nameMap);
262    
263                    layoutPersistence.update(layout);
264    
265                    return layoutPrototype;
266            }
267    
268            /**
269             * @deprecated As of 7.0.0, replaced by {@link #updateLayoutPrototype(long,
270             *             Map, Map, boolean, ServiceContext)}
271             */
272            @Deprecated
273            @Override
274            public LayoutPrototype updateLayoutPrototype(
275                            long layoutPrototypeId, Map<Locale, String> nameMap,
276                            String description, boolean active, ServiceContext serviceContext)
277                    throws PortalException {
278    
279                    Map<Locale, String> descriptionMap = new HashMap<>();
280    
281                    descriptionMap.put(LocaleUtil.getDefault(), description);
282    
283                    return updateLayoutPrototype(
284                            layoutPrototypeId, nameMap, descriptionMap, active, null);
285            }
286    
287    }