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.LayoutPrototype;
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.LayoutPrototypeLocalServiceBaseImpl;
28  
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  /**
34   * <a href="LayoutPrototypeLocalServiceImpl.java.html"><b><i>View Source</i></b>
35   * </a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Jorge Ferrer
39   */
40  public class LayoutPrototypeLocalServiceImpl
41      extends LayoutPrototypeLocalServiceBaseImpl {
42  
43      public LayoutPrototype addLayoutPrototype(
44              long userId, long companyId, Map<Locale, String> nameMap,
45              String description, boolean active)
46          throws PortalException, SystemException {
47  
48          // Layout prototype
49  
50          long layoutPrototypeId = counterLocalService.increment();
51  
52          LayoutPrototype layoutPrototype = layoutPrototypePersistence.create(
53              layoutPrototypeId);
54  
55          layoutPrototype.setCompanyId(companyId);
56          layoutPrototype.setNameMap(nameMap);
57          layoutPrototype.setDescription(description);
58          layoutPrototype.setActive(active);
59  
60          layoutPrototypePersistence.update(layoutPrototype, false);
61  
62          // Resources
63  
64          if (userId > 0) {
65              resourceLocalService.addResources(
66                  companyId, 0, userId, LayoutPrototype.class.getName(),
67                  layoutPrototype.getLayoutPrototypeId(), false, false, false);
68          }
69  
70          // Group
71  
72          String friendlyURL =
73              "/template-" + layoutPrototype.getLayoutPrototypeId();
74  
75          Group group = groupLocalService.addGroup(
76              userId, LayoutPrototype.class.getName(),
77              layoutPrototype.getLayoutPrototypeId(),
78              layoutPrototype.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,
86              String.valueOf(layoutPrototype.getLayoutPrototypeId()), null, null,
87              LayoutConstants.TYPE_PORTLET, false, "/layout", serviceContext);
88  
89          return layoutPrototype;
90      }
91  
92      public void deleteLayoutPrototype(long layoutPrototypeId)
93          throws PortalException, SystemException {
94  
95          LayoutPrototype layoutPrototype =
96              layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
97  
98          // Group
99  
100         Group group = layoutPrototype.getGroup();
101 
102         groupLocalService.deleteGroup(group.getGroupId());
103 
104         // Resources
105 
106         resourceLocalService.deleteResource(
107             layoutPrototype.getCompanyId(), LayoutPrototype.class.getName(),
108             ResourceConstants.SCOPE_INDIVIDUAL,
109             layoutPrototype.getLayoutPrototypeId());
110 
111         // Layout Prototype
112 
113         layoutPrototypePersistence.remove(layoutPrototype);
114 
115         // Permission cache
116 
117         PermissionCacheUtil.clearCache();
118     }
119 
120     public List<LayoutPrototype> search(
121             long companyId, Boolean active, int start, int end,
122             OrderByComparator obc)
123         throws SystemException {
124 
125         if (active != null) {
126             return layoutPrototypePersistence.findByC_A(
127                 companyId, active, start, end, obc);
128         }
129         else {
130             return layoutPrototypePersistence.findByCompanyId(
131                 companyId, start, end, obc);
132         }
133     }
134 
135     public int searchCount(long companyId, Boolean active)
136         throws SystemException {
137 
138         if (active != null) {
139             return layoutPrototypePersistence.countByC_A(companyId, active);
140         }
141         else {
142             return layoutPrototypePersistence.countByCompanyId(companyId);
143         }
144     }
145 
146     public LayoutPrototype updateLayoutPrototype(
147             long layoutPrototypeId, Map<Locale, String> nameMap,
148             String description, boolean active)
149         throws PortalException, SystemException {
150 
151         // Layout prototype
152 
153         LayoutPrototype layoutPrototype =
154             layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
155 
156         layoutPrototype.setNameMap(nameMap);
157         layoutPrototype.setDescription(description);
158         layoutPrototype.setActive(active);
159 
160         layoutPrototypePersistence.update(layoutPrototype, false);
161 
162         // Group
163 
164         Group group = groupLocalService.getLayoutPrototypeGroup(
165             layoutPrototype.getCompanyId(), layoutPrototypeId);
166 
167         group.setName(layoutPrototype.getName(LocaleUtil.getDefault()));
168 
169         groupPersistence.update(group, false);
170 
171         return layoutPrototype;
172     }
173 
174 }