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