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