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