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