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.RequiredLayoutSetPrototypeException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.systemevent.SystemEvent;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.GroupConstants;
026    import com.liferay.portal.model.LayoutConstants;
027    import com.liferay.portal.model.LayoutSetPrototype;
028    import com.liferay.portal.model.ResourceConstants;
029    import com.liferay.portal.model.SystemEventConstants;
030    import com.liferay.portal.model.User;
031    import com.liferay.portal.security.permission.PermissionCacheUtil;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.base.LayoutSetPrototypeLocalServiceBaseImpl;
034    
035    import java.util.Date;
036    import java.util.HashMap;
037    import java.util.List;
038    import java.util.Locale;
039    import java.util.Map;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Ryan Park
044     */
045    public class LayoutSetPrototypeLocalServiceImpl
046            extends LayoutSetPrototypeLocalServiceBaseImpl {
047    
048            @Override
049            public LayoutSetPrototype addLayoutSetPrototype(
050                            long userId, long companyId, Map<Locale, String> nameMap,
051                            Map<Locale, String> descriptionMap, boolean active,
052                            boolean layoutsUpdateable, ServiceContext serviceContext)
053                    throws PortalException {
054    
055                    // Layout set prototype
056    
057                    User user = userPersistence.findByPrimaryKey(userId);
058                    Date now = new Date();
059    
060                    long layoutSetPrototypeId = counterLocalService.increment();
061    
062                    LayoutSetPrototype layoutSetPrototype =
063                            layoutSetPrototypePersistence.create(layoutSetPrototypeId);
064    
065                    layoutSetPrototype.setUuid(serviceContext.getUuid());
066                    layoutSetPrototype.setCompanyId(companyId);
067                    layoutSetPrototype.setUserId(userId);
068                    layoutSetPrototype.setUserName(user.getFullName());
069                    layoutSetPrototype.setCreateDate(serviceContext.getCreateDate(now));
070                    layoutSetPrototype.setModifiedDate(serviceContext.getModifiedDate(now));
071                    layoutSetPrototype.setNameMap(nameMap);
072                    layoutSetPrototype.setDescriptionMap(descriptionMap);
073                    layoutSetPrototype.setActive(active);
074    
075                    UnicodeProperties settingsProperties =
076                            layoutSetPrototype.getSettingsProperties();
077    
078                    settingsProperties.put(
079                            "layoutsUpdateable", String.valueOf(layoutsUpdateable));
080    
081                    layoutSetPrototype.setSettingsProperties(settingsProperties);
082    
083                    layoutSetPrototypePersistence.update(layoutSetPrototype);
084    
085                    // Resources
086    
087                    if (userId > 0) {
088                            resourceLocalService.addResources(
089                                    companyId, 0, userId, LayoutSetPrototype.class.getName(),
090                                    layoutSetPrototype.getLayoutSetPrototypeId(), false, false,
091                                    false);
092                    }
093    
094                    // Group
095    
096                    String friendlyURL =
097                            "/template-" + layoutSetPrototype.getLayoutSetPrototypeId();
098    
099                    Group group = groupLocalService.addGroup(
100                            userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
101                            LayoutSetPrototype.class.getName(),
102                            layoutSetPrototype.getLayoutSetPrototypeId(),
103                            GroupConstants.DEFAULT_LIVE_GROUP_ID,
104                            layoutSetPrototype.getNameMap(), null, 0, true,
105                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, friendlyURL, false,
106                            true, serviceContext);
107    
108                    if (GetterUtil.getBoolean(
109                                    serviceContext.getAttribute("addDefaultLayout"), true)) {
110    
111                            layoutLocalService.addLayout(
112                                    userId, group.getGroupId(), true,
113                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "home", null, null,
114                                    LayoutConstants.TYPE_PORTLET, false, "/home", serviceContext);
115                    }
116    
117                    return layoutSetPrototype;
118            }
119    
120            /**
121             * @deprecated As of 7.0.0, replaced by {@link #addLayoutSetPrototype(long,
122             *             long, Map, Map, boolean, boolean, ServiceContext)}
123             */
124            @Deprecated
125            @Override
126            public LayoutSetPrototype addLayoutSetPrototype(
127                            long userId, long companyId, Map<Locale, String> nameMap,
128                            String description, boolean active, boolean layoutsUpdateable,
129                            ServiceContext serviceContext)
130                    throws PortalException {
131    
132                    Map<Locale, String> descriptionMap = new HashMap<>();
133    
134                    descriptionMap.put(LocaleUtil.getDefault(), description);
135    
136                    return addLayoutSetPrototype(
137                            userId, companyId, nameMap, descriptionMap, active,
138                            layoutsUpdateable, serviceContext);
139            }
140    
141            @Override
142            @SystemEvent(
143                    action = SystemEventConstants.ACTION_SKIP,
144                    type = SystemEventConstants.TYPE_DELETE
145            )
146            public LayoutSetPrototype deleteLayoutSetPrototype(
147                            LayoutSetPrototype layoutSetPrototype)
148                    throws PortalException {
149    
150                    // Group
151    
152                    if (layoutSetPersistence.countByLayoutSetPrototypeUuid(
153                                    layoutSetPrototype.getUuid()) > 0) {
154    
155                            throw new RequiredLayoutSetPrototypeException();
156                    }
157    
158                    Group group = layoutSetPrototype.getGroup();
159    
160                    groupLocalService.deleteGroup(group);
161    
162                    // Resources
163    
164                    resourceLocalService.deleteResource(
165                            layoutSetPrototype.getCompanyId(),
166                            LayoutSetPrototype.class.getName(),
167                            ResourceConstants.SCOPE_INDIVIDUAL,
168                            layoutSetPrototype.getLayoutSetPrototypeId());
169    
170                    // Layout set prototype
171    
172                    layoutSetPrototypePersistence.remove(layoutSetPrototype);
173    
174                    // Permission cache
175    
176                    PermissionCacheUtil.clearCache();
177    
178                    return layoutSetPrototype;
179            }
180    
181            @Override
182            public LayoutSetPrototype deleteLayoutSetPrototype(
183                            long layoutSetPrototypeId)
184                    throws PortalException {
185    
186                    LayoutSetPrototype layoutSetPrototype =
187                            layoutSetPrototypePersistence.findByPrimaryKey(
188                                    layoutSetPrototypeId);
189    
190                    return deleteLayoutSetPrototype(layoutSetPrototype);
191            }
192    
193            @Override
194            public void deleteLayoutSetPrototypes() throws PortalException {
195                    List<LayoutSetPrototype> layoutSetPrototypes =
196                            layoutSetPrototypePersistence.findAll();
197    
198                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
199                            layoutSetPrototypeLocalService.deleteLayoutSetPrototype(
200                                    layoutSetPrototype);
201                    }
202            }
203    
204            @Override
205            public void deleteNondefaultLayoutSetPrototypes(long companyId)
206                    throws PortalException {
207    
208                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
209    
210                    List<LayoutSetPrototype> layoutSetPrototypes =
211                            layoutSetPrototypePersistence.findByCompanyId(companyId);
212    
213                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
214                            if (layoutSetPrototype.getUserId() != defaultUserId) {
215                                    deleteLayoutSetPrototype(layoutSetPrototype);
216                            }
217                    }
218            }
219    
220            /**
221             * @deprecated As of 6.2.0, replaced by {@link
222             *             #getLayoutSetPrototypeByUuidAndCompanyId(String, long)}
223             */
224            @Deprecated
225            @Override
226            public LayoutSetPrototype getLayoutSetPrototypeByUuid(String uuid)
227                    throws PortalException {
228    
229                    return layoutSetPrototypePersistence.findByUuid_First(uuid, null);
230            }
231    
232            @Override
233            public LayoutSetPrototype getLayoutSetPrototypeByUuidAndCompanyId(
234                            String uuid, long companyId)
235                    throws PortalException {
236    
237                    return layoutSetPrototypePersistence.findByUuid_C_First(
238                            uuid, companyId, null);
239            }
240    
241            @Override
242            public List<LayoutSetPrototype> getLayoutSetPrototypes(long companyId) {
243                    return layoutSetPrototypePersistence.findByCompanyId(companyId);
244            }
245    
246            @Override
247            public List<LayoutSetPrototype> search(
248                    long companyId, Boolean active, int start, int end,
249                    OrderByComparator<LayoutSetPrototype> obc) {
250    
251                    if (active != null) {
252                            return layoutSetPrototypePersistence.findByC_A(
253                                    companyId, active, start, end, obc);
254                    }
255                    else {
256                            return layoutSetPrototypePersistence.findByCompanyId(
257                                    companyId, start, end, obc);
258                    }
259            }
260    
261            @Override
262            public int searchCount(long companyId, Boolean active) {
263                    if (active != null) {
264                            return layoutSetPrototypePersistence.countByC_A(companyId, active);
265                    }
266                    else {
267                            return layoutSetPrototypePersistence.countByCompanyId(companyId);
268                    }
269            }
270    
271            @Override
272            public LayoutSetPrototype updateLayoutSetPrototype(
273                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
274                            Map<Locale, String> descriptionMap, boolean active,
275                            boolean layoutsUpdateable, ServiceContext serviceContext)
276                    throws PortalException {
277    
278                    // Layout set prototype
279    
280                    LayoutSetPrototype layoutSetPrototype =
281                            layoutSetPrototypePersistence.findByPrimaryKey(
282                                    layoutSetPrototypeId);
283    
284                    layoutSetPrototype.setModifiedDate(
285                            serviceContext.getModifiedDate(new Date()));
286                    layoutSetPrototype.setNameMap(nameMap);
287                    layoutSetPrototype.setDescriptionMap(descriptionMap);
288                    layoutSetPrototype.setActive(active);
289    
290                    UnicodeProperties settingsProperties =
291                            layoutSetPrototype.getSettingsProperties();
292    
293                    settingsProperties.put(
294                            "layoutsUpdateable", String.valueOf(layoutsUpdateable));
295    
296                    layoutSetPrototype.setSettingsProperties(settingsProperties);
297    
298                    layoutSetPrototypePersistence.update(layoutSetPrototype);
299    
300                    return layoutSetPrototype;
301            }
302    
303            /**
304             * @deprecated As of 7.0.0, replaced by {@link
305             *             #updateLayoutSetPrototype(long, Map, Map, boolean, boolean,
306             *             ServiceContext)}
307             */
308            @Deprecated
309            @Override
310            public LayoutSetPrototype updateLayoutSetPrototype(
311                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
312                            String description, boolean active, boolean layoutsUpdateable,
313                            ServiceContext serviceContext)
314                    throws PortalException {
315    
316                    Map<Locale, String> descriptionMap = new HashMap<>();
317    
318                    descriptionMap.put(LocaleUtil.getDefault(), description);
319    
320                    return updateLayoutSetPrototype(
321                            layoutSetPrototypeId, nameMap, descriptionMap, active,
322                            layoutsUpdateable, serviceContext);
323            }
324    
325            @Override
326            public LayoutSetPrototype updateLayoutSetPrototype(
327                            long layoutSetPrototypeId, String settings)
328                    throws PortalException {
329    
330                    // Layout set prototype
331    
332                    LayoutSetPrototype layoutSetPrototype =
333                            layoutSetPrototypePersistence.findByPrimaryKey(
334                                    layoutSetPrototypeId);
335    
336                    layoutSetPrototype.setModifiedDate(new Date());
337                    layoutSetPrototype.setSettings(settings);
338    
339                    layoutSetPrototypePersistence.update(layoutSetPrototype);
340    
341                    // Group
342    
343                    UnicodeProperties settingsProperties =
344                            layoutSetPrototype.getSettingsProperties();
345    
346                    if (!settingsProperties.containsKey("customJspServletContextName")) {
347                            return layoutSetPrototype;
348                    }
349    
350                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
351                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
352    
353                    UnicodeProperties typeSettingsProperties =
354                            group.getTypeSettingsProperties();
355    
356                    typeSettingsProperties.setProperty(
357                            "customJspServletContextName",
358                            settingsProperties.getProperty("customJspServletContextName"));
359    
360                    group.setTypeSettings(typeSettingsProperties.toString());
361    
362                    groupPersistence.update(group);
363    
364                    return layoutSetPrototype;
365            }
366    
367    }