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