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 = templateKey.trim().toUpperCase();
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            /**
475             * Returns the template matching the group and template key.
476             *
477             * @param  groupId the primary key of the group
478             * @param  classNameId the primary key of the class name for the template's
479             *         related model
480             * @param  templateKey the unique string identifying the template
481             * @return the matching template, or <code>null</code> if a matching
482             *         template could not be found
483             * @throws SystemException if a system exception occurred
484             */
485            @Override
486            public DDMTemplate fetchTemplate(
487                            long groupId, long classNameId, String templateKey)
488                    throws SystemException {
489    
490                    templateKey = templateKey.trim().toUpperCase();
491    
492                    return ddmTemplatePersistence.fetchByG_C_T(
493                            groupId, classNameId, templateKey);
494            }
495    
496            /**
497             * Returns the template matching the group and template key, optionally in
498             * the global scope.
499             *
500             * <p>
501             * This method first searches in the given group. If the template is still
502             * not found and <code>includeGlobalTemplates</code> is set to
503             * <code>true</code>, this method searches the global group.
504             * </p>
505             *
506             * @param  groupId the primary key of the group
507             * @param  classNameId the primary key of the class name for the template's
508             *         related model
509             * @param  templateKey the unique string identifying the template
510             * @param  includeGlobalTemplates whether to include the global scope in the
511             *         search
512             * @return the matching template, or <code>null</code> if a matching
513             *         template could not be found
514             * @throws PortalException if a portal exception occurred
515             * @throws SystemException if a system exception occurred
516             */
517            @Override
518            public DDMTemplate fetchTemplate(
519                            long groupId, long classNameId, String templateKey,
520                            boolean includeGlobalTemplates)
521                    throws PortalException, SystemException {
522    
523                    templateKey = templateKey.trim().toUpperCase();
524    
525                    DDMTemplate template = ddmTemplatePersistence.fetchByG_C_T(
526                            groupId, classNameId, templateKey);
527    
528                    if ((template != null) || !includeGlobalTemplates) {
529                            return template;
530                    }
531    
532                    Group group = groupPersistence.findByPrimaryKey(groupId);
533    
534                    Group companyGroup = groupLocalService.getCompanyGroup(
535                            group.getCompanyId());
536    
537                    return ddmTemplatePersistence.fetchByG_C_T(
538                            companyGroup.getGroupId(), classNameId, templateKey);
539            }
540    
541            /**
542             * Returns the template with the ID.
543             *
544             * @param  templateId the primary key of the template
545             * @return the template with the ID
546             * @throws PortalException if a matching template could not be found
547             * @throws SystemException if a system exception occurred
548             */
549            @Override
550            public DDMTemplate getTemplate(long templateId)
551                    throws PortalException, SystemException {
552    
553                    return ddmTemplatePersistence.findByPrimaryKey(templateId);
554            }
555    
556            /**
557             * Returns the template matching the group and template key.
558             *
559             * @param  groupId the primary key of the group
560             * @param  classNameId the primary key of the class name for the template's
561             *         related model
562             * @param  templateKey the unique string identifying the template
563             * @return the matching template
564             * @throws PortalException if a matching template could not be found
565             * @throws SystemException if a system exception occurred
566             */
567            @Override
568            public DDMTemplate getTemplate(
569                            long groupId, long classNameId, String templateKey)
570                    throws PortalException, SystemException {
571    
572                    templateKey = templateKey.trim().toUpperCase();
573    
574                    return ddmTemplatePersistence.findByG_C_T(
575                            groupId, classNameId, templateKey);
576            }
577    
578            /**
579             * Returns the template matching the group and template key, optionally in
580             * the global scope.
581             *
582             * <p>
583             * This method first searches in the group. If the template is still not
584             * found and <code>includeGlobalTemplates</code> is set to
585             * <code>true</code>, this method searches the global group.
586             * </p>
587             *
588             * @param  groupId the primary key of the group
589             * @param  classNameId the primary key of the class name for the template's
590             *         related model
591             * @param  templateKey the unique string identifying the template
592             * @param  includeGlobalTemplates whether to include the global scope in the
593             *         search
594             * @return the matching template
595             * @throws PortalException if a matching template could not be found
596             * @throws SystemException if a system exception occurred
597             */
598            @Override
599            public DDMTemplate getTemplate(
600                            long groupId, long classNameId, String templateKey,
601                            boolean includeGlobalTemplates)
602                    throws PortalException, SystemException {
603    
604                    templateKey = templateKey.trim().toUpperCase();
605    
606                    DDMTemplate template = ddmTemplatePersistence.fetchByG_C_T(
607                            groupId, classNameId, templateKey);
608    
609                    if (template != null) {
610                            return template;
611                    }
612    
613                    if (!includeGlobalTemplates) {
614                            throw new NoSuchTemplateException(
615                                    "No DDMTemplate exists with the template key " + templateKey);
616                    }
617    
618                    Group group = groupPersistence.findByPrimaryKey(groupId);
619    
620                    Group companyGroup = groupLocalService.getCompanyGroup(
621                            group.getCompanyId());
622    
623                    return ddmTemplatePersistence.findByG_C_T(
624                            companyGroup.getGroupId(), classNameId, templateKey);
625            }
626    
627            @Override
628            public DDMTemplate getTemplateBySmallImageId(long smallImageId)
629                    throws PortalException, SystemException {
630    
631                    return ddmTemplatePersistence.findBySmallImageId(smallImageId);
632            }
633    
634            /**
635             * Returns all the templates with the class PK.
636             *
637             * @param  classPK the primary key of the template's related entity
638             * @return the templates with the class PK
639             * @throws SystemException if a system exception occurred
640             */
641            @Override
642            public List<DDMTemplate> getTemplates(long classPK) throws SystemException {
643                    return ddmTemplatePersistence.findByClassPK(classPK);
644            }
645    
646            /**
647             * Returns all the templates matching the group and class name ID.
648             *
649             * @param  groupId the primary key of the group
650             * @param  classNameId the primary key of the class name for the template's
651             *         related model
652             * @return the matching templates
653             * @throws SystemException if a system exception occurred
654             */
655            @Override
656            public List<DDMTemplate> getTemplates(long groupId, long classNameId)
657                    throws SystemException {
658    
659                    return ddmTemplatePersistence.findByG_C(groupId, classNameId);
660            }
661    
662            /**
663             * Returns all the templates matching the group, class name ID, and class
664             * PK.
665             *
666             * @param  groupId the primary key of the group
667             * @param  classNameId the primary key of the class name for the template's
668             *         related model
669             * @param  classPK the primary key of the template's related entity
670             * @return the matching templates
671             * @throws SystemException if a system exception occurred
672             */
673            @Override
674            public List<DDMTemplate> getTemplates(
675                            long groupId, long classNameId, long classPK)
676                    throws SystemException {
677    
678                    return ddmTemplatePersistence.findByG_C_C(
679                            groupId, classNameId, classPK);
680            }
681    
682            /**
683             * Returns all the templates matching the group, class name ID, class PK,
684             * and type.
685             *
686             * @param  groupId the primary key of the group
687             * @param  classNameId the primary key of the class name for the template's
688             *         related model
689             * @param  classPK the primary key of the template's related entity
690             * @param  type the template's type. For more information, see {@link
691             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
692             * @return the matching templates
693             * @throws SystemException if a system exception occurred
694             */
695            @Override
696            public List<DDMTemplate> getTemplates(
697                            long groupId, long classNameId, long classPK, String type)
698                    throws SystemException {
699    
700                    return ddmTemplatePersistence.findByG_C_C_T(
701                            groupId, classNameId, classPK, type);
702            }
703    
704            /**
705             * Returns all the templates matching the group, class name ID, class PK,
706             * type, and mode.
707             *
708             * @param  groupId the primary key of the group
709             * @param  classNameId the primary key of the class name for the template's
710             *         related model
711             * @param  classPK the primary key of the template's related entity
712             * @param  type the template's type. For more information, see {@link
713             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
714             * @param  mode the template's mode. For more information, see {@link
715             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
716             * @return the matching templates
717             * @throws SystemException if a system exception occurred
718             */
719            @Override
720            public List<DDMTemplate> getTemplates(
721                            long groupId, long classNameId, long classPK, String type,
722                            String mode)
723                    throws SystemException {
724    
725                    return ddmTemplatePersistence.findByG_C_C_T_M(
726                            groupId, classNameId, classPK, type, mode);
727            }
728    
729            @Override
730            public List<DDMTemplate> getTemplatesByClassPK(long groupId, long classPK)
731                    throws SystemException {
732    
733                    return ddmTemplatePersistence.findByG_CPK(groupId, classPK);
734            }
735    
736            @Override
737            public List<DDMTemplate> getTemplatesByClassPK(
738                            long groupId, long classPK, int start, int end)
739                    throws SystemException {
740    
741                    return ddmTemplatePersistence.findByG_CPK(groupId, classPK, start, end);
742            }
743    
744            @Override
745            public List<DDMTemplate> getTemplatesByClassPK(
746                            long[] groupIds, long classPK)
747                    throws SystemException {
748    
749                    return ddmTemplatePersistence.findByG_CPK(groupIds, classPK);
750            }
751    
752            /**
753             * Returns the number of templates matching the group and class PK.
754             *
755             * @param  groupId the primary key of the group
756             * @param  classPK the primary key of the template's related entity
757             * @return the number of templates belonging to the group and class PK
758             * @throws SystemException if a system exception occurred
759             */
760            @Override
761            public int getTemplatesByClassPKCount(long groupId, long classPK)
762                    throws SystemException {
763    
764                    return ddmTemplatePersistence.countByG_CPK(groupId, classPK);
765            }
766    
767            /**
768             * Returns an ordered range of all the templates matching the group and
769             * structure class name ID.
770             *
771             * <p>
772             * Useful when paginating results. Returns a maximum of <code>end -
773             * start</code> instances. <code>start</code> and <code>end</code> are not
774             * primary keys, they are indexes in the result set. Thus, <code>0</code>
775             * refers to the first result in the set. Setting both <code>start</code>
776             * and <code>end</code> to {@link
777             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
778             * result set.
779             * </p>
780             *
781             * @param  groupId the primary key of the group
782             * @param  structureClassNameId the primary key of the class name for the
783             *         template's related structure
784             * @param  start the lower bound of the range of templates to return
785             * @param  end the upper bound of the range of templates to return (not
786             *         inclusive)
787             * @param  orderByComparator the comparator to order the templates
788             *         (optionally <code>null</code>)
789             * @return the range of matching templates ordered by the comparator
790             * @throws SystemException if a system exception occurred
791             */
792            @Override
793            public List<DDMTemplate> getTemplatesByStructureClassNameId(
794                            long groupId, long structureClassNameId, int start, int end,
795                            OrderByComparator orderByComparator)
796                    throws SystemException {
797    
798                    return ddmTemplateFinder.findByG_SC(
799                            groupId, structureClassNameId, start, end, orderByComparator);
800            }
801    
802            /**
803             * Returns the number of templates matching the group and structure class
804             * name ID, including Generic Templates.
805             *
806             * @param  groupId the primary key of the group
807             * @param  structureClassNameId the primary key of the class name for the
808             *         template's related structure
809             * @return the number of matching templates
810             * @throws SystemException if a system exception occurred
811             */
812            @Override
813            public int getTemplatesByStructureClassNameIdCount(
814                            long groupId, long structureClassNameId)
815                    throws SystemException {
816    
817                    return ddmTemplateFinder.countByG_SC(groupId, structureClassNameId);
818            }
819    
820            /**
821             * Returns the number of templates belonging to the group.
822             *
823             * @param  groupId the primary key of the group
824             * @return the number of templates belonging to the group
825             * @throws SystemException if a system exception occurred
826             */
827            @Override
828            public int getTemplatesCount(long groupId) throws SystemException {
829                    return ddmTemplatePersistence.countByGroupId(groupId);
830            }
831    
832            /**
833             * Returns the number of templates matching the group and class name ID.
834             *
835             * @param  groupId the primary key of the group
836             * @param  classNameId the primary key of the class name for the template's
837             *         related model
838             * @return the number of matching templates
839             * @throws SystemException if a system exception occurred
840             */
841            @Override
842            public int getTemplatesCount(long groupId, long classNameId)
843                    throws SystemException {
844    
845                    return ddmTemplatePersistence.countByG_C(groupId, classNameId);
846            }
847    
848            /**
849             * Returns an ordered range of all the templates matching the group, class
850             * name ID, class PK, type, and mode, and matching the keywords in the
851             * template names and descriptions.
852             *
853             * <p>
854             * Useful when paginating results. Returns a maximum of <code>end -
855             * start</code> instances. <code>start</code> and <code>end</code> are not
856             * primary keys, they are indexes in the result set. Thus, <code>0</code>
857             * refers to the first result in the set. Setting both <code>start</code>
858             * and <code>end</code> to {@link
859             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
860             * result set.
861             * </p>
862             *
863             * @param  companyId the primary key of the template's company
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             * @param  keywords the keywords (space separated), which may occur in the
869             *         template's name or description (optionally <code>null</code>)
870             * @param  type the template's type (optionally <code>null</code>). For more
871             *         information, see {@link
872             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
873             * @param  mode the template's mode (optionally <code>null</code>). For more
874             *         information, see {@link
875             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
876             * @param  start the lower bound of the range of templates to return
877             * @param  end the upper bound of the range of templates to return (not
878             *         inclusive)
879             * @param  orderByComparator the comparator to order the templates
880             *         (optionally <code>null</code>)
881             * @return the range of matching templates ordered by the comparator
882             * @throws SystemException if a system exception occurred
883             */
884            @Override
885            public List<DDMTemplate> search(
886                            long companyId, long groupId, long classNameId, long classPK,
887                            String keywords, String type, String mode, int start, int end,
888                            OrderByComparator orderByComparator)
889                    throws SystemException {
890    
891                    return ddmTemplateFinder.findByKeywords(
892                            companyId, groupId, classNameId, classPK, keywords, type, mode,
893                            start, end, orderByComparator);
894            }
895    
896            /**
897             * Returns an ordered range of all the templates matching the group, class
898             * name ID, class PK, name keyword, description keyword, type, mode, and
899             * language.
900             *
901             * <p>
902             * Useful when paginating results. Returns a maximum of <code>end -
903             * start</code> instances. <code>start</code> and <code>end</code> are not
904             * primary keys, they are indexes in the result set. Thus, <code>0</code>
905             * refers to the first result in the set. Setting both <code>start</code>
906             * and <code>end</code> to {@link
907             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
908             * result set.
909             * </p>
910             *
911             * @param  companyId the primary key of the template's company
912             * @param  groupId the primary key of the group
913             * @param  classNameId the primary key of the class name for the template's
914             *         related model
915             * @param  classPK the primary key of the template's related entity
916             * @param  name the name keywords (optionally <code>null</code>)
917             * @param  description the description keywords (optionally
918             *         <code>null</code>)
919             * @param  type the template's type (optionally <code>null</code>). For more
920             *         information, see {@link
921             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
922             * @param  mode the template's mode (optionally <code>null</code>). For more
923             *         information, see {@link
924             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
925             * @param  language the template's script language (optionally
926             *         <code>null</code>). For more information, see {@link
927             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
928             * @param  andOperator whether every field must match its keywords, or just
929             *         one field
930             * @param  start the lower bound of the range of templates to return
931             * @param  end the upper bound of the range of templates to return (not
932             *         inclusive)
933             * @param  orderByComparator the comparator to order the templates
934             *         (optionally <code>null</code>)
935             * @return the range of matching templates ordered by the comparator
936             * @throws SystemException if a system exception occurred
937             */
938            @Override
939            public List<DDMTemplate> search(
940                            long companyId, long groupId, long classNameId, long classPK,
941                            String name, String description, String type, String mode,
942                            String language, boolean andOperator, int start, int end,
943                            OrderByComparator orderByComparator)
944                    throws SystemException {
945    
946                    return ddmTemplateFinder.findByC_G_C_C_N_D_T_M_L(
947                            companyId, groupId, classNameId, classPK, name, description, type,
948                            mode, language, andOperator, start, end, orderByComparator);
949            }
950    
951            /**
952             * Returns an ordered range of all the templates matching the group IDs,
953             * class Name IDs, class PK, type, and mode, and include the keywords on its
954             * names and descriptions.
955             *
956             * <p>
957             * Useful when paginating results. Returns a maximum of <code>end -
958             * start</code> instances. <code>start</code> and <code>end</code> are not
959             * primary keys, they are indexes in the result set. Thus, <code>0</code>
960             * refers to the first result in the set. Setting both <code>start</code>
961             * and <code>end</code> to {@link
962             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
963             * result set.
964             * </p>
965             *
966             * @param  companyId the primary key of the template's company
967             * @param  groupIds the primary keys of the groups
968             * @param  classNameIds the primary keys of the entity's instances the
969             *         templates are related to
970             * @param  classPKs the primary keys of the template's related entities
971             * @param  keywords the keywords (space separated), which may occur in the
972             *         template's name or description (optionally <code>null</code>)
973             * @param  type the template's type (optionally <code>null</code>). For more
974             *         information, see {@link
975             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
976             * @param  mode the template's mode (optionally <code>null</code>). For more
977             *         information, see {@link
978             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
979             * @param  start the lower bound of the range of templates to return
980             * @param  end the upper bound of the range of templates to return (not
981             *         inclusive)
982             * @param  orderByComparator the comparator to order the templates
983             *         (optionally <code>null</code>)
984             * @return the range of matching templates ordered by the comparator
985             * @throws SystemException if a system exception occurred
986             */
987            @Override
988            public List<DDMTemplate> search(
989                            long companyId, long[] groupIds, long[] classNameIds,
990                            long[] classPKs, String keywords, String type, String mode,
991                            int start, int end, OrderByComparator orderByComparator)
992                    throws SystemException {
993    
994                    return ddmTemplateFinder.findByKeywords(
995                            companyId, groupIds, classNameIds, classPKs, keywords, type, mode,
996                            start, end, orderByComparator);
997            }
998    
999            /**
1000             * Returns an ordered range of all the templates matching the group IDs,
1001             * class name IDs, class PK, name keyword, description keyword, type, mode,
1002             * and language.
1003             *
1004             * <p>
1005             * Useful when paginating results. Returns a maximum of <code>end -
1006             * start</code> instances. <code>start</code> and <code>end</code> are not
1007             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1008             * refers to the first result in the set. Setting both <code>start</code>
1009             * and <code>end</code> to {@link
1010             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1011             * result set.
1012             * </p>
1013             *
1014             * @param  companyId the primary key of the template's company
1015             * @param  groupIds the primary keys of the groups
1016             * @param  classNameIds the primary keys of the entity's instances the
1017             *         templates are related to
1018             * @param  classPKs the primary keys of the template's related entities
1019             * @param  name the name keywords (optionally <code>null</code>)
1020             * @param  description the description keywords (optionally
1021             *         <code>null</code>)
1022             * @param  type the template's type (optionally <code>null</code>). For more
1023             *         information, see {@link
1024             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1025             * @param  mode the template's mode (optionally <code>null</code>). For more
1026             *         information, see {@link
1027             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1028             * @param  language the template's script language (optionally
1029             *         <code>null</code>). For more information, see {@link
1030             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1031             * @param  andOperator whether every field must match its keywords, or just
1032             *         one field.
1033             * @param  start the lower bound of the range of templates to return
1034             * @param  end the upper bound of the range of templates to return (not
1035             *         inclusive)
1036             * @param  orderByComparator the comparator to order the templates
1037             *         (optionally <code>null</code>)
1038             * @return the range of matching templates ordered by the comparator
1039             * @throws SystemException if a system exception occurred
1040             */
1041            @Override
1042            public List<DDMTemplate> search(
1043                            long companyId, long[] groupIds, long[] classNameIds,
1044                            long[] classPKs, String name, String description, String type,
1045                            String mode, String language, boolean andOperator, int start,
1046                            int end, OrderByComparator orderByComparator)
1047                    throws SystemException {
1048    
1049                    return ddmTemplateFinder.findByC_G_C_C_N_D_T_M_L(
1050                            companyId, groupIds, classNameIds, classPKs, name, description,
1051                            type, mode, language, andOperator, start, end, orderByComparator);
1052            }
1053    
1054            /**
1055             * Returns the number of templates matching the group, class name ID, class
1056             * PK, type, and matching the keywords in the template names and
1057             * descriptions.
1058             *
1059             * @param  companyId the primary key of the template's company
1060             * @param  groupId the primary key of the group
1061             * @param  classNameId the primary key of the class name for the template's
1062             *         related model
1063             * @param  classPK the primary key of the template's related entity
1064             * @param  keywords the keywords (space separated), which may occur in the
1065             *         template's name or description (optionally <code>null</code>)
1066             * @param  type the template's type (optionally <code>null</code>). For more
1067             *         information, see {@link
1068             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1069             * @param  mode the template's mode (optionally <code>null</code>). For more
1070             *         information, see {@link
1071             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1072             * @return the number of matching templates
1073             * @throws SystemException if a system exception occurred
1074             */
1075            @Override
1076            public int searchCount(
1077                            long companyId, long groupId, long classNameId, long classPK,
1078                            String keywords, String type, String mode)
1079                    throws SystemException {
1080    
1081                    return ddmTemplateFinder.countByKeywords(
1082                            companyId, groupId, classNameId, classPK, keywords, type, mode);
1083            }
1084    
1085            /**
1086             * Returns the number of templates matching the group, class name ID, class
1087             * PK, name keyword, description keyword, type, mode, and language.
1088             *
1089             * @param  companyId the primary key of the template's company
1090             * @param  groupId the primary key of the group
1091             * @param  classNameId the primary key of the class name for the template's
1092             *         related model
1093             * @param  classPK the primary key of the template's related entity
1094             * @param  name the name keywords (optionally <code>null</code>)
1095             * @param  description the description keywords (optionally
1096             *         <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             * @param  language the template's script language (optionally
1104             *         <code>null</code>). For more information, see {@link
1105             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1106             * @param  andOperator whether every field must match its keywords, or just
1107             *         one field.
1108             * @return the number of matching templates
1109             * @throws SystemException if a system exception occurred
1110             */
1111            @Override
1112            public int searchCount(
1113                            long companyId, long groupId, long classNameId, long classPK,
1114                            String name, String description, String type, String mode,
1115                            String language, boolean andOperator)
1116                    throws SystemException {
1117    
1118                    return ddmTemplateFinder.countByC_G_C_C_N_D_T_M_L(
1119                            companyId, groupId, classNameId, classPK, name, description, type,
1120                            mode, language, andOperator);
1121            }
1122    
1123            /**
1124             * Returns the number of templates matching the group IDs, class name IDs,
1125             * class PK, type, and mode, and matching the keywords in the template names
1126             * and descriptions.
1127             *
1128             * @param  companyId the primary key of the template's company
1129             * @param  groupIds the primary keys of the groups
1130             * @param  classNameIds the primary keys of the entity's instance the
1131             *         templates are related to
1132             * @param  classPKs the primary keys of the template's related entities
1133             * @param  keywords the keywords (space separated), which may occur in the
1134             *         template's name or description (optionally <code>null</code>)
1135             * @param  type the template's type (optionally <code>null</code>). For more
1136             *         information, see {@link
1137             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1138             * @param  mode the template's mode (optionally <code>null</code>). For more
1139             *         information, see {@link
1140             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1141             * @return the number of matching templates
1142             * @throws SystemException if a system exception occurred
1143             */
1144            @Override
1145            public int searchCount(
1146                            long companyId, long[] groupIds, long[] classNameIds,
1147                            long[] classPKs, String keywords, String type, String mode)
1148                    throws SystemException {
1149    
1150                    return ddmTemplateFinder.countByKeywords(
1151                            companyId, groupIds, classNameIds, classPKs, keywords, type, mode);
1152            }
1153    
1154            /**
1155             * Returns the number of templates matching the group IDs, class name IDs,
1156             * class PKs, name keyword, description keyword, type, mode, and language.
1157             *
1158             * @param  companyId the primary key of the templates company
1159             * @param  groupIds the primary keys of the groups
1160             * @param  classNameIds the primary keys of the entity's instance the
1161             *         templates are related to
1162             * @param  classPKs the primary keys of the template's related entities
1163             * @param  name the name keywords (optionally <code>null</code>)
1164             * @param  description the description keywords (optionally
1165             *         <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             * @param  language the template's script language (optionally
1173             *         <code>null</code>). For more information, see {@link
1174             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1175             * @param  andOperator whether every field must match its keywords, or just
1176             *         one field.
1177             * @return the number of matching templates
1178             * @throws SystemException if a system exception occurred
1179             */
1180            @Override
1181            public int searchCount(
1182                            long companyId, long[] groupIds, long[] classNameIds,
1183                            long[] classPKs, String name, String description, String type,
1184                            String mode, String language, boolean andOperator)
1185                    throws SystemException {
1186    
1187                    return ddmTemplateFinder.countByC_G_C_C_N_D_T_M_L(
1188                            companyId, groupIds, classNameIds, classPKs, name, description,
1189                            type, mode, language, andOperator);
1190            }
1191    
1192            /**
1193             * Updates the template matching the ID.
1194             *
1195             * @param  templateId the primary key of the template
1196             * @param  classPK the primary key of the template's related entity
1197             * @param  nameMap the template's new locales and localized names
1198             * @param  descriptionMap the template's new locales and localized
1199             *         description
1200             * @param  type the template's type. For more information, see {@link
1201             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1202             * @param  mode the template's mode. For more information, see {@link
1203             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1204             * @param  language the template's script language. For more information,
1205             *         see {@link
1206             *         com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}.
1207             * @param  script the template's script
1208             * @param  cacheable whether the template is cacheable
1209             * @param  smallImage whether the template has a small image
1210             * @param  smallImageURL the template's small image URL (optionally
1211             *         <code>null</code>)
1212             * @param  smallImageFile the template's small image file (optionally
1213             *         <code>null</code>)
1214             * @param  serviceContext the service context to be applied. Can set the
1215             *         modification date.
1216             * @return the updated template
1217             * @throws PortalException if a portal exception occurred
1218             * @throws SystemException if a system exception occurred
1219             */
1220            @Override
1221            public DDMTemplate updateTemplate(
1222                            long templateId, long classPK, Map<Locale, String> nameMap,
1223                            Map<Locale, String> descriptionMap, String type, String mode,
1224                            String language, String script, boolean cacheable,
1225                            boolean smallImage, String smallImageURL, File smallImageFile,
1226                            ServiceContext serviceContext)
1227                    throws PortalException, SystemException {
1228    
1229                    script = formatScript(type, language, script);
1230    
1231                    byte[] smallImageBytes = null;
1232    
1233                    try {
1234                            smallImageBytes = FileUtil.getBytes(smallImageFile);
1235                    }
1236                    catch (IOException ioe) {
1237                    }
1238    
1239                    validate(
1240                            nameMap, script, smallImage, smallImageURL, smallImageFile,
1241                            smallImageBytes);
1242    
1243                    DDMTemplate template = ddmTemplateLocalService.getDDMTemplate(
1244                            templateId);
1245    
1246                    template.setModifiedDate(serviceContext.getModifiedDate(null));
1247    
1248                    if ((template.getClassPK() == 0) && (classPK > 0)) {
1249    
1250                            // Allow users to set the structure if and only if it currently does
1251                            // not have one. Otherwise, you can have bad data because there may
1252                            // be an existing content that has chosen to use a structure and
1253                            // template combination that no longer exists.
1254    
1255                            template.setClassPK(classPK);
1256                    }
1257    
1258                    template.setNameMap(nameMap);
1259                    template.setDescriptionMap(descriptionMap);
1260                    template.setType(type);
1261                    template.setMode(mode);
1262                    template.setLanguage(language);
1263                    template.setScript(script);
1264                    template.setCacheable(cacheable);
1265                    template.setSmallImage(smallImage);
1266                    template.setSmallImageURL(smallImageURL);
1267    
1268                    ddmTemplatePersistence.update(template);
1269    
1270                    // Small image
1271    
1272                    saveImages(
1273                            smallImage, template.getSmallImageId(), smallImageFile,
1274                            smallImageBytes);
1275    
1276                    return template;
1277            }
1278    
1279            protected File copySmallImage(DDMTemplate template) throws SystemException {
1280                    File smallImageFile = null;
1281    
1282                    if (template.isSmallImage() &&
1283                            Validator.isNull(template.getSmallImageURL())) {
1284    
1285                            Image smallImage = ImageUtil.fetchByPrimaryKey(
1286                                    template.getSmallImageId());
1287    
1288                            if (smallImage != null) {
1289                                    smallImageFile = FileUtil.createTempFile(smallImage.getType());
1290    
1291                                    try {
1292                                            FileUtil.write(smallImageFile, smallImage.getTextObj());
1293                                    }
1294                                    catch (IOException ioe) {
1295                                            _log.error(ioe, ioe);
1296                                    }
1297                            }
1298                    }
1299    
1300                    return smallImageFile;
1301            }
1302    
1303            protected DDMTemplate copyTemplate(
1304                            long userId, DDMTemplate template, long classPK,
1305                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
1306                            ServiceContext serviceContext)
1307                    throws PortalException, SystemException {
1308    
1309                    File smallImageFile = copySmallImage(template);
1310    
1311                    return addTemplate(
1312                            userId, template.getGroupId(), template.getClassNameId(), classPK,
1313                            null, nameMap, descriptionMap, template.getType(),
1314                            template.getMode(), template.getLanguage(), template.getScript(),
1315                            template.isCacheable(), template.isSmallImage(),
1316                            template.getSmallImageURL(), smallImageFile, serviceContext);
1317            }
1318    
1319            protected String formatScript(String type, String language, String script)
1320                    throws PortalException {
1321    
1322                    if (type.equals(DDMTemplateConstants.TEMPLATE_TYPE_FORM) ||
1323                            language.equals(TemplateConstants.LANG_TYPE_XSL)) {
1324    
1325                            try {
1326                                    script = DDMXMLUtil.formatXML(script);
1327                            }
1328                            catch (Exception e) {
1329                                    throw new TemplateScriptException();
1330                            }
1331                    }
1332    
1333                    return script;
1334            }
1335    
1336            protected void saveImages(
1337                            boolean smallImage, long smallImageId, File smallImageFile,
1338                            byte[] smallImageBytes)
1339                    throws PortalException, SystemException {
1340    
1341                    if (smallImage) {
1342                            if ((smallImageFile != null) && (smallImageBytes != null)) {
1343                                    imageLocalService.updateImage(smallImageId, smallImageBytes);
1344                            }
1345                    }
1346                    else {
1347                            imageLocalService.deleteImage(smallImageId);
1348                    }
1349            }
1350    
1351            protected void validate(
1352                            long groupId, long classNameId, String templateKey,
1353                            Map<Locale, String> nameMap, String script, boolean smallImage,
1354                            String smallImageURL, File smallImageFile, byte[] smallImageBytes)
1355                    throws PortalException, SystemException {
1356    
1357                    templateKey = templateKey.trim().toUpperCase();
1358    
1359                    DDMTemplate template = ddmTemplatePersistence.fetchByG_C_T(
1360                            groupId, classNameId, templateKey);
1361    
1362                    if (template != null) {
1363                            throw new TemplateDuplicateTemplateKeyException();
1364                    }
1365    
1366                    validate(
1367                            nameMap, script, smallImage, smallImageURL, smallImageFile,
1368                            smallImageBytes);
1369            }
1370    
1371            protected void validate(
1372                            Map<Locale, String> nameMap, String script, boolean smallImage,
1373                            String smallImageURL, File smallImageFile, byte[] smallImageBytes)
1374                    throws PortalException, SystemException {
1375    
1376                    validateName(nameMap);
1377    
1378                    if (Validator.isNull(script)) {
1379                            throw new TemplateScriptException();
1380                    }
1381    
1382                    String[] imageExtensions = PrefsPropsUtil.getStringArray(
1383                            PropsKeys.DYNAMIC_DATA_MAPPING_IMAGE_EXTENSIONS, StringPool.COMMA);
1384    
1385                    if (!smallImage || Validator.isNotNull(smallImageURL) ||
1386                            (smallImageFile == null) || (smallImageBytes == null)) {
1387    
1388                            return;
1389                    }
1390    
1391                    String smallImageName = smallImageFile.getName();
1392    
1393                    if (smallImageName != null) {
1394                            boolean validSmallImageExtension = false;
1395    
1396                            for (int i = 0; i < imageExtensions.length; i++) {
1397                                    if (StringPool.STAR.equals(imageExtensions[i]) ||
1398                                            StringUtil.endsWith(
1399                                                    smallImageName, imageExtensions[i])) {
1400    
1401                                            validSmallImageExtension = true;
1402    
1403                                            break;
1404                                    }
1405                            }
1406    
1407                            if (!validSmallImageExtension) {
1408                                    throw new TemplateSmallImageNameException(smallImageName);
1409                            }
1410                    }
1411    
1412                    long smallImageMaxSize = PrefsPropsUtil.getLong(
1413                            PropsKeys.DYNAMIC_DATA_MAPPING_IMAGE_SMALL_MAX_SIZE);
1414    
1415                    if ((smallImageMaxSize > 0) &&
1416                            ((smallImageBytes == null) ||
1417                             (smallImageBytes.length > smallImageMaxSize))) {
1418    
1419                            throw new TemplateSmallImageSizeException();
1420                    }
1421            }
1422    
1423            protected void validateName(Map<Locale, String> nameMap)
1424                    throws PortalException {
1425    
1426                    String name = nameMap.get(LocaleUtil.getSiteDefault());
1427    
1428                    if (Validator.isNull(name)) {
1429                            throw new TemplateNameException();
1430                    }
1431            }
1432    
1433            private static Log _log = LogFactoryUtil.getLog(
1434                    DDMTemplateLocalServiceImpl.class);
1435    
1436    }