001    /**
002     * Copyright (c) 2000-2013 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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.systemevent.SystemEvent;
022    import com.liferay.portal.kernel.template.TemplateConstants;
023    import com.liferay.portal.kernel.util.FileUtil;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.OrderByComparator;
026    import com.liferay.portal.kernel.util.PropsKeys;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.Group;
031    import com.liferay.portal.model.Image;
032    import com.liferay.portal.model.ResourceConstants;
033    import com.liferay.portal.model.SystemEventConstants;
034    import com.liferay.portal.model.User;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portal.service.persistence.ImageUtil;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portal.util.PrefsPropsUtil;
039    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
040    import com.liferay.portlet.dynamicdatamapping.RequiredTemplateException;
041    import com.liferay.portlet.dynamicdatamapping.TemplateDuplicateTemplateKeyException;
042    import com.liferay.portlet.dynamicdatamapping.TemplateNameException;
043    import com.liferay.portlet.dynamicdatamapping.TemplateScriptException;
044    import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageNameException;
045    import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageSizeException;
046    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
047    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
048    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
049    import com.liferay.portlet.dynamicdatamapping.service.base.DDMTemplateLocalServiceBaseImpl;
050    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
051    import com.liferay.portlet.journal.model.JournalArticle;
052    import com.liferay.portlet.journal.service.persistence.JournalArticleUtil;
053    
054    import java.io.File;
055    import java.io.IOException;
056    
057    import java.util.ArrayList;
058    import java.util.Date;
059    import java.util.List;
060    import java.util.Locale;
061    import java.util.Map;
062    
063    /**
064     * Provides the local service for accessing, adding, copying, deleting, and
065     * updating dynamic data mapping (DDM) templates.
066     *
067     * <p>
068     * DDM templates (templates) are used in Liferay to render templated content
069     * like applications, forms, dynamic data lists, or web contents.
070     * </p>
071     *
072     * <p>
073     * Templates support a variety of templating languages, like Velocity or
074     * FreeMarker. They also support multi-language names and descriptions.
075     * </p>
076     *
077     * <p>
078     * Templates can be related to many models in Liferay, such as those for
079     * structures, dynamic data lists, and applications. This relationship can be
080     * established via the class name ID and class PK.
081     * </p>
082     *
083     * @author Brian Wing Shun Chan
084     * @author Eduardo Lundgren
085     * @author Marcellus Tavares
086     */
087    public class DDMTemplateLocalServiceImpl
088            extends DDMTemplateLocalServiceBaseImpl {
089    
090            /**
091             * Adds a template.
092             *
093             * @param  userId the primary key of the template's creator/owner
094             * @param  groupId the primary key of the group
095             * @param  classNameId the primary key of the class name for the template's
096             *         related model
097             * @param  classPK the primary key of the template's related entity
098             * @param  nameMap the template's locales and localized names
099             * @param  descriptionMap the template's locales and localized descriptions
100             * @param  type the template's type. For more information, see {@link
101             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
102             * @param  mode the template's mode. For more information, see {@link
103             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
104             * @param  language the template's script language. For more information,
105             *         see {@link
106             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
107             * @param  script the template's script
108             * @param  serviceContext the service context to be applied. Can set the
109             *         UUID, creation date, modification date, guest permissions, and
110             *         group permissions for the template.
111             * @return the template
112             * @throws PortalException if a portal exception occurred
113             * @throws SystemException if a system exception occurred
114             */
115            @Override
116            public DDMTemplate addTemplate(
117                            long userId, long groupId, long classNameId, long classPK,
118                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
119                            String type, String mode, String language, String script,
120                            ServiceContext serviceContext)
121                    throws PortalException, SystemException {
122    
123                    return addTemplate(
124                            userId, groupId, classNameId, classPK, null, nameMap,
125                            descriptionMap, type, mode, language, script, false, false, null,
126                            null, serviceContext);
127            }
128    
129            /**
130             * Adds a template with additional parameters.
131             *
132             * @param  userId the primary key of the template's creator/owner
133             * @param  groupId the primary key of the group
134             * @param  classNameId the primary key of the class name for the template's
135             *         related model
136             * @param  classPK the primary key of the template's related entity
137             * @param  templateKey the unique string identifying the template
138             *         (optionally <code>null</code>)
139             * @param  nameMap the template's locales and localized names
140             * @param  descriptionMap the template's locales and localized descriptions
141             * @param  type the template's type. For more information, see {@link
142             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
143             * @param  mode the template's mode. For more information, see {@link
144             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
145             * @param  language the template's script language. For more information,
146             *         see {@link
147             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
148             * @param  script the template's script
149             * @param  cacheable whether the template is cacheable
150             * @param  smallImage whether the template has a small image
151             * @param  smallImageURL the template's small image URL (optionally
152             *         <code>null</code>)
153             * @param  smallImageFile the template's small image file (optionally
154             *         <code>null</code>)
155             * @param  serviceContext the service context to be applied. Can set the
156             *         UUID, creation date, modification date, guest permissions, and
157             *         group permissions for the template.
158             * @return the template
159             * @throws PortalException if a portal exception occurred
160             * @throws SystemException if a system exception occurred
161             */
162            @Override
163            public DDMTemplate addTemplate(
164                            long userId, long groupId, long classNameId, long classPK,
165                            String templateKey, Map<Locale, String> nameMap,
166                            Map<Locale, String> descriptionMap, String type, String mode,
167                            String language, String script, boolean cacheable,
168                            boolean smallImage, String smallImageURL, File smallImageFile,
169                            ServiceContext serviceContext)
170                    throws PortalException, SystemException {
171    
172                    // Template
173    
174                    User user = userPersistence.findByPrimaryKey(userId);
175                    Date now = new Date();
176    
177                    if (Validator.isNull(templateKey)) {
178                            templateKey = String.valueOf(counterLocalService.increment());
179                    }
180                    else {
181                            templateKey = StringUtil.toUpperCase(templateKey.trim());
182                    }
183    
184                    script = formatScript(type, language, script);
185    
186                    byte[] smallImageBytes = null;
187    
188                    if (smallImage) {
189                            try {
190                                    smallImageBytes = FileUtil.getBytes(smallImageFile);
191                            }
192                            catch (IOException ioe) {
193                            }
194    
195                            if ((smallImageBytes == null) || Validator.isUrl(smallImageURL)) {
196                                    smallImage = false;
197                            }
198                    }
199    
200                    validate(
201                            groupId, classNameId, templateKey, nameMap, script, smallImage,
202                            smallImageURL, smallImageFile, smallImageBytes);
203    
204                    long templateId = counterLocalService.increment();
205    
206                    DDMTemplate template = ddmTemplatePersistence.create(templateId);
207    
208                    template.setUuid(serviceContext.getUuid());
209                    template.setGroupId(groupId);
210                    template.setCompanyId(user.getCompanyId());
211                    template.setUserId(user.getUserId());
212                    template.setUserName(user.getFullName());
213                    template.setCreateDate(serviceContext.getCreateDate(now));
214                    template.setModifiedDate(serviceContext.getModifiedDate(now));
215                    template.setClassNameId(classNameId);
216                    template.setClassPK(classPK);
217                    template.setTemplateKey(templateKey);
218                    template.setNameMap(nameMap);
219                    template.setDescriptionMap(descriptionMap);
220                    template.setType(type);
221                    template.setMode(mode);
222                    template.setLanguage(language);
223                    template.setScript(script);
224                    template.setCacheable(cacheable);
225                    template.setSmallImage(smallImage);
226                    template.setSmallImageId(counterLocalService.increment());
227                    template.setSmallImageURL(smallImageURL);
228    
229                    ddmTemplatePersistence.update(template);
230    
231                    // Resources
232    
233                    if (serviceContext.isAddGroupPermissions() ||
234                            serviceContext.isAddGuestPermissions()) {
235    
236                            addTemplateResources(
237                                    template, serviceContext.isAddGroupPermissions(),
238                                    serviceContext.isAddGuestPermissions());
239                    }
240                    else {
241                            addTemplateResources(
242                                    template, serviceContext.getGroupPermissions(),
243                                    serviceContext.getGuestPermissions());
244                    }
245    
246                    // Small image
247    
248                    saveImages(
249                            smallImage, template.getSmallImageId(), smallImageFile,
250                            smallImageBytes);
251    
252                    return template;
253            }
254    
255            /**
256             * Adds the resources to the template.
257             *
258             * @param  template the template to add resources to
259             * @param  addGroupPermissions whether to add group permissions
260             * @param  addGuestPermissions whether to add guest permissions
261             * @throws PortalException if a portal exception occurred
262             * @throws SystemException if a system exception occurred
263             */
264            @Override
265            public void addTemplateResources(
266                            DDMTemplate template, boolean addGroupPermissions,
267                            boolean addGuestPermissions)
268                    throws PortalException, SystemException {
269    
270                    resourceLocalService.addResources(
271                            template.getCompanyId(), template.getGroupId(),
272                            template.getUserId(), DDMTemplate.class.getName(),
273                            template.getTemplateId(), false, addGroupPermissions,
274                            addGuestPermissions);
275            }
276    
277            /**
278             * Adds the model resources with the permissions to the template.
279             *
280             * @param  template the template to add resources to
281             * @param  groupPermissions the group permissions to be added
282             * @param  guestPermissions the guest permissions to be added
283             * @throws PortalException if a portal exception occurred
284             * @throws SystemException if a system exception occurred
285             */
286            @Override
287            public void addTemplateResources(
288                            DDMTemplate template, String[] groupPermissions,
289                            String[] guestPermissions)
290                    throws PortalException, SystemException {
291    
292                    resourceLocalService.addModelResources(
293                            template.getCompanyId(), template.getGroupId(),
294                            template.getUserId(), DDMTemplate.class.getName(),
295                            template.getTemplateId(), groupPermissions, guestPermissions);
296            }
297    
298            /**
299             * Copies the template, creating a new template with all the values
300             * extracted from the original one. This method supports defining a new name
301             * and description.
302             *
303             * @param  userId the primary key of the template's creator/owner
304             * @param  templateId the primary key of the template to be copied
305             * @param  nameMap the new template's locales and localized names
306             * @param  descriptionMap the new template's locales and localized
307             *         descriptions
308             * @param  serviceContext the service context to be applied. Can set the
309             *         UUID, creation date, modification date, guest permissions, and
310             *         group permissions for the template.
311             * @return the new template
312             * @throws PortalException if a portal exception occurred
313             * @throws SystemException if a system exception occurred
314             */
315            @Override
316            public DDMTemplate copyTemplate(
317                            long userId, long templateId, Map<Locale, String> nameMap,
318                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
319                    throws PortalException, SystemException {
320    
321                    DDMTemplate template = ddmTemplatePersistence.findByPrimaryKey(
322                            templateId);
323    
324                    return copyTemplate(
325                            userId, template, template.getClassPK(), nameMap, descriptionMap,
326                            serviceContext);
327            }
328    
329            @Override
330            public DDMTemplate copyTemplate(
331                            long userId, long templateId, ServiceContext serviceContext)
332                    throws PortalException, SystemException {
333    
334                    DDMTemplate template = ddmTemplatePersistence.findByPrimaryKey(
335                            templateId);
336    
337                    return copyTemplate(
338                            userId, template, template.getClassPK(), template.getNameMap(),
339                            template.getDescriptionMap(), serviceContext);
340            }
341    
342            /**
343             * Copies all the templates matching the class name ID, class PK, and type.
344             * This method creates new templates, extracting all the values from the old
345             * ones and updating their class PKs.
346             *
347             * @param  userId the primary key of the template's creator/owner
348             * @param  classNameId the primary key of the class name for the template's
349             *         related model
350             * @param  oldClassPK the primary key of the old template's related entity
351             * @param  newClassPK the primary key of the new template's related entity
352             * @param  type the template's type. For more information, see {@link
353             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
354             * @param  serviceContext the service context to be applied. Can set the
355             *         creation date, modification date, guest permissions, and group
356             *         permissions for the new templates.
357             * @return the new templates
358             * @throws PortalException if a portal exception occurred
359             * @throws SystemException if a system exception occurred
360             */
361            @Override
362            public List<DDMTemplate> copyTemplates(
363                            long userId, long classNameId, long oldClassPK, long newClassPK,
364                            String type, ServiceContext serviceContext)
365                    throws PortalException, SystemException {
366    
367                    List<DDMTemplate> newTemplates = new ArrayList<DDMTemplate>();
368    
369                    List<DDMTemplate> oldTemplates = ddmTemplatePersistence.findByC_C_T(
370                            classNameId, oldClassPK, type);
371    
372                    for (DDMTemplate oldTemplate : oldTemplates) {
373                            DDMTemplate newTemplate = copyTemplate(
374                                    userId, oldTemplate, newClassPK, oldTemplate.getNameMap(),
375                                    oldTemplate.getDescriptionMap(), serviceContext);
376    
377                            newTemplates.add(newTemplate);
378                    }
379    
380                    return newTemplates;
381            }
382    
383            /**
384             * Deletes the template and its resources.
385             *
386             * @param  template the template to be deleted
387             * @throws PortalException if a portal exception occurred
388             * @throws SystemException if a system exception occurred
389             */
390            @Override
391            @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
392            public void deleteTemplate(DDMTemplate template)
393                    throws PortalException, SystemException {
394    
395                    // Template
396    
397                    if (template.getClassNameId() ==
398                                    PortalUtil.getClassNameId(DDMStructure.class.getName())) {
399    
400                            DDMStructure structure = ddmStructureLocalService.fetchDDMStructure(
401                                    template.getClassPK());
402    
403                            if ((structure != null) &&
404                                    (structure.getClassNameId() ==
405                                            PortalUtil.getClassNameId(
406                                                    JournalArticle.class.getName()))) {
407    
408                                    Group companyGroup = groupLocalService.getCompanyGroup(
409                                            template.getCompanyId());
410    
411                                    if (template.getGroupId() == companyGroup.getGroupId()) {
412                                            if (JournalArticleUtil.countByTemplateId(
413                                                            template.getTemplateKey()) > 0) {
414    
415                                                    throw new RequiredTemplateException();
416                                            }
417                                    }
418                                    else {
419                                            if (JournalArticleUtil.countByG_T(
420                                                            template.getGroupId(),
421                                                            template.getTemplateKey()) > 0) {
422    
423                                                    throw new RequiredTemplateException();
424                                            }
425                                    }
426                            }
427                    }
428    
429                    ddmTemplatePersistence.remove(template);
430    
431                    // Resources
432    
433                    resourceLocalService.deleteResource(
434                            template.getCompanyId(), DDMTemplate.class.getName(),
435                            ResourceConstants.SCOPE_INDIVIDUAL, template.getTemplateId());
436            }
437    
438            /**
439             * Deletes the template and its resources.
440             *
441             * @param  templateId the primary key of the template to be deleted
442             * @throws PortalException if a portal exception occurred
443             * @throws SystemException if a system exception occurred
444             */
445            @Override
446            public void deleteTemplate(long templateId)
447                    throws PortalException, SystemException {
448    
449                    DDMTemplate template = ddmTemplatePersistence.findByPrimaryKey(
450                            templateId);
451    
452                    ddmTemplateLocalService.deleteTemplate(template);
453            }
454    
455            /**
456             * Deletes all the templates of the group.
457             *
458             * @param  groupId the primary key of the group
459             * @throws PortalException if a portal exception occurred
460             * @throws SystemException if a system exception occurred
461             */
462            @Override
463            public void deleteTemplates(long groupId)
464                    throws PortalException, SystemException {
465    
466                    List<DDMTemplate> templates = ddmTemplatePersistence.findByGroupId(
467                            groupId);
468    
469                    for (DDMTemplate template : templates) {
470                            ddmTemplateLocalService.deleteTemplate(template);
471                    }
472            }
473    
474            @Override
475            public void deleteTemplates(long groupId, long classNameId)
476                    throws PortalException, SystemException {
477    
478                    List<DDMTemplate> templates = ddmTemplatePersistence.findByG_C(
479                            groupId, classNameId);
480    
481                    for (DDMTemplate template : templates) {
482                            ddmTemplateLocalService.deleteTemplate(template);
483                    }
484            }
485    
486            /**
487             * Returns the template matching the group and template key.
488             *
489             * @param  groupId the primary key of the group
490             * @param  classNameId the primary key of the class name for the template's
491             *         related model
492             * @param  templateKey the unique string identifying the template
493             * @return the matching template, or <code>null</code> if a matching
494             *         template could not be found
495             * @throws SystemException if a system exception occurred
496             */
497            @Override
498            public DDMTemplate fetchTemplate(
499                            long groupId, long classNameId, String templateKey)
500                    throws SystemException {
501    
502                    templateKey = StringUtil.toUpperCase(templateKey.trim());
503    
504                    return ddmTemplatePersistence.fetchByG_C_T(
505                            groupId, classNameId, templateKey);
506            }
507    
508            /**
509             * Returns the template matching the group and template key, optionally in
510             * the global scope.
511             *
512             * <p>
513             * This method first searches in the given group. If the template is still
514             * not found and <code>includeGlobalTemplates</code> is set to
515             * <code>true</code>, this method searches the global group.
516             * </p>
517             *
518             * @param  groupId the primary key of the group
519             * @param  classNameId the primary key of the class name for the template's
520             *         related model
521             * @param  templateKey the unique string identifying the template
522             * @param  includeGlobalTemplates whether to include the global scope in the
523             *         search
524             * @return the matching template, or <code>null</code> if a matching
525             *         template could not be found
526             * @throws PortalException if a portal exception occurred
527             * @throws SystemException if a system exception occurred
528             */
529            @Override
530            public DDMTemplate fetchTemplate(
531                            long groupId, long classNameId, String templateKey,
532                            boolean includeGlobalTemplates)
533                    throws PortalException, SystemException {
534    
535                    templateKey = StringUtil.toUpperCase(templateKey.trim());
536    
537                    DDMTemplate template = ddmTemplatePersistence.fetchByG_C_T(
538                            groupId, classNameId, templateKey);
539    
540                    if ((template != null) || !includeGlobalTemplates) {
541                            return template;
542                    }
543    
544                    Group group = groupPersistence.findByPrimaryKey(groupId);
545    
546                    Group companyGroup = groupLocalService.getCompanyGroup(
547                            group.getCompanyId());
548    
549                    return ddmTemplatePersistence.fetchByG_C_T(
550                            companyGroup.getGroupId(), classNameId, templateKey);
551            }
552    
553            /**
554             * Returns the template with the ID.
555             *
556             * @param  templateId the primary key of the template
557             * @return the template with the ID
558             * @throws PortalException if a matching template could not be found
559             * @throws SystemException if a system exception occurred
560             */
561            @Override
562            public DDMTemplate getTemplate(long templateId)
563                    throws PortalException, SystemException {
564    
565                    return ddmTemplatePersistence.findByPrimaryKey(templateId);
566            }
567    
568            /**
569             * Returns the template matching the group and template key.
570             *
571             * @param  groupId the primary key of the group
572             * @param  classNameId the primary key of the class name for the template's
573             *         related model
574             * @param  templateKey the unique string identifying the template
575             * @return the matching template
576             * @throws PortalException if a matching template could not be found
577             * @throws SystemException if a system exception occurred
578             */
579            @Override
580            public DDMTemplate getTemplate(
581                            long groupId, long classNameId, String templateKey)
582                    throws PortalException, SystemException {
583    
584                    templateKey = StringUtil.toUpperCase(templateKey.trim());
585    
586                    return ddmTemplatePersistence.findByG_C_T(
587                            groupId, classNameId, templateKey);
588            }
589    
590            /**
591             * Returns the template matching the group and template key, optionally in
592             * the global scope.
593             *
594             * <p>
595             * This method first searches in the group. If the template is still not
596             * found and <code>includeGlobalTemplates</code> is set to
597             * <code>true</code>, this method searches the global group.
598             * </p>
599             *
600             * @param  groupId the primary key of the group
601             * @param  classNameId the primary key of the class name for the template's
602             *         related model
603             * @param  templateKey the unique string identifying the template
604             * @param  includeGlobalTemplates whether to include the global scope in the
605             *         search
606             * @return the matching template
607             * @throws PortalException if a matching template could not be found
608             * @throws SystemException if a system exception occurred
609             */
610            @Override
611            public DDMTemplate getTemplate(
612                            long groupId, long classNameId, String templateKey,
613                            boolean includeGlobalTemplates)
614                    throws PortalException, SystemException {
615    
616                    templateKey = StringUtil.toUpperCase(templateKey.trim());
617    
618                    DDMTemplate template = ddmTemplatePersistence.fetchByG_C_T(
619                            groupId, classNameId, templateKey);
620    
621                    if (template != null) {
622                            return template;
623                    }
624    
625                    if (!includeGlobalTemplates) {
626                            throw new NoSuchTemplateException(
627                                    "No DDMTemplate exists with the template key " + templateKey);
628                    }
629    
630                    Group group = groupPersistence.findByPrimaryKey(groupId);
631    
632                    Group companyGroup = groupLocalService.getCompanyGroup(
633                            group.getCompanyId());
634    
635                    return ddmTemplatePersistence.findByG_C_T(
636                            companyGroup.getGroupId(), classNameId, templateKey);
637            }
638    
639            @Override
640            public DDMTemplate getTemplateBySmallImageId(long smallImageId)
641                    throws PortalException, SystemException {
642    
643                    return ddmTemplatePersistence.findBySmallImageId(smallImageId);
644            }
645    
646            /**
647             * Returns all the templates with the class PK.
648             *
649             * @param  classPK the primary key of the template's related entity
650             * @return the templates with the class PK
651             * @throws SystemException if a system exception occurred
652             */
653            @Override
654            public List<DDMTemplate> getTemplates(long classPK) throws SystemException {
655                    return ddmTemplatePersistence.findByClassPK(classPK);
656            }
657    
658            /**
659             * Returns all the templates matching the group and class name ID.
660             *
661             * @param  groupId the primary key of the group
662             * @param  classNameId the primary key of the class name for the template's
663             *         related model
664             * @return the matching templates
665             * @throws SystemException if a system exception occurred
666             */
667            @Override
668            public List<DDMTemplate> getTemplates(long groupId, long classNameId)
669                    throws SystemException {
670    
671                    return ddmTemplatePersistence.findByG_C(groupId, classNameId);
672            }
673    
674            /**
675             * Returns all the templates matching the group, class name ID, and class
676             * PK.
677             *
678             * @param  groupId the primary key of the group
679             * @param  classNameId the primary key of the class name for the template's
680             *         related model
681             * @param  classPK the primary key of the template's related entity
682             * @return the matching templates
683             * @throws SystemException if a system exception occurred
684             */
685            @Override
686            public List<DDMTemplate> getTemplates(
687                            long groupId, long classNameId, long classPK)
688                    throws SystemException {
689    
690                    return ddmTemplatePersistence.findByG_C_C(
691                            groupId, classNameId, classPK);
692            }
693    
694            /**
695             * Returns all the templates matching the group, class name ID, class PK,
696             * and type.
697             *
698             * @param  groupId the primary key of the group
699             * @param  classNameId the primary key of the class name for the template's
700             *         related model
701             * @param  classPK the primary key of the template's related entity
702             * @param  type the template's type. For more information, see {@link
703             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
704             * @return the matching templates
705             * @throws SystemException if a system exception occurred
706             */
707            @Override
708            public List<DDMTemplate> getTemplates(
709                            long groupId, long classNameId, long classPK, String type)
710                    throws SystemException {
711    
712                    return ddmTemplatePersistence.findByG_C_C_T(
713                            groupId, classNameId, classPK, type);
714            }
715    
716            /**
717             * Returns all the templates matching the group, class name ID, class PK,
718             * type, and mode.
719             *
720             * @param  groupId the primary key of the group
721             * @param  classNameId the primary key of the class name for the template's
722             *         related model
723             * @param  classPK the primary key of the template's related entity
724             * @param  type the template's type. For more information, see {@link
725             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
726             * @param  mode the template's mode. For more information, see {@link
727             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
728             * @return the matching templates
729             * @throws SystemException if a system exception occurred
730             */
731            @Override
732            public List<DDMTemplate> getTemplates(
733                            long groupId, long classNameId, long classPK, String type,
734                            String mode)
735                    throws SystemException {
736    
737                    return ddmTemplatePersistence.findByG_C_C_T_M(
738                            groupId, classNameId, classPK, type, mode);
739            }
740    
741            @Override
742            public List<DDMTemplate> getTemplatesByClassPK(long groupId, long classPK)
743                    throws SystemException {
744    
745                    return ddmTemplatePersistence.findByG_CPK(groupId, classPK);
746            }
747    
748            @Override
749            public List<DDMTemplate> getTemplatesByClassPK(
750                            long groupId, long classPK, int start, int end)
751                    throws SystemException {
752    
753                    return ddmTemplatePersistence.findByG_CPK(groupId, classPK, start, end);
754            }
755    
756            @Override
757            public List<DDMTemplate> getTemplatesByClassPK(
758                            long[] groupIds, long classPK)
759                    throws SystemException {
760    
761                    return ddmTemplatePersistence.findByG_CPK(groupIds, classPK);
762            }
763    
764            /**
765             * Returns the number of templates matching the group and class PK.
766             *
767             * @param  groupId the primary key of the group
768             * @param  classPK the primary key of the template's related entity
769             * @return the number of templates belonging to the group and class PK
770             * @throws SystemException if a system exception occurred
771             */
772            @Override
773            public int getTemplatesByClassPKCount(long groupId, long classPK)
774                    throws SystemException {
775    
776                    return ddmTemplatePersistence.countByG_CPK(groupId, classPK);
777            }
778    
779            /**
780             * Returns an ordered range of all the templates matching the group and
781             * structure class name ID.
782             *
783             * <p>
784             * Useful when paginating results. Returns a maximum of <code>end -
785             * start</code> instances. <code>start</code> and <code>end</code> are not
786             * primary keys, they are indexes in the result set. Thus, <code>0</code>
787             * refers to the first result in the set. Setting both <code>start</code>
788             * and <code>end</code> to {@link
789             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
790             * result set.
791             * </p>
792             *
793             * @param  groupId the primary key of the group
794             * @param  structureClassNameId the primary key of the class name for the
795             *         template's related structure
796             * @param  start the lower bound of the range of templates to return
797             * @param  end the upper bound of the range of templates to return (not
798             *         inclusive)
799             * @param  orderByComparator the comparator to order the templates
800             *         (optionally <code>null</code>)
801             * @return the range of matching templates ordered by the comparator
802             * @throws SystemException if a system exception occurred
803             */
804            @Override
805            public List<DDMTemplate> getTemplatesByStructureClassNameId(
806                            long groupId, long structureClassNameId, int start, int end,
807                            OrderByComparator orderByComparator)
808                    throws SystemException {
809    
810                    return ddmTemplateFinder.findByG_SC(
811                            groupId, structureClassNameId, start, end, orderByComparator);
812            }
813    
814            /**
815             * Returns the number of templates matching the group and structure class
816             * name ID, including Generic Templates.
817             *
818             * @param  groupId the primary key of the group
819             * @param  structureClassNameId the primary key of the class name for the
820             *         template's related structure
821             * @return the number of matching templates
822             * @throws SystemException if a system exception occurred
823             */
824            @Override
825            public int getTemplatesByStructureClassNameIdCount(
826                            long groupId, long structureClassNameId)
827                    throws SystemException {
828    
829                    return ddmTemplateFinder.countByG_SC(groupId, structureClassNameId);
830            }
831    
832            /**
833             * Returns the number of templates belonging to the group.
834             *
835             * @param  groupId the primary key of the group
836             * @return the number of templates belonging to the group
837             * @throws SystemException if a system exception occurred
838             */
839            @Override
840            public int getTemplatesCount(long groupId) throws SystemException {
841                    return ddmTemplatePersistence.countByGroupId(groupId);
842            }
843    
844            /**
845             * Returns the number of templates matching the group and class name ID.
846             *
847             * @param  groupId the primary key of the group
848             * @param  classNameId the primary key of the class name for the template's
849             *         related model
850             * @return the number of matching templates
851             * @throws SystemException if a system exception occurred
852             */
853            @Override
854            public int getTemplatesCount(long groupId, long classNameId)
855                    throws SystemException {
856    
857                    return ddmTemplatePersistence.countByG_C(groupId, classNameId);
858            }
859    
860            /**
861             * Returns the number of templates matching the group, class name ID, and
862             * class PK.
863             *
864             * @param  groupId the primary key of the group
865             * @param  classNameId the primary key of the class name for the template's
866             *         related model
867             * @param  classPK the primary key of the template's related entity
868             * @return the number of matching templates
869             * @throws SystemException if a system exception occurred
870             */
871            @Override
872            public int getTemplatesCount(long groupId, long classNameId, long classPK)
873                    throws SystemException {
874    
875                    return ddmTemplatePersistence.countByG_C_C(
876                            groupId, classNameId, classPK);
877            }
878    
879            /**
880             * Returns an ordered range of all the templates matching the group, class
881             * name ID, class PK, type, and mode, and matching the keywords in the
882             * template names and descriptions.
883             *
884             * <p>
885             * Useful when paginating results. Returns a maximum of <code>end -
886             * start</code> instances. <code>start</code> and <code>end</code> are not
887             * primary keys, they are indexes in the result set. Thus, <code>0</code>
888             * refers to the first result in the set. Setting both <code>start</code>
889             * and <code>end</code> to {@link
890             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
891             * result set.
892             * </p>
893             *
894             * @param  companyId the primary key of the template's company
895             * @param  groupId the primary key of the group
896             * @param  classNameId the primary key of the class name for the template's
897             *         related model
898             * @param  classPK the primary key of the template's related entity
899             * @param  keywords the keywords (space separated), which may occur in the
900             *         template's name or description (optionally <code>null</code>)
901             * @param  type the template's type (optionally <code>null</code>). For more
902             *         information, see {@link
903             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
904             * @param  mode the template's mode (optionally <code>null</code>). For more
905             *         information, see {@link
906             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
907             * @param  start the lower bound of the range of templates to return
908             * @param  end the upper bound of the range of templates to return (not
909             *         inclusive)
910             * @param  orderByComparator the comparator to order the templates
911             *         (optionally <code>null</code>)
912             * @return the range of matching templates ordered by the comparator
913             * @throws SystemException if a system exception occurred
914             */
915            @Override
916            public List<DDMTemplate> search(
917                            long companyId, long groupId, long classNameId, long classPK,
918                            String keywords, String type, String mode, int start, int end,
919                            OrderByComparator orderByComparator)
920                    throws SystemException {
921    
922                    return ddmTemplateFinder.findByKeywords(
923                            companyId, groupId, classNameId, classPK, keywords, type, mode,
924                            start, end, orderByComparator);
925            }
926    
927            /**
928             * Returns an ordered range of all the templates matching the group, class
929             * name ID, class PK, name keyword, description keyword, type, mode, and
930             * language.
931             *
932             * <p>
933             * Useful when paginating results. Returns a maximum of <code>end -
934             * start</code> instances. <code>start</code> and <code>end</code> are not
935             * primary keys, they are indexes in the result set. Thus, <code>0</code>
936             * refers to the first result in the set. Setting both <code>start</code>
937             * and <code>end</code> to {@link
938             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
939             * result set.
940             * </p>
941             *
942             * @param  companyId the primary key of the template's company
943             * @param  groupId the primary key of the group
944             * @param  classNameId the primary key of the class name for the template's
945             *         related model
946             * @param  classPK the primary key of the template's related entity
947             * @param  name the name keywords (optionally <code>null</code>)
948             * @param  description the description keywords (optionally
949             *         <code>null</code>)
950             * @param  type the template's type (optionally <code>null</code>). For more
951             *         information, see {@link
952             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
953             * @param  mode the template's mode (optionally <code>null</code>). For more
954             *         information, see {@link
955             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
956             * @param  language the template's script language (optionally
957             *         <code>null</code>). For more information, see {@link
958             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
959             * @param  andOperator whether every field must match its keywords, or just
960             *         one field
961             * @param  start the lower bound of the range of templates to return
962             * @param  end the upper bound of the range of templates to return (not
963             *         inclusive)
964             * @param  orderByComparator the comparator to order the templates
965             *         (optionally <code>null</code>)
966             * @return the range of matching templates ordered by the comparator
967             * @throws SystemException if a system exception occurred
968             */
969            @Override
970            public List<DDMTemplate> search(
971                            long companyId, long groupId, long classNameId, long classPK,
972                            String name, String description, String type, String mode,
973                            String language, boolean andOperator, int start, int end,
974                            OrderByComparator orderByComparator)
975                    throws SystemException {
976    
977                    return ddmTemplateFinder.findByC_G_C_C_N_D_T_M_L(
978                            companyId, groupId, classNameId, classPK, name, description, type,
979                            mode, language, andOperator, start, end, orderByComparator);
980            }
981    
982            /**
983             * Returns an ordered range of all the templates matching the group IDs,
984             * class Name IDs, class PK, type, and mode, and include the keywords on its
985             * names and descriptions.
986             *
987             * <p>
988             * Useful when paginating results. Returns a maximum of <code>end -
989             * start</code> instances. <code>start</code> and <code>end</code> are not
990             * primary keys, they are indexes in the result set. Thus, <code>0</code>
991             * refers to the first result in the set. Setting both <code>start</code>
992             * and <code>end</code> to {@link
993             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
994             * result set.
995             * </p>
996             *
997             * @param  companyId the primary key of the template's company
998             * @param  groupIds the primary keys of the groups
999             * @param  classNameIds the primary keys of the entity's instances the
1000             *         templates are related to
1001             * @param  classPKs the primary keys of the template's related entities
1002             * @param  keywords the keywords (space separated), which may occur in the
1003             *         template's name or description (optionally <code>null</code>)
1004             * @param  type the template's type (optionally <code>null</code>). For more
1005             *         information, see {@link
1006             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1007             * @param  mode the template's mode (optionally <code>null</code>). For more
1008             *         information, see {@link
1009             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1010             * @param  start the lower bound of the range of templates to return
1011             * @param  end the upper bound of the range of templates to return (not
1012             *         inclusive)
1013             * @param  orderByComparator the comparator to order the templates
1014             *         (optionally <code>null</code>)
1015             * @return the range of matching templates ordered by the comparator
1016             * @throws SystemException if a system exception occurred
1017             */
1018            @Override
1019            public List<DDMTemplate> search(
1020                            long companyId, long[] groupIds, long[] classNameIds,
1021                            long[] classPKs, String keywords, String type, String mode,
1022                            int start, int end, OrderByComparator orderByComparator)
1023                    throws SystemException {
1024    
1025                    return ddmTemplateFinder.findByKeywords(
1026                            companyId, groupIds, classNameIds, classPKs, keywords, type, mode,
1027                            start, end, orderByComparator);
1028            }
1029    
1030            /**
1031             * Returns an ordered range of all the templates matching the group IDs,
1032             * class name IDs, class PK, name keyword, description keyword, type, mode,
1033             * and language.
1034             *
1035             * <p>
1036             * Useful when paginating results. Returns a maximum of <code>end -
1037             * start</code> instances. <code>start</code> and <code>end</code> are not
1038             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1039             * refers to the first result in the set. Setting both <code>start</code>
1040             * and <code>end</code> to {@link
1041             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1042             * result set.
1043             * </p>
1044             *
1045             * @param  companyId the primary key of the template's company
1046             * @param  groupIds the primary keys of the groups
1047             * @param  classNameIds the primary keys of the entity's instances the
1048             *         templates are related to
1049             * @param  classPKs the primary keys of the template's related entities
1050             * @param  name the name keywords (optionally <code>null</code>)
1051             * @param  description the description keywords (optionally
1052             *         <code>null</code>)
1053             * @param  type the template's type (optionally <code>null</code>). For more
1054             *         information, see {@link
1055             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1056             * @param  mode the template's mode (optionally <code>null</code>). For more
1057             *         information, see {@link
1058             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1059             * @param  language the template's script language (optionally
1060             *         <code>null</code>). For more information, see {@link
1061             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1062             * @param  andOperator whether every field must match its keywords, or just
1063             *         one field.
1064             * @param  start the lower bound of the range of templates to return
1065             * @param  end the upper bound of the range of templates to return (not
1066             *         inclusive)
1067             * @param  orderByComparator the comparator to order the templates
1068             *         (optionally <code>null</code>)
1069             * @return the range of matching templates ordered by the comparator
1070             * @throws SystemException if a system exception occurred
1071             */
1072            @Override
1073            public List<DDMTemplate> search(
1074                            long companyId, long[] groupIds, long[] classNameIds,
1075                            long[] classPKs, String name, String description, String type,
1076                            String mode, String language, boolean andOperator, int start,
1077                            int end, OrderByComparator orderByComparator)
1078                    throws SystemException {
1079    
1080                    return ddmTemplateFinder.findByC_G_C_C_N_D_T_M_L(
1081                            companyId, groupIds, classNameIds, classPKs, name, description,
1082                            type, mode, language, andOperator, start, end, orderByComparator);
1083            }
1084    
1085            /**
1086             * Returns the number of templates matching the group, class name ID, class
1087             * PK, type, and matching the keywords in the template names and
1088             * descriptions.
1089             *
1090             * @param  companyId the primary key of the template's company
1091             * @param  groupId the primary key of the group
1092             * @param  classNameId the primary key of the class name for the template's
1093             *         related model
1094             * @param  classPK the primary key of the template's related entity
1095             * @param  keywords the keywords (space separated), which may occur in the
1096             *         template's name or description (optionally <code>null</code>)
1097             * @param  type the template's type (optionally <code>null</code>). For more
1098             *         information, see {@link
1099             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1100             * @param  mode the template's mode (optionally <code>null</code>). For more
1101             *         information, see {@link
1102             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1103             * @return the number of matching templates
1104             * @throws SystemException if a system exception occurred
1105             */
1106            @Override
1107            public int searchCount(
1108                            long companyId, long groupId, long classNameId, long classPK,
1109                            String keywords, String type, String mode)
1110                    throws SystemException {
1111    
1112                    return ddmTemplateFinder.countByKeywords(
1113                            companyId, groupId, classNameId, classPK, keywords, type, mode);
1114            }
1115    
1116            /**
1117             * Returns the number of templates matching the group, class name ID, class
1118             * PK, name keyword, description keyword, type, mode, and language.
1119             *
1120             * @param  companyId the primary key of the template's company
1121             * @param  groupId the primary key of the group
1122             * @param  classNameId the primary key of the class name for the template's
1123             *         related model
1124             * @param  classPK the primary key of the template's related entity
1125             * @param  name the name keywords (optionally <code>null</code>)
1126             * @param  description the description keywords (optionally
1127             *         <code>null</code>)
1128             * @param  type the template's type (optionally <code>null</code>). For more
1129             *         information, see {@link
1130             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1131             * @param  mode the template's mode (optionally <code>null</code>). For more
1132             *         information, see {@link
1133             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1134             * @param  language the template's script language (optionally
1135             *         <code>null</code>). For more information, see {@link
1136             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1137             * @param  andOperator whether every field must match its keywords, or just
1138             *         one field.
1139             * @return the number of matching templates
1140             * @throws SystemException if a system exception occurred
1141             */
1142            @Override
1143            public int searchCount(
1144                            long companyId, long groupId, long classNameId, long classPK,
1145                            String name, String description, String type, String mode,
1146                            String language, boolean andOperator)
1147                    throws SystemException {
1148    
1149                    return ddmTemplateFinder.countByC_G_C_C_N_D_T_M_L(
1150                            companyId, groupId, classNameId, classPK, name, description, type,
1151                            mode, language, andOperator);
1152            }
1153    
1154            /**
1155             * Returns the number of templates matching the group IDs, class name IDs,
1156             * class PK, type, and mode, and matching the keywords in the template names
1157             * and descriptions.
1158             *
1159             * @param  companyId the primary key of the template's company
1160             * @param  groupIds the primary keys of the groups
1161             * @param  classNameIds the primary keys of the entity's instance the
1162             *         templates are related to
1163             * @param  classPKs the primary keys of the template's related entities
1164             * @param  keywords the keywords (space separated), which may occur in the
1165             *         template's name or description (optionally <code>null</code>)
1166             * @param  type the template's type (optionally <code>null</code>). For more
1167             *         information, see {@link
1168             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1169             * @param  mode the template's mode (optionally <code>null</code>). For more
1170             *         information, see {@link
1171             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1172             * @return the number of matching templates
1173             * @throws SystemException if a system exception occurred
1174             */
1175            @Override
1176            public int searchCount(
1177                            long companyId, long[] groupIds, long[] classNameIds,
1178                            long[] classPKs, String keywords, String type, String mode)
1179                    throws SystemException {
1180    
1181                    return ddmTemplateFinder.countByKeywords(
1182                            companyId, groupIds, classNameIds, classPKs, keywords, type, mode);
1183            }
1184    
1185            /**
1186             * Returns the number of templates matching the group IDs, class name IDs,
1187             * class PKs, name keyword, description keyword, type, mode, and language.
1188             *
1189             * @param  companyId the primary key of the templates company
1190             * @param  groupIds the primary keys of the groups
1191             * @param  classNameIds the primary keys of the entity's instance the
1192             *         templates are related to
1193             * @param  classPKs the primary keys of the template's related entities
1194             * @param  name the name keywords (optionally <code>null</code>)
1195             * @param  description the description keywords (optionally
1196             *         <code>null</code>)
1197             * @param  type the template's type (optionally <code>null</code>). For more
1198             *         information, see {@link
1199             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1200             * @param  mode the template's mode (optionally <code>null</code>). For more
1201             *         information, see {@link
1202             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1203             * @param  language the template's script language (optionally
1204             *         <code>null</code>). For more information, see {@link
1205             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1206             * @param  andOperator whether every field must match its keywords, or just
1207             *         one field.
1208             * @return the number of matching templates
1209             * @throws SystemException if a system exception occurred
1210             */
1211            @Override
1212            public int searchCount(
1213                            long companyId, long[] groupIds, long[] classNameIds,
1214                            long[] classPKs, String name, String description, String type,
1215                            String mode, String language, boolean andOperator)
1216                    throws SystemException {
1217    
1218                    return ddmTemplateFinder.countByC_G_C_C_N_D_T_M_L(
1219                            companyId, groupIds, classNameIds, classPKs, name, description,
1220                            type, mode, language, andOperator);
1221            }
1222    
1223            /**
1224             * Updates the template matching the ID.
1225             *
1226             * @param  templateId the primary key of the template
1227             * @param  classPK the primary key of the template's related entity
1228             * @param  nameMap the template's new locales and localized names
1229             * @param  descriptionMap the template's new locales and localized
1230             *         description
1231             * @param  type the template's type. For more information, see {@link
1232             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1233             * @param  mode the template's mode. For more information, see {@link
1234             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1235             * @param  language the template's script language. For more information,
1236             *         see {@link
1237             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1238             * @param  script the template's script
1239             * @param  cacheable whether the template is cacheable
1240             * @param  smallImage whether the template has a small image
1241             * @param  smallImageURL the template's small image URL (optionally
1242             *         <code>null</code>)
1243             * @param  smallImageFile the template's small image file (optionally
1244             *         <code>null</code>)
1245             * @param  serviceContext the service context to be applied. Can set the
1246             *         modification date.
1247             * @return the updated template
1248             * @throws PortalException if a portal exception occurred
1249             * @throws SystemException if a system exception occurred
1250             */
1251            @Override
1252            public DDMTemplate updateTemplate(
1253                            long templateId, long classPK, Map<Locale, String> nameMap,
1254                            Map<Locale, String> descriptionMap, String type, String mode,
1255                            String language, String script, boolean cacheable,
1256                            boolean smallImage, String smallImageURL, File smallImageFile,
1257                            ServiceContext serviceContext)
1258                    throws PortalException, SystemException {
1259    
1260                    script = formatScript(type, language, script);
1261    
1262                    byte[] smallImageBytes = null;
1263    
1264                    try {
1265                            smallImageBytes = FileUtil.getBytes(smallImageFile);
1266                    }
1267                    catch (IOException ioe) {
1268                    }
1269    
1270                    validate(
1271                            nameMap, script, smallImage, smallImageURL, smallImageFile,
1272                            smallImageBytes);
1273    
1274                    DDMTemplate template = ddmTemplateLocalService.getDDMTemplate(
1275                            templateId);
1276    
1277                    template.setModifiedDate(serviceContext.getModifiedDate(null));
1278    
1279                    if ((template.getClassPK() == 0) && (classPK > 0)) {
1280    
1281                            // Allow users to set the structure if and only if it currently does
1282                            // not have one. Otherwise, you can have bad data because there may
1283                            // be an existing content that has chosen to use a structure and
1284                            // template combination that no longer exists.
1285    
1286                            template.setClassPK(classPK);
1287                    }
1288    
1289                    template.setNameMap(nameMap);
1290                    template.setDescriptionMap(descriptionMap);
1291                    template.setType(type);
1292                    template.setMode(mode);
1293                    template.setLanguage(language);
1294                    template.setScript(script);
1295                    template.setCacheable(cacheable);
1296                    template.setSmallImage(smallImage);
1297                    template.setSmallImageURL(smallImageURL);
1298    
1299                    ddmTemplatePersistence.update(template);
1300    
1301                    // Small image
1302    
1303                    saveImages(
1304                            smallImage, template.getSmallImageId(), smallImageFile,
1305                            smallImageBytes);
1306    
1307                    return template;
1308            }
1309    
1310            protected File copySmallImage(DDMTemplate template) throws SystemException {
1311                    File smallImageFile = null;
1312    
1313                    if (template.isSmallImage() &&
1314                            Validator.isNull(template.getSmallImageURL())) {
1315    
1316                            Image smallImage = ImageUtil.fetchByPrimaryKey(
1317                                    template.getSmallImageId());
1318    
1319                            if (smallImage != null) {
1320                                    smallImageFile = FileUtil.createTempFile(smallImage.getType());
1321    
1322                                    try {
1323                                            FileUtil.write(smallImageFile, smallImage.getTextObj());
1324                                    }
1325                                    catch (IOException ioe) {
1326                                            _log.error(ioe, ioe);
1327                                    }
1328                            }
1329                    }
1330    
1331                    return smallImageFile;
1332            }
1333    
1334            protected DDMTemplate copyTemplate(
1335                            long userId, DDMTemplate template, long classPK,
1336                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
1337                            ServiceContext serviceContext)
1338                    throws PortalException, SystemException {
1339    
1340                    File smallImageFile = copySmallImage(template);
1341    
1342                    return addTemplate(
1343                            userId, template.getGroupId(), template.getClassNameId(), classPK,
1344                            null, nameMap, descriptionMap, template.getType(),
1345                            template.getMode(), template.getLanguage(), template.getScript(),
1346                            template.isCacheable(), template.isSmallImage(),
1347                            template.getSmallImageURL(), smallImageFile, serviceContext);
1348            }
1349    
1350            protected String formatScript(String type, String language, String script)
1351                    throws PortalException {
1352    
1353                    if (type.equals(DDMTemplateConstants.TEMPLATE_TYPE_FORM) ||
1354                            language.equals(TemplateConstants.LANG_TYPE_XSL)) {
1355    
1356                            try {
1357                                    script = DDMXMLUtil.validateXML(script);
1358                                    script = DDMXMLUtil.formatXML(script);
1359                            }
1360                            catch (Exception e) {
1361                                    throw new TemplateScriptException();
1362                            }
1363                    }
1364    
1365                    return script;
1366            }
1367    
1368            protected void saveImages(
1369                            boolean smallImage, long smallImageId, File smallImageFile,
1370                            byte[] smallImageBytes)
1371                    throws PortalException, SystemException {
1372    
1373                    if (smallImage) {
1374                            if ((smallImageFile != null) && (smallImageBytes != null)) {
1375                                    imageLocalService.updateImage(smallImageId, smallImageBytes);
1376                            }
1377                    }
1378                    else {
1379                            imageLocalService.deleteImage(smallImageId);
1380                    }
1381            }
1382    
1383            protected void validate(
1384                            long groupId, long classNameId, String templateKey,
1385                            Map<Locale, String> nameMap, String script, boolean smallImage,
1386                            String smallImageURL, File smallImageFile, byte[] smallImageBytes)
1387                    throws PortalException, SystemException {
1388    
1389                    templateKey = StringUtil.toUpperCase(templateKey.trim());
1390    
1391                    DDMTemplate template = ddmTemplatePersistence.fetchByG_C_T(
1392                            groupId, classNameId, templateKey);
1393    
1394                    if (template != null) {
1395                            throw new TemplateDuplicateTemplateKeyException();
1396                    }
1397    
1398                    validate(
1399                            nameMap, script, smallImage, smallImageURL, smallImageFile,
1400                            smallImageBytes);
1401            }
1402    
1403            protected void validate(
1404                            Map<Locale, String> nameMap, String script, boolean smallImage,
1405                            String smallImageURL, File smallImageFile, byte[] smallImageBytes)
1406                    throws PortalException, SystemException {
1407    
1408                    validateName(nameMap);
1409    
1410                    if (Validator.isNull(script)) {
1411                            throw new TemplateScriptException();
1412                    }
1413    
1414                    String[] imageExtensions = PrefsPropsUtil.getStringArray(
1415                            PropsKeys.DYNAMIC_DATA_MAPPING_IMAGE_EXTENSIONS, StringPool.COMMA);
1416    
1417                    if (!smallImage || Validator.isNotNull(smallImageURL) ||
1418                            (smallImageFile == null) || (smallImageBytes == null)) {
1419    
1420                            return;
1421                    }
1422    
1423                    String smallImageName = smallImageFile.getName();
1424    
1425                    if (smallImageName != null) {
1426                            boolean validSmallImageExtension = false;
1427    
1428                            for (int i = 0; i < imageExtensions.length; i++) {
1429                                    if (StringPool.STAR.equals(imageExtensions[i]) ||
1430                                            StringUtil.endsWith(
1431                                                    smallImageName, imageExtensions[i])) {
1432    
1433                                            validSmallImageExtension = true;
1434    
1435                                            break;
1436                                    }
1437                            }
1438    
1439                            if (!validSmallImageExtension) {
1440                                    throw new TemplateSmallImageNameException(smallImageName);
1441                            }
1442                    }
1443    
1444                    long smallImageMaxSize = PrefsPropsUtil.getLong(
1445                            PropsKeys.DYNAMIC_DATA_MAPPING_IMAGE_SMALL_MAX_SIZE);
1446    
1447                    if ((smallImageMaxSize > 0) &&
1448                            ((smallImageBytes == null) ||
1449                             (smallImageBytes.length > smallImageMaxSize))) {
1450    
1451                            throw new TemplateSmallImageSizeException();
1452                    }
1453            }
1454    
1455            protected void validateName(Map<Locale, String> nameMap)
1456                    throws PortalException {
1457    
1458                    String name = nameMap.get(LocaleUtil.getSiteDefault());
1459    
1460                    if (Validator.isNull(name)) {
1461                            throw new TemplateNameException();
1462                    }
1463            }
1464    
1465            private static Log _log = LogFactoryUtil.getLog(
1466                    DDMTemplateLocalServiceImpl.class);
1467    
1468    }