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