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 deleteLayoutSetPrototypes()
173                    throws PortalException, SystemException {
174    
175                    List<LayoutSetPrototype> layoutSetPrototypes =
176                            layoutSetPrototypePersistence.findAll();
177    
178                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
179                            layoutSetPrototypeLocalService.deleteLayoutSetPrototype(
180                                    layoutSetPrototype);
181                    }
182            }
183    
184            @Override
185            public void deleteNondefaultLayoutSetPrototypes(long companyId)
186                    throws PortalException, SystemException {
187    
188                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
189    
190                    List<LayoutSetPrototype> layoutSetPrototypes =
191                            layoutSetPrototypePersistence.findByCompanyId(companyId);
192    
193                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
194                            if (layoutSetPrototype.getUserId() != defaultUserId) {
195                                    deleteLayoutSetPrototype(layoutSetPrototype);
196                            }
197                    }
198            }
199    
200            /**
201             * @deprecated As of 6.2.0, replaced by {@link
202             *             #getLayoutSetPrototypeByUuidAndCompanyId(String, long)}
203             */
204            @Override
205            public LayoutSetPrototype getLayoutSetPrototypeByUuid(String uuid)
206                    throws PortalException, SystemException {
207    
208                    return layoutSetPrototypePersistence.findByUuid_First(uuid, null);
209            }
210    
211            @Override
212            public LayoutSetPrototype getLayoutSetPrototypeByUuidAndCompanyId(
213                            String uuid, long companyId)
214                    throws PortalException, SystemException {
215    
216                    return layoutSetPrototypePersistence.findByUuid_C_First(
217                            uuid, companyId, null);
218            }
219    
220            @Override
221            public List<LayoutSetPrototype> getLayoutSetPrototypes(long companyId)
222                    throws SystemException {
223    
224                    return layoutSetPrototypePersistence.findByCompanyId(companyId);
225            }
226    
227            @Override
228            public List<LayoutSetPrototype> search(
229                            long companyId, Boolean active, int start, int end,
230                            OrderByComparator obc)
231                    throws SystemException {
232    
233                    if (active != null) {
234                            return layoutSetPrototypePersistence.findByC_A(
235                                    companyId, active, start, end, obc);
236                    }
237                    else {
238                            return layoutSetPrototypePersistence.findByCompanyId(
239                                    companyId, start, end, obc);
240                    }
241            }
242    
243            @Override
244            public int searchCount(long companyId, Boolean active)
245                    throws SystemException {
246    
247                    if (active != null) {
248                            return layoutSetPrototypePersistence.countByC_A(companyId, active);
249                    }
250                    else {
251                            return layoutSetPrototypePersistence.countByCompanyId(companyId);
252                    }
253            }
254    
255            @Override
256            public LayoutSetPrototype updateLayoutSetPrototype(
257                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
258                            String description, boolean active, boolean layoutsUpdateable,
259                            ServiceContext serviceContext)
260                    throws PortalException, SystemException {
261    
262                    // Layout set prototype
263    
264                    LayoutSetPrototype layoutSetPrototype =
265                            layoutSetPrototypePersistence.findByPrimaryKey(
266                                    layoutSetPrototypeId);
267    
268                    layoutSetPrototype.setModifiedDate(
269                            serviceContext.getModifiedDate(new Date()));
270                    layoutSetPrototype.setNameMap(nameMap);
271                    layoutSetPrototype.setDescription(description);
272                    layoutSetPrototype.setActive(active);
273    
274                    UnicodeProperties settingsProperties =
275                            layoutSetPrototype.getSettingsProperties();
276    
277                    settingsProperties.put(
278                            "layoutsUpdateable", String.valueOf(layoutsUpdateable));
279    
280                    layoutSetPrototype.setSettingsProperties(settingsProperties);
281    
282                    layoutSetPrototypePersistence.update(layoutSetPrototype);
283    
284                    return layoutSetPrototype;
285            }
286    
287            @Override
288            public LayoutSetPrototype updateLayoutSetPrototype(
289                            long layoutSetPrototypeId, String settings)
290                    throws PortalException, SystemException {
291    
292                    // Layout set prototype
293    
294                    LayoutSetPrototype layoutSetPrototype =
295                            layoutSetPrototypePersistence.findByPrimaryKey(
296                                    layoutSetPrototypeId);
297    
298                    layoutSetPrototype.setModifiedDate(new Date());
299                    layoutSetPrototype.setSettings(settings);
300    
301                    layoutSetPrototypePersistence.update(layoutSetPrototype);
302    
303                    // Group
304    
305                    UnicodeProperties settingsProperties =
306                            layoutSetPrototype.getSettingsProperties();
307    
308                    if (!settingsProperties.containsKey("customJspServletContextName")) {
309                            return layoutSetPrototype;
310                    }
311    
312                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
313                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
314    
315                    UnicodeProperties typeSettingsProperties =
316                            group.getTypeSettingsProperties();
317    
318                    typeSettingsProperties.setProperty(
319                            "customJspServletContextName",
320                            settingsProperties.getProperty("customJspServletContextName"));
321    
322                    group.setTypeSettings(typeSettingsProperties.toString());
323    
324                    groupPersistence.update(group);
325    
326                    return layoutSetPrototype;
327            }
328    
329    }