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