001    /**
002     * Copyright (c) 2000-2010 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.LayoutSetPrototype;
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.LayoutSetPrototypeLocalServiceBaseImpl;
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     */
036    public class LayoutSetPrototypeLocalServiceImpl
037            extends LayoutSetPrototypeLocalServiceBaseImpl {
038    
039            public LayoutSetPrototype addLayoutSetPrototype(
040                            long userId, long companyId, Map<Locale, String> nameMap,
041                            String description, boolean active)
042                    throws PortalException, SystemException {
043    
044                    // Layout set prototype
045    
046                    long layoutSetPrototypeId = counterLocalService.increment();
047    
048                    LayoutSetPrototype layoutSetPrototype =
049                            layoutSetPrototypePersistence.create(layoutSetPrototypeId);
050    
051                    layoutSetPrototype.setCompanyId(companyId);
052                    layoutSetPrototype.setNameMap(nameMap);
053                    layoutSetPrototype.setDescription(description);
054                    layoutSetPrototype.setActive(active);
055    
056                    layoutSetPrototypePersistence.update(layoutSetPrototype, false);
057    
058                    // Resources
059    
060                    if (userId > 0) {
061                            resourceLocalService.addResources(
062                                    companyId, 0, userId, LayoutSetPrototype.class.getName(),
063                                    layoutSetPrototype.getLayoutSetPrototypeId(), false, false,
064                                    false);
065                    }
066    
067                    // Group
068    
069                    String friendlyURL =
070                            "/template-" + layoutSetPrototype.getLayoutSetPrototypeId();
071    
072                    Group group = groupLocalService.addGroup(
073                            userId, LayoutSetPrototype.class.getName(),
074                            layoutSetPrototype.getLayoutSetPrototypeId(),
075                            layoutSetPrototype.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, "home", null, null,
083                            LayoutConstants.TYPE_PORTLET, false, "/home", serviceContext);
084    
085                    return layoutSetPrototype;
086            }
087    
088            public void deleteLayoutSetPrototype(long layoutSetPrototypeId)
089                    throws PortalException, SystemException {
090    
091                    LayoutSetPrototype layoutSetPrototype =
092                            layoutSetPrototypePersistence.findByPrimaryKey(
093                                    layoutSetPrototypeId);
094    
095                    // Group
096    
097                    Group group = layoutSetPrototype.getGroup();
098    
099                    groupLocalService.deleteGroup(group.getGroupId());
100    
101                    // Resources
102    
103                    resourceLocalService.deleteResource(
104                            layoutSetPrototype.getCompanyId(),
105                            LayoutSetPrototype.class.getName(),
106                            ResourceConstants.SCOPE_INDIVIDUAL,
107                            layoutSetPrototype.getLayoutSetPrototypeId());
108    
109                    // Layout set prototype
110    
111                    layoutSetPrototypePersistence.remove(layoutSetPrototype);
112    
113                    // Permission cache
114    
115                    PermissionCacheUtil.clearCache();
116            }
117    
118            public List<LayoutSetPrototype> search(
119                            long companyId, Boolean active, int start, int end,
120                            OrderByComparator obc)
121                    throws SystemException {
122    
123                    if (active != null) {
124                            return layoutSetPrototypePersistence.findByC_A(
125                                    companyId, active, start, end, obc);
126                    }
127                    else {
128                            return layoutSetPrototypePersistence.findByCompanyId(
129                                    companyId, start, end, obc);
130                    }
131            }
132    
133            public int searchCount(long companyId, Boolean active)
134                    throws SystemException {
135    
136                    if (active != null) {
137                            return layoutSetPrototypePersistence.countByC_A(companyId, active);
138                    }
139                    else {
140                            return layoutSetPrototypePersistence.countByCompanyId(companyId);
141                    }
142            }
143    
144            public LayoutSetPrototype updateLayoutSetPrototype(
145                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
146                            String description, boolean active)
147                    throws PortalException, SystemException {
148    
149                    // Layout set prototype
150    
151                    LayoutSetPrototype layoutSetPrototype =
152                            layoutSetPrototypePersistence.findByPrimaryKey(
153                                    layoutSetPrototypeId);
154    
155                    layoutSetPrototype.setNameMap(nameMap);
156                    layoutSetPrototype.setDescription(description);
157                    layoutSetPrototype.setActive(active);
158    
159                    layoutSetPrototypePersistence.update(layoutSetPrototype, false);
160    
161                    // Group
162    
163                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
164                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
165    
166                    group.setName(layoutSetPrototype.getName(LocaleUtil.getDefault()));
167    
168                    groupPersistence.update(group, false);
169    
170                    return layoutSetPrototype;
171            }
172    
173    }