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