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