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