1
22
23 package com.liferay.portlet.journal.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.util.GetterUtil;
29 import com.liferay.portal.kernel.util.OrderByComparator;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portal.kernel.util.StringUtil;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.model.User;
34 import com.liferay.portal.model.impl.ResourceImpl;
35 import com.liferay.portal.service.ResourceLocalServiceUtil;
36 import com.liferay.portal.service.impl.ImageLocalUtil;
37 import com.liferay.portal.service.persistence.UserUtil;
38 import com.liferay.portal.util.PortalUtil;
39 import com.liferay.portal.util.PropsUtil;
40 import com.liferay.portlet.journal.DuplicateTemplateIdException;
41 import com.liferay.portlet.journal.NoSuchTemplateException;
42 import com.liferay.portlet.journal.RequiredTemplateException;
43 import com.liferay.portlet.journal.TemplateDescriptionException;
44 import com.liferay.portlet.journal.TemplateIdException;
45 import com.liferay.portlet.journal.TemplateNameException;
46 import com.liferay.portlet.journal.TemplateSmallImageNameException;
47 import com.liferay.portlet.journal.TemplateSmallImageSizeException;
48 import com.liferay.portlet.journal.TemplateXslException;
49 import com.liferay.portlet.journal.model.JournalTemplate;
50 import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
51 import com.liferay.portlet.journal.service.base.JournalTemplateLocalServiceBaseImpl;
52 import com.liferay.portlet.journal.service.persistence.JournalArticleUtil;
53 import com.liferay.portlet.journal.service.persistence.JournalTemplateFinder;
54 import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
55 import com.liferay.portlet.journal.util.JournalUtil;
56 import com.liferay.util.FileUtil;
57
58 import java.io.File;
59 import java.io.IOException;
60
61 import java.util.Date;
62 import java.util.List;
63
64 import org.apache.commons.logging.Log;
65 import org.apache.commons.logging.LogFactory;
66
67 import org.dom4j.DocumentException;
68
69
76 public class JournalTemplateLocalServiceImpl
77 extends JournalTemplateLocalServiceBaseImpl {
78
79 public JournalTemplate addTemplate(
80 long userId, String templateId, boolean autoTemplateId, long plid,
81 String structureId, String name, String description, String xsl,
82 boolean formatXsl, String langType, boolean smallImage,
83 String smallImageURL, File smallFile,
84 boolean addCommunityPermissions, boolean addGuestPermissions)
85 throws PortalException, SystemException {
86
87 return addTemplate(
88 userId, templateId, autoTemplateId, plid, structureId, name,
89 description, xsl, formatXsl, langType, smallImage, smallImageURL,
90 smallFile, Boolean.valueOf(addCommunityPermissions),
91 Boolean.valueOf(addGuestPermissions), null, null);
92 }
93
94 public JournalTemplate addTemplate(
95 long userId, String templateId, boolean autoTemplateId, long plid,
96 String structureId, String name, String description, String xsl,
97 boolean formatXsl, String langType, boolean smallImage,
98 String smallImageURL, File smallFile, String[] communityPermissions,
99 String[] guestPermissions)
100 throws PortalException, SystemException {
101
102 return addTemplate(
103 userId, templateId, autoTemplateId, plid, structureId, name,
104 description, xsl, formatXsl, langType, smallImage, smallImageURL,
105 smallFile, null, null, communityPermissions, guestPermissions);
106 }
107
108 public JournalTemplate addTemplate(
109 long userId, String templateId, boolean autoTemplateId, long plid,
110 String structureId, String name, String description, String xsl,
111 boolean formatXsl, String langType, boolean smallImage,
112 String smallImageURL, File smallFile,
113 Boolean addCommunityPermissions, Boolean addGuestPermissions,
114 String[] communityPermissions, String[] guestPermissions)
115 throws PortalException, SystemException {
116
117 long groupId = PortalUtil.getPortletGroupId(plid);
118
119 return addTemplateToGroup(
120 userId, templateId, autoTemplateId, groupId, structureId, name,
121 description, xsl, formatXsl, langType, smallImage, smallImageURL,
122 smallFile, addCommunityPermissions, addGuestPermissions,
123 communityPermissions, guestPermissions);
124 }
125
126 public JournalTemplate addTemplateToGroup(
127 long userId, String templateId, boolean autoTemplateId,
128 long groupId, String structureId, String name, String description,
129 String xsl, boolean formatXsl, String langType, boolean smallImage,
130 String smallImageURL, File smallFile,
131 Boolean addCommunityPermissions, Boolean addGuestPermissions,
132 String[] communityPermissions, String[] guestPermissions)
133 throws PortalException, SystemException {
134
135
137 User user = UserUtil.findByPrimaryKey(userId);
138 templateId = templateId.trim().toUpperCase();
139 Date now = new Date();
140
141 try {
142 if (formatXsl) {
143 if (langType.equals(JournalTemplateImpl.LANG_TYPE_VM)) {
144 xsl = JournalUtil.formatVM(xsl);
145 }
146 else {
147 xsl = JournalUtil.formatXML(xsl);
148 }
149 }
150 }
151 catch (DocumentException de) {
152 throw new TemplateXslException();
153 }
154 catch (IOException ioe) {
155 throw new TemplateXslException();
156 }
157
158 byte[] smallBytes = null;
159
160 try {
161 smallBytes = FileUtil.getBytes(smallFile);
162 }
163 catch (IOException ioe) {
164 }
165
166 validate(
167 groupId, templateId, autoTemplateId, name, description, xsl,
168 smallImage, smallImageURL, smallFile, smallBytes);
169
170 if (autoTemplateId) {
171 templateId = String.valueOf(CounterLocalServiceUtil.increment());
172 }
173
174 long id = CounterLocalServiceUtil.increment();
175
176 JournalTemplate template = JournalTemplateUtil.create(id);
177
178 template.setGroupId(groupId);
179 template.setCompanyId(user.getCompanyId());
180 template.setUserId(user.getUserId());
181 template.setUserName(user.getFullName());
182 template.setCreateDate(now);
183 template.setModifiedDate(now);
184 template.setTemplateId(templateId);
185 template.setStructureId(structureId);
186 template.setName(name);
187 template.setDescription(description);
188 template.setXsl(xsl);
189 template.setLangType(langType);
190 template.setSmallImage(smallImage);
191 template.setSmallImageId(CounterLocalServiceUtil.increment());
192 template.setSmallImageURL(smallImageURL);
193
194 JournalTemplateUtil.update(template);
195
196
198 saveImages(
199 smallImage, template.getSmallImageId(), smallFile, smallBytes);
200
201
203 if ((addCommunityPermissions != null) &&
204 (addGuestPermissions != null)) {
205
206 addTemplateResources(
207 template, addCommunityPermissions.booleanValue(),
208 addGuestPermissions.booleanValue());
209 }
210 else {
211 addTemplateResources(
212 template, communityPermissions, guestPermissions);
213 }
214
215 return template;
216 }
217
218 public void addTemplateResources(
219 long groupId, String templateId, boolean addCommunityPermissions,
220 boolean addGuestPermissions)
221 throws PortalException, SystemException {
222
223 JournalTemplate template = JournalTemplateUtil.findByG_T(
224 groupId, templateId);
225
226 addTemplateResources(
227 template, addCommunityPermissions, addGuestPermissions);
228 }
229
230 public void addTemplateResources(
231 JournalTemplate template, boolean addCommunityPermissions,
232 boolean addGuestPermissions)
233 throws PortalException, SystemException {
234
235 ResourceLocalServiceUtil.addResources(
236 template.getCompanyId(), template.getGroupId(),
237 template.getUserId(), JournalTemplate.class.getName(),
238 template.getId(), false, addCommunityPermissions,
239 addGuestPermissions);
240 }
241
242 public void addTemplateResources(
243 long groupId, String templateId, String[] communityPermissions,
244 String[] guestPermissions)
245 throws PortalException, SystemException {
246
247 JournalTemplate template = JournalTemplateUtil.findByG_T(
248 groupId, templateId);
249
250 addTemplateResources(template, communityPermissions, guestPermissions);
251 }
252
253 public void addTemplateResources(
254 JournalTemplate template, String[] communityPermissions,
255 String[] guestPermissions)
256 throws PortalException, SystemException {
257
258 ResourceLocalServiceUtil.addModelResources(
259 template.getCompanyId(), template.getGroupId(),
260 template.getUserId(), JournalTemplate.class.getName(),
261 template.getId(), communityPermissions, guestPermissions);
262 }
263
264 public void checkNewLine(long groupId, String templateId)
265 throws PortalException, SystemException {
266
267 JournalTemplate template = JournalTemplateUtil.findByG_T(
268 groupId, templateId);
269
270 String xsl = template.getXsl();
271
272 if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
273 xsl = StringUtil.replace(
274 xsl,
275 new String[] {"\\n", "\\r"},
276 new String[] {"\n", "\r"});
277
278 template.setXsl(xsl);
279
280 JournalTemplateUtil.update(template);
281 }
282 }
283
284 public void deleteTemplate(long groupId, String templateId)
285 throws PortalException, SystemException {
286
287 templateId = templateId.trim().toUpperCase();
288
289 JournalTemplate template = JournalTemplateUtil.findByG_T(
290 groupId, templateId);
291
292 deleteTemplate(template);
293 }
294
295 public void deleteTemplate(JournalTemplate template)
296 throws PortalException, SystemException {
297
298 if (JournalArticleUtil.countByG_T(
299 template.getGroupId(), template.getTemplateId()) > 0) {
300
301 throw new RequiredTemplateException();
302 }
303
304
306 ImageLocalUtil.deleteImage(template.getSmallImageId());
307
308
310 ResourceLocalServiceUtil.deleteResource(
311 template.getCompanyId(), JournalTemplate.class.getName(),
312 ResourceImpl.SCOPE_INDIVIDUAL, template.getId());
313
314
316 JournalTemplateUtil.remove(template.getPrimaryKey());
317 }
318
319 public List getStructureTemplates(long groupId, String structureId)
320 throws SystemException {
321
322 return JournalTemplateUtil.findByG_S(groupId, structureId);
323 }
324
325 public List getStructureTemplates(
326 long groupId, String structureId, int begin, int end)
327 throws SystemException {
328
329 return JournalTemplateUtil.findByG_S(groupId, structureId, begin, end);
330 }
331
332 public int getStructureTemplatesCount(long groupId, String structureId)
333 throws SystemException {
334
335 return JournalTemplateUtil.countByG_S(groupId, structureId);
336 }
337
338 public JournalTemplate getTemplate(long id)
339 throws PortalException, SystemException {
340
341 return JournalTemplateUtil.findByPrimaryKey(id);
342 }
343
344 public JournalTemplate getTemplate(long groupId, String templateId)
345 throws PortalException, SystemException {
346
347 templateId = GetterUtil.getString(templateId).toUpperCase();
348
349 if (groupId == 0) {
350 _log.error(
351 "No group id was passed for " + templateId + ". Group id is " +
352 "required since 4.2.0. Please update all custom code and " +
353 "data that references templates without a group id.");
354
355 List templates = JournalTemplateUtil.findByTemplateId(templateId);
356
357 if (templates.size() == 0) {
358 throw new NoSuchTemplateException(
359 "No JournalTemplate exists with the template id " +
360 templateId);
361 }
362 else {
363 return (JournalTemplate)templates.get(0);
364 }
365 }
366 else {
367 return JournalTemplateUtil.findByG_T(groupId, templateId);
368 }
369 }
370
371 public List getTemplates() throws SystemException {
372 return JournalTemplateUtil.findAll();
373 }
374
375 public List getTemplates(long groupId) throws SystemException {
376 return JournalTemplateUtil.findByGroupId(groupId);
377 }
378
379 public List getTemplates(long groupId, int begin, int end)
380 throws SystemException {
381
382 return JournalTemplateUtil.findByGroupId(groupId, begin, end);
383 }
384
385 public int getTemplatesCount(long groupId) throws SystemException {
386 return JournalTemplateUtil.countByGroupId(groupId);
387 }
388
389 public boolean hasTemplate(long groupId, String templateId)
390 throws SystemException {
391
392 try {
393 getTemplate(groupId, templateId);
394
395 return true;
396 }
397 catch (PortalException pe) {
398 return false;
399 }
400 }
401
402 public List search(
403 long companyId, long groupId, String keywords, String structureId,
404 String structureIdComparator, int begin, int end,
405 OrderByComparator obc)
406 throws SystemException {
407
408 return JournalTemplateFinder.findByKeywords(
409 companyId, groupId, keywords, structureId, structureIdComparator,
410 begin, end, obc);
411 }
412
413 public List search(
414 long companyId, long groupId, String templateId, String structureId,
415 String structureIdComparator, String name, String description,
416 boolean andOperator, int begin, int end, OrderByComparator obc)
417 throws SystemException {
418
419 return JournalTemplateFinder.findByC_G_T_S_N_D(
420 companyId, groupId, templateId, structureId, structureIdComparator,
421 name, description, andOperator, begin, end, obc);
422 }
423
424 public int searchCount(
425 long companyId, long groupId, String keywords, String structureId,
426 String structureIdComparator)
427 throws SystemException {
428
429 return JournalTemplateFinder.countByKeywords(
430 companyId, groupId, keywords, structureId, structureIdComparator);
431 }
432
433 public int searchCount(
434 long companyId, long groupId, String templateId, String structureId,
435 String structureIdComparator, String name, String description,
436 boolean andOperator)
437 throws SystemException {
438
439 return JournalTemplateFinder.countByC_G_T_S_N_D(
440 companyId, groupId, templateId, structureId, structureIdComparator,
441 name, description, andOperator);
442 }
443
444 public JournalTemplate updateTemplate(
445 long groupId, String templateId, String structureId, String name,
446 String description, String xsl, boolean formatXsl, String langType,
447 boolean smallImage, String smallImageURL, File smallFile)
448 throws PortalException, SystemException {
449
450
452 templateId = templateId.trim().toUpperCase();
453
454 try {
455 if (formatXsl) {
456 if (langType.equals(JournalTemplateImpl.LANG_TYPE_VM)) {
457 xsl = JournalUtil.formatVM(xsl);
458 }
459 else {
460 xsl = JournalUtil.formatXML(xsl);
461 }
462 }
463 }
464 catch (DocumentException de) {
465 throw new TemplateXslException();
466 }
467 catch (IOException ioe) {
468 throw new TemplateXslException();
469 }
470
471 byte[] smallBytes = null;
472
473 try {
474 smallBytes = FileUtil.getBytes(smallFile);
475 }
476 catch (IOException ioe) {
477 }
478
479 validate(
480 name, description, xsl, smallImage, smallImageURL, smallFile,
481 smallBytes);
482
483 JournalTemplate template = JournalTemplateUtil.findByG_T(
484 groupId, templateId);
485
486 template.setModifiedDate(new Date());
487
488 if (Validator.isNull(template.getStructureId()) &&
489 Validator.isNotNull(structureId)) {
490
491
496 template.setStructureId(structureId);
497 }
498
499 template.setName(name);
500 template.setDescription(description);
501 template.setXsl(xsl);
502 template.setLangType(langType);
503 template.setSmallImage(smallImage);
504 template.setSmallImageURL(smallImageURL);
505
506 JournalTemplateUtil.update(template);
507
508
510 saveImages(
511 smallImage, template.getSmallImageId(), smallFile, smallBytes);
512
513 return template;
514 }
515
516 protected void saveImages(
517 boolean smallImage, long smallImageId, File smallFile,
518 byte[] smallBytes)
519 throws SystemException {
520
521 if (smallImage) {
522 if ((smallFile != null) && (smallBytes != null)) {
523 ImageLocalUtil.updateImage(smallImageId, smallBytes);
524 }
525 }
526 else {
527 ImageLocalUtil.deleteImage(smallImageId);
528 }
529 }
530
531 protected void validate(
532 long groupId, String templateId, boolean autoTemplateId,
533 String name, String description, String xsl, boolean smallImage,
534 String smallImageURL, File smallFile, byte[] smallBytes)
535 throws PortalException, SystemException {
536
537 if (!autoTemplateId) {
538 if ((Validator.isNull(templateId)) ||
539 (Validator.isNumber(templateId)) ||
540 (templateId.indexOf(StringPool.SPACE) != -1)) {
541
542 throw new TemplateIdException();
543 }
544
545 try {
546 JournalTemplateUtil.findByG_T(groupId, templateId);
547
548 throw new DuplicateTemplateIdException();
549 }
550 catch (NoSuchTemplateException nste) {
551 }
552 }
553
554 validate(
555 name, description, xsl, smallImage, smallImageURL, smallFile,
556 smallBytes);
557 }
558
559 protected void validate(
560 String name, String description, String xsl, boolean smallImage,
561 String smallImageURL, File smallFile, byte[] smallBytes)
562 throws PortalException {
563
564 if (Validator.isNull(name)) {
565 throw new TemplateNameException();
566 }
567 else if (Validator.isNull(description)) {
568 throw new TemplateDescriptionException();
569 }
570 else if (Validator.isNull(xsl)) {
571 throw new TemplateXslException();
572 }
573
574 String[] imageExtensions =
575 PropsUtil.getArray(PropsUtil.JOURNAL_IMAGE_EXTENSIONS);
576
577 if (smallImage && Validator.isNull(smallImageURL) &&
578 smallFile != null && smallBytes != null) {
579
580 String smallImageName = smallFile.getName();
581
582 if (smallImageName != null) {
583 boolean validSmallImageExtension = false;
584
585 for (int i = 0; i < imageExtensions.length; i++) {
586 if (StringPool.STAR.equals(imageExtensions[i]) ||
587 StringUtil.endsWith(
588 smallImageName, imageExtensions[i])) {
589
590 validSmallImageExtension = true;
591
592 break;
593 }
594 }
595
596 if (!validSmallImageExtension) {
597 throw new TemplateSmallImageNameException(smallImageName);
598 }
599 }
600
601 long smallImageMaxSize = GetterUtil.getLong(
602 PropsUtil.get(PropsUtil.JOURNAL_IMAGE_SMALL_MAX_SIZE));
603
604 if ((smallImageMaxSize > 0) &&
605 ((smallBytes == null) ||
606 (smallBytes.length > smallImageMaxSize))) {
607
608 throw new TemplateSmallImageSizeException();
609 }
610 }
611 }
612
613 private static Log _log =
614 LogFactory.getLog(JournalTemplateLocalServiceImpl.class);
615
616 }