1
22
23 package com.liferay.portlet.journal.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.util.FileUtil;
28 import com.liferay.portal.kernel.util.GetterUtil;
29 import com.liferay.portal.kernel.util.OrderByComparator;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portal.kernel.util.StringUtil;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.model.Image;
34 import com.liferay.portal.model.ResourceConstants;
35 import com.liferay.portal.model.User;
36 import com.liferay.portal.util.PortalUtil;
37 import com.liferay.portal.util.PropsKeys;
38 import com.liferay.portal.util.PropsUtil;
39 import com.liferay.portlet.journal.DuplicateTemplateIdException;
40 import com.liferay.portlet.journal.NoSuchTemplateException;
41 import com.liferay.portlet.journal.RequiredTemplateException;
42 import com.liferay.portlet.journal.TemplateDescriptionException;
43 import com.liferay.portlet.journal.TemplateIdException;
44 import com.liferay.portlet.journal.TemplateNameException;
45 import com.liferay.portlet.journal.TemplateSmallImageNameException;
46 import com.liferay.portlet.journal.TemplateSmallImageSizeException;
47 import com.liferay.portlet.journal.TemplateXslException;
48 import com.liferay.portlet.journal.model.JournalTemplate;
49 import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
50 import com.liferay.portlet.journal.service.base.JournalTemplateLocalServiceBaseImpl;
51 import com.liferay.portlet.journal.util.JournalUtil;
52
53 import java.io.File;
54 import java.io.IOException;
55
56 import java.util.Date;
57 import java.util.List;
58
59 import org.apache.commons.logging.Log;
60 import org.apache.commons.logging.LogFactory;
61
62 import org.dom4j.DocumentException;
63
64
71 public class JournalTemplateLocalServiceImpl
72 extends JournalTemplateLocalServiceBaseImpl {
73
74 public JournalTemplate addTemplate(
75 long userId, String templateId, boolean autoTemplateId, long plid,
76 String structureId, String name, String description, String xsl,
77 boolean formatXsl, String langType, boolean cacheable,
78 boolean smallImage, String smallImageURL, File smallFile,
79 boolean addCommunityPermissions, boolean addGuestPermissions)
80 throws PortalException, SystemException {
81
82 return addTemplate(
83 null, userId, templateId, autoTemplateId, plid, structureId, name,
84 description, xsl, formatXsl, langType, cacheable, smallImage,
85 smallImageURL, smallFile, Boolean.valueOf(addCommunityPermissions),
86 Boolean.valueOf(addGuestPermissions), null, null);
87 }
88
89 public JournalTemplate addTemplate(
90 String uuid, long userId, String templateId, boolean autoTemplateId,
91 long plid, String structureId, String name, String description,
92 String xsl, boolean formatXsl, String langType, boolean cacheable,
93 boolean smallImage, String smallImageURL, File smallFile,
94 boolean addCommunityPermissions, boolean addGuestPermissions)
95 throws PortalException, SystemException {
96
97 return addTemplate(
98 uuid, userId, templateId, autoTemplateId, plid, structureId, name,
99 description, xsl, formatXsl, langType, cacheable, smallImage,
100 smallImageURL, smallFile, Boolean.valueOf(addCommunityPermissions),
101 Boolean.valueOf(addGuestPermissions), null, null);
102 }
103
104 public JournalTemplate addTemplate(
105 long userId, String templateId, boolean autoTemplateId, long plid,
106 String structureId, String name, String description, String xsl,
107 boolean formatXsl, String langType, boolean cacheable,
108 boolean smallImage, String smallImageURL, File smallFile,
109 String[] communityPermissions, String[] guestPermissions)
110 throws PortalException, SystemException {
111
112 return addTemplate(
113 null, userId, templateId, autoTemplateId, plid, structureId, name,
114 description, xsl, formatXsl, langType, cacheable, smallImage,
115 smallImageURL, smallFile, null, null, communityPermissions,
116 guestPermissions);
117 }
118
119 public JournalTemplate addTemplate(
120 String uuid, long userId, String templateId, boolean autoTemplateId,
121 long plid, String structureId, String name, String description,
122 String xsl, boolean formatXsl, String langType, boolean cacheable,
123 boolean smallImage, String smallImageURL, File smallFile,
124 Boolean addCommunityPermissions, Boolean addGuestPermissions,
125 String[] communityPermissions, String[] guestPermissions)
126 throws PortalException, SystemException {
127
128 long groupId = PortalUtil.getPortletGroupId(plid);
129
130 return addTemplateToGroup(
131 uuid, userId, templateId, autoTemplateId, groupId, structureId,
132 name, description, xsl, formatXsl, langType, cacheable, smallImage,
133 smallImageURL, smallFile, addCommunityPermissions,
134 addGuestPermissions, communityPermissions, guestPermissions);
135 }
136
137 public JournalTemplate addTemplateToGroup(
138 String uuid, long userId, String templateId, boolean autoTemplateId,
139 long groupId, String structureId, String name, String description,
140 String xsl, boolean formatXsl, String langType, boolean cacheable,
141 boolean smallImage, String smallImageURL, File smallFile,
142 Boolean addCommunityPermissions, Boolean addGuestPermissions,
143 String[] communityPermissions, String[] guestPermissions)
144 throws PortalException, SystemException {
145
146
148 User user = userPersistence.findByPrimaryKey(userId);
149 templateId = templateId.trim().toUpperCase();
150 Date now = new Date();
151
152 try {
153 if (formatXsl) {
154 if (langType.equals(JournalTemplateImpl.LANG_TYPE_VM)) {
155 xsl = JournalUtil.formatVM(xsl);
156 }
157 else {
158 xsl = JournalUtil.formatXML(xsl);
159 }
160 }
161 }
162 catch (DocumentException de) {
163 throw new TemplateXslException();
164 }
165 catch (IOException ioe) {
166 throw new TemplateXslException();
167 }
168
169 byte[] smallBytes = null;
170
171 try {
172 smallBytes = FileUtil.getBytes(smallFile);
173 }
174 catch (IOException ioe) {
175 }
176
177 validate(
178 groupId, templateId, autoTemplateId, name, description, xsl,
179 smallImage, smallImageURL, smallFile, smallBytes);
180
181 if (autoTemplateId) {
182 templateId = String.valueOf(counterLocalService.increment());
183 }
184
185 long id = counterLocalService.increment();
186
187 JournalTemplate template = journalTemplatePersistence.create(id);
188
189 template.setUuid(uuid);
190 template.setGroupId(groupId);
191 template.setCompanyId(user.getCompanyId());
192 template.setUserId(user.getUserId());
193 template.setUserName(user.getFullName());
194 template.setCreateDate(now);
195 template.setModifiedDate(now);
196 template.setTemplateId(templateId);
197 template.setStructureId(structureId);
198 template.setName(name);
199 template.setDescription(description);
200 template.setXsl(xsl);
201 template.setLangType(langType);
202 template.setCacheable(cacheable);
203 template.setSmallImage(smallImage);
204 template.setSmallImageId(counterLocalService.increment());
205 template.setSmallImageURL(smallImageURL);
206
207 journalTemplatePersistence.update(template, false);
208
209
211 saveImages(
212 smallImage, template.getSmallImageId(), smallFile, smallBytes);
213
214
216 if ((addCommunityPermissions != null) &&
217 (addGuestPermissions != null)) {
218
219 addTemplateResources(
220 template, addCommunityPermissions.booleanValue(),
221 addGuestPermissions.booleanValue());
222 }
223 else {
224 addTemplateResources(
225 template, communityPermissions, guestPermissions);
226 }
227
228 return template;
229 }
230
231 public void addTemplateResources(
232 long groupId, String templateId, boolean addCommunityPermissions,
233 boolean addGuestPermissions)
234 throws PortalException, SystemException {
235
236 JournalTemplate template = journalTemplatePersistence.findByG_T(
237 groupId, templateId);
238
239 addTemplateResources(
240 template, addCommunityPermissions, addGuestPermissions);
241 }
242
243 public void addTemplateResources(
244 JournalTemplate template, boolean addCommunityPermissions,
245 boolean addGuestPermissions)
246 throws PortalException, SystemException {
247
248 resourceLocalService.addResources(
249 template.getCompanyId(), template.getGroupId(),
250 template.getUserId(), JournalTemplate.class.getName(),
251 template.getId(), false, addCommunityPermissions,
252 addGuestPermissions);
253 }
254
255 public void addTemplateResources(
256 long groupId, String templateId, String[] communityPermissions,
257 String[] guestPermissions)
258 throws PortalException, SystemException {
259
260 JournalTemplate template = journalTemplatePersistence.findByG_T(
261 groupId, templateId);
262
263 addTemplateResources(template, communityPermissions, guestPermissions);
264 }
265
266 public void addTemplateResources(
267 JournalTemplate template, String[] communityPermissions,
268 String[] guestPermissions)
269 throws PortalException, SystemException {
270
271 resourceLocalService.addModelResources(
272 template.getCompanyId(), template.getGroupId(),
273 template.getUserId(), JournalTemplate.class.getName(),
274 template.getId(), communityPermissions, guestPermissions);
275 }
276
277 public void checkNewLine(long groupId, String templateId)
278 throws PortalException, SystemException {
279
280 JournalTemplate template = journalTemplatePersistence.findByG_T(
281 groupId, templateId);
282
283 String xsl = template.getXsl();
284
285 if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
286 xsl = StringUtil.replace(
287 xsl,
288 new String[] {"\\n", "\\r"},
289 new String[] {"\n", "\r"});
290
291 template.setXsl(xsl);
292
293 journalTemplatePersistence.update(template, false);
294 }
295 }
296
297 public JournalTemplate copyTemplate(
298 long userId, long groupId, String oldTemplateId,
299 String newTemplateId, boolean autoTemplateId)
300 throws PortalException, SystemException {
301
302
304 User user = userPersistence.findByPrimaryKey(userId);
305 oldTemplateId = oldTemplateId.trim().toUpperCase();
306 newTemplateId = newTemplateId.trim().toUpperCase();
307 Date now = new Date();
308
309 JournalTemplate oldTemplate = journalTemplatePersistence.findByG_T(
310 groupId, oldTemplateId);
311
312 if (autoTemplateId) {
313 newTemplateId = String.valueOf(counterLocalService.increment());
314 }
315 else {
316 validate(newTemplateId);
317
318 JournalTemplate newTemplate = journalTemplatePersistence.fetchByG_T(
319 groupId, newTemplateId);
320
321 if (newTemplate != null) {
322 throw new DuplicateTemplateIdException();
323 }
324 }
325
326 long id = counterLocalService.increment();
327
328 JournalTemplate newTemplate = journalTemplatePersistence.create(id);
329
330 newTemplate.setGroupId(groupId);
331 newTemplate.setCompanyId(user.getCompanyId());
332 newTemplate.setUserId(user.getUserId());
333 newTemplate.setUserName(user.getFullName());
334 newTemplate.setCreateDate(now);
335 newTemplate.setModifiedDate(now);
336 newTemplate.setTemplateId(newTemplateId);
337 newTemplate.setStructureId(oldTemplate.getStructureId());
338 newTemplate.setName(oldTemplate.getName());
339 newTemplate.setDescription(oldTemplate.getDescription());
340 newTemplate.setXsl(oldTemplate.getXsl());
341 newTemplate.setLangType(oldTemplate.getLangType());
342 newTemplate.setCacheable(oldTemplate.isCacheable());
343 newTemplate.setSmallImage(oldTemplate.isSmallImage());
344 newTemplate.setSmallImageId(counterLocalService.increment());
345 newTemplate.setSmallImageURL(oldTemplate.getSmallImageURL());
346
347 journalTemplatePersistence.update(newTemplate, false);
348
349
351 if (oldTemplate.getSmallImage()) {
352 Image image = imageLocalService.getImage(
353 oldTemplate.getSmallImageId());
354
355 byte[] smallBytes = image.getTextObj();
356
357 imageLocalService.updateImage(
358 newTemplate.getSmallImageId(), smallBytes);
359 }
360
361
363 addTemplateResources(newTemplate, true, true);
364
365 return null;
366 }
367
368 public void deleteTemplate(long groupId, String templateId)
369 throws PortalException, SystemException {
370
371 templateId = templateId.trim().toUpperCase();
372
373 JournalTemplate template = journalTemplatePersistence.findByG_T(
374 groupId, templateId);
375
376 deleteTemplate(template);
377 }
378
379 public void deleteTemplate(JournalTemplate template)
380 throws PortalException, SystemException {
381
382 if (journalArticlePersistence.countByG_T(
383 template.getGroupId(), template.getTemplateId()) > 0) {
384
385 throw new RequiredTemplateException();
386 }
387
388
390 imageLocalService.deleteImage(template.getSmallImageId());
391
392
394 webDAVPropsLocalService.deleteWebDAVProps(
395 JournalTemplate.class.getName(), template.getPrimaryKey());
396
397
399 resourceLocalService.deleteResource(
400 template.getCompanyId(), JournalTemplate.class.getName(),
401 ResourceConstants.SCOPE_INDIVIDUAL, template.getId());
402
403
405 journalTemplatePersistence.remove(template.getPrimaryKey());
406 }
407
408 public void deleteTemplates(long groupId)
409 throws PortalException, SystemException {
410
411 for (JournalTemplate template :
412 journalTemplatePersistence.findByGroupId(groupId)) {
413
414 deleteTemplate(template);
415 }
416 }
417
418 public List<JournalTemplate> getStructureTemplates(
419 long groupId, String structureId)
420 throws SystemException {
421
422 return journalTemplatePersistence.findByG_S(groupId, structureId);
423 }
424
425 public List<JournalTemplate> getStructureTemplates(
426 long groupId, String structureId, int start, int end)
427 throws SystemException {
428
429 return journalTemplatePersistence.findByG_S(
430 groupId, structureId, start, end);
431 }
432
433 public int getStructureTemplatesCount(long groupId, String structureId)
434 throws SystemException {
435
436 return journalTemplatePersistence.countByG_S(groupId, structureId);
437 }
438
439 public JournalTemplate getTemplate(long id)
440 throws PortalException, SystemException {
441
442 return journalTemplatePersistence.findByPrimaryKey(id);
443 }
444
445 public JournalTemplate getTemplate(long groupId, String templateId)
446 throws PortalException, SystemException {
447
448 templateId = GetterUtil.getString(templateId).toUpperCase();
449
450 if (groupId == 0) {
451 _log.error(
452 "No group id was passed for " + templateId + ". Group id is " +
453 "required since 4.2.0. Please update all custom code and " +
454 "data that references templates without a group id.");
455
456 List<JournalTemplate> templates =
457 journalTemplatePersistence.findByTemplateId(
458 templateId);
459
460 if (templates.size() == 0) {
461 throw new NoSuchTemplateException(
462 "No JournalTemplate exists with the template id " +
463 templateId);
464 }
465 else {
466 return templates.get(0);
467 }
468 }
469 else {
470 return journalTemplatePersistence.findByG_T(groupId, templateId);
471 }
472 }
473
474 public JournalTemplate getTemplateBySmallImageId(long smallImageId)
475 throws PortalException, SystemException {
476
477 return journalTemplatePersistence.findBySmallImageId(smallImageId);
478 }
479
480 public List<JournalTemplate> getTemplates() throws SystemException {
481 return journalTemplatePersistence.findAll();
482 }
483
484 public List<JournalTemplate> getTemplates(long groupId)
485 throws SystemException {
486
487 return journalTemplatePersistence.findByGroupId(groupId);
488 }
489
490 public List<JournalTemplate> getTemplates(long groupId, int start, int end)
491 throws SystemException {
492
493 return journalTemplatePersistence.findByGroupId(groupId, start, end);
494 }
495
496 public int getTemplatesCount(long groupId) throws SystemException {
497 return journalTemplatePersistence.countByGroupId(groupId);
498 }
499
500 public boolean hasTemplate(long groupId, String templateId)
501 throws SystemException {
502
503 try {
504 getTemplate(groupId, templateId);
505
506 return true;
507 }
508 catch (PortalException pe) {
509 return false;
510 }
511 }
512
513 public List<JournalTemplate> search(
514 long companyId, long groupId, String keywords, String structureId,
515 String structureIdComparator, int start, int end,
516 OrderByComparator obc)
517 throws SystemException {
518
519 return journalTemplateFinder.findByKeywords(
520 companyId, groupId, keywords, structureId, structureIdComparator,
521 start, end, obc);
522 }
523
524 public List<JournalTemplate> search(
525 long companyId, long groupId, String templateId, String structureId,
526 String structureIdComparator, String name, String description,
527 boolean andOperator, int start, int end, OrderByComparator obc)
528 throws SystemException {
529
530 return journalTemplateFinder.findByC_G_T_S_N_D(
531 companyId, groupId, templateId, structureId, structureIdComparator,
532 name, description, andOperator, start, end, obc);
533 }
534
535 public int searchCount(
536 long companyId, long groupId, String keywords, String structureId,
537 String structureIdComparator)
538 throws SystemException {
539
540 return journalTemplateFinder.countByKeywords(
541 companyId, groupId, keywords, structureId, structureIdComparator);
542 }
543
544 public int searchCount(
545 long companyId, long groupId, String templateId, String structureId,
546 String structureIdComparator, String name, String description,
547 boolean andOperator)
548 throws SystemException {
549
550 return journalTemplateFinder.countByC_G_T_S_N_D(
551 companyId, groupId, templateId, structureId, structureIdComparator,
552 name, description, andOperator);
553 }
554
555 public JournalTemplate updateTemplate(
556 long groupId, String templateId, String structureId, String name,
557 String description, String xsl, boolean formatXsl, String langType,
558 boolean cacheable, boolean smallImage, String smallImageURL,
559 File smallFile)
560 throws PortalException, SystemException {
561
562
564 templateId = templateId.trim().toUpperCase();
565
566 try {
567 if (formatXsl) {
568 if (langType.equals(JournalTemplateImpl.LANG_TYPE_VM)) {
569 xsl = JournalUtil.formatVM(xsl);
570 }
571 else {
572 xsl = JournalUtil.formatXML(xsl);
573 }
574 }
575 }
576 catch (DocumentException de) {
577 throw new TemplateXslException();
578 }
579 catch (IOException ioe) {
580 throw new TemplateXslException();
581 }
582
583 byte[] smallBytes = null;
584
585 try {
586 smallBytes = FileUtil.getBytes(smallFile);
587 }
588 catch (IOException ioe) {
589 }
590
591 validate(
592 name, description, xsl, smallImage, smallImageURL, smallFile,
593 smallBytes);
594
595 JournalTemplate template = journalTemplatePersistence.findByG_T(
596 groupId, templateId);
597
598 template.setModifiedDate(new Date());
599
600 if (Validator.isNull(template.getStructureId()) &&
601 Validator.isNotNull(structureId)) {
602
603
608 template.setStructureId(structureId);
609 }
610
611 template.setName(name);
612 template.setDescription(description);
613 template.setXsl(xsl);
614 template.setLangType(langType);
615 template.setCacheable(cacheable);
616 template.setSmallImage(smallImage);
617 template.setSmallImageURL(smallImageURL);
618
619 journalTemplatePersistence.update(template, false);
620
621
623 saveImages(
624 smallImage, template.getSmallImageId(), smallFile, smallBytes);
625
626 return template;
627 }
628
629 protected void saveImages(
630 boolean smallImage, long smallImageId, File smallFile,
631 byte[] smallBytes)
632 throws PortalException, SystemException {
633
634 if (smallImage) {
635 if ((smallFile != null) && (smallBytes != null)) {
636 imageLocalService.updateImage(smallImageId, smallBytes);
637 }
638 }
639 else {
640 imageLocalService.deleteImage(smallImageId);
641 }
642 }
643
644 protected void validate(String templateId) throws PortalException {
645 if ((Validator.isNull(templateId)) ||
646 (Validator.isNumber(templateId)) ||
647 (templateId.indexOf(StringPool.SPACE) != -1)) {
648
649 throw new TemplateIdException();
650 }
651 }
652
653 protected void validate(
654 long groupId, String templateId, boolean autoTemplateId,
655 String name, String description, String xsl, boolean smallImage,
656 String smallImageURL, File smallFile, byte[] smallBytes)
657 throws PortalException, SystemException {
658
659 if (!autoTemplateId) {
660 validate(templateId);
661
662 try {
663 journalTemplatePersistence.findByG_T(groupId, templateId);
664
665 throw new DuplicateTemplateIdException();
666 }
667 catch (NoSuchTemplateException nste) {
668 }
669 }
670
671 validate(
672 name, description, xsl, smallImage, smallImageURL, smallFile,
673 smallBytes);
674 }
675
676 protected void validate(
677 String name, String description, String xsl, boolean smallImage,
678 String smallImageURL, File smallFile, byte[] smallBytes)
679 throws PortalException {
680
681 if (Validator.isNull(name)) {
682 throw new TemplateNameException();
683 }
684 else if (Validator.isNull(description)) {
685 throw new TemplateDescriptionException();
686 }
687 else if (Validator.isNull(xsl)) {
688 throw new TemplateXslException();
689 }
690
691 String[] imageExtensions =
692 PropsUtil.getArray(PropsKeys.JOURNAL_IMAGE_EXTENSIONS);
693
694 if (smallImage && Validator.isNull(smallImageURL) &&
695 smallFile != null && smallBytes != null) {
696
697 String smallImageName = smallFile.getName();
698
699 if (smallImageName != null) {
700 boolean validSmallImageExtension = false;
701
702 for (int i = 0; i < imageExtensions.length; i++) {
703 if (StringPool.STAR.equals(imageExtensions[i]) ||
704 StringUtil.endsWith(
705 smallImageName, imageExtensions[i])) {
706
707 validSmallImageExtension = true;
708
709 break;
710 }
711 }
712
713 if (!validSmallImageExtension) {
714 throw new TemplateSmallImageNameException(smallImageName);
715 }
716 }
717
718 long smallImageMaxSize = GetterUtil.getLong(
719 PropsUtil.get(PropsKeys.JOURNAL_IMAGE_SMALL_MAX_SIZE));
720
721 if ((smallImageMaxSize > 0) &&
722 ((smallBytes == null) ||
723 (smallBytes.length > smallImageMaxSize))) {
724
725 throw new TemplateSmallImageSizeException();
726 }
727 }
728 }
729
730 private static Log _log =
731 LogFactory.getLog(JournalTemplateLocalServiceImpl.class);
732
733 }