001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.journal.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.util.CharPool;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.Image;
030    import com.liferay.portal.model.ResourceConstants;
031    import com.liferay.portal.model.User;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.util.PrefsPropsUtil;
034    import com.liferay.portlet.expando.model.ExpandoBridge;
035    import com.liferay.portlet.journal.DuplicateTemplateIdException;
036    import com.liferay.portlet.journal.NoSuchTemplateException;
037    import com.liferay.portlet.journal.RequiredTemplateException;
038    import com.liferay.portlet.journal.TemplateDescriptionException;
039    import com.liferay.portlet.journal.TemplateIdException;
040    import com.liferay.portlet.journal.TemplateNameException;
041    import com.liferay.portlet.journal.TemplateSmallImageNameException;
042    import com.liferay.portlet.journal.TemplateSmallImageSizeException;
043    import com.liferay.portlet.journal.TemplateXslException;
044    import com.liferay.portlet.journal.model.JournalTemplate;
045    import com.liferay.portlet.journal.model.JournalTemplateConstants;
046    import com.liferay.portlet.journal.service.base.JournalTemplateLocalServiceBaseImpl;
047    import com.liferay.portlet.journal.util.JournalUtil;
048    
049    import java.io.File;
050    import java.io.IOException;
051    
052    import java.util.Date;
053    import java.util.List;
054    
055    /**
056     * @author Brian Wing Shun Chan
057     * @author Raymond Augé
058     */
059    public class JournalTemplateLocalServiceImpl
060            extends JournalTemplateLocalServiceBaseImpl {
061    
062            public JournalTemplate addTemplate(
063                            long userId, long groupId, String templateId,
064                            boolean autoTemplateId, String structureId, String name,
065                            String description, String xsl, boolean formatXsl, String langType,
066                            boolean cacheable, boolean smallImage, String smallImageURL,
067                            File smallFile, ServiceContext serviceContext)
068                    throws PortalException, SystemException {
069    
070                    // Template
071    
072                    User user = userPersistence.findByPrimaryKey(userId);
073                    templateId = templateId.trim().toUpperCase();
074                    Date now = new Date();
075    
076                    try {
077                            if (formatXsl) {
078                                    if (langType.equals(JournalTemplateConstants.LANG_TYPE_VM)) {
079                                            xsl = JournalUtil.formatVM(xsl);
080                                    }
081                                    else {
082                                            xsl = JournalUtil.formatXML(xsl);
083                                    }
084                            }
085                    }
086                    catch (Exception e) {
087                            throw new TemplateXslException();
088                    }
089    
090                    byte[] smallBytes = null;
091    
092                    try {
093                            smallBytes = FileUtil.getBytes(smallFile);
094                    }
095                    catch (IOException ioe) {
096                    }
097    
098                    validate(
099                            groupId, templateId, autoTemplateId, name, description, xsl,
100                            smallImage, smallImageURL, smallFile, smallBytes);
101    
102                    if (autoTemplateId) {
103                            templateId = String.valueOf(counterLocalService.increment());
104                    }
105    
106                    long id = counterLocalService.increment();
107    
108                    JournalTemplate template = journalTemplatePersistence.create(id);
109    
110                    template.setUuid(serviceContext.getUuid());
111                    template.setGroupId(groupId);
112                    template.setCompanyId(user.getCompanyId());
113                    template.setUserId(user.getUserId());
114                    template.setUserName(user.getFullName());
115                    template.setCreateDate(serviceContext.getCreateDate(now));
116                    template.setModifiedDate(serviceContext.getModifiedDate(now));
117                    template.setTemplateId(templateId);
118                    template.setStructureId(structureId);
119                    template.setName(name);
120                    template.setDescription(description);
121                    template.setXsl(xsl);
122                    template.setLangType(langType);
123                    template.setCacheable(cacheable);
124                    template.setSmallImage(smallImage);
125                    template.setSmallImageId(counterLocalService.increment());
126                    template.setSmallImageURL(smallImageURL);
127    
128                    journalTemplatePersistence.update(template, false);
129    
130                    // Resources
131    
132                    if (serviceContext.getAddCommunityPermissions() ||
133                            serviceContext.getAddGuestPermissions()) {
134    
135                            addTemplateResources(
136                                    template, serviceContext.getAddCommunityPermissions(),
137                                    serviceContext.getAddGuestPermissions());
138                    }
139                    else {
140                            addTemplateResources(
141                                    template, serviceContext.getCommunityPermissions(),
142                                    serviceContext.getGuestPermissions());
143                    }
144    
145                    // Expando
146    
147                    ExpandoBridge expandoBridge = template.getExpandoBridge();
148    
149                    expandoBridge.setAttributes(serviceContext);
150    
151                    // Small image
152    
153                    saveImages(
154                            smallImage, template.getSmallImageId(), smallFile, smallBytes);
155    
156                    return template;
157            }
158    
159            public void addTemplateResources(
160                            long groupId, String templateId, boolean addCommunityPermissions,
161                            boolean addGuestPermissions)
162                    throws PortalException, SystemException {
163    
164                    JournalTemplate template = journalTemplatePersistence.findByG_T(
165                            groupId, templateId);
166    
167                    addTemplateResources(
168                            template, addCommunityPermissions, addGuestPermissions);
169            }
170    
171            public void addTemplateResources(
172                            JournalTemplate template, boolean addCommunityPermissions,
173                            boolean addGuestPermissions)
174                    throws PortalException, SystemException {
175    
176                    resourceLocalService.addResources(
177                            template.getCompanyId(), template.getGroupId(),
178                            template.getUserId(), JournalTemplate.class.getName(),
179                            template.getId(), false, addCommunityPermissions,
180                            addGuestPermissions);
181            }
182    
183            public void addTemplateResources(
184                            long groupId, String templateId, String[] communityPermissions,
185                            String[] guestPermissions)
186                    throws PortalException, SystemException {
187    
188                    JournalTemplate template = journalTemplatePersistence.findByG_T(
189                            groupId, templateId);
190    
191                    addTemplateResources(template, communityPermissions, guestPermissions);
192            }
193    
194            public void addTemplateResources(
195                            JournalTemplate template, String[] communityPermissions,
196                            String[] guestPermissions)
197                    throws PortalException, SystemException {
198    
199                    resourceLocalService.addModelResources(
200                            template.getCompanyId(), template.getGroupId(),
201                            template.getUserId(), JournalTemplate.class.getName(),
202                            template.getId(), communityPermissions, guestPermissions);
203            }
204    
205            public void checkNewLine(long groupId, String templateId)
206                    throws PortalException, SystemException {
207    
208                    JournalTemplate template = journalTemplatePersistence.findByG_T(
209                            groupId, templateId);
210    
211                    String xsl = template.getXsl();
212    
213                    if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
214                            xsl = StringUtil.replace(
215                                    xsl,
216                                    new String[] {"\\n", "\\r"},
217                                    new String[] {"\n", "\r"});
218    
219                            template.setXsl(xsl);
220    
221                            journalTemplatePersistence.update(template, false);
222                    }
223            }
224    
225            public JournalTemplate copyTemplate(
226                            long userId, long groupId, String oldTemplateId,
227                            String newTemplateId, boolean autoTemplateId)
228                    throws PortalException, SystemException {
229    
230                    // Template
231    
232                    User user = userPersistence.findByPrimaryKey(userId);
233                    oldTemplateId = oldTemplateId.trim().toUpperCase();
234                    newTemplateId = newTemplateId.trim().toUpperCase();
235                    Date now = new Date();
236    
237                    JournalTemplate oldTemplate = journalTemplatePersistence.findByG_T(
238                            groupId, oldTemplateId);
239    
240                    if (autoTemplateId) {
241                            newTemplateId = String.valueOf(counterLocalService.increment());
242                    }
243                    else {
244                            validate(newTemplateId);
245    
246                            JournalTemplate newTemplate = journalTemplatePersistence.fetchByG_T(
247                                    groupId, newTemplateId);
248    
249                            if (newTemplate != null) {
250                                    throw new DuplicateTemplateIdException();
251                            }
252                    }
253    
254                    long id = counterLocalService.increment();
255    
256                    JournalTemplate newTemplate = journalTemplatePersistence.create(id);
257    
258                    newTemplate.setGroupId(groupId);
259                    newTemplate.setCompanyId(user.getCompanyId());
260                    newTemplate.setUserId(user.getUserId());
261                    newTemplate.setUserName(user.getFullName());
262                    newTemplate.setCreateDate(now);
263                    newTemplate.setModifiedDate(now);
264                    newTemplate.setTemplateId(newTemplateId);
265                    newTemplate.setStructureId(oldTemplate.getStructureId());
266                    newTemplate.setName(oldTemplate.getName());
267                    newTemplate.setDescription(oldTemplate.getDescription());
268                    newTemplate.setXsl(oldTemplate.getXsl());
269                    newTemplate.setLangType(oldTemplate.getLangType());
270                    newTemplate.setCacheable(oldTemplate.isCacheable());
271                    newTemplate.setSmallImage(oldTemplate.isSmallImage());
272                    newTemplate.setSmallImageId(counterLocalService.increment());
273                    newTemplate.setSmallImageURL(oldTemplate.getSmallImageURL());
274    
275                    journalTemplatePersistence.update(newTemplate, false);
276    
277                    // Small image
278    
279                    if (oldTemplate.getSmallImage()) {
280                            Image image = imageLocalService.getImage(
281                                    oldTemplate.getSmallImageId());
282    
283                            byte[] smallBytes = image.getTextObj();
284    
285                            imageLocalService.updateImage(
286                                    newTemplate.getSmallImageId(), smallBytes);
287                    }
288    
289                    // Resources
290    
291                    addTemplateResources(newTemplate, true, true);
292    
293                    return newTemplate;
294            }
295    
296            public void deleteTemplate(long groupId, String templateId)
297                    throws PortalException, SystemException {
298    
299                    templateId = templateId.trim().toUpperCase();
300    
301                    JournalTemplate template = journalTemplatePersistence.findByG_T(
302                            groupId, templateId);
303    
304                    deleteTemplate(template);
305            }
306    
307            public void deleteTemplate(JournalTemplate template)
308                    throws PortalException, SystemException {
309    
310                    if (journalArticlePersistence.countByG_T(
311                                    template.getGroupId(), template.getTemplateId()) > 0) {
312    
313                            throw new RequiredTemplateException();
314                    }
315    
316                    // WebDAVProps
317    
318                    webDAVPropsLocalService.deleteWebDAVProps(
319                            JournalTemplate.class.getName(), template.getId());
320    
321                    // Small image
322    
323                    imageLocalService.deleteImage(template.getSmallImageId());
324    
325                    // Expando
326    
327                    expandoValueLocalService.deleteValues(
328                            JournalTemplate.class.getName(), template.getId());
329    
330                    // Resources
331    
332                    resourceLocalService.deleteResource(
333                            template.getCompanyId(), JournalTemplate.class.getName(),
334                            ResourceConstants.SCOPE_INDIVIDUAL, template.getId());
335    
336                    // Template
337    
338                    journalTemplatePersistence.remove(template);
339            }
340    
341            public void deleteTemplates(long groupId)
342                    throws PortalException, SystemException {
343    
344                    for (JournalTemplate template :
345                                    journalTemplatePersistence.findByGroupId(groupId)) {
346    
347                            deleteTemplate(template);
348                    }
349            }
350    
351            public List<JournalTemplate> getStructureTemplates(
352                            long groupId, String structureId)
353                    throws SystemException {
354    
355                    return journalTemplatePersistence.findByG_S(groupId, structureId);
356            }
357    
358            public List<JournalTemplate> getStructureTemplates(
359                            long groupId, String structureId, int start, int end)
360                    throws SystemException {
361    
362                    return journalTemplatePersistence.findByG_S(
363                            groupId, structureId, start, end);
364            }
365    
366            public int getStructureTemplatesCount(long groupId, String structureId)
367                    throws SystemException {
368    
369                    return journalTemplatePersistence.countByG_S(groupId, structureId);
370            }
371    
372            public JournalTemplate getTemplate(long id)
373                    throws PortalException, SystemException {
374    
375                    return journalTemplatePersistence.findByPrimaryKey(id);
376            }
377    
378            public JournalTemplate getTemplate(long groupId, String templateId)
379                    throws PortalException, SystemException {
380    
381                    templateId = GetterUtil.getString(templateId).toUpperCase();
382    
383                    if (groupId == 0) {
384                            _log.error(
385                                    "No group id was passed for " + templateId + ". Group id is " +
386                                            "required since 4.2.0. Please update all custom code and " +
387                                                    "data that references templates without a group id.");
388    
389                            List<JournalTemplate> templates =
390                                    journalTemplatePersistence.findByTemplateId(
391                                            templateId);
392    
393                            if (templates.size() == 0) {
394                                    throw new NoSuchTemplateException(
395                                            "No JournalTemplate exists with the template id " +
396                                                    templateId);
397                            }
398                            else {
399                                    return templates.get(0);
400                            }
401                    }
402                    else {
403                            return journalTemplatePersistence.findByG_T(groupId, templateId);
404                    }
405            }
406    
407            public JournalTemplate getTemplateBySmallImageId(long smallImageId)
408                    throws PortalException, SystemException {
409    
410                    return journalTemplatePersistence.findBySmallImageId(smallImageId);
411            }
412    
413            public List<JournalTemplate> getTemplates() throws SystemException {
414                    return journalTemplatePersistence.findAll();
415            }
416    
417            public List<JournalTemplate> getTemplates(long groupId)
418                    throws SystemException {
419    
420                    return journalTemplatePersistence.findByGroupId(groupId);
421            }
422    
423            public List<JournalTemplate> getTemplates(long groupId, int start, int end)
424                    throws SystemException {
425    
426                    return journalTemplatePersistence.findByGroupId(groupId, start, end);
427            }
428    
429            public int getTemplatesCount(long groupId) throws SystemException {
430                    return journalTemplatePersistence.countByGroupId(groupId);
431            }
432    
433            public boolean hasTemplate(long groupId, String templateId)
434                    throws SystemException {
435    
436                    try {
437                            getTemplate(groupId, templateId);
438    
439                            return true;
440                    }
441                    catch (PortalException pe) {
442                            return false;
443                    }
444            }
445    
446            public List<JournalTemplate> search(
447                            long companyId, long groupId, String keywords, String structureId,
448                            String structureIdComparator, int start, int end,
449                            OrderByComparator obc)
450                    throws SystemException {
451    
452                    return journalTemplateFinder.findByKeywords(
453                            companyId, groupId, keywords, structureId, structureIdComparator,
454                            start, end, obc);
455            }
456    
457            public List<JournalTemplate> search(
458                            long companyId, long groupId, String templateId, String structureId,
459                            String structureIdComparator, String name, String description,
460                            boolean andOperator, int start, int end, OrderByComparator obc)
461                    throws SystemException {
462    
463                    return journalTemplateFinder.findByC_G_T_S_N_D(
464                            companyId, groupId, templateId, structureId, structureIdComparator,
465                            name, description, andOperator, start, end, obc);
466            }
467    
468            public int searchCount(
469                            long companyId, long groupId, String keywords, String structureId,
470                            String structureIdComparator)
471                    throws SystemException {
472    
473                    return journalTemplateFinder.countByKeywords(
474                            companyId, groupId, keywords, structureId, structureIdComparator);
475            }
476    
477            public int searchCount(
478                            long companyId, long groupId, String templateId, String structureId,
479                            String structureIdComparator, String name, String description,
480                            boolean andOperator)
481                    throws SystemException {
482    
483                    return journalTemplateFinder.countByC_G_T_S_N_D(
484                            companyId, groupId, templateId, structureId, structureIdComparator,
485                            name, description, andOperator);
486            }
487    
488            public JournalTemplate updateTemplate(
489                            long groupId, String templateId, String structureId, String name,
490                            String description, String xsl, boolean formatXsl, String langType,
491                            boolean cacheable, boolean smallImage, String smallImageURL,
492                            File smallFile, ServiceContext serviceContext)
493                    throws PortalException, SystemException {
494    
495                    // Template
496    
497                    templateId = templateId.trim().toUpperCase();
498    
499                    try {
500                            if (formatXsl) {
501                                    if (langType.equals(JournalTemplateConstants.LANG_TYPE_VM)) {
502                                            xsl = JournalUtil.formatVM(xsl);
503                                    }
504                                    else {
505                                            xsl = JournalUtil.formatXML(xsl);
506                                    }
507                            }
508                    }
509                    catch (Exception e) {
510                            throw new TemplateXslException();
511                    }
512    
513                    byte[] smallBytes = null;
514    
515                    try {
516                            smallBytes = FileUtil.getBytes(smallFile);
517                    }
518                    catch (IOException ioe) {
519                    }
520    
521                    validate(
522                            name, description, xsl, smallImage, smallImageURL, smallFile,
523                            smallBytes);
524    
525                    JournalTemplate template = journalTemplatePersistence.findByG_T(
526                            groupId, templateId);
527    
528                    template.setModifiedDate(new Date());
529    
530                    if (Validator.isNull(template.getStructureId()) &&
531                            Validator.isNotNull(structureId)) {
532    
533                            // Allow users to set the structure if and only if it currently
534                            // does not have one. Otherwise, you can have bad data because there
535                            // may be an existing article that has chosen to use a structure and
536                            // template combination that no longer exists.
537    
538                            template.setStructureId(structureId);
539                    }
540    
541                    template.setName(name);
542                    template.setDescription(description);
543                    template.setXsl(xsl);
544                    template.setLangType(langType);
545                    template.setCacheable(cacheable);
546                    template.setSmallImage(smallImage);
547                    template.setSmallImageURL(smallImageURL);
548                    template.setModifiedDate(serviceContext.getModifiedDate(null));
549    
550                    journalTemplatePersistence.update(template, false);
551    
552                    // Expando
553    
554                    ExpandoBridge expandoBridge = template.getExpandoBridge();
555    
556                    expandoBridge.setAttributes(serviceContext);
557    
558                    // Small image
559    
560                    saveImages(
561                            smallImage, template.getSmallImageId(), smallFile, smallBytes);
562    
563                    return template;
564            }
565    
566            protected void saveImages(
567                            boolean smallImage, long smallImageId, File smallFile,
568                            byte[] smallBytes)
569                    throws PortalException, SystemException {
570    
571                    if (smallImage) {
572                            if ((smallFile != null) && (smallBytes != null)) {
573                                    imageLocalService.updateImage(smallImageId, smallBytes);
574                            }
575                    }
576                    else {
577                            imageLocalService.deleteImage(smallImageId);
578                    }
579            }
580    
581            protected void validate(String templateId) throws PortalException {
582                    if ((Validator.isNull(templateId)) ||
583                            (Validator.isNumber(templateId)) ||
584                            (templateId.indexOf(CharPool.SPACE) != -1)) {
585    
586                            throw new TemplateIdException();
587                    }
588            }
589    
590            protected void validate(
591                            long groupId, String templateId, boolean autoTemplateId,
592                            String name, String description, String xsl, boolean smallImage,
593                            String smallImageURL, File smallFile, byte[] smallBytes)
594                    throws PortalException, SystemException {
595    
596                    if (!autoTemplateId) {
597                            validate(templateId);
598    
599                            JournalTemplate template = journalTemplatePersistence.fetchByG_T(
600                                    groupId, templateId);
601    
602                            if (template != null) {
603                                    throw new DuplicateTemplateIdException();
604                            }
605                    }
606    
607                    validate(
608                            name, description, xsl, smallImage, smallImageURL, smallFile,
609                            smallBytes);
610            }
611    
612            protected void validate(
613                            String name, String description, String xsl, boolean smallImage,
614                            String smallImageURL, File smallFile, byte[] smallBytes)
615                    throws PortalException, SystemException {
616    
617                    if (Validator.isNull(name)) {
618                            throw new TemplateNameException();
619                    }
620                    else if (Validator.isNull(description)) {
621                            throw new TemplateDescriptionException();
622                    }
623                    else if (Validator.isNull(xsl)) {
624                            throw new TemplateXslException();
625                    }
626    
627                    String[] imageExtensions = PrefsPropsUtil.getStringArray(
628                            PropsKeys.JOURNAL_IMAGE_EXTENSIONS, StringPool.COMMA);
629    
630                    if (smallImage && Validator.isNull(smallImageURL) &&
631                            smallFile != null && smallBytes != null) {
632    
633                            String smallImageName = smallFile.getName();
634    
635                            if (smallImageName != null) {
636                                    boolean validSmallImageExtension = false;
637    
638                                    for (int i = 0; i < imageExtensions.length; i++) {
639                                            if (StringPool.STAR.equals(imageExtensions[i]) ||
640                                                    StringUtil.endsWith(
641                                                            smallImageName, imageExtensions[i])) {
642    
643                                                    validSmallImageExtension = true;
644    
645                                                    break;
646                                            }
647                                    }
648    
649                                    if (!validSmallImageExtension) {
650                                            throw new TemplateSmallImageNameException(smallImageName);
651                                    }
652                            }
653    
654                            long smallImageMaxSize = PrefsPropsUtil.getLong(
655                                    PropsKeys.JOURNAL_IMAGE_SMALL_MAX_SIZE);
656    
657                            if ((smallImageMaxSize > 0) &&
658                                    ((smallBytes == null) ||
659                                            (smallBytes.length > smallImageMaxSize))) {
660    
661                                    throw new TemplateSmallImageSizeException();
662                            }
663                    }
664            }
665    
666            private static Log _log = LogFactoryUtil.getLog(
667                    JournalTemplateLocalServiceImpl.class);
668    
669    }