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