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.journal.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.model.Image;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
034    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
035    import com.liferay.portlet.journal.DuplicateTemplateIdException;
036    import com.liferay.portlet.journal.NoSuchTemplateException;
037    import com.liferay.portlet.journal.TemplateIdException;
038    import com.liferay.portlet.journal.model.JournalArticle;
039    import com.liferay.portlet.journal.model.JournalStructure;
040    import com.liferay.portlet.journal.model.JournalTemplate;
041    import com.liferay.portlet.journal.model.JournalTemplateAdapter;
042    import com.liferay.portlet.journal.service.base.JournalTemplateLocalServiceBaseImpl;
043    import com.liferay.portlet.journal.util.JournalUtil;
044    
045    import java.io.File;
046    
047    import java.util.Date;
048    import java.util.List;
049    import java.util.Locale;
050    import java.util.Map;
051    
052    /**
053     * @author     Brian Wing Shun Chan
054     * @author     Raymond Aug??
055     * @author     Marcellus Tavares
056     * @deprecated As of 6.2.0, since Web Content Administration now uses the
057     *             Dynamic Data Mapping framework to handle templates
058     */
059    public class JournalTemplateLocalServiceImpl
060            extends JournalTemplateLocalServiceBaseImpl {
061    
062            @Override
063            public JournalTemplate addJournalTemplate(JournalTemplate template)
064                    throws PortalException, SystemException {
065    
066                    template.setNew(true);
067    
068                    return updateTemplate(template);
069            }
070    
071            @Override
072            public JournalTemplate addTemplate(
073                            long userId, long groupId, String templateId,
074                            boolean autoTemplateId, String structureId,
075                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
076                            String xsl, boolean formatXsl, String langType, boolean cacheable,
077                            boolean smallImage, String smallImageURL, File smallImageFile,
078                            ServiceContext serviceContext)
079                    throws PortalException, SystemException {
080    
081                    long classPK = 0;
082    
083                    if (Validator.isNotNull(structureId)) {
084                            JournalStructure structure =
085                                    journalStructureLocalService.getStructure(
086                                            groupId, structureId, true);
087    
088                            classPK = structure.getPrimaryKey();
089                    }
090    
091                    DDMTemplate ddmTemplate = ddmTemplateLocalService.addTemplate(
092                            userId, groupId, PortalUtil.getClassNameId(DDMStructure.class),
093                            classPK, templateId, nameMap, descriptionMap,
094                            DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY,
095                            DDMTemplateConstants.TEMPLATE_MODE_CREATE, langType, xsl, cacheable,
096                            smallImage, smallImageURL, smallImageFile, serviceContext);
097    
098                    return new JournalTemplateAdapter(ddmTemplate);
099            }
100    
101            @Override
102            public void addTemplateResources(
103                            JournalTemplate template, boolean addGroupPermissions,
104                            boolean addGuestPermissions)
105                    throws PortalException, SystemException {
106    
107                    DDMTemplate ddmTemplate = getDDMTemplate(template);
108    
109                    ddmTemplateLocalService.addTemplateResources(
110                            ddmTemplate, addGroupPermissions, addGuestPermissions);
111            }
112    
113            @Override
114            public void addTemplateResources(
115                            JournalTemplate template, String[] groupPermissions,
116                            String[] guestPermissions)
117                    throws PortalException, SystemException {
118    
119                    DDMTemplate ddmTemplate = getDDMTemplate(template);
120    
121                    ddmTemplateLocalService.addTemplateResources(
122                            ddmTemplate, groupPermissions, guestPermissions);
123            }
124    
125            @Override
126            public void addTemplateResources(
127                            long groupId, String templateId, boolean addGroupPermissions,
128                            boolean addGuestPermissions)
129                    throws PortalException, SystemException {
130    
131                    JournalTemplate template = doGetTemplate(groupId, templateId);
132    
133                    addTemplateResources(
134                            template, addGroupPermissions, addGuestPermissions);
135            }
136    
137            @Override
138            public void addTemplateResources(
139                            long groupId, String templateId, String[] groupPermissions,
140                            String[] guestPermissions)
141                    throws PortalException, SystemException {
142    
143                    JournalTemplate template = doGetTemplate(groupId, templateId);
144    
145                    addTemplateResources(template, groupPermissions, guestPermissions);
146            }
147    
148            @Override
149            public void checkNewLine(long groupId, String templateId)
150                    throws PortalException, SystemException {
151    
152                    JournalTemplate template = doGetTemplate(groupId, templateId);
153    
154                    String xsl = template.getXsl();
155    
156                    if ((xsl != null) && xsl.contains("\\n")) {
157                            xsl = StringUtil.replace(
158                                    xsl, new String[] {"\\n", "\\r"}, new String[] {"\n", "\r"});
159    
160                            template.setXsl(xsl);
161    
162                            updateTemplate(template);
163                    }
164            }
165    
166            @Override
167            public JournalTemplate copyTemplate(
168                            long userId, long groupId, String oldTemplateId,
169                            String newTemplateId, boolean autoTemplateId)
170                    throws PortalException, SystemException {
171    
172                    // Template
173    
174                    User user = userPersistence.findByPrimaryKey(userId);
175                    oldTemplateId = StringUtil.toUpperCase(oldTemplateId.trim());
176                    newTemplateId = StringUtil.toUpperCase(newTemplateId.trim());
177                    Date now = new Date();
178    
179                    JournalTemplate oldTemplate = doGetTemplate(groupId, oldTemplateId);
180    
181                    if (autoTemplateId) {
182                            newTemplateId = String.valueOf(counterLocalService.increment());
183                    }
184                    else {
185                            validateTemplateId(newTemplateId);
186    
187                            JournalTemplate newTemplate = fetchTemplate(groupId, newTemplateId);
188    
189                            if (newTemplate != null) {
190                                    StringBundler sb = new StringBundler(5);
191    
192                                    sb.append("{groupId=");
193                                    sb.append(groupId);
194                                    sb.append(", templateId=");
195                                    sb.append(newTemplateId);
196                                    sb.append("}");
197    
198                                    throw new DuplicateTemplateIdException(sb.toString());
199                            }
200                    }
201    
202                    long id = counterLocalService.increment();
203    
204                    JournalTemplate newTemplate = createJournalTemplate(id);
205    
206                    newTemplate.setGroupId(groupId);
207                    newTemplate.setCompanyId(user.getCompanyId());
208                    newTemplate.setUserId(user.getUserId());
209                    newTemplate.setUserName(user.getFullName());
210                    newTemplate.setCreateDate(now);
211                    newTemplate.setModifiedDate(now);
212                    newTemplate.setTemplateId(newTemplateId);
213                    newTemplate.setStructureId(oldTemplate.getStructureId());
214                    newTemplate.setNameMap(oldTemplate.getNameMap());
215                    newTemplate.setDescriptionMap(oldTemplate.getDescriptionMap());
216                    newTemplate.setXsl(oldTemplate.getXsl());
217                    newTemplate.setLangType(oldTemplate.getLangType());
218                    newTemplate.setCacheable(oldTemplate.isCacheable());
219                    newTemplate.setSmallImage(oldTemplate.isSmallImage());
220                    newTemplate.setSmallImageId(counterLocalService.increment());
221                    newTemplate.setSmallImageURL(oldTemplate.getSmallImageURL());
222                    newTemplate.setExpandoBridgeAttributes(oldTemplate);
223    
224                    updateTemplate(newTemplate);
225    
226                    // Small image
227    
228                    if (oldTemplate.getSmallImage()) {
229                            Image image = imageLocalService.getImage(
230                                    oldTemplate.getSmallImageId());
231    
232                            byte[] smallImageBytes = image.getTextObj();
233    
234                            imageLocalService.updateImage(
235                                    newTemplate.getSmallImageId(), smallImageBytes);
236                    }
237    
238                    return newTemplate;
239            }
240    
241            @Override
242            public JournalTemplate createJournalTemplate(long id) {
243                    DDMTemplate ddmTemplate = ddmTemplateLocalService.createDDMTemplate(id);
244    
245                    return new JournalTemplateAdapter(ddmTemplate);
246            }
247    
248            @Override
249            public void deleteTemplate(JournalTemplate template)
250                    throws PortalException, SystemException {
251    
252                    DDMTemplate ddmTemplate = getDDMTemplate(template);
253    
254                    ddmTemplateLocalService.deleteTemplate(ddmTemplate);
255            }
256    
257            @Override
258            public void deleteTemplate(long groupId, String templateId)
259                    throws PortalException, SystemException {
260    
261                    JournalTemplate template = doGetTemplate(groupId, templateId);
262    
263                    deleteTemplate(template);
264            }
265    
266            @Override
267            public void deleteTemplates(long groupId)
268                    throws PortalException, SystemException {
269    
270                    List<JournalTemplate> templates = doGetTemplates(
271                            groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
272    
273                    for (JournalTemplate template : templates) {
274                            deleteTemplate(template);
275                    }
276            }
277    
278            @Override
279            public List<JournalTemplate> getStructureTemplates(
280                            long groupId, String structureId)
281                    throws PortalException, SystemException {
282    
283                    return getStructureTemplates(groupId, structureId, false);
284            }
285    
286            @Override
287            public List<JournalTemplate> getStructureTemplates(
288                            long groupId, String structureId, boolean includeGlobalTemplates)
289                    throws PortalException, SystemException {
290    
291                    long[] groupIds = new long[] {groupId};
292    
293                    if (includeGlobalTemplates) {
294                            groupIds = PortalUtil.getSiteAndCompanyGroupIds(groupId);
295                    }
296    
297                    JournalStructure structure = journalStructureLocalService.getStructure(
298                            groupId, structureId, true);
299    
300                    List<DDMTemplate> ddmTemplates =
301                            ddmTemplateLocalService.getTemplatesByClassPK(
302                                    groupIds, structure.getPrimaryKey());
303    
304                    return JournalUtil.toJournalTemplates(ddmTemplates);
305            }
306    
307            @Override
308            public List<JournalTemplate> getStructureTemplates(
309                            long groupId, String structureId, int start, int end)
310                    throws PortalException, SystemException {
311    
312                    JournalStructure structure = journalStructureLocalService.getStructure(
313                            groupId, structureId, true);
314    
315                    List<DDMTemplate> ddmTemplates =
316                            ddmTemplateLocalService.getTemplatesByClassPK(
317                                    groupId, structure.getPrimaryKey(), start, end);
318    
319                    return JournalUtil.toJournalTemplates(ddmTemplates);
320            }
321    
322            @Override
323            public int getStructureTemplatesCount(long groupId, String structureId)
324                    throws PortalException, SystemException {
325    
326                    JournalStructure structure = journalStructureLocalService.getStructure(
327                            groupId, structureId, true);
328    
329                    return ddmTemplateLocalService.getTemplatesByClassPKCount(
330                            groupId, structure.getPrimaryKey());
331            }
332    
333            @Override
334            public JournalTemplate getTemplate(long groupId, String templateId)
335                    throws PortalException, SystemException {
336    
337                    return getTemplate(groupId, templateId, false);
338            }
339    
340            @Override
341            public JournalTemplate getTemplate(
342                            long groupId, String templateId, boolean includeGlobalTemplates)
343                    throws PortalException, SystemException {
344    
345                    templateId = StringUtil.toUpperCase(GetterUtil.getString(templateId));
346    
347                    JournalTemplate template = fetchTemplate(groupId, templateId);
348    
349                    if (template != null) {
350                            return template;
351                    }
352    
353                    if (!includeGlobalTemplates) {
354                            throw new NoSuchTemplateException(
355                                    "No JournalTemplate exists with the template ID " + templateId);
356                    }
357    
358                    Group group = groupPersistence.findByPrimaryKey(groupId);
359    
360                    Group companyGroup = groupLocalService.getCompanyGroup(
361                            group.getCompanyId());
362    
363                    return doGetTemplate(companyGroup.getGroupId(), templateId);
364            }
365    
366            @Override
367            public JournalTemplate getTemplateBySmallImageId(long smallImageId)
368                    throws PortalException, SystemException {
369    
370                    DDMTemplate ddmTemplate =
371                            ddmTemplateLocalService.getTemplateBySmallImageId(smallImageId);
372    
373                    return new JournalTemplateAdapter(ddmTemplate);
374            }
375    
376            @Override
377            public List<JournalTemplate> getTemplates() throws SystemException {
378                    List<DDMTemplate> ddmTemplates = ddmTemplateFinder.findByG_SC(
379                            null, PortalUtil.getClassNameId(JournalArticle.class),
380                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
381    
382                    return JournalUtil.toJournalTemplates(ddmTemplates);
383            }
384    
385            @Override
386            public List<JournalTemplate> getTemplates(long groupId)
387                    throws SystemException {
388    
389                    return doGetTemplates(groupId);
390            }
391    
392            @Override
393            public List<JournalTemplate> getTemplates(long groupId, int start, int end)
394                    throws SystemException {
395    
396                    return doGetTemplates(groupId, start, end);
397            }
398    
399            @Override
400            public int getTemplatesCount(long groupId) throws SystemException {
401                    return doGetTemplatesCount(groupId);
402            }
403    
404            @Override
405            public boolean hasTemplate(long groupId, String templateId)
406                    throws SystemException {
407    
408                    try {
409                            getTemplate(groupId, templateId);
410    
411                            return true;
412                    }
413                    catch (PortalException pe) {
414                            return false;
415                    }
416            }
417    
418            @Override
419            public List<JournalTemplate> search(
420                            long companyId, long[] groupIds, String keywords,
421                            String structureId, String structureIdComparator, int start,
422                            int end, OrderByComparator obc)
423                    throws SystemException {
424    
425                    long[] classNameIds = {PortalUtil.getClassNameId(DDMStructure.class)};
426                    long[] classPKs = JournalUtil.getStructureClassPKs(
427                            groupIds, structureId);
428    
429                    List<DDMTemplate> ddmTemplates = ddmTemplateFinder.findByKeywords(
430                            companyId, groupIds, classNameIds, classPKs, keywords, null, null,
431                            start, end, obc);
432    
433                    return JournalUtil.toJournalTemplates(ddmTemplates);
434            }
435    
436            @Override
437            public List<JournalTemplate> search(
438                            long companyId, long[] groupIds, String templateId,
439                            String structureId, String structureIdComparator, String name,
440                            String description, boolean andOperator, int start, int end,
441                            OrderByComparator obc)
442                    throws SystemException {
443    
444                    long[] classNameIds = {PortalUtil.getClassNameId(DDMStructure.class)};
445                    long[] classPKs = JournalUtil.getStructureClassPKs(
446                            groupIds, structureId);
447    
448                    List<DDMTemplate> ddmTemplates =
449                            ddmTemplateFinder.findByC_G_C_C_N_D_T_M_L(
450                                    companyId, groupIds, classNameIds, classPKs, name, description,
451                                    null, null, null, andOperator, start, end, obc);
452    
453                    return JournalUtil.toJournalTemplates(ddmTemplates);
454            }
455    
456            @Override
457            public int searchCount(
458                            long companyId, long[] groupIds, String keywords,
459                            String structureId, String structureIdComparator)
460                    throws SystemException {
461    
462                    long[] classNameIds = {PortalUtil.getClassNameId(DDMStructure.class)};
463                    long[] classPKs = JournalUtil.getStructureClassPKs(
464                            groupIds, structureId);
465    
466                    return ddmTemplateFinder.countByKeywords(
467                            companyId, groupIds, classNameIds, classPKs, keywords, null, null);
468            }
469    
470            @Override
471            public int searchCount(
472                            long companyId, long[] groupIds, String templateId,
473                            String structureId, String structureIdComparator, String name,
474                            String description, boolean andOperator)
475                    throws SystemException {
476    
477                    long[] classNameIds = {PortalUtil.getClassNameId(DDMStructure.class)};
478                    long[] classPKs = JournalUtil.getStructureClassPKs(
479                            groupIds, structureId);
480    
481                    return ddmTemplateFinder.countByC_G_C_C_N_D_T_M_L(
482                            companyId, groupIds, classNameIds, classPKs, name, description,
483                            null, null, null, andOperator);
484            }
485    
486            @Override
487            public JournalTemplate updateJournalTemplate(JournalTemplate template)
488                    throws PortalException, SystemException {
489    
490                    return updateTemplate(template);
491            }
492    
493            @Override
494            public JournalTemplate updateTemplate(
495                            long groupId, String templateId, String structureId,
496                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
497                            String xsl, boolean formatXsl, String langType, boolean cacheable,
498                            boolean smallImage, String smallImageURL, File smallImageFile,
499                            ServiceContext serviceContext)
500                    throws PortalException, SystemException {
501    
502                    JournalTemplate template = doGetTemplate(groupId, templateId);
503    
504                    long classPK = 0;
505    
506                    if (Validator.isNotNull(structureId)) {
507                            JournalStructure structure =
508                                    journalStructureLocalService.getStructure(
509                                            groupId, structureId, true);
510    
511                            classPK = structure.getPrimaryKey();
512                    }
513    
514                    DDMTemplate ddmTemplate = ddmTemplateLocalService.updateTemplate(
515                            template.getPrimaryKey(), classPK, nameMap, descriptionMap,
516                            DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY,
517                            DDMTemplateConstants.TEMPLATE_MODE_CREATE, langType, xsl, cacheable,
518                            smallImage, smallImageURL, smallImageFile, serviceContext);
519    
520                    return new JournalTemplateAdapter(ddmTemplate);
521            }
522    
523            protected JournalTemplate doGetTemplate(long groupId, String templateId)
524                    throws PortalException, SystemException {
525    
526                    DDMTemplate ddmTemplate = getDDMTemplate(groupId, templateId);
527    
528                    return new JournalTemplateAdapter(ddmTemplate);
529            }
530    
531            protected List<JournalTemplate> doGetTemplates(long groupId)
532                    throws SystemException {
533    
534                    return doGetTemplates(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
535            }
536    
537            protected List<JournalTemplate> doGetTemplates(
538                            long groupId, int start, int end)
539                    throws SystemException {
540    
541                    List<DDMTemplate> ddmTemplates = ddmTemplateFinder.findByG_SC(
542                            groupId, PortalUtil.getClassNameId(JournalArticle.class), start,
543                            end, null);
544    
545                    return JournalUtil.toJournalTemplates(ddmTemplates);
546            }
547    
548            protected int doGetTemplatesCount(long groupId) throws SystemException {
549                    return ddmTemplateFinder.countByG_SC(
550                            groupId, PortalUtil.getClassNameId(JournalArticle.class));
551            }
552    
553            protected DDMTemplate fetchDDMTemplate(long groupId, String templateId)
554                    throws SystemException {
555    
556                    return ddmTemplateLocalService.fetchTemplate(
557                            groupId, PortalUtil.getClassNameId(DDMStructure.class), templateId);
558            }
559    
560            protected JournalTemplate fetchTemplate(long groupId, String templateId)
561                    throws SystemException {
562    
563                    DDMTemplate ddmTemplate = fetchDDMTemplate(groupId, templateId);
564    
565                    if (ddmTemplate != null) {
566                            return new JournalTemplateAdapter(ddmTemplate);
567                    }
568    
569                    return null;
570            }
571    
572            protected DDMTemplate getDDMTemplate(JournalTemplate template)
573                    throws PortalException, SystemException {
574    
575                    return getDDMTemplate(template.getGroupId(), template.getTemplateId());
576            }
577    
578            protected DDMTemplate getDDMTemplate(long groupId, String templateId)
579                    throws PortalException, SystemException {
580    
581                    try {
582                            return ddmTemplateLocalService.getTemplate(
583                                    groupId, PortalUtil.getClassNameId(DDMStructure.class),
584                                    templateId);
585                    }
586                    catch (PortalException pe) {
587                            throw new NoSuchTemplateException(pe);
588                    }
589            }
590    
591            protected String getUuid(JournalTemplate template) {
592                    String uuid = template.getUuid();
593    
594                    if (Validator.isNotNull(uuid)) {
595                            return uuid;
596                    }
597    
598                    return PortalUUIDUtil.generate();
599            }
600    
601            protected JournalTemplate updateTemplate(JournalTemplate template)
602                    throws PortalException, SystemException {
603    
604                    ServiceContext serviceContext = new ServiceContext();
605    
606                    serviceContext.setAddGroupPermissions(true);
607                    serviceContext.setAddGuestPermissions(true);
608    
609                    String uuid = getUuid(template);
610    
611                    serviceContext.setUuid(uuid);
612    
613                    if (template.isNew()) {
614                            return addTemplate(
615                                    template.getUserId(), template.getGroupId(),
616                                    template.getTemplateId(), false, template.getStructureId(),
617                                    template.getNameMap(), template.getDescriptionMap(),
618                                    template.getXsl(), true, template.getLangType(),
619                                    template.isCacheable(), template.isSmallImage(),
620                                    template.getSmallImageURL(), template.getSmallImageFile(),
621                                    serviceContext);
622                    }
623    
624                    return updateTemplate(
625                            template.getGroupId(), template.getTemplateId(),
626                            template.getStructureId(), template.getNameMap(),
627                            template.getDescriptionMap(), template.getXsl(), true,
628                            template.getLangType(), template.isCacheable(),
629                            template.isSmallImage(), template.getSmallImageURL(),
630                            template.getSmallImageFile(), serviceContext);
631            }
632    
633            protected void validateTemplateId(String templateId)
634                    throws PortalException {
635    
636                    if (Validator.isNull(templateId) ||
637                            Validator.isNumber(templateId) ||
638                            (templateId.indexOf(CharPool.COMMA) != -1) ||
639                            (templateId.indexOf(CharPool.SPACE) != -1)) {
640    
641                            throw new TemplateIdException();
642                    }
643            }
644    
645    }