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.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.UnicodeProperties;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.GroupConstants;
025    import com.liferay.portal.model.LayoutConstants;
026    import com.liferay.portal.model.LayoutSetPrototype;
027    import com.liferay.portal.model.ResourceConstants;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.security.permission.PermissionCacheUtil;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.service.base.LayoutSetPrototypeLocalServiceBaseImpl;
032    
033    import java.util.Date;
034    import java.util.List;
035    import java.util.Locale;
036    import java.util.Map;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     * @author Ryan Park
041     */
042    public class LayoutSetPrototypeLocalServiceImpl
043            extends LayoutSetPrototypeLocalServiceBaseImpl {
044    
045            public LayoutSetPrototype addLayoutSetPrototype(
046                            long userId, long companyId, Map<Locale, String> nameMap,
047                            String description, boolean active, boolean layoutsUpdateable,
048                            ServiceContext serviceContext)
049                    throws PortalException, SystemException {
050    
051                    // Layout set prototype
052    
053                    User user = userPersistence.findByPrimaryKey(userId);
054                    Date now = new Date();
055    
056                    long layoutSetPrototypeId = counterLocalService.increment();
057    
058                    LayoutSetPrototype layoutSetPrototype =
059                            layoutSetPrototypePersistence.create(layoutSetPrototypeId);
060    
061                    layoutSetPrototype.setUuid(serviceContext.getUuid());
062                    layoutSetPrototype.setCompanyId(companyId);
063                    layoutSetPrototype.setUserId(userId);
064                    layoutSetPrototype.setUserName(user.getFullName());
065                    layoutSetPrototype.setCreateDate(serviceContext.getCreateDate(now));
066                    layoutSetPrototype.setModifiedDate(serviceContext.getModifiedDate(now));
067                    layoutSetPrototype.setNameMap(nameMap);
068                    layoutSetPrototype.setDescription(description);
069                    layoutSetPrototype.setActive(active);
070    
071                    UnicodeProperties settingsProperties =
072                            layoutSetPrototype.getSettingsProperties();
073    
074                    settingsProperties.put(
075                            "layoutsUpdateable", String.valueOf(layoutsUpdateable));
076    
077                    layoutSetPrototype.setSettingsProperties(settingsProperties);
078    
079                    layoutSetPrototypePersistence.update(layoutSetPrototype);
080    
081                    // Resources
082    
083                    if (userId > 0) {
084                            resourceLocalService.addResources(
085                                    companyId, 0, userId, LayoutSetPrototype.class.getName(),
086                                    layoutSetPrototype.getLayoutSetPrototypeId(), false, false,
087                                    false);
088                    }
089    
090                    // Group
091    
092                    String friendlyURL =
093                            "/template-" + layoutSetPrototype.getLayoutSetPrototypeId();
094    
095                    Group group = groupLocalService.addGroup(
096                            userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
097                            LayoutSetPrototype.class.getName(),
098                            layoutSetPrototype.getLayoutSetPrototypeId(),
099                            GroupConstants.DEFAULT_LIVE_GROUP_ID,
100                            layoutSetPrototype.getName(LocaleUtil.getDefault()), null, 0,
101                            friendlyURL, false, true, serviceContext);
102    
103                    layoutLocalService.addLayout(
104                            userId, group.getGroupId(), true,
105                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "home", null, null,
106                            LayoutConstants.TYPE_PORTLET, false, "/home", serviceContext);
107    
108                    return layoutSetPrototype;
109            }
110    
111            @Override
112            public LayoutSetPrototype deleteLayoutSetPrototype(
113                            LayoutSetPrototype layoutSetPrototype)
114                    throws PortalException, SystemException {
115    
116                    // Group
117    
118                    if (layoutSetPersistence.countByLayoutSetPrototypeUuid(
119                                    layoutSetPrototype.getUuid()) > 0) {
120    
121                            throw new RequiredLayoutSetPrototypeException();
122                    }
123    
124                    Group group = layoutSetPrototype.getGroup();
125    
126                    groupLocalService.deleteGroup(group);
127    
128                    // Resources
129    
130                    resourceLocalService.deleteResource(
131                            layoutSetPrototype.getCompanyId(),
132                            LayoutSetPrototype.class.getName(),
133                            ResourceConstants.SCOPE_INDIVIDUAL,
134                            layoutSetPrototype.getLayoutSetPrototypeId());
135    
136                    // Layout set prototype
137    
138                    layoutSetPrototypePersistence.remove(layoutSetPrototype);
139    
140                    // Permission cache
141    
142                    PermissionCacheUtil.clearCache();
143    
144                    return layoutSetPrototype;
145            }
146    
147            @Override
148            public LayoutSetPrototype deleteLayoutSetPrototype(
149                            long layoutSetPrototypeId)
150                    throws PortalException, SystemException {
151    
152                    LayoutSetPrototype layoutSetPrototype =
153                            layoutSetPrototypePersistence.findByPrimaryKey(
154                                    layoutSetPrototypeId);
155    
156                    return deleteLayoutSetPrototype(layoutSetPrototype);
157            }
158    
159            public LayoutSetPrototype fetchLayoutSetPrototypeByUuidAndCompanyId(
160                            String uuid, long companyId)
161                    throws SystemException {
162    
163                    return layoutSetPrototypePersistence.fetchByUuid_C_First(
164                            uuid, companyId, null);
165            }
166    
167            /**
168             * @deprecated As of 6.2.0, replaced by {@link
169             *             #getLayoutSetPrototypeByUuidAndCompanyId(String, long)}
170             */
171            public LayoutSetPrototype getLayoutSetPrototypeByUuid(String uuid)
172                    throws PortalException, SystemException {
173    
174                    return layoutSetPrototypePersistence.findByUuid_First(uuid, null);
175            }
176    
177            public LayoutSetPrototype getLayoutSetPrototypeByUuidAndCompanyId(
178                            String uuid, long companyId)
179                    throws PortalException, SystemException {
180    
181                    return layoutSetPrototypePersistence.findByUuid_C_First(
182                            uuid, companyId, null);
183            }
184    
185            public List<LayoutSetPrototype> search(
186                            long companyId, Boolean active, int start, int end,
187                            OrderByComparator obc)
188                    throws SystemException {
189    
190                    if (active != null) {
191                            return layoutSetPrototypePersistence.findByC_A(
192                                    companyId, active, start, end, obc);
193                    }
194                    else {
195                            return layoutSetPrototypePersistence.findByCompanyId(
196                                    companyId, start, end, obc);
197                    }
198            }
199    
200            public int searchCount(long companyId, Boolean active)
201                    throws SystemException {
202    
203                    if (active != null) {
204                            return layoutSetPrototypePersistence.countByC_A(companyId, active);
205                    }
206                    else {
207                            return layoutSetPrototypePersistence.countByCompanyId(companyId);
208                    }
209            }
210    
211            public LayoutSetPrototype updateLayoutSetPrototype(
212                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
213                            String description, boolean active, boolean layoutsUpdateable,
214                            ServiceContext serviceContext)
215                    throws PortalException, SystemException {
216    
217                    // Layout set prototype
218    
219                    LayoutSetPrototype layoutSetPrototype =
220                            layoutSetPrototypePersistence.findByPrimaryKey(
221                                    layoutSetPrototypeId);
222    
223                    layoutSetPrototype.setModifiedDate(
224                            serviceContext.getModifiedDate(new Date()));
225                    layoutSetPrototype.setNameMap(nameMap);
226                    layoutSetPrototype.setDescription(description);
227                    layoutSetPrototype.setActive(active);
228    
229                    UnicodeProperties settingsProperties =
230                            layoutSetPrototype.getSettingsProperties();
231    
232                    settingsProperties.put(
233                            "layoutsUpdateable", String.valueOf(layoutsUpdateable));
234    
235                    layoutSetPrototype.setSettingsProperties(settingsProperties);
236    
237                    layoutSetPrototypePersistence.update(layoutSetPrototype);
238    
239                    // Group
240    
241                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
242                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
243    
244                    group.setName(layoutSetPrototype.getName(LocaleUtil.getDefault()));
245    
246                    groupPersistence.update(group);
247    
248                    return layoutSetPrototype;
249            }
250    
251            public LayoutSetPrototype updateLayoutSetPrototype(
252                            long layoutSetPrototypeId, String settings)
253                    throws PortalException, SystemException {
254    
255                    // Layout set prototype
256    
257                    LayoutSetPrototype layoutSetPrototype =
258                            layoutSetPrototypePersistence.findByPrimaryKey(
259                                    layoutSetPrototypeId);
260    
261                    layoutSetPrototype.setModifiedDate(new Date());
262                    layoutSetPrototype.setSettings(settings);
263    
264                    layoutSetPrototypePersistence.update(layoutSetPrototype);
265    
266                    // Group
267    
268                    UnicodeProperties settingsProperties =
269                            layoutSetPrototype.getSettingsProperties();
270    
271                    if (!settingsProperties.containsKey("customJspServletContextName")) {
272                            return layoutSetPrototype;
273                    }
274    
275                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
276                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
277    
278                    UnicodeProperties typeSettingsProperties =
279                            group.getTypeSettingsProperties();
280    
281                    typeSettingsProperties.setProperty(
282                            "customJspServletContextName",
283                            settingsProperties.getProperty("customJspServletContextName"));
284    
285                    group.setTypeSettings(typeSettingsProperties.toString());
286    
287                    groupPersistence.update(group);
288    
289                    return layoutSetPrototype;
290            }
291    
292    }