001    /**
002     * Copyright (c) 2000-2013 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.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                    resourceLocalService.addResources(
095                            companyId, 0, userId, LayoutPrototype.class.getName(),
096                            layoutPrototype.getLayoutPrototypeId(), false, true, false);
097    
098                    // Group
099    
100                    String friendlyURL =
101                            "/template-" + layoutPrototype.getLayoutPrototypeId();
102    
103                    Group group = groupLocalService.addGroup(
104                            userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
105                            LayoutPrototype.class.getName(),
106                            layoutPrototype.getLayoutPrototypeId(),
107                            GroupConstants.DEFAULT_LIVE_GROUP_ID,
108                            layoutPrototype.getName(LocaleUtil.getDefault()), null, 0, true,
109                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, friendlyURL, false,
110                            true, null);
111    
112                    if (GetterUtil.getBoolean(
113                                    serviceContext.getAttribute("addDefaultLayout"), true)) {
114    
115                            layoutLocalService.addLayout(
116                                    userId, group.getGroupId(), true,
117                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
118                                    layoutPrototype.getName(LocaleUtil.getDefault()), null, null,
119                                    LayoutConstants.TYPE_PORTLET, false, "/layout", serviceContext);
120                    }
121    
122                    return layoutPrototype;
123            }
124    
125            @Override
126            @SystemEvent(
127                    action = SystemEventConstants.ACTION_SKIP,
128                    type = SystemEventConstants.TYPE_DELETE)
129            public LayoutPrototype deleteLayoutPrototype(
130                            LayoutPrototype layoutPrototype)
131                    throws PortalException, SystemException {
132    
133                    // Group
134    
135                    if (layoutPersistence.countByLayoutPrototypeUuid(
136                                    layoutPrototype.getUuid()) > 0) {
137    
138                            throw new RequiredLayoutPrototypeException();
139                    }
140    
141                    Group group = layoutPrototype.getGroup();
142    
143                    groupLocalService.deleteGroup(group);
144    
145                    // Resources
146    
147                    resourceLocalService.deleteResource(
148                            layoutPrototype.getCompanyId(), LayoutPrototype.class.getName(),
149                            ResourceConstants.SCOPE_INDIVIDUAL,
150                            layoutPrototype.getLayoutPrototypeId());
151    
152                    // Layout Prototype
153    
154                    layoutPrototypePersistence.remove(layoutPrototype);
155    
156                    // Permission cache
157    
158                    PermissionCacheUtil.clearCache();
159    
160                    return layoutPrototype;
161            }
162    
163            @Override
164            public LayoutPrototype deleteLayoutPrototype(long layoutPrototypeId)
165                    throws PortalException, SystemException {
166    
167                    LayoutPrototype layoutPrototype =
168                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
169    
170                    return deleteLayoutPrototype(layoutPrototype);
171            }
172    
173            @Override
174            public void deleteNondefaultLayoutPrototypes(long companyId)
175                    throws PortalException, SystemException {
176    
177                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
178    
179                    List<LayoutPrototype> layoutPrototypes =
180                            layoutPrototypePersistence.findByCompanyId(companyId);
181    
182                    for (LayoutPrototype layoutPrototype : layoutPrototypes) {
183                            if (layoutPrototype.getUserId() != defaultUserId) {
184                                    deleteLayoutPrototype(layoutPrototype);
185                            }
186                    }
187            }
188    
189            /**
190             * @deprecated As of 6.2.0, replaced by {@link
191             *             #getLayoutPrototypeByUuidAndCompanyId(String, long)}
192             */
193            @Override
194            public LayoutPrototype getLayoutPrototypeByUuid(String uuid)
195                    throws PortalException, SystemException {
196    
197                    return layoutPrototypePersistence.findByUuid_First(uuid, null);
198            }
199    
200            @Override
201            public LayoutPrototype getLayoutPrototypeByUuidAndCompanyId(
202                            String uuid, long companyId)
203                    throws PortalException, SystemException {
204    
205                    return layoutPrototypePersistence.findByUuid_C_First(
206                            uuid, companyId, null);
207            }
208    
209            @Override
210            public List<LayoutPrototype> search(
211                            long companyId, Boolean active, int start, int end,
212                            OrderByComparator obc)
213                    throws SystemException {
214    
215                    if (active != null) {
216                            return layoutPrototypePersistence.findByC_A(
217                                    companyId, active, start, end, obc);
218                    }
219                    else {
220                            return layoutPrototypePersistence.findByCompanyId(
221                                    companyId, start, end, obc);
222                    }
223            }
224    
225            @Override
226            public int searchCount(long companyId, Boolean active)
227                    throws SystemException {
228    
229                    if (active != null) {
230                            return layoutPrototypePersistence.countByC_A(companyId, active);
231                    }
232                    else {
233                            return layoutPrototypePersistence.countByCompanyId(companyId);
234                    }
235            }
236    
237            /**
238             * @deprecated As of 6.2.0, replaced by {@link #updateLayoutPrototype(long,
239             *             Map, String, boolean, ServiceContext)}
240             */
241            @Override
242            public LayoutPrototype updateLayoutPrototype(
243                            long layoutPrototypeId, Map<Locale, String> nameMap,
244                            String description, boolean active)
245                    throws PortalException, SystemException {
246    
247                    return updateLayoutPrototype(
248                            layoutPrototypeId, nameMap, description, active, null);
249            }
250    
251            @Override
252            public LayoutPrototype updateLayoutPrototype(
253                            long layoutPrototypeId, Map<Locale, String> nameMap,
254                            String description, boolean active, ServiceContext serviceContext)
255                    throws PortalException, SystemException {
256    
257                    // Layout prototype
258    
259                    LayoutPrototype layoutPrototype =
260                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
261    
262                    layoutPrototype.setModifiedDate(
263                            serviceContext.getModifiedDate(new Date()));
264                    layoutPrototype.setNameMap(nameMap);
265                    layoutPrototype.setDescription(description);
266                    layoutPrototype.setActive(active);
267    
268                    layoutPrototypePersistence.update(layoutPrototype);
269    
270                    // Layout
271    
272                    Layout layout = layoutPrototype.getLayout();
273    
274                    layout.setModifiedDate(layoutPrototype.getModifiedDate());
275                    layout.setNameMap(nameMap);
276    
277                    layoutPersistence.update(layout);
278    
279                    return layoutPrototype;
280            }
281    
282    }