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