001
014
015 package com.liferay.portlet.journal.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.CharPool;
022 import com.liferay.portal.kernel.util.FileUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.LocaleUtil;
025 import com.liferay.portal.kernel.util.OrderByComparator;
026 import com.liferay.portal.kernel.util.PropsKeys;
027 import com.liferay.portal.kernel.util.StringPool;
028 import com.liferay.portal.kernel.util.StringUtil;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.model.Group;
031 import com.liferay.portal.model.Image;
032 import com.liferay.portal.model.ResourceConstants;
033 import com.liferay.portal.model.User;
034 import com.liferay.portal.service.ServiceContext;
035 import com.liferay.portal.util.PortalUtil;
036 import com.liferay.portal.util.PrefsPropsUtil;
037 import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
038 import com.liferay.portlet.expando.model.ExpandoBridge;
039 import com.liferay.portlet.journal.DuplicateTemplateIdException;
040 import com.liferay.portlet.journal.NoSuchTemplateException;
041 import com.liferay.portlet.journal.RequiredTemplateException;
042 import com.liferay.portlet.journal.TemplateIdException;
043 import com.liferay.portlet.journal.TemplateNameException;
044 import com.liferay.portlet.journal.TemplateSmallImageNameException;
045 import com.liferay.portlet.journal.TemplateSmallImageSizeException;
046 import com.liferay.portlet.journal.TemplateXslException;
047 import com.liferay.portlet.journal.model.JournalStructure;
048 import com.liferay.portlet.journal.model.JournalTemplate;
049 import com.liferay.portlet.journal.model.JournalTemplateConstants;
050 import com.liferay.portlet.journal.service.base.JournalTemplateLocalServiceBaseImpl;
051 import com.liferay.portlet.journal.util.JournalUtil;
052
053 import java.io.File;
054 import java.io.IOException;
055
056 import java.util.Date;
057 import java.util.List;
058 import java.util.Locale;
059 import java.util.Map;
060
061
065 public class JournalTemplateLocalServiceImpl
066 extends JournalTemplateLocalServiceBaseImpl {
067
068 @Override
069 public JournalTemplate addTemplate(
070 long userId, long groupId, String templateId,
071 boolean autoTemplateId, String structureId,
072 Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
073 String xsl, boolean formatXsl, String langType, boolean cacheable,
074 boolean smallImage, String smallImageURL, File smallImageFile,
075 ServiceContext serviceContext)
076 throws PortalException, SystemException {
077
078
079
080 User user = userPersistence.findByPrimaryKey(userId);
081 templateId = templateId.trim().toUpperCase();
082 Date now = new Date();
083
084 try {
085 if (formatXsl) {
086 if (langType.equals(JournalTemplateConstants.LANG_TYPE_VM)) {
087 xsl = JournalUtil.formatVM(xsl);
088 }
089 else {
090 xsl = DDMXMLUtil.formatXML(xsl);
091 }
092 }
093 }
094 catch (Exception e) {
095 throw new TemplateXslException();
096 }
097
098 byte[] smallImageBytes = null;
099
100 try {
101 smallImageBytes = FileUtil.getBytes(smallImageFile);
102 }
103 catch (IOException ioe) {
104 }
105
106 validate(
107 groupId, templateId, autoTemplateId, nameMap, xsl, smallImage,
108 smallImageURL, smallImageFile, smallImageBytes);
109
110 if (autoTemplateId) {
111 templateId = String.valueOf(counterLocalService.increment());
112 }
113
114 long id = counterLocalService.increment();
115
116 JournalTemplate template = journalTemplatePersistence.create(id);
117
118 template.setUuid(serviceContext.getUuid());
119 template.setGroupId(groupId);
120 template.setCompanyId(user.getCompanyId());
121 template.setUserId(user.getUserId());
122 template.setUserName(user.getFullName());
123 template.setCreateDate(serviceContext.getCreateDate(now));
124 template.setModifiedDate(serviceContext.getModifiedDate(now));
125 template.setTemplateId(templateId);
126 template.setStructureId(structureId);
127 template.setNameMap(nameMap);
128 template.setDescriptionMap(descriptionMap);
129 template.setXsl(xsl);
130 template.setLangType(langType);
131 template.setCacheable(cacheable);
132 template.setSmallImage(smallImage);
133 template.setSmallImageId(counterLocalService.increment());
134 template.setSmallImageURL(smallImageURL);
135
136 journalTemplatePersistence.update(template, false);
137
138
139
140 if (serviceContext.isAddGroupPermissions() ||
141 serviceContext.isAddGuestPermissions()) {
142
143 addTemplateResources(
144 template, serviceContext.isAddGroupPermissions(),
145 serviceContext.isAddGuestPermissions());
146 }
147 else {
148 addTemplateResources(
149 template, serviceContext.getGroupPermissions(),
150 serviceContext.getGuestPermissions());
151 }
152
153
154
155 ExpandoBridge expandoBridge = template.getExpandoBridge();
156
157 expandoBridge.setAttributes(serviceContext);
158
159
160
161 saveImages(
162 smallImage, template.getSmallImageId(), smallImageFile,
163 smallImageBytes);
164
165 return template;
166 }
167
168 @Override
169 public void addTemplateResources(
170 JournalTemplate template, boolean addGroupPermissions,
171 boolean addGuestPermissions)
172 throws PortalException, SystemException {
173
174 resourceLocalService.addResources(
175 template.getCompanyId(), template.getGroupId(),
176 template.getUserId(), JournalTemplate.class.getName(),
177 template.getId(), false, addGroupPermissions, addGuestPermissions);
178 }
179
180 @Override
181 public void addTemplateResources(
182 JournalTemplate template, String[] groupPermissions,
183 String[] guestPermissions)
184 throws PortalException, SystemException {
185
186 resourceLocalService.addModelResources(
187 template.getCompanyId(), template.getGroupId(),
188 template.getUserId(), JournalTemplate.class.getName(),
189 template.getId(), groupPermissions, guestPermissions);
190 }
191
192 @Override
193 public void addTemplateResources(
194 long groupId, String templateId, boolean addGroupPermissions,
195 boolean addGuestPermissions)
196 throws PortalException, SystemException {
197
198 JournalTemplate template = journalTemplatePersistence.findByG_T(
199 groupId, templateId);
200
201 addTemplateResources(
202 template, addGroupPermissions, addGuestPermissions);
203 }
204
205 @Override
206 public void addTemplateResources(
207 long groupId, String templateId, String[] groupPermissions,
208 String[] guestPermissions)
209 throws PortalException, SystemException {
210
211 JournalTemplate template = journalTemplatePersistence.findByG_T(
212 groupId, templateId);
213
214 addTemplateResources(template, groupPermissions, guestPermissions);
215 }
216
217 @Override
218 public void checkNewLine(long groupId, String templateId)
219 throws PortalException, SystemException {
220
221 JournalTemplate template = journalTemplatePersistence.findByG_T(
222 groupId, templateId);
223
224 String xsl = template.getXsl();
225
226 if ((xsl != null) && xsl.contains("\\n")) {
227 xsl = StringUtil.replace(
228 xsl, new String[] {"\\n", "\\r"}, new String[] {"\n", "\r"});
229
230 template.setXsl(xsl);
231
232 journalTemplatePersistence.update(template, false);
233 }
234 }
235
236 @Override
237 public JournalTemplate copyTemplate(
238 long userId, long groupId, String oldTemplateId,
239 String newTemplateId, boolean autoTemplateId)
240 throws PortalException, SystemException {
241
242
243
244 User user = userPersistence.findByPrimaryKey(userId);
245 oldTemplateId = oldTemplateId.trim().toUpperCase();
246 newTemplateId = newTemplateId.trim().toUpperCase();
247 Date now = new Date();
248
249 JournalTemplate oldTemplate = journalTemplatePersistence.findByG_T(
250 groupId, oldTemplateId);
251
252 if (autoTemplateId) {
253 newTemplateId = String.valueOf(counterLocalService.increment());
254 }
255 else {
256 validate(newTemplateId);
257
258 JournalTemplate newTemplate = journalTemplatePersistence.fetchByG_T(
259 groupId, newTemplateId);
260
261 if (newTemplate != null) {
262 throw new DuplicateTemplateIdException();
263 }
264 }
265
266 long id = counterLocalService.increment();
267
268 JournalTemplate newTemplate = journalTemplatePersistence.create(id);
269
270 newTemplate.setGroupId(groupId);
271 newTemplate.setCompanyId(user.getCompanyId());
272 newTemplate.setUserId(user.getUserId());
273 newTemplate.setUserName(user.getFullName());
274 newTemplate.setCreateDate(now);
275 newTemplate.setModifiedDate(now);
276 newTemplate.setTemplateId(newTemplateId);
277 newTemplate.setStructureId(oldTemplate.getStructureId());
278 newTemplate.setNameMap(oldTemplate.getNameMap());
279 newTemplate.setDescriptionMap(oldTemplate.getDescriptionMap());
280 newTemplate.setXsl(oldTemplate.getXsl());
281 newTemplate.setLangType(oldTemplate.getLangType());
282 newTemplate.setCacheable(oldTemplate.isCacheable());
283 newTemplate.setSmallImage(oldTemplate.isSmallImage());
284 newTemplate.setSmallImageId(counterLocalService.increment());
285 newTemplate.setSmallImageURL(oldTemplate.getSmallImageURL());
286
287 journalTemplatePersistence.update(newTemplate, false);
288
289
290
291 if (oldTemplate.getSmallImage()) {
292 Image image = imageLocalService.getImage(
293 oldTemplate.getSmallImageId());
294
295 byte[] smallImageBytes = image.getTextObj();
296
297 imageLocalService.updateImage(
298 newTemplate.getSmallImageId(), smallImageBytes);
299 }
300
301
302
303 addTemplateResources(newTemplate, true, true);
304
305
306
307 ExpandoBridge oldExpandoBridge = oldTemplate.getExpandoBridge();
308
309 ExpandoBridge newExpandoBridge = newTemplate.getExpandoBridge();
310
311 newExpandoBridge.setAttributes(oldExpandoBridge.getAttributes());
312
313 return newTemplate;
314 }
315
316 @Override
317 public void deleteTemplate(JournalTemplate template)
318 throws PortalException, SystemException {
319
320 Group companyGroup = groupLocalService.getCompanyGroup(
321 template.getCompanyId());
322
323 if (template.getGroupId() == companyGroup.getGroupId()) {
324 if (journalArticlePersistence.countByTemplateId(
325 template.getTemplateId()) > 0) {
326
327 throw new RequiredTemplateException();
328 }
329 }
330 else {
331 if (journalArticlePersistence.countByG_C_T(
332 template.getGroupId(), 0, template.getTemplateId()) > 0) {
333
334 throw new RequiredTemplateException();
335 }
336 }
337
338
339
340 webDAVPropsLocalService.deleteWebDAVProps(
341 JournalTemplate.class.getName(), template.getId());
342
343
344
345 imageLocalService.deleteImage(template.getSmallImageId());
346
347
348
349 expandoValueLocalService.deleteValues(
350 JournalTemplate.class.getName(), template.getId());
351
352
353
354 resourceLocalService.deleteResource(
355 template.getCompanyId(), JournalTemplate.class.getName(),
356 ResourceConstants.SCOPE_INDIVIDUAL, template.getId());
357
358
359
360 journalArticleLocalService.updateTemplateId(
361 template.getGroupId(),
362 PortalUtil.getClassNameId(JournalStructure.class.getName()),
363 template.getTemplateId(), StringPool.BLANK);
364
365
366
367 journalTemplatePersistence.remove(template);
368 }
369
370 @Override
371 public void deleteTemplate(long groupId, String templateId)
372 throws PortalException, SystemException {
373
374 templateId = templateId.trim().toUpperCase();
375
376 JournalTemplate template = journalTemplatePersistence.findByG_T(
377 groupId, templateId);
378
379 deleteTemplate(template);
380 }
381
382 @Override
383 public void deleteTemplates(long groupId)
384 throws PortalException, SystemException {
385
386 for (JournalTemplate template :
387 journalTemplatePersistence.findByGroupId(groupId)) {
388
389 deleteTemplate(template);
390 }
391 }
392
393 @Override
394 public List<JournalTemplate> getStructureTemplates(
395 long groupId, String structureId)
396 throws SystemException {
397
398 return journalTemplatePersistence.findByG_S(groupId, structureId);
399 }
400
401 @Override
402 public List<JournalTemplate> getStructureTemplates(
403 long groupId, String structureId, int start, int end)
404 throws SystemException {
405
406 return journalTemplatePersistence.findByG_S(
407 groupId, structureId, start, end);
408 }
409
410 @Override
411 public int getStructureTemplatesCount(long groupId, String structureId)
412 throws SystemException {
413
414 return journalTemplatePersistence.countByG_S(groupId, structureId);
415 }
416
417 @Override
418 public JournalTemplate getTemplate(long id)
419 throws PortalException, SystemException {
420
421 return journalTemplatePersistence.findByPrimaryKey(id);
422 }
423
424 @Override
425 public JournalTemplate getTemplate(long groupId, String templateId)
426 throws PortalException, SystemException {
427
428 return getTemplate(groupId, templateId, false);
429 }
430
431 @Override
432 public JournalTemplate getTemplate(
433 long groupId, String templateId, boolean includeGlobalTemplates)
434 throws PortalException, SystemException {
435
436 templateId = GetterUtil.getString(templateId).toUpperCase();
437
438 if (groupId == 0) {
439 _log.error(
440 "No group ID was passed for " + templateId + ". Group ID is " +
441 "required since 4.2.0. Please update all custom code and " +
442 "data that references templates without a group ID.");
443
444 List<JournalTemplate> templates =
445 journalTemplatePersistence.findByTemplateId(templateId);
446
447 if (!templates.isEmpty()) {
448 return templates.get(0);
449 }
450
451 throw new NoSuchTemplateException(
452 "No JournalTemplate exists with the template ID " + templateId);
453 }
454
455 JournalTemplate template = journalTemplatePersistence.fetchByG_T(
456 groupId, templateId);
457
458 if (template != null) {
459 return template;
460 }
461
462 if (!includeGlobalTemplates) {
463 throw new NoSuchTemplateException(
464 "No JournalTemplate exists with the template ID " + templateId);
465 }
466
467 Group group = groupPersistence.findByPrimaryKey(groupId);
468
469 Group companyGroup = groupLocalService.getCompanyGroup(
470 group.getCompanyId());
471
472 return journalTemplatePersistence.findByG_T(
473 companyGroup.getGroupId(), templateId);
474 }
475
476 @Override
477 public JournalTemplate getTemplateBySmallImageId(long smallImageId)
478 throws PortalException, SystemException {
479
480 return journalTemplatePersistence.findBySmallImageId(smallImageId);
481 }
482
483 @Override
484 public List<JournalTemplate> getTemplates() throws SystemException {
485 return journalTemplatePersistence.findAll();
486 }
487
488 @Override
489 public List<JournalTemplate> getTemplates(long groupId)
490 throws SystemException {
491
492 return journalTemplatePersistence.findByGroupId(groupId);
493 }
494
495 @Override
496 public List<JournalTemplate> getTemplates(long groupId, int start, int end)
497 throws SystemException {
498
499 return journalTemplatePersistence.findByGroupId(groupId, start, end);
500 }
501
502 @Override
503 public int getTemplatesCount(long groupId) throws SystemException {
504 return journalTemplatePersistence.countByGroupId(groupId);
505 }
506
507 @Override
508 public boolean hasTemplate(long groupId, String templateId)
509 throws SystemException {
510
511 try {
512 getTemplate(groupId, templateId);
513
514 return true;
515 }
516 catch (PortalException pe) {
517 return false;
518 }
519 }
520
521 @Override
522 public List<JournalTemplate> search(
523 long companyId, long[] groupIds, String keywords,
524 String structureId, String structureIdComparator, int start,
525 int end, OrderByComparator obc)
526 throws SystemException {
527
528 return journalTemplateFinder.findByKeywords(
529 companyId, groupIds, keywords, structureId, structureIdComparator,
530 start, end, obc);
531 }
532
533 @Override
534 public List<JournalTemplate> search(
535 long companyId, long[] groupIds, String templateId,
536 String structureId, String structureIdComparator, String name,
537 String description, boolean andOperator, int start, int end,
538 OrderByComparator obc)
539 throws SystemException {
540
541 return journalTemplateFinder.findByC_G_T_S_N_D(
542 companyId, groupIds, templateId, structureId, structureIdComparator,
543 name, description, andOperator, start, end, obc);
544 }
545
546 @Override
547 public int searchCount(
548 long companyId, long[] groupIds, String keywords,
549 String structureId, String structureIdComparator)
550 throws SystemException {
551
552 return journalTemplateFinder.countByKeywords(
553 companyId, groupIds, keywords, structureId, structureIdComparator);
554 }
555
556 @Override
557 public int searchCount(
558 long companyId, long[] groupIds, String templateId,
559 String structureId, String structureIdComparator, String name,
560 String description, boolean andOperator)
561 throws SystemException {
562
563 return journalTemplateFinder.countByC_G_T_S_N_D(
564 companyId, groupIds, templateId, structureId, structureIdComparator,
565 name, description, andOperator);
566 }
567
568 @Override
569 public JournalTemplate updateTemplate(
570 long groupId, String templateId, String structureId,
571 Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
572 String xsl, boolean formatXsl, String langType, boolean cacheable,
573 boolean smallImage, String smallImageURL, File smallImageFile,
574 ServiceContext serviceContext)
575 throws PortalException, SystemException {
576
577
578
579 templateId = templateId.trim().toUpperCase();
580
581 try {
582 if (formatXsl) {
583 if (langType.equals(JournalTemplateConstants.LANG_TYPE_VM)) {
584 xsl = JournalUtil.formatVM(xsl);
585 }
586 else {
587 xsl = DDMXMLUtil.formatXML(xsl);
588 }
589 }
590 }
591 catch (Exception e) {
592 throw new TemplateXslException();
593 }
594
595 byte[] smallImageBytes = null;
596
597 try {
598 smallImageBytes = FileUtil.getBytes(smallImageFile);
599 }
600 catch (IOException ioe) {
601 }
602
603 validate(
604 nameMap, xsl, smallImage, smallImageURL, smallImageFile,
605 smallImageBytes);
606
607 JournalTemplate template = journalTemplatePersistence.findByG_T(
608 groupId, templateId);
609
610 template.setModifiedDate(new Date());
611
612 if (Validator.isNull(template.getStructureId()) &&
613 Validator.isNotNull(structureId)) {
614
615
616
617
618
619
620 template.setStructureId(structureId);
621 }
622
623 template.setNameMap(nameMap);
624 template.setDescriptionMap(descriptionMap);
625 template.setXsl(xsl);
626 template.setLangType(langType);
627 template.setCacheable(cacheable);
628 template.setSmallImage(smallImage);
629 template.setSmallImageURL(smallImageURL);
630 template.setModifiedDate(serviceContext.getModifiedDate(null));
631
632 journalTemplatePersistence.update(template, false);
633
634
635
636 ExpandoBridge expandoBridge = template.getExpandoBridge();
637
638 expandoBridge.setAttributes(serviceContext);
639
640
641
642 saveImages(
643 smallImage, template.getSmallImageId(), smallImageFile,
644 smallImageBytes);
645
646 return template;
647 }
648
649 protected void saveImages(
650 boolean smallImage, long smallImageId, File smallImageFile,
651 byte[] smallImageBytes)
652 throws PortalException, SystemException {
653
654 if (smallImage) {
655 if ((smallImageFile != null) && (smallImageBytes != null)) {
656 imageLocalService.updateImage(smallImageId, smallImageBytes);
657 }
658 }
659 else {
660 imageLocalService.deleteImage(smallImageId);
661 }
662 }
663
664 protected void validate(
665 long groupId, String templateId, boolean autoTemplateId,
666 Map<Locale, String> nameMap, String xsl, boolean smallImage,
667 String smallImageURL, File smallImageFile, byte[] smallImageBytes)
668 throws PortalException, SystemException {
669
670 if (!autoTemplateId) {
671 validate(templateId);
672
673 JournalTemplate template = journalTemplatePersistence.fetchByG_T(
674 groupId, templateId);
675
676 if (template != null) {
677 throw new DuplicateTemplateIdException();
678 }
679 }
680
681 validate(
682 nameMap, xsl, smallImage, smallImageURL, smallImageFile,
683 smallImageBytes);
684 }
685
686 protected void validate(
687 Map<Locale, String> nameMap, String xsl, boolean smallImage,
688 String smallImageURL, File smallImageFile, byte[] smallImageBytes)
689 throws PortalException, SystemException {
690
691 Locale locale = LocaleUtil.getDefault();
692
693 if (nameMap.isEmpty() || Validator.isNull(nameMap.get(locale))) {
694 throw new TemplateNameException();
695 }
696 else if (Validator.isNull(xsl)) {
697 throw new TemplateXslException();
698 }
699
700 String[] imageExtensions = PrefsPropsUtil.getStringArray(
701 PropsKeys.JOURNAL_IMAGE_EXTENSIONS, StringPool.COMMA);
702
703 if (!smallImage || Validator.isNotNull(smallImageURL) ||
704 (smallImageFile == null) || (smallImageBytes == null)) {
705
706 return;
707 }
708
709 String smallImageName = smallImageFile.getName();
710
711 if (smallImageName != null) {
712 boolean validSmallImageExtension = false;
713
714 for (int i = 0; i < imageExtensions.length; i++) {
715 if (StringPool.STAR.equals(imageExtensions[i]) ||
716 StringUtil.endsWith(
717 smallImageName, imageExtensions[i])) {
718
719 validSmallImageExtension = true;
720
721 break;
722 }
723 }
724
725 if (!validSmallImageExtension) {
726 throw new TemplateSmallImageNameException(smallImageName);
727 }
728 }
729
730 long smallImageMaxSize = PrefsPropsUtil.getLong(
731 PropsKeys.JOURNAL_IMAGE_SMALL_MAX_SIZE);
732
733 if ((smallImageMaxSize > 0) &&
734 ((smallImageBytes == null) ||
735 (smallImageBytes.length > smallImageMaxSize))) {
736
737 throw new TemplateSmallImageSizeException();
738 }
739 }
740
741 protected void validate(String templateId) throws PortalException {
742 if (Validator.isNull(templateId) ||
743 Validator.isNumber(templateId) ||
744 (templateId.indexOf(CharPool.SPACE) != -1)) {
745
746 throw new TemplateIdException();
747 }
748 }
749
750 private static Log _log = LogFactoryUtil.getLog(
751 JournalTemplateLocalServiceImpl.class);
752
753 }