1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.security.permission.ActionKeys;
28  import com.liferay.portal.service.permission.PortletPermissionUtil;
29  import com.liferay.portal.util.PortletKeys;
30  import com.liferay.portlet.journal.model.JournalTemplate;
31  import com.liferay.portlet.journal.service.base.JournalTemplateServiceBaseImpl;
32  import com.liferay.portlet.journal.service.permission.JournalStructurePermission;
33  import com.liferay.portlet.journal.service.permission.JournalTemplatePermission;
34  
35  import java.io.File;
36  
37  import java.util.ArrayList;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   * <a href="JournalTemplateServiceImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class JournalTemplateServiceImpl extends JournalTemplateServiceBaseImpl {
48  
49      public JournalTemplate addTemplate(
50              String templateId, boolean autoTemplateId, long plid,
51              String structureId, String name, String description, String xsl,
52              boolean formatXsl, String langType, boolean cacheable,
53              boolean smallImage, String smallImageURL, File smallFile,
54              boolean addCommunityPermissions, boolean addGuestPermissions)
55          throws PortalException, SystemException {
56  
57          PortletPermissionUtil.check(
58              getPermissionChecker(), plid, PortletKeys.JOURNAL,
59              ActionKeys.ADD_TEMPLATE);
60  
61          return journalTemplateLocalService.addTemplate(
62              getUserId(), templateId, autoTemplateId, plid, structureId, name,
63              description, xsl, formatXsl, langType, cacheable, smallImage,
64              smallImageURL, smallFile, addCommunityPermissions,
65              addGuestPermissions);
66      }
67  
68      public JournalTemplate addTemplate(
69              String templateId, boolean autoTemplateId, long plid,
70              String structureId, String name, String description, String xsl,
71              boolean formatXsl, String langType, boolean cacheable,
72              boolean smallImage, String smallImageURL, File smallFile,
73              String[] communityPermissions, String[] guestPermissions)
74          throws PortalException, SystemException {
75  
76          PortletPermissionUtil.check(
77              getPermissionChecker(), plid, PortletKeys.JOURNAL,
78              ActionKeys.ADD_TEMPLATE);
79  
80          return journalTemplateLocalService.addTemplate(
81              getUserId(), templateId, autoTemplateId, plid, structureId, name,
82              description, xsl, formatXsl, langType, cacheable, smallImage,
83              smallImageURL, smallFile, communityPermissions, guestPermissions);
84      }
85  
86      public JournalTemplate copyTemplate(
87              long groupId, String oldTemplateId, String newTemplateId,
88              boolean autoTemplateId)
89          throws PortalException, SystemException {
90  
91          JournalTemplatePermission.check(
92              getPermissionChecker(), groupId, oldTemplateId,
93              ActionKeys.ADD_TEMPLATE);
94  
95          return journalTemplateLocalService.copyTemplate(
96              getUserId(), groupId, oldTemplateId, newTemplateId, autoTemplateId);
97      }
98  
99      public void deleteTemplate(long groupId, String templateId)
100         throws PortalException, SystemException {
101 
102         JournalTemplatePermission.check(
103             getPermissionChecker(), groupId, templateId, ActionKeys.DELETE);
104 
105         journalTemplateLocalService.deleteTemplate(groupId, templateId);
106     }
107 
108     public List<JournalTemplate> getStructureTemplates(
109             long groupId, String structureId)
110         throws PortalException, SystemException {
111 
112         if (!JournalStructurePermission.contains(
113                 getPermissionChecker(), groupId, structureId,
114                 ActionKeys.VIEW)) {
115 
116             return new ArrayList<JournalTemplate>();
117         }
118 
119         List<JournalTemplate> list =
120             journalTemplateLocalService.getStructureTemplates(
121                 groupId, structureId);
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 }