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