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