001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
020    import com.liferay.portal.kernel.systemevent.SystemEvent;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.UnicodeProperties;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.GroupConstants;
027    import com.liferay.portal.model.LayoutConstants;
028    import com.liferay.portal.model.LayoutSetPrototype;
029    import com.liferay.portal.model.ResourceConstants;
030    import com.liferay.portal.model.SystemEventConstants;
031    import com.liferay.portal.model.User;
032    import com.liferay.portal.security.permission.PermissionCacheUtil;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portal.service.base.LayoutSetPrototypeLocalServiceBaseImpl;
035    
036    import java.util.Date;
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                            String description, boolean active, boolean layoutsUpdateable,
052                            ServiceContext serviceContext)
053                    throws PortalException, SystemException {
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.setDescription(description);
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            @Override
121            @SystemEvent(
122                    action = SystemEventConstants.ACTION_SKIP,
123                    type = SystemEventConstants.TYPE_DELETE)
124            public LayoutSetPrototype deleteLayoutSetPrototype(
125                            LayoutSetPrototype layoutSetPrototype)
126                    throws PortalException, SystemException {
127    
128                    // Group
129    
130                    if (layoutSetPersistence.countByLayoutSetPrototypeUuid(
131                                    layoutSetPrototype.getUuid()) > 0) {
132    
133                            throw new RequiredLayoutSetPrototypeException();
134                    }
135    
136                    Group group = layoutSetPrototype.getGroup();
137    
138                    groupLocalService.deleteGroup(group);
139    
140                    // Resources
141    
142                    resourceLocalService.deleteResource(
143                            layoutSetPrototype.getCompanyId(),
144                            LayoutSetPrototype.class.getName(),
145                            ResourceConstants.SCOPE_INDIVIDUAL,
146                            layoutSetPrototype.getLayoutSetPrototypeId());
147    
148                    // Layout set prototype
149    
150                    layoutSetPrototypePersistence.remove(layoutSetPrototype);
151    
152                    // Permission cache
153    
154                    PermissionCacheUtil.clearCache();
155    
156                    return layoutSetPrototype;
157            }
158    
159            @Override
160            public LayoutSetPrototype deleteLayoutSetPrototype(
161                            long layoutSetPrototypeId)
162                    throws PortalException, SystemException {
163    
164                    LayoutSetPrototype layoutSetPrototype =
165                            layoutSetPrototypePersistence.findByPrimaryKey(
166                                    layoutSetPrototypeId);
167    
168                    return deleteLayoutSetPrototype(layoutSetPrototype);
169            }
170    
171            @Override
172            public void deleteNondefaultLayoutSetPrototypes(long companyId)
173                    throws PortalException, SystemException {
174    
175                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
176    
177                    List<LayoutSetPrototype> layoutSetPrototypes =
178                            layoutSetPrototypePersistence.findByCompanyId(companyId);
179    
180                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
181                            if (layoutSetPrototype.getUserId() != defaultUserId) {
182                                    deleteLayoutSetPrototype(layoutSetPrototype);
183                            }
184                    }
185            }
186    
187            /**
188             * @deprecated As of 6.2.0, replaced by {@link
189             *             #getLayoutSetPrototypeByUuidAndCompanyId(String, long)}
190             */
191            @Override
192            public LayoutSetPrototype getLayoutSetPrototypeByUuid(String uuid)
193                    throws PortalException, SystemException {
194    
195                    return layoutSetPrototypePersistence.findByUuid_First(uuid, null);
196            }
197    
198            @Override
199            public LayoutSetPrototype getLayoutSetPrototypeByUuidAndCompanyId(
200                            String uuid, long companyId)
201                    throws PortalException, SystemException {
202    
203                    return layoutSetPrototypePersistence.findByUuid_C_First(
204                            uuid, companyId, null);
205            }
206    
207            @Override
208            public List<LayoutSetPrototype> getLayoutSetPrototypes(long companyId)
209                    throws SystemException {
210    
211                    return layoutSetPrototypePersistence.findByCompanyId(companyId);
212            }
213    
214            @Override
215            public List<LayoutSetPrototype> search(
216                            long companyId, Boolean active, int start, int end,
217                            OrderByComparator obc)
218                    throws SystemException {
219    
220                    if (active != null) {
221                            return layoutSetPrototypePersistence.findByC_A(
222                                    companyId, active, start, end, obc);
223                    }
224                    else {
225                            return layoutSetPrototypePersistence.findByCompanyId(
226                                    companyId, start, end, obc);
227                    }
228            }
229    
230            @Override
231            public int searchCount(long companyId, Boolean active)
232                    throws SystemException {
233    
234                    if (active != null) {
235                            return layoutSetPrototypePersistence.countByC_A(companyId, active);
236                    }
237                    else {
238                            return layoutSetPrototypePersistence.countByCompanyId(companyId);
239                    }
240            }
241    
242            @Override
243            public LayoutSetPrototype updateLayoutSetPrototype(
244                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
245                            String description, boolean active, boolean layoutsUpdateable,
246                            ServiceContext serviceContext)
247                    throws PortalException, SystemException {
248    
249                    // Layout set prototype
250    
251                    LayoutSetPrototype layoutSetPrototype =
252                            layoutSetPrototypePersistence.findByPrimaryKey(
253                                    layoutSetPrototypeId);
254    
255                    layoutSetPrototype.setModifiedDate(
256                            serviceContext.getModifiedDate(new Date()));
257                    layoutSetPrototype.setNameMap(nameMap);
258                    layoutSetPrototype.setDescription(description);
259                    layoutSetPrototype.setActive(active);
260    
261                    UnicodeProperties settingsProperties =
262                            layoutSetPrototype.getSettingsProperties();
263    
264                    settingsProperties.put(
265                            "layoutsUpdateable", String.valueOf(layoutsUpdateable));
266    
267                    layoutSetPrototype.setSettingsProperties(settingsProperties);
268    
269                    layoutSetPrototypePersistence.update(layoutSetPrototype);
270    
271                    // Group
272    
273                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
274                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
275    
276                    group.setName(layoutSetPrototype.getName(LocaleUtil.getDefault()));
277    
278                    groupPersistence.update(group);
279    
280                    return layoutSetPrototype;
281            }
282    
283            @Override
284            public LayoutSetPrototype updateLayoutSetPrototype(
285                            long layoutSetPrototypeId, String settings)
286                    throws PortalException, SystemException {
287    
288                    // Layout set prototype
289    
290                    LayoutSetPrototype layoutSetPrototype =
291                            layoutSetPrototypePersistence.findByPrimaryKey(
292                                    layoutSetPrototypeId);
293    
294                    layoutSetPrototype.setModifiedDate(new Date());
295                    layoutSetPrototype.setSettings(settings);
296    
297                    layoutSetPrototypePersistence.update(layoutSetPrototype);
298    
299                    // Group
300    
301                    UnicodeProperties settingsProperties =
302                            layoutSetPrototype.getSettingsProperties();
303    
304                    if (!settingsProperties.containsKey("customJspServletContextName")) {
305                            return layoutSetPrototype;
306                    }
307    
308                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
309                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
310    
311                    UnicodeProperties typeSettingsProperties =
312                            group.getTypeSettingsProperties();
313    
314                    typeSettingsProperties.setProperty(
315                            "customJspServletContextName",
316                            settingsProperties.getProperty("customJspServletContextName"));
317    
318                    group.setTypeSettings(typeSettingsProperties.toString());
319    
320                    groupPersistence.update(group);
321    
322                    return layoutSetPrototype;
323            }
324    
325    }