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.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            @Override
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, false);
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, LayoutPrototype.class.getName(),
077                            layoutPrototype.getLayoutPrototypeId(),
078                            layoutPrototype.getName(LocaleUtil.getDefault()), null, 0,
079                            friendlyURL, false, true, null);
080    
081                    ServiceContext serviceContext = new ServiceContext();
082    
083                    layoutLocalService.addLayout(
084                            userId, group.getGroupId(), true,
085                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
086                            layoutPrototype.getName(LocaleUtil.getDefault()), null, null,
087                            LayoutConstants.TYPE_PORTLET, false, "/layout", serviceContext);
088    
089                    return layoutPrototype;
090            }
091    
092            @Override
093            public LayoutPrototype deleteLayoutPrototype(
094                            LayoutPrototype layoutPrototype)
095                    throws PortalException, SystemException {
096    
097                    // Group
098    
099                    if (layoutPersistence.countByLayoutPrototypeUuid(
100                                    layoutPrototype.getUuid()) > 0) {
101    
102                            throw new RequiredLayoutPrototypeException();
103                    }
104    
105                    Group group = layoutPrototype.getGroup();
106    
107                    groupLocalService.deleteGroup(group);
108    
109                    // Resources
110    
111                    resourceLocalService.deleteResource(
112                            layoutPrototype.getCompanyId(), LayoutPrototype.class.getName(),
113                            ResourceConstants.SCOPE_INDIVIDUAL,
114                            layoutPrototype.getLayoutPrototypeId());
115    
116                    // Layout Prototype
117    
118                    layoutPrototypePersistence.remove(layoutPrototype);
119    
120                    // Permission cache
121    
122                    PermissionCacheUtil.clearCache();
123    
124                    return layoutPrototype;
125            }
126    
127            @Override
128            public LayoutPrototype deleteLayoutPrototype(long layoutPrototypeId)
129                    throws PortalException, SystemException {
130    
131                    LayoutPrototype layoutPrototype =
132                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
133    
134                    return deleteLayoutPrototype(layoutPrototype);
135            }
136    
137            @Override
138            public LayoutPrototype getLayoutPrototypeByUuid(String uuid)
139                    throws PortalException, SystemException {
140    
141                    return layoutPrototypePersistence.findByUuid_First(uuid, null);
142            }
143    
144            @Override
145            public List<LayoutPrototype> search(
146                            long companyId, Boolean active, int start, int end,
147                            OrderByComparator obc)
148                    throws SystemException {
149    
150                    if (active != null) {
151                            return layoutPrototypePersistence.findByC_A(
152                                    companyId, active, start, end, obc);
153                    }
154                    else {
155                            return layoutPrototypePersistence.findByCompanyId(
156                                    companyId, start, end, obc);
157                    }
158            }
159    
160            @Override
161            public int searchCount(long companyId, Boolean active)
162                    throws SystemException {
163    
164                    if (active != null) {
165                            return layoutPrototypePersistence.countByC_A(companyId, active);
166                    }
167                    else {
168                            return layoutPrototypePersistence.countByCompanyId(companyId);
169                    }
170            }
171    
172            @Override
173            public LayoutPrototype updateLayoutPrototype(
174                            long layoutPrototypeId, Map<Locale, String> nameMap,
175                            String description, boolean active)
176                    throws PortalException, SystemException {
177    
178                    // Layout prototype
179    
180                    LayoutPrototype layoutPrototype =
181                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
182    
183                    layoutPrototype.setNameMap(nameMap);
184                    layoutPrototype.setDescription(description);
185                    layoutPrototype.setActive(active);
186    
187                    layoutPrototypePersistence.update(layoutPrototype, false);
188    
189                    // Group
190    
191                    Group group = groupLocalService.getLayoutPrototypeGroup(
192                            layoutPrototype.getCompanyId(), layoutPrototypeId);
193    
194                    group.setName(layoutPrototype.getName(LocaleUtil.getDefault()));
195    
196                    groupPersistence.update(group, false);
197    
198                    return layoutPrototype;
199            }
200    
201    }