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