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