001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.OrderByComparator;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
025    import com.liferay.portlet.dynamicdatamapping.service.base.DDMTemplateServiceBaseImpl;
026    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMPermission;
027    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMTemplatePermission;
028    
029    import java.io.File;
030    
031    import java.util.List;
032    import java.util.Locale;
033    import java.util.Map;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Eduardo Lundgren
038     * @author Marcellus Tavares
039     */
040    public class DDMTemplateServiceImpl extends DDMTemplateServiceBaseImpl {
041    
042            public DDMTemplate addTemplate(
043                            long groupId, long classNameId, long classPK,
044                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
045                            String type, String mode, String language, String script,
046                            ServiceContext serviceContext)
047                    throws PortalException, SystemException {
048    
049                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
050    
051                    String ddmResourceActionId = getDDMResourceActionId(
052                            ddmResource, serviceContext);
053    
054                    DDMPermission.check(
055                            getPermissionChecker(), serviceContext.getScopeGroupId(),
056                            ddmResource, ddmResourceActionId);
057    
058                    return ddmTemplateLocalService.addTemplate(
059                            getUserId(), groupId, classNameId, classPK, null, nameMap,
060                            descriptionMap, type, mode, language, script, false, false, null,
061                            null, serviceContext);
062            }
063    
064            public DDMTemplate addTemplate(
065                            long groupId, long classNameId, long classPK, String templateKey,
066                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
067                            String type, String mode, String language, String script,
068                            boolean cacheable, boolean smallImage, String smallImageURL,
069                            File smallImageFile, ServiceContext serviceContext)
070                    throws PortalException, SystemException {
071    
072                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
073    
074                    String ddmResourceActionId = getDDMResourceActionId(
075                            ddmResource, serviceContext);
076    
077                    DDMPermission.check(
078                            getPermissionChecker(), serviceContext.getScopeGroupId(),
079                            ddmResource, ddmResourceActionId);
080    
081                    return ddmTemplateLocalService.addTemplate(
082                            getUserId(), groupId, classNameId, classPK, templateKey, nameMap,
083                            descriptionMap, type, mode, language, script, cacheable, smallImage,
084                            smallImageURL, smallImageFile, serviceContext);
085            }
086    
087            public DDMTemplate copyTemplate(
088                            long templateId, Map<Locale, String> nameMap,
089                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
090                    throws PortalException, SystemException {
091    
092                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
093    
094                    String ddmResourceActionId = getDDMResourceActionId(
095                            ddmResource, serviceContext);
096    
097                    DDMPermission.check(
098                            getPermissionChecker(), serviceContext.getScopeGroupId(),
099                            ddmResource, ddmResourceActionId);
100    
101                    return ddmTemplateLocalService.copyTemplate(
102                            getUserId(), templateId, nameMap, descriptionMap, serviceContext);
103            }
104    
105            public List<DDMTemplate> copyTemplates(
106                            long classNameId, long classPK, long newClassPK, String type,
107                            ServiceContext serviceContext)
108                    throws PortalException, SystemException {
109    
110                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
111    
112                    String ddmResourceActionId = getDDMResourceActionId(
113                            ddmResource, serviceContext);
114    
115                    DDMPermission.check(
116                            getPermissionChecker(), serviceContext.getScopeGroupId(),
117                            ddmResource, ddmResourceActionId);
118    
119                    return ddmTemplateLocalService.copyTemplates(
120                            getUserId(), classNameId, classPK, newClassPK, type,
121                            serviceContext);
122            }
123    
124            public void deleteTemplate(long templateId)
125                    throws PortalException, SystemException {
126    
127                    DDMTemplatePermission.check(
128                            getPermissionChecker(), templateId, ActionKeys.DELETE);
129    
130                    ddmTemplateLocalService.deleteTemplate(templateId);
131            }
132    
133            public DDMTemplate fetchTemplate(long groupId, String templateKey)
134                    throws SystemException {
135    
136                    return ddmTemplateLocalService.fetchTemplate(groupId, templateKey);
137            }
138    
139            public DDMTemplate getTemplate(long templateId)
140                    throws PortalException, SystemException {
141    
142                    DDMTemplatePermission.check(
143                            getPermissionChecker(), templateId, ActionKeys.VIEW);
144    
145                    return ddmTemplatePersistence.findByPrimaryKey(templateId);
146            }
147    
148            public DDMTemplate getTemplate(long groupId, String templateKey)
149                    throws PortalException, SystemException {
150    
151                    return ddmTemplateLocalService.getTemplate(groupId, templateKey);
152            }
153    
154            public DDMTemplate getTemplate(
155                            long groupId, String templateKey, boolean includeGlobalTemplates)
156                    throws PortalException, SystemException {
157    
158                    return ddmTemplateLocalService.getTemplate(
159                            groupId, templateKey, includeGlobalTemplates);
160            }
161    
162            public List<DDMTemplate> getTemplates(long groupId, long classNameId)
163                    throws SystemException {
164    
165                    return ddmTemplatePersistence.findByG_C(groupId, classNameId);
166            }
167    
168            public List<DDMTemplate> getTemplates(
169                            long groupId, long classNameId, long classPK)
170                    throws SystemException {
171    
172                    return ddmTemplatePersistence.findByG_C_C(
173                            groupId, classNameId, classPK);
174            }
175    
176            public List<DDMTemplate> getTemplates(
177                            long classNameId, long classPK, String type, String mode)
178                    throws SystemException {
179    
180                    return ddmTemplatePersistence.findByC_C_T_M(
181                            classNameId, classPK, type, mode);
182            }
183    
184            public List<DDMTemplate> search(
185                            long companyId, long groupId, long classNameId, long classPK,
186                            String keywords, String type, String mode, int start, int end,
187                            OrderByComparator orderByComparator)
188                    throws SystemException {
189    
190                    return ddmTemplateFinder.filterFindByKeywords(
191                            companyId, groupId, classNameId, classPK, keywords, type, mode,
192                            start, end, orderByComparator);
193            }
194    
195            public List<DDMTemplate> search(
196                            long companyId, long groupId, long classNameId, long classPK,
197                            String name, String description, String type, String mode,
198                            String language, boolean andOperator, int start, int end,
199                            OrderByComparator orderByComparator)
200                    throws SystemException {
201    
202                    return ddmTemplateFinder.filterFindByC_G_C_C_N_D_T_M_L(
203                            companyId, groupId, classNameId, classPK, name, description, type,
204                            mode, language, andOperator, start, end, orderByComparator);
205            }
206    
207            public List<DDMTemplate> search(
208                            long companyId, long[] groupIds, long[] classNameIds, long classPK,
209                            String keywords, String type, String mode, int start, int end,
210                            OrderByComparator orderByComparator)
211                    throws SystemException {
212    
213                    return ddmTemplateFinder.filterFindByKeywords(
214                            companyId, groupIds, classNameIds, classPK, keywords, type, mode,
215                            start, end, orderByComparator);
216            }
217    
218            public List<DDMTemplate> search(
219                            long companyId, long[] groupIds, long[] classNameIds, long classPK,
220                            String name, String description, String type, String mode,
221                            String language, boolean andOperator, int start, int end,
222                            OrderByComparator orderByComparator)
223                    throws SystemException {
224    
225                    return ddmTemplateFinder.filterFindByC_G_C_C_N_D_T_M_L(
226                            companyId, groupIds, classNameIds, classPK, name, description, type,
227                            mode, language, andOperator, start, end, orderByComparator);
228            }
229    
230            public int searchCount(
231                            long companyId, long groupId, long classNameId, long classPK,
232                            String keywords, String type, String mode)
233                    throws SystemException {
234    
235                    return ddmTemplateFinder.filterCountByKeywords(
236                            companyId, groupId, classNameId, classPK, keywords, type, mode);
237            }
238    
239            public int searchCount(
240                            long companyId, long groupId, long classNameId, long classPK,
241                            String name, String description, String type, String mode,
242                            String language, boolean andOperator)
243                    throws SystemException {
244    
245                    return ddmTemplateFinder.filterCountByC_G_C_C_N_D_T_M_L(
246                            companyId, groupId, classNameId, classPK, name, description, type,
247                            mode, language, andOperator);
248            }
249    
250            public int searchCount(
251                            long companyId, long[] groupIds, long[] classNameIds, long classPK,
252                            String keywords, String type, String mode)
253                    throws SystemException {
254    
255                    return ddmTemplateFinder.filterCountByKeywords(
256                            companyId, groupIds, classNameIds, classPK, keywords, type, mode);
257            }
258    
259            public int searchCount(
260                            long companyId, long[] groupIds, long[] classNameIds, long classPK,
261                            String name, String description, String type, String mode,
262                            String language, boolean andOperator)
263                    throws SystemException {
264    
265                    return ddmTemplateFinder.filterCountByC_G_C_C_N_D_T_M_L(
266                            companyId, groupIds, classNameIds, classPK, name, description, type,
267                            mode, language, andOperator);
268            }
269    
270            public DDMTemplate updateTemplate(
271                            long templateId, Map<Locale, String> nameMap,
272                            Map<Locale, String> descriptionMap, String type, String mode,
273                            String language, String script, boolean cacheable,
274                            boolean smallImage, String smallImageURL, File smallImageFile,
275                            ServiceContext serviceContext)
276                    throws PortalException, SystemException {
277    
278                    DDMTemplatePermission.check(
279                            getPermissionChecker(), templateId, ActionKeys.UPDATE);
280    
281                    return ddmTemplateLocalService.updateTemplate(
282                            templateId, nameMap, descriptionMap, type, mode, language, script,
283                            cacheable, smallImage, smallImageURL, smallImageFile,
284                            serviceContext);
285            }
286    
287            protected String getDDMResourceActionId(
288                    String ddmResource, ServiceContext serviceContext) {
289    
290                    String ddmResourceActionId = ParamUtil.getString(
291                            serviceContext, "ddmResourceActionId");
292    
293                    if (Validator.isNull(ddmResourceActionId)) {
294                            if (ddmResource.equals(_DDL_CLASS_NAME)) {
295                                    ddmResourceActionId = ActionKeys.ADD_TEMPLATE;
296                            }
297                            else {
298                                    ddmResourceActionId = ActionKeys.ADD_PORTLET_DISPLAY_TEMPLATE;
299                            }
300                    }
301    
302                    return ddmResourceActionId;
303            }
304    
305            private static final String _DDL_CLASS_NAME =
306                    "com.liferay.portlet.dynamicdatalists";
307    
308    }