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.LayoutConstants;
025    import com.liferay.portal.model.LayoutSet;
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, 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 void deleteLayoutSetPrototype(LayoutSetPrototype layoutSetPrototype)
107                    throws PortalException, SystemException {
108    
109                    List<LayoutSet> layoutSets =
110                            layoutSetLocalService.getLayoutSetsByLayoutSetPrototypeUuid(
111                                    layoutSetPrototype.getUuid());
112    
113                    if (!layoutSets.isEmpty()) {
114                            throw new RequiredLayoutSetPrototypeException();
115                    }
116    
117                    // Group
118    
119                    Group group = layoutSetPrototype.getGroup();
120    
121                    groupLocalService.deleteGroup(group);
122    
123                    // Resources
124    
125                    resourceLocalService.deleteResource(
126                            layoutSetPrototype.getCompanyId(),
127                            LayoutSetPrototype.class.getName(),
128                            ResourceConstants.SCOPE_INDIVIDUAL,
129                            layoutSetPrototype.getLayoutSetPrototypeId());
130    
131                    // Layout set prototype
132    
133                    layoutSetPrototypePersistence.remove(layoutSetPrototype);
134    
135                    // Permission cache
136    
137                    PermissionCacheUtil.clearCache();
138            }
139    
140            @Override
141            public void deleteLayoutSetPrototype(long layoutSetPrototypeId)
142                    throws PortalException, SystemException {
143    
144                    LayoutSetPrototype layoutSetPrototype =
145                            layoutSetPrototypePersistence.findByPrimaryKey(
146                                    layoutSetPrototypeId);
147    
148                    deleteLayoutSetPrototype(layoutSetPrototype);
149            }
150    
151            public LayoutSetPrototype getLayoutSetPrototypeByUuid(String uuid)
152                    throws PortalException, SystemException {
153    
154                    return layoutSetPrototypePersistence.findByUuid_First(uuid, null);
155            }
156    
157            public List<LayoutSetPrototype> search(
158                            long companyId, Boolean active, int start, int end,
159                            OrderByComparator obc)
160                    throws SystemException {
161    
162                    if (active != null) {
163                            return layoutSetPrototypePersistence.findByC_A(
164                                    companyId, active, start, end, obc);
165                    }
166                    else {
167                            return layoutSetPrototypePersistence.findByCompanyId(
168                                    companyId, start, end, obc);
169                    }
170            }
171    
172            public int searchCount(long companyId, Boolean active)
173                    throws SystemException {
174    
175                    if (active != null) {
176                            return layoutSetPrototypePersistence.countByC_A(companyId, active);
177                    }
178                    else {
179                            return layoutSetPrototypePersistence.countByCompanyId(companyId);
180                    }
181            }
182    
183            public LayoutSetPrototype updateLayoutSetPrototype(
184                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
185                            String description, boolean active, boolean layoutsUpdateable,
186                            ServiceContext serviceContext)
187                    throws PortalException, SystemException {
188    
189                    // Layout set prototype
190    
191                    LayoutSetPrototype layoutSetPrototype =
192                            layoutSetPrototypePersistence.findByPrimaryKey(
193                                    layoutSetPrototypeId);
194    
195                    layoutSetPrototype.setModifiedDate(
196                            serviceContext.getModifiedDate(new Date()));
197                    layoutSetPrototype.setNameMap(nameMap);
198                    layoutSetPrototype.setDescription(description);
199                    layoutSetPrototype.setActive(active);
200    
201                    UnicodeProperties settingsProperties =
202                            layoutSetPrototype.getSettingsProperties();
203    
204                    settingsProperties.put(
205                            "layoutsUpdateable", String.valueOf(layoutsUpdateable));
206    
207                    layoutSetPrototype.setSettingsProperties(settingsProperties);
208    
209                    layoutSetPrototypePersistence.update(layoutSetPrototype, false);
210    
211                    // Group
212    
213                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
214                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
215    
216                    group.setName(layoutSetPrototype.getName(LocaleUtil.getDefault()));
217    
218                    groupPersistence.update(group, false);
219    
220                    return layoutSetPrototype;
221            }
222    
223            public LayoutSetPrototype updateLayoutSetPrototype(
224                            long layoutSetPrototypeId, String settings)
225                    throws PortalException, SystemException {
226    
227                    // Layout set prototype
228    
229                    LayoutSetPrototype layoutSetPrototype =
230                            layoutSetPrototypePersistence.findByPrimaryKey(
231                                    layoutSetPrototypeId);
232    
233                    layoutSetPrototype.setModifiedDate(new Date());
234                    layoutSetPrototype.setSettings(settings);
235    
236                    layoutSetPrototypePersistence.update(layoutSetPrototype, false);
237    
238                    // Group
239    
240                    UnicodeProperties settingsProperties =
241                            layoutSetPrototype.getSettingsProperties();
242    
243                    if (!settingsProperties.containsKey("customJspServletContextName")) {
244                            return layoutSetPrototype;
245                    }
246    
247                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
248                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
249    
250                    UnicodeProperties typeSettingsProperties =
251                            group.getTypeSettingsProperties();
252    
253                    typeSettingsProperties.setProperty(
254                            "customJspServletContextName",
255                            settingsProperties.getProperty("customJspServletContextName"));
256    
257                    group.setTypeSettings(typeSettingsProperties.toString());
258    
259                    groupPersistence.update(group, false);
260    
261                    return layoutSetPrototype;
262            }
263    
264    }