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