1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.util.ListUtil;
25  import com.liferay.portal.security.permission.ActionKeys;
26  import com.liferay.portal.service.permission.PortletPermissionUtil;
27  import com.liferay.portal.util.PortletKeys;
28  import com.liferay.portlet.journal.model.JournalTemplate;
29  import com.liferay.portlet.journal.service.base.JournalTemplateServiceBaseImpl;
30  import com.liferay.portlet.journal.service.permission.JournalStructurePermission;
31  import com.liferay.portlet.journal.service.permission.JournalTemplatePermission;
32  
33  import java.io.File;
34  
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="JournalTemplateServiceImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class JournalTemplateServiceImpl extends JournalTemplateServiceBaseImpl {
46  
47      public JournalTemplate addTemplate(
48              String templateId, boolean autoTemplateId, long plid,
49              String structureId, String name, String description, String xsl,
50              boolean formatXsl, String langType, boolean cacheable,
51              boolean smallImage, String smallImageURL, File smallFile,
52              boolean addCommunityPermissions, boolean addGuestPermissions)
53          throws PortalException, SystemException {
54  
55          PortletPermissionUtil.check(
56              getPermissionChecker(), plid, PortletKeys.JOURNAL,
57              ActionKeys.ADD_TEMPLATE);
58  
59          return journalTemplateLocalService.addTemplate(
60              getUserId(), templateId, autoTemplateId, plid, structureId, name,
61              description, xsl, formatXsl, langType, cacheable, smallImage,
62              smallImageURL, smallFile, addCommunityPermissions,
63              addGuestPermissions);
64      }
65  
66      public JournalTemplate addTemplate(
67              String templateId, boolean autoTemplateId, long plid,
68              String structureId, String name, String description, String xsl,
69              boolean formatXsl, String langType, boolean cacheable,
70              boolean smallImage, String smallImageURL, File smallFile,
71              String[] communityPermissions, String[] guestPermissions)
72          throws PortalException, SystemException {
73  
74          PortletPermissionUtil.check(
75              getPermissionChecker(), plid, PortletKeys.JOURNAL,
76              ActionKeys.ADD_TEMPLATE);
77  
78          return journalTemplateLocalService.addTemplate(
79              getUserId(), templateId, autoTemplateId, plid, structureId, name,
80              description, xsl, formatXsl, langType, cacheable, smallImage,
81              smallImageURL, smallFile, communityPermissions, guestPermissions);
82      }
83  
84      public JournalTemplate copyTemplate(
85              long groupId, String oldTemplateId, String newTemplateId,
86              boolean autoTemplateId)
87          throws PortalException, SystemException {
88  
89          JournalTemplatePermission.check(
90              getPermissionChecker(), groupId, oldTemplateId,
91              ActionKeys.ADD_TEMPLATE);
92  
93          return journalTemplateLocalService.copyTemplate(
94              getUserId(), groupId, oldTemplateId, newTemplateId, autoTemplateId);
95      }
96  
97      public void deleteTemplate(long groupId, String templateId)
98          throws PortalException, SystemException {
99  
100         JournalTemplatePermission.check(
101             getPermissionChecker(), groupId, templateId, ActionKeys.DELETE);
102 
103         journalTemplateLocalService.deleteTemplate(groupId, templateId);
104     }
105 
106     public List<JournalTemplate> getStructureTemplates(
107             long groupId, String structureId)
108         throws PortalException, SystemException {
109 
110         if (!JournalStructurePermission.contains(
111                 getPermissionChecker(), groupId, structureId,
112                 ActionKeys.VIEW)) {
113 
114             return new ArrayList<JournalTemplate>();
115         }
116 
117         List<JournalTemplate> list =
118             journalTemplateLocalService.getStructureTemplates(
119                 groupId, structureId);
120 
121         list = ListUtil.copy(list);
122 
123         Iterator<JournalTemplate> itr = list.iterator();
124 
125         while (itr.hasNext()) {
126             JournalTemplate template = itr.next();
127 
128             if (!JournalTemplatePermission.contains(
129                     getPermissionChecker(), template, ActionKeys.VIEW)) {
130 
131                 itr.remove();
132             }
133         }
134 
135         return list;
136     }
137 
138     public JournalTemplate getTemplate(long groupId, String templateId)
139         throws PortalException, SystemException {
140 
141         JournalTemplatePermission.check(
142             getPermissionChecker(), groupId, templateId, ActionKeys.VIEW);
143 
144         return journalTemplateLocalService.getTemplate(groupId, templateId);
145     }
146 
147     public JournalTemplate updateTemplate(
148             long groupId, String templateId, String structureId, String name,
149             String description, String xsl, boolean formatXsl, String langType,
150             boolean cacheable, boolean smallImage, String smallImageURL,
151             File smallFile)
152         throws PortalException, SystemException {
153 
154         JournalTemplatePermission.check(
155             getPermissionChecker(), groupId, templateId, ActionKeys.UPDATE);
156 
157         return journalTemplateLocalService.updateTemplate(
158             groupId, templateId, structureId, name, description, xsl, formatXsl,
159             langType, cacheable, smallImage, smallImageURL, smallFile);
160     }
161 
162 }