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