1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.LocaleUtil;
20  import com.liferay.portal.kernel.util.OrderByComparator;
21  import com.liferay.portal.model.Group;
22  import com.liferay.portal.model.LayoutConstants;
23  import com.liferay.portal.model.LayoutSetPrototype;
24  import com.liferay.portal.model.ResourceConstants;
25  import com.liferay.portal.security.permission.PermissionCacheUtil;
26  import com.liferay.portal.service.ServiceContext;
27  import com.liferay.portal.service.base.LayoutSetPrototypeLocalServiceBaseImpl;
28  
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  /**
34   * <a href="LayoutSetPrototypeLocalServiceImpl.java.html"><b><i>View Source</i>
35   * </b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class LayoutSetPrototypeLocalServiceImpl
40      extends LayoutSetPrototypeLocalServiceBaseImpl {
41  
42      public LayoutSetPrototype addLayoutSetPrototype(
43              long userId, long companyId, Map<Locale, String> nameMap,
44              String description, boolean active)
45          throws PortalException, SystemException {
46  
47          // Layout set prototype
48  
49          long layoutSetPrototypeId = counterLocalService.increment();
50  
51          LayoutSetPrototype layoutSetPrototype =
52              layoutSetPrototypePersistence.create(layoutSetPrototypeId);
53  
54          layoutSetPrototype.setCompanyId(companyId);
55          layoutSetPrototype.setNameMap(nameMap);
56          layoutSetPrototype.setDescription(description);
57          layoutSetPrototype.setActive(active);
58  
59          layoutSetPrototypePersistence.update(layoutSetPrototype, false);
60  
61          // Resources
62  
63          if (userId > 0) {
64              resourceLocalService.addResources(
65                  companyId, 0, userId, LayoutSetPrototype.class.getName(),
66                  layoutSetPrototype.getLayoutSetPrototypeId(), false, false,
67                  false);
68          }
69  
70          // Group
71  
72          String friendlyURL =
73              "/template-" + layoutSetPrototype.getLayoutSetPrototypeId();
74  
75          Group group = groupLocalService.addGroup(
76              userId, LayoutSetPrototype.class.getName(),
77              layoutSetPrototype.getLayoutSetPrototypeId(),
78              layoutSetPrototype.getName(LocaleUtil.getDefault()), null, 0,
79              friendlyURL, true, null);
80  
81          ServiceContext serviceContext = new ServiceContext();
82  
83          layoutLocalService.addLayout(
84              userId, group.getGroupId(), true,
85              LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "home", null, null,
86              LayoutConstants.TYPE_PORTLET, false, "/home", serviceContext);
87  
88          return layoutSetPrototype;
89      }
90  
91      public void deleteLayoutSetPrototype(long layoutSetPrototypeId)
92          throws PortalException, SystemException {
93  
94          LayoutSetPrototype layoutSetPrototype =
95              layoutSetPrototypePersistence.findByPrimaryKey(
96                  layoutSetPrototypeId);
97  
98          // Group
99  
100         Group group = layoutSetPrototype.getGroup();
101 
102         groupLocalService.deleteGroup(group.getGroupId());
103 
104         // Resources
105 
106         resourceLocalService.deleteResource(
107             layoutSetPrototype.getCompanyId(),
108             LayoutSetPrototype.class.getName(),
109             ResourceConstants.SCOPE_INDIVIDUAL,
110             layoutSetPrototype.getLayoutSetPrototypeId());
111 
112         // Layout set prototype
113 
114         layoutSetPrototypePersistence.remove(layoutSetPrototype);
115 
116         // Permission cache
117 
118         PermissionCacheUtil.clearCache();
119     }
120 
121     public List<LayoutSetPrototype> search(
122             long companyId, Boolean active, int start, int end,
123             OrderByComparator obc)
124         throws SystemException {
125 
126         if (active != null) {
127             return layoutSetPrototypePersistence.findByC_A(
128                 companyId, active, start, end, obc);
129         }
130         else {
131             return layoutSetPrototypePersistence.findByCompanyId(
132                 companyId, start, end, obc);
133         }
134     }
135 
136     public int searchCount(long companyId, Boolean active)
137         throws SystemException {
138 
139         if (active != null) {
140             return layoutSetPrototypePersistence.countByC_A(companyId, active);
141         }
142         else {
143             return layoutSetPrototypePersistence.countByCompanyId(companyId);
144         }
145     }
146 
147     public LayoutSetPrototype updateLayoutSetPrototype(
148             long layoutSetPrototypeId, Map<Locale, String> nameMap,
149             String description, boolean active)
150         throws PortalException, SystemException {
151 
152         // Layout set prototype
153 
154         LayoutSetPrototype layoutSetPrototype =
155             layoutSetPrototypePersistence.findByPrimaryKey(
156                 layoutSetPrototypeId);
157 
158         layoutSetPrototype.setNameMap(nameMap);
159         layoutSetPrototype.setDescription(description);
160         layoutSetPrototype.setActive(active);
161 
162         layoutSetPrototypePersistence.update(layoutSetPrototype, false);
163 
164         // Group
165 
166         Group group = groupLocalService.getLayoutSetPrototypeGroup(
167             layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
168 
169         group.setName(layoutSetPrototype.getName(LocaleUtil.getDefault()));
170 
171         groupPersistence.update(group, false);
172 
173         return layoutSetPrototype;
174     }
175 
176 }