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     * The Dynamic Data Mapping (DDM) Template local service is responsible for
037     * accessing, creating, modifying, and deleting templates.
038     *
039     * @author Brian Wing Shun Chan
040     * @author Eduardo Lundgren
041     * @author Marcellus Tavares
042     * @see    com.liferay.portlet.dynamicdatamapping.service.impl.DDMTemplateLocalServiceImpl
043     */
044    public class DDMTemplateServiceImpl extends DDMTemplateServiceBaseImpl {
045    
046            /**
047             * Adds a template.
048             *
049             * @param  groupId the primary key of the group
050             * @param  classNameId the primary key of the class name for template's
051             *         related model
052             * @param  classPK the primary key of the template's related entity
053             * @param  nameMap the template's locales and localized names
054             * @param  descriptionMap the template's locales and localized descriptions
055             * @param  type the template's type. For more information, see {@link
056             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
057             * @param  mode the template's mode. For more information, see {@link
058             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
059             * @param  language the template's script language. For more information,
060             *         see {@link
061             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
062             * @param  script the template's script
063             * @param  serviceContext the template's service context. Must have the
064             *         <code>ddmResource</code> attribute to check permissions. Can set
065             *         the UUID, creation date, modification date, guest permissions,
066             *         and group permissions for the template.
067             * @return the template
068             * @throws PortalException if the user did not have permission to add the
069             *         template or if a portal exception occurred
070             * @throws SystemException if a system exception occurred
071             */
072            public DDMTemplate addTemplate(
073                            long groupId, long classNameId, long classPK,
074                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
075                            String type, String mode, String language, String script,
076                            ServiceContext serviceContext)
077                    throws PortalException, SystemException {
078    
079                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
080    
081                    String ddmResourceActionId = getDDMResourceActionId(
082                            ddmResource, serviceContext);
083    
084                    DDMPermission.check(
085                            getPermissionChecker(), serviceContext.getScopeGroupId(),
086                            ddmResource, ddmResourceActionId);
087    
088                    return ddmTemplateLocalService.addTemplate(
089                            getUserId(), groupId, classNameId, classPK, null, nameMap,
090                            descriptionMap, type, mode, language, script, false, false, null,
091                            null, serviceContext);
092            }
093    
094            /**
095             * Adds a template with additional parameters.
096             *
097             * @param  groupId the primary key of the group
098             * @param  classNameId the primary key of the class name for template's
099             *         related model
100             * @param  classPK the primary key of the template's related entity
101             * @param  templateKey the unique string identifying the template
102             *         (optionally <code>null</code>)
103             * @param  nameMap the template's locales and localized names
104             * @param  descriptionMap the template's locales and localized descriptions
105             * @param  type the template's type. For more information, see {@link
106             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
107             * @param  mode the template's mode. For more information, see {@link
108             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
109             * @param  language the template's script language. For more information,
110             *         see {@link
111             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
112             * @param  script the template's script
113             * @param  cacheable whether the template is cacheable
114             * @param  smallImage whether the template has a small image
115             * @param  smallImageURL the template's small image URL (optionally
116             *         <code>null</code>)
117             * @param  smallImageFile the template's small image file (optionally
118             *         <code>null</code>)
119             * @param  serviceContext the template's service context. Must have the
120             *         <code>ddmResource</code> attribute to check permissions. Can set
121             *         the UUID, creation date, modification date, guest permissions,
122             *         and group permissions for the template.
123             * @return the template
124             * @throws PortalException if the user did not have permission to add the
125             *         template or if a portal exception occurred
126             * @throws SystemException if a system exception occurred
127             */
128            public DDMTemplate addTemplate(
129                            long groupId, long classNameId, long classPK, String templateKey,
130                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
131                            String type, String mode, String language, String script,
132                            boolean cacheable, boolean smallImage, String smallImageURL,
133                            File smallImageFile, ServiceContext serviceContext)
134                    throws PortalException, SystemException {
135    
136                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
137    
138                    String ddmResourceActionId = getDDMResourceActionId(
139                            ddmResource, serviceContext);
140    
141                    DDMPermission.check(
142                            getPermissionChecker(), serviceContext.getScopeGroupId(),
143                            ddmResource, ddmResourceActionId);
144    
145                    return ddmTemplateLocalService.addTemplate(
146                            getUserId(), groupId, classNameId, classPK, templateKey, nameMap,
147                            descriptionMap, type, mode, language, script, cacheable, smallImage,
148                            smallImageURL, smallImageFile, serviceContext);
149            }
150    
151            /**
152             * Copies the template, creating a new template with all the values
153             * extracted from the original one. This method supports defining a new name
154             * and description.
155             *
156             * @param  templateId the primary key of the template to be copied
157             * @param  nameMap the new template's locales and localized names
158             * @param  descriptionMap the new template's locales and localized
159             *         descriptions
160             * @param  serviceContext the service context to be applied. Must have the
161             *         <code>ddmResource</code> attribute to check permissions. Can set
162             *         the UUID, creation date, modification date, guest permissions,
163             *         and group permissions for the template.
164             * @return the new template
165             * @throws PortalException if the user did not have permission to add the
166             *         template or if a portal exception occurred
167             * @throws SystemException if a system exception occurred
168             */
169            public DDMTemplate copyTemplate(
170                            long templateId, Map<Locale, String> nameMap,
171                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
172                    throws PortalException, SystemException {
173    
174                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
175    
176                    String ddmResourceActionId = getDDMResourceActionId(
177                            ddmResource, serviceContext);
178    
179                    DDMPermission.check(
180                            getPermissionChecker(), serviceContext.getScopeGroupId(),
181                            ddmResource, ddmResourceActionId);
182    
183                    return ddmTemplateLocalService.copyTemplate(
184                            getUserId(), templateId, nameMap, descriptionMap, serviceContext);
185            }
186    
187            public DDMTemplate copyTemplate(
188                            long templateId, ServiceContext serviceContext)
189                    throws PortalException, SystemException {
190    
191                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
192    
193                    String ddmResourceActionId = getDDMResourceActionId(
194                            ddmResource, serviceContext);
195    
196                    DDMPermission.check(
197                            getPermissionChecker(), serviceContext.getScopeGroupId(),
198                            ddmResource, ddmResourceActionId);
199    
200                    return ddmTemplateLocalService.copyTemplate(
201                            getUserId(), templateId, serviceContext);
202            }
203    
204            /**
205             * Copies all the templates matching the class name ID, class PK, and type.
206             * This method creates new templates, extracting all the values from the old
207             * ones and updating their class PKs.
208             *
209             * @param  classNameId the primary key of the class name for template's
210             *         related model
211             * @param  classPK the primary key of the original template's related entity
212             * @param  newClassPK the primary key of the new template's related entity
213             * @param  type the template's type. For more information, see {@link
214             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
215             * @param  serviceContext the service context to be applied. Must have the
216             *         <code>ddmResource</code> attribute to check permissions. Can set
217             *         the UUID, creation date, modification date, guest permissions,
218             *         and group permissions for the template.
219             * @return the new template
220             * @throws PortalException if the user did not have permission to add the
221             *         template or if a portal exception occurred
222             * @throws SystemException if a system exception occurred
223             */
224            public List<DDMTemplate> copyTemplates(
225                            long classNameId, long classPK, long newClassPK, String type,
226                            ServiceContext serviceContext)
227                    throws PortalException, SystemException {
228    
229                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
230    
231                    String ddmResourceActionId = getDDMResourceActionId(
232                            ddmResource, serviceContext);
233    
234                    DDMPermission.check(
235                            getPermissionChecker(), serviceContext.getScopeGroupId(),
236                            ddmResource, ddmResourceActionId);
237    
238                    return ddmTemplateLocalService.copyTemplates(
239                            getUserId(), classNameId, classPK, newClassPK, type,
240                            serviceContext);
241            }
242    
243            /**
244             * Deletes the template and its resources.
245             *
246             * @param  templateId the primary key of the template to be deleted
247             * @throws PortalException if the user did not have permission to delete the
248             *         template or if a portal exception occurred
249             * @throws SystemException if a system exception occurred
250             */
251            public void deleteTemplate(long templateId)
252                    throws PortalException, SystemException {
253    
254                    DDMTemplatePermission.check(
255                            getPermissionChecker(), templateId, ActionKeys.DELETE);
256    
257                    ddmTemplateLocalService.deleteTemplate(templateId);
258            }
259    
260            /**
261             * Returns the template matching the group and template key.
262             *
263             * @param  groupId the primary key of the group
264             * @param  classNameId the primary key of the class name for template's
265             *         related model
266             * @param  templateKey the unique string identifying the template
267             * @return the matching template, or <code>null</code> if a matching
268             *         template could not be found
269             * @throws PortalException if the user did not have permission to delete the
270             *         template
271             * @throws SystemException if a system exception occurred
272             */
273            public DDMTemplate fetchTemplate(
274                            long groupId, long classNameId, String templateKey)
275                    throws PortalException, SystemException {
276    
277                    DDMTemplate ddmTemplate = ddmTemplateLocalService.fetchTemplate(
278                            groupId, classNameId, templateKey);
279    
280                    if (ddmTemplate != null) {
281                            DDMTemplatePermission.check(
282                                    getPermissionChecker(), ddmTemplate, ActionKeys.VIEW);
283                    }
284    
285                    return ddmTemplate;
286            }
287    
288            /**
289             * Returns the template with the ID.
290             *
291             * @param  templateId the primary key of the template
292             * @return the template with the ID
293             * @throws PortalException if the user did not have permission to view the
294             *         template or if a matching template could not be found
295             * @throws SystemException if a system exception occurred
296             */
297            public DDMTemplate getTemplate(long templateId)
298                    throws PortalException, SystemException {
299    
300                    DDMTemplatePermission.check(
301                            getPermissionChecker(), templateId, ActionKeys.VIEW);
302    
303                    return ddmTemplatePersistence.findByPrimaryKey(templateId);
304            }
305    
306            /**
307             * Returns the template matching the group and template key.
308             *
309             * @param  groupId the primary key of the group
310             * @param  classNameId the primary key of the class name for template's
311             *         related model
312             * @param  templateKey the unique string identifying the template
313             * @return the matching template
314             * @throws PortalException if a matching template could not be found
315             * @throws SystemException if a system exception occurred
316             */
317            public DDMTemplate getTemplate(
318                            long groupId, long classNameId, String templateKey)
319                    throws PortalException, SystemException {
320    
321                    DDMTemplate ddmTemplate = ddmTemplateLocalService.getTemplate(
322                            groupId, classNameId, templateKey);
323    
324                    DDMTemplatePermission.check(
325                            getPermissionChecker(), ddmTemplate, ActionKeys.VIEW);
326    
327                    return ddmTemplate;
328            }
329    
330            /**
331             * Returns the template matching the group and template key, optionally in
332             * the global scope.
333             *
334             * <p>
335             * This method first searches in the group. If the template is still not
336             * found and <code>includeGlobalTemplates</code> is set to
337             * <code>true</code>, this method searches the global group.
338             * </p>
339             *
340             * @param  groupId the primary key of the group
341             * @param  classNameId the primary key of the class name for template's
342             *         related model
343             * @param  templateKey the unique string identifying the template
344             * @param  includeGlobalTemplates whether to include the global scope in the
345             *         search
346             * @return the matching template
347             * @throws PortalException if a matching template could not be found
348             * @throws SystemException if a system exception occurred
349             */
350            public DDMTemplate getTemplate(
351                            long groupId, long classNameId, String templateKey,
352                            boolean includeGlobalTemplates)
353                    throws PortalException, SystemException {
354    
355                    DDMTemplate ddmTemplate = ddmTemplateLocalService.getTemplate(
356                            groupId, classNameId, templateKey, includeGlobalTemplates);
357    
358                    DDMTemplatePermission.check(
359                            getPermissionChecker(), ddmTemplate, ActionKeys.VIEW);
360    
361                    return ddmTemplate;
362            }
363    
364            /**
365             * Returns all the templates matching the group and class name ID.
366             *
367             * @param  groupId the primary key of the group
368             * @param  classNameId the primary key of the class name for template's
369             *         related model
370             * @return the matching templates
371             * @throws SystemException if a system exception occurred
372             */
373            public List<DDMTemplate> getTemplates(long groupId, long classNameId)
374                    throws SystemException {
375    
376                    return ddmTemplatePersistence.filterFindByG_C(groupId, classNameId);
377            }
378    
379            /**
380             * Returns all the templates matching the group, class name ID, and class
381             * PK.
382             *
383             * @param  groupId the primary key of the group
384             * @param  classNameId the primary key of the class name for template's
385             *         related model
386             * @param  classPK the primary key of the template's related entity
387             * @return the matching templates
388             * @throws SystemException if a system exception occurred
389             */
390            public List<DDMTemplate> getTemplates(
391                            long groupId, long classNameId, long classPK)
392                    throws SystemException {
393    
394                    return ddmTemplatePersistence.filterFindByG_C_C(
395                            groupId, classNameId, classPK);
396            }
397    
398            /**
399             * Returns all the templates matching the class name ID, class PK, type, and
400             * mode.
401             *
402             * @param  groupId the primary key of the group
403             * @param  classNameId the primary key of the class name for template's
404             *         related model
405             * @param  classPK the primary key of the template's related entity
406             * @param  type the template's type. For more information, see {@link
407             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
408             * @return the matching templates
409             * @throws SystemException if a system exception occurred
410             */
411            public List<DDMTemplate> getTemplates(
412                            long groupId, long classNameId, long classPK, String type)
413                    throws SystemException {
414    
415                    return ddmTemplatePersistence.filterFindByG_C_C_T(
416                            groupId, classNameId, classPK, type);
417            }
418    
419            public List<DDMTemplate> getTemplates(
420                            long groupId, long classNameId, long classPK, String type,
421                            String mode)
422                    throws SystemException {
423    
424                    return ddmTemplatePersistence.filterFindByG_C_C_T_M(
425                            groupId, classNameId, classPK, type, mode);
426            }
427    
428            /**
429             * Returns an ordered range of all the templates matching the group, class
430             * name ID, class PK, type, and mode, and matching the keywords in the
431             * template names and descriptions.
432             *
433             * <p>
434             * Useful when paginating results. Returns a maximum of <code>end -
435             * start</code> instances. <code>start</code> and <code>end</code> are not
436             * primary keys, they are indexes in the result set. Thus, <code>0</code>
437             * refers to the first result in the set. Setting both <code>start</code>
438             * and <code>end</code> to {@link
439             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
440             * result set.
441             * </p>
442             *
443             * @param  companyId the primary key of the template's company
444             * @param  groupId the primary key of the group
445             * @param  classNameId the primary key of the class name for template's
446             *         related model
447             * @param  classPK the primary key of the template's related entity
448             * @param  keywords the keywords (space separated), which may occur in the
449             *         template's name or description (optionally <code>null</code>)
450             * @param  type the template's type (optionally <code>null</code>). For more
451             *         information, see {@link
452             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
453             * @param  mode the template's mode (optionally <code>null</code>) For more
454             *         information, see {@link
455             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
456             * @param  start the lower bound of the range of templates to return
457             * @param  end the upper bound of the range of templates to return (not
458             *         inclusive)
459             * @param  orderByComparator the comparator to order the templates
460             *         (optionally <code>null</code>)
461             * @return the matching templates ordered by the comparator
462             * @throws SystemException if a system exception occurred
463             */
464            public List<DDMTemplate> search(
465                            long companyId, long groupId, long classNameId, long classPK,
466                            String keywords, String type, String mode, int start, int end,
467                            OrderByComparator orderByComparator)
468                    throws SystemException {
469    
470                    return ddmTemplateFinder.filterFindByKeywords(
471                            companyId, groupId, classNameId, classPK, keywords, type, mode,
472                            start, end, orderByComparator);
473            }
474    
475            /**
476             * Returns an ordered range of all the templates matching the group, class
477             * name ID, class PK, name keyword, description keyword, type, mode, and
478             * language.
479             *
480             * <p>
481             * Useful when paginating results. Returns a maximum of <code>end -
482             * start</code> instances. <code>start</code> and <code>end</code> are not
483             * primary keys, they are indexes in the result set. Thus, <code>0</code>
484             * refers to the first result in the set. Setting both <code>start</code>
485             * and <code>end</code> to {@link
486             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
487             * result set.
488             * </p>
489             *
490             * @param  companyId the primary key of the template's company
491             * @param  groupId the primary key of the group
492             * @param  classNameId the primary key of the class name for template's
493             *         related model
494             * @param  classPK the primary key of the template's related entity
495             * @param  name the name keywords (optionally <code>null</code>)
496             * @param  description the description keywords (optionally
497             *         <code>null</code>)
498             * @param  type the template's type (optionally <code>null</code>). For more
499             *         information, see {@link
500             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
501             * @param  mode the template's mode (optionally <code>null</code>). For more
502             *         information, see {@link
503             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
504             * @param  language the template's script language (optionally
505             *         <code>null</code>). For more information, see {@link
506             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
507             * @param  andOperator whether every field must match its keywords, or just
508             *         one field.
509             * @param  start the lower bound of the range of templates to return
510             * @param  end the upper bound of the range of templates to return (not
511             *         inclusive)
512             * @param  orderByComparator the comparator to order the templates
513             *         (optionally <code>null</code>)
514             * @return the matching templates ordered by the comparator
515             * @throws SystemException if a system exception occurred
516             */
517            public List<DDMTemplate> search(
518                            long companyId, long groupId, long classNameId, long classPK,
519                            String name, String description, String type, String mode,
520                            String language, boolean andOperator, int start, int end,
521                            OrderByComparator orderByComparator)
522                    throws SystemException {
523    
524                    return ddmTemplateFinder.filterFindByC_G_C_C_N_D_T_M_L(
525                            companyId, groupId, classNameId, classPK, name, description, type,
526                            mode, language, andOperator, start, end, orderByComparator);
527            }
528    
529            /**
530             * Returns an ordered range of all the templates matching the group IDs,
531             * class name IDs, class PK, type, and mode, and matching the keywords in
532             * the template names and descriptions.
533             *
534             * <p>
535             * Useful when paginating results. Returns a maximum of <code>end -
536             * start</code> instances. <code>start</code> and <code>end</code> are not
537             * primary keys, they are indexes in the result set. Thus, <code>0</code>
538             * refers to the first result in the set. Setting both <code>start</code>
539             * and <code>end</code> to {@link
540             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
541             * result set.
542             * </p>
543             *
544             * @param  companyId the primary key of the template's company
545             * @param  groupIds the primary keys of the groups
546             * @param  classNameIds the primary keys of the entity's instances the
547             *         templates are related to
548             * @param  classPK the primary key of the template's related entity
549             * @param  keywords the keywords (space separated), which may occur in the
550             *         template's name or description (optionally <code>null</code>)
551             * @param  type the template's type (optionally <code>null</code>). For more
552             *         information, see {@link
553             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
554             * @param  mode the template's mode (optionally <code>null</code>). For more
555             *         information, see {@link
556             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
557             * @param  start the lower bound of the range of templates to return
558             * @param  end the upper bound of the range of templates to return (not
559             *         inclusive)
560             * @param  orderByComparator the comparator to order the templates
561             *         (optionally <code>null</code>)
562             * @return the matching templates ordered by the comparator
563             * @throws SystemException if a system exception occurred
564             */
565            public List<DDMTemplate> search(
566                            long companyId, long[] groupIds, long[] classNameIds, long classPK,
567                            String keywords, String type, String mode, int start, int end,
568                            OrderByComparator orderByComparator)
569                    throws SystemException {
570    
571                    return ddmTemplateFinder.filterFindByKeywords(
572                            companyId, groupIds, classNameIds, classPK, keywords, type, mode,
573                            start, end, orderByComparator);
574            }
575    
576            /**
577             * Returns an ordered range of all the templates matching the group IDs,
578             * class name IDs, class PK, name keyword, description keyword, type, mode,
579             * and language.
580             *
581             * <p>
582             * Useful when paginating results. Returns a maximum of <code>end -
583             * start</code> instances. <code>start</code> and <code>end</code> are not
584             * primary keys, they are indexes in the result set. Thus, <code>0</code>
585             * refers to the first result in the set. Setting both <code>start</code>
586             * and <code>end</code> to {@link
587             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
588             * result set.
589             * </p>
590             *
591             * @param  companyId the primary key of the template's company
592             * @param  groupIds the primary keys of the groups
593             * @param  classNameIds the primary keys of the entity's instances the
594             *         templates are related to
595             * @param  classPK the primary key of the template's related entity
596             * @param  name the name keywords (optionally <code>null</code>)
597             * @param  description the description keywords (optionally
598             *         <code>null</code>)
599             * @param  type the template's type (optionally <code>null</code>). For more
600             *         information, see {@link
601             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
602             * @param  mode the template's mode (optionally <code>null</code>). For more
603             *         information, see {@link
604             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
605             * @param  language the template's script language (optionally
606             *         <code>null</code>). For more information, see {@link
607             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
608             * @param  andOperator whether every field must match its keywords, or just
609             *         one field.
610             * @param  start the lower bound of the range of templates to return
611             * @param  end the upper bound of the range of templates to return (not
612             *         inclusive)
613             * @param  orderByComparator the comparator to order the templates
614             *         (optionally <code>null</code>)
615             * @return the matching templates ordered by the comparator
616             * @throws SystemException if a system exception occurred
617             */
618            public List<DDMTemplate> search(
619                            long companyId, long[] groupIds, long[] classNameIds, long classPK,
620                            String name, String description, String type, String mode,
621                            String language, boolean andOperator, int start, int end,
622                            OrderByComparator orderByComparator)
623                    throws SystemException {
624    
625                    return ddmTemplateFinder.filterFindByC_G_C_C_N_D_T_M_L(
626                            companyId, groupIds, classNameIds, classPK, name, description, type,
627                            mode, language, andOperator, start, end, orderByComparator);
628            }
629    
630            /**
631             * Returns the number of templates matching the group, class name ID, class
632             * PK, type, and mode, and matching the keywords in the template names and
633             * descriptions.
634             *
635             * @param  companyId the primary key of the template's company
636             * @param  groupId the primary key of the group
637             * @param  classNameId the primary key of the class name for template's
638             *         related model
639             * @param  classPK the primary key of the template's related entity
640             * @param  keywords the keywords (space separated), which may occur in the
641             *         template's name or description (optionally <code>null</code>)
642             * @param  type the template's type (optionally <code>null</code>). For more
643             *         information, see {@link
644             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
645             * @param  mode the template's mode (optionally <code>null</code>). For more
646             *         information, see {@link
647             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
648             * @return the number of matching templates
649             * @throws SystemException if a system exception occurred
650             */
651            public int searchCount(
652                            long companyId, long groupId, long classNameId, long classPK,
653                            String keywords, String type, String mode)
654                    throws SystemException {
655    
656                    return ddmTemplateFinder.filterCountByKeywords(
657                            companyId, groupId, classNameId, classPK, keywords, type, mode);
658            }
659    
660            /**
661             * Returns the number of templates matching the group, class name ID, class
662             * PK, name keyword, description keyword, type, mode, and language.
663             *
664             * @param  companyId the primary key of the template's company
665             * @param  groupId the primary key of the group
666             * @param  classNameId the primary key of the class name for template's
667             *         related model
668             * @param  classPK the primary key of the template's related entity
669             * @param  name the name keywords (optionally <code>null</code>)
670             * @param  description the description keywords (optionally
671             *         <code>null</code>)
672             * @param  type the template's type (optionally <code>null</code>). For more
673             *         information, see {@link
674             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
675             * @param  mode the template's mode (optionally <code>null</code>). For more
676             *         information, see {@link
677             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
678             * @param  language the template's script language (optionally
679             *         <code>null</code>). For more information, see {@link
680             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
681             * @param  andOperator whether every field must match its keywords, or just
682             *         one field.
683             * @return the number of matching templates
684             * @throws SystemException if a system exception occurred
685             */
686            public int searchCount(
687                            long companyId, long groupId, long classNameId, long classPK,
688                            String name, String description, String type, String mode,
689                            String language, boolean andOperator)
690                    throws SystemException {
691    
692                    return ddmTemplateFinder.filterCountByC_G_C_C_N_D_T_M_L(
693                            companyId, groupId, classNameId, classPK, name, description, type,
694                            mode, language, andOperator);
695            }
696    
697            /**
698             * Returns the number of templates matching the group IDs, class name IDs,
699             * class PK, type, and mode, and matching the keywords in the template names
700             * and descriptions.
701             *
702             * @param  companyId the primary key of the template's company
703             * @param  groupIds the primary keys of the groups
704             * @param  classNameIds the primary keys of the entity's instances the
705             *         templates are related to
706             * @param  classPK the primary key of the template's related entity
707             * @param  keywords the keywords (space separated), which may occur in the
708             *         template's name or description (optionally <code>null</code>)
709             * @param  type the template's type (optionally <code>null</code>). For more
710             *         information, see {@link
711             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
712             * @param  mode the template's mode (optionally <code>null</code>). For more
713             *         information, see {@link
714             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
715             * @return the number of matching templates
716             * @throws SystemException if a system exception occurred
717             */
718            public int searchCount(
719                            long companyId, long[] groupIds, long[] classNameIds, long classPK,
720                            String keywords, String type, String mode)
721                    throws SystemException {
722    
723                    return ddmTemplateFinder.filterCountByKeywords(
724                            companyId, groupIds, classNameIds, classPK, keywords, type, mode);
725            }
726    
727            /**
728             * Returns the number of templates matching the group IDs, class name IDs,
729             * class PK, name keyword, description keyword, type, mode, and language.
730             *
731             * @param  companyId the primary key of the template's company
732             * @param  groupIds the primary keys of the groups
733             * @param  classNameIds the primary keys of the entity's instances the
734             *         templates are related to
735             * @param  classPK the primary key of the template's related entity
736             * @param  name the name keywords (optionally <code>null</code>)
737             * @param  description the description keywords (optionally
738             *         <code>null</code>)
739             * @param  type the template's type (optionally <code>null</code>). For more
740             *         information, see {@link
741             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
742             * @param  mode the template's mode (optionally <code>null</code>). For more
743             *         information, see {@link
744             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
745             * @param  language the template's script language (optionally
746             *         <code>null</code>). For more information, see {@link
747             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
748             * @param  andOperator whether every field must match its keywords, or just
749             *         one field.
750             * @return the number of matching templates
751             * @throws SystemException if a system exception occurred
752             */
753            public int searchCount(
754                            long companyId, long[] groupIds, long[] classNameIds, long classPK,
755                            String name, String description, String type, String mode,
756                            String language, boolean andOperator)
757                    throws SystemException {
758    
759                    return ddmTemplateFinder.filterCountByC_G_C_C_N_D_T_M_L(
760                            companyId, groupIds, classNameIds, classPK, name, description, type,
761                            mode, language, andOperator);
762            }
763    
764            /**
765             * Updates the template matching the ID.
766             *
767             * @param  templateId the primary key of the template
768             * @param  nameMap the template's new locales and localized names
769             * @param  descriptionMap the template's new locales and localized
770             *         description
771             * @param  type the template's type. For more information, see {@link
772             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
773             * @param  mode the template's mode. For more information, see {@link
774             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
775             * @param  language the template's script language. For more information,
776             *         see {@link
777             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
778             * @param  script the template's script
779             * @param  cacheable whether the template is cacheable
780             * @param  smallImage whether the template has a small image
781             * @param  smallImageURL the template's small image URL (optionally
782             *         <code>null</code>)
783             * @param  smallImageFile the template's small image file (optionally
784             *         <code>null</code>)
785             * @param  serviceContext the service context to be applied. Can set the
786             *         modification date.
787             * @return the updated template
788             * @throws PortalException if the user did not have permission to update the
789             *         template or if a portal exception occurred
790             * @throws SystemException if a system exception occurred
791             */
792            public DDMTemplate updateTemplate(
793                            long templateId, Map<Locale, String> nameMap,
794                            Map<Locale, String> descriptionMap, String type, String mode,
795                            String language, String script, boolean cacheable,
796                            boolean smallImage, String smallImageURL, File smallImageFile,
797                            ServiceContext serviceContext)
798                    throws PortalException, SystemException {
799    
800                    DDMTemplatePermission.check(
801                            getPermissionChecker(), templateId, ActionKeys.UPDATE);
802    
803                    return ddmTemplateLocalService.updateTemplate(
804                            templateId, nameMap, descriptionMap, type, mode, language, script,
805                            cacheable, smallImage, smallImageURL, smallImageFile,
806                            serviceContext);
807            }
808    
809            protected String getDDMResourceActionId(
810                    String ddmResource, ServiceContext serviceContext) {
811    
812                    String ddmResourceActionId = ParamUtil.getString(
813                            serviceContext, "ddmResourceActionId");
814    
815                    if (Validator.isNull(ddmResourceActionId) &&
816                            ddmResource.equals(_DDL_CLASS_NAME)) {
817    
818                            ddmResourceActionId = ActionKeys.ADD_TEMPLATE;
819                    }
820    
821                    return ddmResourceActionId;
822            }
823    
824            private static final String _DDL_CLASS_NAME =
825                    "com.liferay.portlet.dynamicdatalists";
826    
827    }