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