001    /**
002     * Copyright (c) 2000-2013 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.RequiredLayoutPrototypeException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.systemevent.SystemEvent;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.GroupConstants;
026    import com.liferay.portal.model.LayoutConstants;
027    import com.liferay.portal.model.LayoutPrototype;
028    import com.liferay.portal.model.ResourceConstants;
029    import com.liferay.portal.model.SystemEventConstants;
030    import com.liferay.portal.model.User;
031    import com.liferay.portal.security.permission.PermissionCacheUtil;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.base.LayoutPrototypeLocalServiceBaseImpl;
034    
035    import java.util.Date;
036    import java.util.List;
037    import java.util.Locale;
038    import java.util.Map;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Jorge Ferrer
043     * @author Vilmos Papp
044     */
045    public class LayoutPrototypeLocalServiceImpl
046            extends LayoutPrototypeLocalServiceBaseImpl {
047    
048            /**
049             * @deprecated As of 6.2.0, replaced by {@link #addLayoutPrototype(long,
050             *             long, Map, String, boolean, ServiceContext)}
051             */
052            @Override
053            public LayoutPrototype addLayoutPrototype(
054                            long userId, long companyId, Map<Locale, String> nameMap,
055                            String description, boolean active)
056                    throws PortalException, SystemException {
057    
058                    return addLayoutPrototype(
059                            userId, companyId, nameMap, description, active,
060                            new ServiceContext());
061            }
062    
063            @Override
064            public LayoutPrototype addLayoutPrototype(
065                            long userId, long companyId, Map<Locale, String> nameMap,
066                            String description, boolean active, ServiceContext serviceContext)
067                    throws PortalException, SystemException {
068    
069                    // Layout prototype
070    
071                    User user = userPersistence.findByPrimaryKey(userId);
072                    Date now = new Date();
073    
074                    long layoutPrototypeId = counterLocalService.increment();
075    
076                    LayoutPrototype layoutPrototype = layoutPrototypePersistence.create(
077                            layoutPrototypeId);
078    
079                    layoutPrototype.setUuid(serviceContext.getUuid());
080                    layoutPrototype.setCompanyId(companyId);
081                    layoutPrototype.setUserId(userId);
082                    layoutPrototype.setUserName(user.getFullName());
083                    layoutPrototype.setCreateDate(serviceContext.getCreateDate(now));
084                    layoutPrototype.setModifiedDate(serviceContext.getModifiedDate(now));
085                    layoutPrototype.setNameMap(nameMap);
086                    layoutPrototype.setDescription(description);
087                    layoutPrototype.setActive(active);
088    
089                    layoutPrototypePersistence.update(layoutPrototype);
090    
091                    // Resources
092    
093                    if (userId > 0) {
094                            resourceLocalService.addResources(
095                                    companyId, 0, userId, LayoutPrototype.class.getName(),
096                                    layoutPrototype.getLayoutPrototypeId(), false, false, false);
097                    }
098    
099                    // Group
100    
101                    String friendlyURL =
102                            "/template-" + layoutPrototype.getLayoutPrototypeId();
103    
104                    Group group = groupLocalService.addGroup(
105                            userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
106                            LayoutPrototype.class.getName(),
107                            layoutPrototype.getLayoutPrototypeId(),
108                            GroupConstants.DEFAULT_LIVE_GROUP_ID,
109                            layoutPrototype.getName(LocaleUtil.getDefault()), null, 0, true,
110                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, friendlyURL, false,
111                            true, null);
112    
113                    if (GetterUtil.getBoolean(
114                                    serviceContext.getAttribute("addDefaultLayout"), true)) {
115    
116                            layoutLocalService.addLayout(
117                                    userId, group.getGroupId(), true,
118                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
119                                    layoutPrototype.getName(LocaleUtil.getDefault()), null, null,
120                                    LayoutConstants.TYPE_PORTLET, false, "/layout", serviceContext);
121                    }
122    
123                    return layoutPrototype;
124            }
125    
126            @Override
127            @SystemEvent(
128                    action = SystemEventConstants.ACTION_SKIP,
129                    type = SystemEventConstants.TYPE_DELETE)
130            public LayoutPrototype deleteLayoutPrototype(
131                            LayoutPrototype layoutPrototype)
132                    throws PortalException, SystemException {
133    
134                    // Group
135    
136                    if (layoutPersistence.countByLayoutPrototypeUuid(
137                                    layoutPrototype.getUuid()) > 0) {
138    
139                            throw new RequiredLayoutPrototypeException();
140                    }
141    
142                    Group group = layoutPrototype.getGroup();
143    
144                    groupLocalService.deleteGroup(group);
145    
146                    // Resources
147    
148                    resourceLocalService.deleteResource(
149                            layoutPrototype.getCompanyId(), LayoutPrototype.class.getName(),
150                            ResourceConstants.SCOPE_INDIVIDUAL,
151                            layoutPrototype.getLayoutPrototypeId());
152    
153                    // Layout Prototype
154    
155                    layoutPrototypePersistence.remove(layoutPrototype);
156    
157                    // Permission cache
158    
159                    PermissionCacheUtil.clearCache();
160    
161                    return layoutPrototype;
162            }
163    
164            @Override
165            public LayoutPrototype deleteLayoutPrototype(long layoutPrototypeId)
166                    throws PortalException, SystemException {
167    
168                    LayoutPrototype layoutPrototype =
169                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
170    
171                    return deleteLayoutPrototype(layoutPrototype);
172            }
173    
174            @Override
175            public void deleteNondefaultLayoutPrototypes(long companyId)
176                    throws PortalException, SystemException {
177    
178                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
179    
180                    List<LayoutPrototype> layoutPrototypes =
181                            layoutPrototypePersistence.findByCompanyId(companyId);
182    
183                    for (LayoutPrototype layoutPrototype : layoutPrototypes) {
184                            if (layoutPrototype.getUserId() != defaultUserId) {
185                                    deleteLayoutPrototype(layoutPrototype);
186                            }
187                    }
188            }
189    
190            /**
191             * @deprecated As of 6.2.0, replaced by {@link
192             *             #getLayoutPrototypeByUuidAndCompanyId(String, long)}
193             */
194            @Override
195            public LayoutPrototype getLayoutPrototypeByUuid(String uuid)
196                    throws PortalException, SystemException {
197    
198                    return layoutPrototypePersistence.findByUuid_First(uuid, null);
199            }
200    
201            @Override
202            public LayoutPrototype getLayoutPrototypeByUuidAndCompanyId(
203                            String uuid, long companyId)
204                    throws PortalException, SystemException {
205    
206                    return layoutPrototypePersistence.findByUuid_C_First(
207                            uuid, companyId, null);
208            }
209    
210            @Override
211            public List<LayoutPrototype> search(
212                            long companyId, Boolean active, int start, int end,
213                            OrderByComparator obc)
214                    throws SystemException {
215    
216                    if (active != null) {
217                            return layoutPrototypePersistence.findByC_A(
218                                    companyId, active, start, end, obc);
219                    }
220                    else {
221                            return layoutPrototypePersistence.findByCompanyId(
222                                    companyId, start, end, obc);
223                    }
224            }
225    
226            @Override
227            public int searchCount(long companyId, Boolean active)
228                    throws SystemException {
229    
230                    if (active != null) {
231                            return layoutPrototypePersistence.countByC_A(companyId, active);
232                    }
233                    else {
234                            return layoutPrototypePersistence.countByCompanyId(companyId);
235                    }
236            }
237    
238            /**
239             * @deprecated As of 6.2.0, replaced by {@link #updateLayoutPrototype(long,
240             *             Map, String, boolean, ServiceContext)}
241             */
242            @Override
243            public LayoutPrototype updateLayoutPrototype(
244                            long layoutPrototypeId, Map<Locale, String> nameMap,
245                            String description, boolean active)
246                    throws PortalException, SystemException {
247    
248                    return updateLayoutPrototype(
249                            layoutPrototypeId, nameMap, description, active, null);
250            }
251    
252            @Override
253            public LayoutPrototype updateLayoutPrototype(
254                            long layoutPrototypeId, Map<Locale, String> nameMap,
255                            String description, boolean active, ServiceContext serviceContext)
256                    throws PortalException, SystemException {
257    
258                    // Layout prototype
259    
260                    LayoutPrototype layoutPrototype =
261                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
262    
263                    layoutPrototype.setModifiedDate(
264                            serviceContext.getModifiedDate(new Date()));
265                    layoutPrototype.setNameMap(nameMap);
266                    layoutPrototype.setDescription(description);
267                    layoutPrototype.setActive(active);
268    
269                    layoutPrototypePersistence.update(layoutPrototype);
270    
271                    // Group
272    
273                    Group group = groupLocalService.getLayoutPrototypeGroup(
274                            layoutPrototype.getCompanyId(), layoutPrototypeId);
275    
276                    group.setName(layoutPrototype.getName(LocaleUtil.getDefault()));
277    
278                    groupPersistence.update(group);
279    
280                    return layoutPrototype;
281            }
282    
283    }