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