1   /**
2    * Copyright (c) 2000-2009 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.kernel.util.ListUtil;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portal.service.permission.PortletPermissionUtil;
30  import com.liferay.portal.util.PortletKeys;
31  import com.liferay.portlet.journal.model.JournalTemplate;
32  import com.liferay.portlet.journal.service.base.JournalTemplateServiceBaseImpl;
33  import com.liferay.portlet.journal.service.permission.JournalStructurePermission;
34  import com.liferay.portlet.journal.service.permission.JournalTemplatePermission;
35  
36  import java.io.File;
37  
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * <a href="JournalTemplateServiceImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class JournalTemplateServiceImpl extends JournalTemplateServiceBaseImpl {
49  
50      public JournalTemplate addTemplate(
51              String templateId, boolean autoTemplateId, long plid,
52              String structureId, String name, String description, String xsl,
53              boolean formatXsl, String langType, boolean cacheable,
54              boolean smallImage, String smallImageURL, File smallFile,
55              boolean addCommunityPermissions, boolean addGuestPermissions)
56          throws PortalException, SystemException {
57  
58          PortletPermissionUtil.check(
59              getPermissionChecker(), plid, PortletKeys.JOURNAL,
60              ActionKeys.ADD_TEMPLATE);
61  
62          return journalTemplateLocalService.addTemplate(
63              getUserId(), templateId, autoTemplateId, plid, structureId, name,
64              description, xsl, formatXsl, langType, cacheable, smallImage,
65              smallImageURL, smallFile, addCommunityPermissions,
66              addGuestPermissions);
67      }
68  
69      public JournalTemplate addTemplate(
70              String templateId, boolean autoTemplateId, long plid,
71              String structureId, String name, String description, String xsl,
72              boolean formatXsl, String langType, boolean cacheable,
73              boolean smallImage, String smallImageURL, File smallFile,
74              String[] communityPermissions, String[] guestPermissions)
75          throws PortalException, SystemException {
76  
77          PortletPermissionUtil.check(
78              getPermissionChecker(), plid, PortletKeys.JOURNAL,
79              ActionKeys.ADD_TEMPLATE);
80  
81          return journalTemplateLocalService.addTemplate(
82              getUserId(), templateId, autoTemplateId, plid, structureId, name,
83              description, xsl, formatXsl, langType, cacheable, smallImage,
84              smallImageURL, smallFile, communityPermissions, guestPermissions);
85      }
86  
87      public JournalTemplate copyTemplate(
88              long groupId, String oldTemplateId, String newTemplateId,
89              boolean autoTemplateId)
90          throws PortalException, SystemException {
91  
92          JournalTemplatePermission.check(
93              getPermissionChecker(), groupId, oldTemplateId,
94              ActionKeys.ADD_TEMPLATE);
95  
96          return journalTemplateLocalService.copyTemplate(
97              getUserId(), groupId, oldTemplateId, newTemplateId, autoTemplateId);
98      }
99  
100     public void deleteTemplate(long groupId, String templateId)
101         throws PortalException, SystemException {
102 
103         JournalTemplatePermission.check(
104             getPermissionChecker(), groupId, templateId, ActionKeys.DELETE);
105 
106         journalTemplateLocalService.deleteTemplate(groupId, templateId);
107     }
108 
109     public List<JournalTemplate> getStructureTemplates(
110             long groupId, String structureId)
111         throws PortalException, SystemException {
112 
113         if (!JournalStructurePermission.contains(
114                 getPermissionChecker(), groupId, structureId,
115                 ActionKeys.VIEW)) {
116 
117             return new ArrayList<JournalTemplate>();
118         }
119 
120         List<JournalTemplate> list =
121             journalTemplateLocalService.getStructureTemplates(
122                 groupId, structureId);
123 
124         list = ListUtil.copy(list);
125 
126         Iterator<JournalTemplate> itr = list.iterator();
127 
128         while (itr.hasNext()) {
129             JournalTemplate template = itr.next();
130 
131             if (!JournalTemplatePermission.contains(
132                     getPermissionChecker(), template, ActionKeys.VIEW)) {
133 
134                 itr.remove();
135             }
136         }
137 
138         return list;
139     }
140 
141     public JournalTemplate getTemplate(long groupId, String templateId)
142         throws PortalException, SystemException {
143 
144         JournalTemplatePermission.check(
145             getPermissionChecker(), groupId, templateId, ActionKeys.VIEW);
146 
147         return journalTemplateLocalService.getTemplate(groupId, templateId);
148     }
149 
150     public JournalTemplate updateTemplate(
151             long groupId, String templateId, String structureId, String name,
152             String description, String xsl, boolean formatXsl, String langType,
153             boolean cacheable, boolean smallImage, String smallImageURL,
154             File smallFile)
155         throws PortalException, SystemException {
156 
157         JournalTemplatePermission.check(
158             getPermissionChecker(), groupId, templateId, ActionKeys.UPDATE);
159 
160         return journalTemplateLocalService.updateTemplate(
161             groupId, templateId, structureId, name, description, xsl, formatXsl,
162             langType, cacheable, smallImage, smallImageURL, smallFile);
163     }
164 
165 }