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