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                    if (userId > 0) {
082                            resourceLocalService.addResources(
083                                    companyId, 0, userId, LayoutPrototype.class.getName(),
084                                    layoutPrototype.getLayoutPrototypeId(), false, false, false);
085                    }
086    
087                    // Group
088    
089                    String friendlyURL =
090                            "/template-" + layoutPrototype.getLayoutPrototypeId();
091    
092                    Group group = groupLocalService.addGroup(
093                            userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
094                            LayoutPrototype.class.getName(),
095                            layoutPrototype.getLayoutPrototypeId(),
096                            GroupConstants.DEFAULT_LIVE_GROUP_ID, layoutPrototype.getNameMap(),
097                            null, 0, true, GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
098                            friendlyURL, false, true, null);
099    
100                    if (GetterUtil.getBoolean(
101                                    serviceContext.getAttribute("addDefaultLayout"), true)) {
102    
103                            Map<Locale, String> friendlyURLMap = new HashMap<>();
104    
105                            friendlyURLMap.put(LocaleUtil.getSiteDefault(), "/layout");
106    
107                            layoutLocalService.addLayout(
108                                    userId, group.getGroupId(), true,
109                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
110                                    layoutPrototype.getNameMap(), null, null, null, null,
111                                    LayoutConstants.TYPE_PORTLET, StringPool.BLANK, false,
112                                    friendlyURLMap, serviceContext);
113                    }
114    
115                    return layoutPrototype;
116            }
117    
118            /**
119             * @deprecated As of 7.0.0, replaced by {@link #addLayoutPrototype(long,
120             *             long, Map, Map, boolean, ServiceContext)}
121             */
122            @Deprecated
123            @Override
124            public LayoutPrototype addLayoutPrototype(
125                            long userId, long companyId, Map<Locale, String> nameMap,
126                            String description, boolean active, ServiceContext serviceContext)
127                    throws PortalException {
128    
129                    Map<Locale, String> descriptionMap = new HashMap<>();
130    
131                    descriptionMap.put(LocaleUtil.getDefault(), description);
132    
133                    return addLayoutPrototype(
134                            userId, companyId, nameMap, descriptionMap, active, serviceContext);
135            }
136    
137            @Override
138            @SystemEvent(
139                    action = SystemEventConstants.ACTION_SKIP,
140                    type = SystemEventConstants.TYPE_DELETE
141            )
142            public LayoutPrototype deleteLayoutPrototype(
143                            LayoutPrototype layoutPrototype)
144                    throws PortalException {
145    
146                    // Group
147    
148                    if (layoutPersistence.countByLayoutPrototypeUuid(
149                                    layoutPrototype.getUuid()) > 0) {
150    
151                            throw new RequiredLayoutPrototypeException();
152                    }
153    
154                    Group group = layoutPrototype.getGroup();
155    
156                    groupLocalService.deleteGroup(group);
157    
158                    // Resources
159    
160                    resourceLocalService.deleteResource(
161                            layoutPrototype.getCompanyId(), LayoutPrototype.class.getName(),
162                            ResourceConstants.SCOPE_INDIVIDUAL,
163                            layoutPrototype.getLayoutPrototypeId());
164    
165                    // Layout Prototype
166    
167                    layoutPrototypePersistence.remove(layoutPrototype);
168    
169                    // Permission cache
170    
171                    PermissionCacheUtil.clearCache();
172    
173                    return layoutPrototype;
174            }
175    
176            @Override
177            public LayoutPrototype deleteLayoutPrototype(long layoutPrototypeId)
178                    throws PortalException {
179    
180                    LayoutPrototype layoutPrototype =
181                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
182    
183                    return layoutPrototypeLocalService.deleteLayoutPrototype(
184                            layoutPrototype);
185            }
186    
187            @Override
188            public void deleteNondefaultLayoutPrototypes(long companyId)
189                    throws PortalException {
190    
191                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
192    
193                    List<LayoutPrototype> layoutPrototypes =
194                            layoutPrototypePersistence.findByCompanyId(companyId);
195    
196                    for (LayoutPrototype layoutPrototype : layoutPrototypes) {
197                            if (layoutPrototype.getUserId() != defaultUserId) {
198                                    layoutPrototypeLocalService.deleteLayoutPrototype(
199                                            layoutPrototype);
200                            }
201                    }
202            }
203    
204            @Override
205            public LayoutPrototype getLayoutPrototypeByUuidAndCompanyId(
206                            String uuid, long companyId)
207                    throws PortalException {
208    
209                    return layoutPrototypePersistence.findByUuid_C_First(
210                            uuid, companyId, null);
211            }
212    
213            @Override
214            public List<LayoutPrototype> search(
215                    long companyId, Boolean active, int start, int end,
216                    OrderByComparator<LayoutPrototype> obc) {
217    
218                    if (active != null) {
219                            return layoutPrototypePersistence.findByC_A(
220                                    companyId, active, start, end, obc);
221                    }
222                    else {
223                            return layoutPrototypePersistence.findByCompanyId(
224                                    companyId, start, end, obc);
225                    }
226            }
227    
228            @Override
229            public int searchCount(long companyId, Boolean active) {
230                    if (active != null) {
231                            return layoutPrototypePersistence.countByC_A(companyId, active);
232                    }
233                    else {
234                            return layoutPrototypePersistence.countByCompanyId(companyId);
235                    }
236            }
237    
238            @Override
239            public LayoutPrototype updateLayoutPrototype(
240                            long layoutPrototypeId, Map<Locale, String> nameMap,
241                            Map<Locale, String> descriptionMap, boolean active,
242                            ServiceContext serviceContext)
243                    throws PortalException {
244    
245                    // Layout prototype
246    
247                    LayoutPrototype layoutPrototype =
248                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
249    
250                    layoutPrototype.setModifiedDate(
251                            serviceContext.getModifiedDate(new Date()));
252                    layoutPrototype.setNameMap(nameMap);
253                    layoutPrototype.setDescriptionMap(descriptionMap);
254                    layoutPrototype.setActive(active);
255    
256                    layoutPrototypePersistence.update(layoutPrototype);
257    
258                    // Layout
259    
260                    Layout layout = layoutPrototype.getLayout();
261    
262                    layout.setModifiedDate(layoutPrototype.getModifiedDate());
263                    layout.setNameMap(nameMap);
264    
265                    layoutPersistence.update(layout);
266    
267                    return layoutPrototype;
268            }
269    
270            /**
271             * @deprecated As of 7.0.0, replaced by {@link #updateLayoutPrototype(long,
272             *             Map, Map, boolean, ServiceContext)}
273             */
274            @Deprecated
275            @Override
276            public LayoutPrototype updateLayoutPrototype(
277                            long layoutPrototypeId, Map<Locale, String> nameMap,
278                            String description, boolean active, ServiceContext serviceContext)
279                    throws PortalException {
280    
281                    Map<Locale, String> descriptionMap = new HashMap<>();
282    
283                    descriptionMap.put(LocaleUtil.getDefault(), description);
284    
285                    return updateLayoutPrototype(
286                            layoutPrototypeId, nameMap, descriptionMap, active, null);
287            }
288    
289    }