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