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