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.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.getName(LocaleUtil.getDefault()), 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<Locale, String>();
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            public LayoutSetPrototype deleteLayoutSetPrototype(
146                            LayoutSetPrototype layoutSetPrototype)
147                    throws PortalException {
148    
149                    // Group
150    
151                    if (layoutSetPersistence.countByLayoutSetPrototypeUuid(
152                                    layoutSetPrototype.getUuid()) > 0) {
153    
154                            throw new RequiredLayoutSetPrototypeException();
155                    }
156    
157                    Group group = layoutSetPrototype.getGroup();
158    
159                    groupLocalService.deleteGroup(group);
160    
161                    // Resources
162    
163                    resourceLocalService.deleteResource(
164                            layoutSetPrototype.getCompanyId(),
165                            LayoutSetPrototype.class.getName(),
166                            ResourceConstants.SCOPE_INDIVIDUAL,
167                            layoutSetPrototype.getLayoutSetPrototypeId());
168    
169                    // Layout set prototype
170    
171                    layoutSetPrototypePersistence.remove(layoutSetPrototype);
172    
173                    // Permission cache
174    
175                    PermissionCacheUtil.clearCache();
176    
177                    return layoutSetPrototype;
178            }
179    
180            @Override
181            public LayoutSetPrototype deleteLayoutSetPrototype(
182                            long layoutSetPrototypeId)
183                    throws PortalException {
184    
185                    LayoutSetPrototype layoutSetPrototype =
186                            layoutSetPrototypePersistence.findByPrimaryKey(
187                                    layoutSetPrototypeId);
188    
189                    return deleteLayoutSetPrototype(layoutSetPrototype);
190            }
191    
192            @Override
193            public void deleteLayoutSetPrototypes() throws PortalException {
194                    List<LayoutSetPrototype> layoutSetPrototypes =
195                            layoutSetPrototypePersistence.findAll();
196    
197                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
198                            layoutSetPrototypeLocalService.deleteLayoutSetPrototype(
199                                    layoutSetPrototype);
200                    }
201            }
202    
203            @Override
204            public void deleteNondefaultLayoutSetPrototypes(long companyId)
205                    throws PortalException {
206    
207                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
208    
209                    List<LayoutSetPrototype> layoutSetPrototypes =
210                            layoutSetPrototypePersistence.findByCompanyId(companyId);
211    
212                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
213                            if (layoutSetPrototype.getUserId() != defaultUserId) {
214                                    deleteLayoutSetPrototype(layoutSetPrototype);
215                            }
216                    }
217            }
218    
219            /**
220             * @deprecated As of 6.2.0, replaced by {@link
221             *             #getLayoutSetPrototypeByUuidAndCompanyId(String, long)}
222             */
223            @Deprecated
224            @Override
225            public LayoutSetPrototype getLayoutSetPrototypeByUuid(String uuid)
226                    throws PortalException {
227    
228                    return layoutSetPrototypePersistence.findByUuid_First(uuid, null);
229            }
230    
231            @Override
232            public LayoutSetPrototype getLayoutSetPrototypeByUuidAndCompanyId(
233                            String uuid, long companyId)
234                    throws PortalException {
235    
236                    return layoutSetPrototypePersistence.findByUuid_C_First(
237                            uuid, companyId, null);
238            }
239    
240            @Override
241            public List<LayoutSetPrototype> getLayoutSetPrototypes(long companyId) {
242                    return layoutSetPrototypePersistence.findByCompanyId(companyId);
243            }
244    
245            @Override
246            public List<LayoutSetPrototype> search(
247                    long companyId, Boolean active, int start, int end,
248                    OrderByComparator<LayoutSetPrototype> obc) {
249    
250                    if (active != null) {
251                            return layoutSetPrototypePersistence.findByC_A(
252                                    companyId, active, start, end, obc);
253                    }
254                    else {
255                            return layoutSetPrototypePersistence.findByCompanyId(
256                                    companyId, start, end, obc);
257                    }
258            }
259    
260            @Override
261            public int searchCount(long companyId, Boolean active) {
262                    if (active != null) {
263                            return layoutSetPrototypePersistence.countByC_A(companyId, active);
264                    }
265                    else {
266                            return layoutSetPrototypePersistence.countByCompanyId(companyId);
267                    }
268            }
269    
270            @Override
271            public LayoutSetPrototype updateLayoutSetPrototype(
272                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
273                            Map<Locale, String> descriptionMap, boolean active,
274                            boolean layoutsUpdateable, ServiceContext serviceContext)
275                    throws PortalException {
276    
277                    // Layout set prototype
278    
279                    LayoutSetPrototype layoutSetPrototype =
280                            layoutSetPrototypePersistence.findByPrimaryKey(
281                                    layoutSetPrototypeId);
282    
283                    layoutSetPrototype.setModifiedDate(
284                            serviceContext.getModifiedDate(new Date()));
285                    layoutSetPrototype.setNameMap(nameMap);
286                    layoutSetPrototype.setDescriptionMap(descriptionMap);
287                    layoutSetPrototype.setActive(active);
288    
289                    UnicodeProperties settingsProperties =
290                            layoutSetPrototype.getSettingsProperties();
291    
292                    settingsProperties.put(
293                            "layoutsUpdateable", String.valueOf(layoutsUpdateable));
294    
295                    layoutSetPrototype.setSettingsProperties(settingsProperties);
296    
297                    layoutSetPrototypePersistence.update(layoutSetPrototype);
298    
299                    return layoutSetPrototype;
300            }
301    
302            /**
303             * @deprecated As of 7.0.0, replaced by {@link
304             *             #updateLayoutSetPrototype(long, Map, Map, boolean, boolean,
305             *             ServiceContext)}
306             */
307            @Deprecated
308            @Override
309            public LayoutSetPrototype updateLayoutSetPrototype(
310                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
311                            String description, boolean active, boolean layoutsUpdateable,
312                            ServiceContext serviceContext)
313                    throws PortalException {
314    
315                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
316    
317                    descriptionMap.put(LocaleUtil.getDefault(), description);
318    
319                    return updateLayoutSetPrototype(
320                            layoutSetPrototypeId, nameMap, descriptionMap, active,
321                            layoutsUpdateable, serviceContext);
322            }
323    
324            @Override
325            public LayoutSetPrototype updateLayoutSetPrototype(
326                            long layoutSetPrototypeId, String settings)
327                    throws PortalException {
328    
329                    // Layout set prototype
330    
331                    LayoutSetPrototype layoutSetPrototype =
332                            layoutSetPrototypePersistence.findByPrimaryKey(
333                                    layoutSetPrototypeId);
334    
335                    layoutSetPrototype.setModifiedDate(new Date());
336                    layoutSetPrototype.setSettings(settings);
337    
338                    layoutSetPrototypePersistence.update(layoutSetPrototype);
339    
340                    // Group
341    
342                    UnicodeProperties settingsProperties =
343                            layoutSetPrototype.getSettingsProperties();
344    
345                    if (!settingsProperties.containsKey("customJspServletContextName")) {
346                            return layoutSetPrototype;
347                    }
348    
349                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
350                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
351    
352                    UnicodeProperties typeSettingsProperties =
353                            group.getTypeSettingsProperties();
354    
355                    typeSettingsProperties.setProperty(
356                            "customJspServletContextName",
357                            settingsProperties.getProperty("customJspServletContextName"));
358    
359                    group.setTypeSettings(typeSettingsProperties.toString());
360    
361                    groupPersistence.update(group);
362    
363                    return layoutSetPrototype;
364            }
365    
366    }