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