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            @Override
221            public LayoutSetPrototype getLayoutSetPrototypeByUuidAndCompanyId(
222                            String uuid, long companyId)
223                    throws PortalException {
224    
225                    return layoutSetPrototypePersistence.findByUuid_C_First(
226                            uuid, companyId, null);
227            }
228    
229            @Override
230            public List<LayoutSetPrototype> getLayoutSetPrototypes(long companyId) {
231                    return layoutSetPrototypePersistence.findByCompanyId(companyId);
232            }
233    
234            @Override
235            public List<LayoutSetPrototype> search(
236                    long companyId, Boolean active, int start, int end,
237                    OrderByComparator<LayoutSetPrototype> obc) {
238    
239                    if (active != null) {
240                            return layoutSetPrototypePersistence.findByC_A(
241                                    companyId, active, start, end, obc);
242                    }
243                    else {
244                            return layoutSetPrototypePersistence.findByCompanyId(
245                                    companyId, start, end, obc);
246                    }
247            }
248    
249            @Override
250            public int searchCount(long companyId, Boolean active) {
251                    if (active != null) {
252                            return layoutSetPrototypePersistence.countByC_A(companyId, active);
253                    }
254                    else {
255                            return layoutSetPrototypePersistence.countByCompanyId(companyId);
256                    }
257            }
258    
259            @Override
260            public LayoutSetPrototype updateLayoutSetPrototype(
261                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
262                            Map<Locale, String> descriptionMap, boolean active,
263                            boolean layoutsUpdateable, ServiceContext serviceContext)
264                    throws PortalException {
265    
266                    // Layout set prototype
267    
268                    LayoutSetPrototype layoutSetPrototype =
269                            layoutSetPrototypePersistence.findByPrimaryKey(
270                                    layoutSetPrototypeId);
271    
272                    layoutSetPrototype.setModifiedDate(
273                            serviceContext.getModifiedDate(new Date()));
274                    layoutSetPrototype.setNameMap(nameMap);
275                    layoutSetPrototype.setDescriptionMap(descriptionMap);
276                    layoutSetPrototype.setActive(active);
277    
278                    UnicodeProperties settingsProperties =
279                            layoutSetPrototype.getSettingsProperties();
280    
281                    settingsProperties.put(
282                            "layoutsUpdateable", String.valueOf(layoutsUpdateable));
283    
284                    layoutSetPrototype.setSettingsProperties(settingsProperties);
285    
286                    layoutSetPrototypePersistence.update(layoutSetPrototype);
287    
288                    return layoutSetPrototype;
289            }
290    
291            /**
292             * @deprecated As of 7.0.0, replaced by {@link
293             *             #updateLayoutSetPrototype(long, Map, Map, boolean, boolean,
294             *             ServiceContext)}
295             */
296            @Deprecated
297            @Override
298            public LayoutSetPrototype updateLayoutSetPrototype(
299                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
300                            String description, boolean active, boolean layoutsUpdateable,
301                            ServiceContext serviceContext)
302                    throws PortalException {
303    
304                    Map<Locale, String> descriptionMap = new HashMap<>();
305    
306                    descriptionMap.put(LocaleUtil.getDefault(), description);
307    
308                    return updateLayoutSetPrototype(
309                            layoutSetPrototypeId, nameMap, descriptionMap, active,
310                            layoutsUpdateable, serviceContext);
311            }
312    
313            @Override
314            public LayoutSetPrototype updateLayoutSetPrototype(
315                            long layoutSetPrototypeId, String settings)
316                    throws PortalException {
317    
318                    // Layout set prototype
319    
320                    LayoutSetPrototype layoutSetPrototype =
321                            layoutSetPrototypePersistence.findByPrimaryKey(
322                                    layoutSetPrototypeId);
323    
324                    layoutSetPrototype.setModifiedDate(new Date());
325                    layoutSetPrototype.setSettings(settings);
326    
327                    layoutSetPrototypePersistence.update(layoutSetPrototype);
328    
329                    // Group
330    
331                    UnicodeProperties settingsProperties =
332                            layoutSetPrototype.getSettingsProperties();
333    
334                    if (!settingsProperties.containsKey("customJspServletContextName")) {
335                            return layoutSetPrototype;
336                    }
337    
338                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
339                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
340    
341                    UnicodeProperties typeSettingsProperties =
342                            group.getTypeSettingsProperties();
343    
344                    typeSettingsProperties.setProperty(
345                            "customJspServletContextName",
346                            settingsProperties.getProperty("customJspServletContextName"));
347    
348                    group.setTypeSettings(typeSettingsProperties.toString());
349    
350                    groupPersistence.update(group);
351    
352                    return layoutSetPrototype;
353            }
354    
355    }