001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journal.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.OrderByComparator;
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.User;
031: import com.liferay.portal.model.impl.ResourceImpl;
032: import com.liferay.portal.service.impl.ImageLocalUtil;
033: import com.liferay.portal.util.PortalUtil;
034: import com.liferay.portal.util.PropsUtil;
035: import com.liferay.portlet.journal.DuplicateTemplateIdException;
036: import com.liferay.portlet.journal.NoSuchTemplateException;
037: import com.liferay.portlet.journal.RequiredTemplateException;
038: import com.liferay.portlet.journal.TemplateDescriptionException;
039: import com.liferay.portlet.journal.TemplateIdException;
040: import com.liferay.portlet.journal.TemplateNameException;
041: import com.liferay.portlet.journal.TemplateSmallImageNameException;
042: import com.liferay.portlet.journal.TemplateSmallImageSizeException;
043: import com.liferay.portlet.journal.TemplateXslException;
044: import com.liferay.portlet.journal.model.JournalTemplate;
045: import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
046: import com.liferay.portlet.journal.service.base.JournalTemplateLocalServiceBaseImpl;
047: import com.liferay.portlet.journal.util.JournalUtil;
048: import com.liferay.util.FileUtil;
049:
050: import java.io.File;
051: import java.io.IOException;
052:
053: import java.util.Date;
054: import java.util.Iterator;
055: import java.util.List;
056:
057: import org.apache.commons.logging.Log;
058: import org.apache.commons.logging.LogFactory;
059:
060: import org.dom4j.DocumentException;
061:
062: /**
063: * <a href="JournalTemplateLocalServiceImpl.java.html"><b><i>View Source</i></b>
064: * </a>
065: *
066: * @author Brian Wing Shun Chan
067: *
068: */
069: public class JournalTemplateLocalServiceImpl extends
070: JournalTemplateLocalServiceBaseImpl {
071:
072: public JournalTemplate addTemplate(long userId, String templateId,
073: boolean autoTemplateId, long plid, String structureId,
074: String name, String description, String xsl,
075: boolean formatXsl, String langType, boolean cacheable,
076: boolean smallImage, String smallImageURL, File smallFile,
077: boolean addCommunityPermissions, boolean addGuestPermissions)
078: throws PortalException, SystemException {
079:
080: return addTemplate(null, userId, templateId, autoTemplateId,
081: plid, structureId, name, description, xsl, formatXsl,
082: langType, cacheable, smallImage, smallImageURL,
083: smallFile, Boolean.valueOf(addCommunityPermissions),
084: Boolean.valueOf(addGuestPermissions), null, null);
085: }
086:
087: public JournalTemplate addTemplate(String uuid, long userId,
088: String templateId, boolean autoTemplateId, long plid,
089: String structureId, String name, String description,
090: String xsl, boolean formatXsl, String langType,
091: boolean cacheable, boolean smallImage,
092: String smallImageURL, File smallFile,
093: boolean addCommunityPermissions, boolean addGuestPermissions)
094: throws PortalException, SystemException {
095:
096: return addTemplate(uuid, userId, templateId, autoTemplateId,
097: plid, structureId, name, description, xsl, formatXsl,
098: langType, cacheable, smallImage, smallImageURL,
099: smallFile, Boolean.valueOf(addCommunityPermissions),
100: Boolean.valueOf(addGuestPermissions), null, null);
101: }
102:
103: public JournalTemplate addTemplate(long userId, String templateId,
104: boolean autoTemplateId, long plid, String structureId,
105: String name, String description, String xsl,
106: boolean formatXsl, String langType, boolean cacheable,
107: boolean smallImage, String smallImageURL, File smallFile,
108: String[] communityPermissions, String[] guestPermissions)
109: throws PortalException, SystemException {
110:
111: return addTemplate(null, userId, templateId, autoTemplateId,
112: plid, structureId, name, description, xsl, formatXsl,
113: langType, cacheable, smallImage, smallImageURL,
114: smallFile, null, null, communityPermissions,
115: guestPermissions);
116: }
117:
118: public JournalTemplate addTemplate(String uuid, long userId,
119: String templateId, boolean autoTemplateId, long plid,
120: String structureId, String name, String description,
121: String xsl, boolean formatXsl, String langType,
122: boolean cacheable, boolean smallImage,
123: String smallImageURL, File smallFile,
124: Boolean addCommunityPermissions,
125: Boolean addGuestPermissions, String[] communityPermissions,
126: String[] guestPermissions) throws PortalException,
127: SystemException {
128:
129: long groupId = PortalUtil.getPortletGroupId(plid);
130:
131: return addTemplateToGroup(uuid, userId, templateId,
132: autoTemplateId, groupId, structureId, name,
133: description, xsl, formatXsl, langType, cacheable,
134: smallImage, smallImageURL, smallFile,
135: addCommunityPermissions, addGuestPermissions,
136: communityPermissions, guestPermissions);
137: }
138:
139: public JournalTemplate addTemplateToGroup(String uuid, long userId,
140: String templateId, boolean autoTemplateId, long groupId,
141: String structureId, String name, String description,
142: String xsl, boolean formatXsl, String langType,
143: boolean cacheable, boolean smallImage,
144: String smallImageURL, File smallFile,
145: Boolean addCommunityPermissions,
146: Boolean addGuestPermissions, String[] communityPermissions,
147: String[] guestPermissions) throws PortalException,
148: SystemException {
149:
150: // Template
151:
152: User user = userPersistence.findByPrimaryKey(userId);
153: templateId = templateId.trim().toUpperCase();
154: Date now = new Date();
155:
156: try {
157: if (formatXsl) {
158: if (langType.equals(JournalTemplateImpl.LANG_TYPE_VM)) {
159: xsl = JournalUtil.formatVM(xsl);
160: } else {
161: xsl = JournalUtil.formatXML(xsl);
162: }
163: }
164: } catch (DocumentException de) {
165: throw new TemplateXslException();
166: } catch (IOException ioe) {
167: throw new TemplateXslException();
168: }
169:
170: byte[] smallBytes = null;
171:
172: try {
173: smallBytes = FileUtil.getBytes(smallFile);
174: } catch (IOException ioe) {
175: }
176:
177: validate(groupId, templateId, autoTemplateId, name,
178: description, xsl, smallImage, smallImageURL, smallFile,
179: smallBytes);
180:
181: if (autoTemplateId) {
182: templateId = String
183: .valueOf(counterLocalService.increment());
184: }
185:
186: long id = counterLocalService.increment();
187:
188: JournalTemplate template = journalTemplatePersistence
189: .create(id);
190:
191: template.setUuid(uuid);
192: template.setGroupId(groupId);
193: template.setCompanyId(user.getCompanyId());
194: template.setUserId(user.getUserId());
195: template.setUserName(user.getFullName());
196: template.setCreateDate(now);
197: template.setModifiedDate(now);
198: template.setTemplateId(templateId);
199: template.setStructureId(structureId);
200: template.setName(name);
201: template.setDescription(description);
202: template.setXsl(xsl);
203: template.setLangType(langType);
204: template.setCacheable(cacheable);
205: template.setSmallImage(smallImage);
206: template.setSmallImageId(counterLocalService.increment());
207: template.setSmallImageURL(smallImageURL);
208:
209: journalTemplatePersistence.update(template);
210:
211: // Small image
212:
213: saveImages(smallImage, template.getSmallImageId(), smallFile,
214: smallBytes);
215:
216: // Resources
217:
218: if ((addCommunityPermissions != null)
219: && (addGuestPermissions != null)) {
220:
221: addTemplateResources(template, addCommunityPermissions
222: .booleanValue(), addGuestPermissions.booleanValue());
223: } else {
224: addTemplateResources(template, communityPermissions,
225: guestPermissions);
226: }
227:
228: return template;
229: }
230:
231: public void addTemplateResources(long groupId, String templateId,
232: boolean addCommunityPermissions, boolean addGuestPermissions)
233: throws PortalException, SystemException {
234:
235: JournalTemplate template = journalTemplatePersistence
236: .findByG_T(groupId, templateId);
237:
238: addTemplateResources(template, addCommunityPermissions,
239: addGuestPermissions);
240: }
241:
242: public void addTemplateResources(JournalTemplate template,
243: boolean addCommunityPermissions, boolean addGuestPermissions)
244: throws PortalException, SystemException {
245:
246: resourceLocalService.addResources(template.getCompanyId(),
247: template.getGroupId(), template.getUserId(),
248: JournalTemplate.class.getName(), template.getId(),
249: false, addCommunityPermissions, addGuestPermissions);
250: }
251:
252: public void addTemplateResources(long groupId, String templateId,
253: String[] communityPermissions, String[] guestPermissions)
254: throws PortalException, SystemException {
255:
256: JournalTemplate template = journalTemplatePersistence
257: .findByG_T(groupId, templateId);
258:
259: addTemplateResources(template, communityPermissions,
260: guestPermissions);
261: }
262:
263: public void addTemplateResources(JournalTemplate template,
264: String[] communityPermissions, String[] guestPermissions)
265: throws PortalException, SystemException {
266:
267: resourceLocalService.addModelResources(template.getCompanyId(),
268: template.getGroupId(), template.getUserId(),
269: JournalTemplate.class.getName(), template.getId(),
270: communityPermissions, guestPermissions);
271: }
272:
273: public void checkNewLine(long groupId, String templateId)
274: throws PortalException, SystemException {
275:
276: JournalTemplate template = journalTemplatePersistence
277: .findByG_T(groupId, templateId);
278:
279: String xsl = template.getXsl();
280:
281: if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
282: xsl = StringUtil.replace(xsl,
283: new String[] { "\\n", "\\r" }, new String[] { "\n",
284: "\r" });
285:
286: template.setXsl(xsl);
287:
288: journalTemplatePersistence.update(template);
289: }
290: }
291:
292: public void deleteTemplate(long groupId, String templateId)
293: throws PortalException, SystemException {
294:
295: templateId = templateId.trim().toUpperCase();
296:
297: JournalTemplate template = journalTemplatePersistence
298: .findByG_T(groupId, templateId);
299:
300: deleteTemplate(template);
301: }
302:
303: public void deleteTemplate(JournalTemplate template)
304: throws PortalException, SystemException {
305:
306: if (journalArticlePersistence.countByG_T(template.getGroupId(),
307: template.getTemplateId()) > 0) {
308:
309: throw new RequiredTemplateException();
310: }
311:
312: // Small image
313:
314: ImageLocalUtil.deleteImage(template.getSmallImageId());
315:
316: // Resources
317:
318: resourceLocalService.deleteResource(template.getCompanyId(),
319: JournalTemplate.class.getName(),
320: ResourceImpl.SCOPE_INDIVIDUAL, template.getId());
321:
322: // WebDAVProps
323:
324: webDAVPropsLocalService.deleteWebDAVProps(JournalTemplate.class
325: .getName(), template.getPrimaryKey());
326:
327: // Template
328:
329: journalTemplatePersistence.remove(template.getPrimaryKey());
330: }
331:
332: public void deleteTemplates(long groupId) throws PortalException,
333: SystemException {
334:
335: Iterator itr = journalTemplatePersistence
336: .findByGroupId(groupId).iterator();
337:
338: while (itr.hasNext()) {
339: JournalTemplate template = (JournalTemplate) itr.next();
340:
341: deleteTemplate(template);
342: }
343: }
344:
345: public List getStructureTemplates(long groupId, String structureId)
346: throws SystemException {
347:
348: return journalTemplatePersistence.findByG_S(groupId,
349: structureId);
350: }
351:
352: public List getStructureTemplates(long groupId, String structureId,
353: int begin, int end) throws SystemException {
354:
355: return journalTemplatePersistence.findByG_S(groupId,
356: structureId, begin, end);
357: }
358:
359: public int getStructureTemplatesCount(long groupId,
360: String structureId) throws SystemException {
361:
362: return journalTemplatePersistence.countByG_S(groupId,
363: structureId);
364: }
365:
366: public JournalTemplate getTemplate(long id) throws PortalException,
367: SystemException {
368:
369: return journalTemplatePersistence.findByPrimaryKey(id);
370: }
371:
372: public JournalTemplate getTemplate(long groupId, String templateId)
373: throws PortalException, SystemException {
374:
375: templateId = GetterUtil.getString(templateId).toUpperCase();
376:
377: if (groupId == 0) {
378: _log
379: .error("No group id was passed for "
380: + templateId
381: + ". Group id is "
382: + "required since 4.2.0. Please update all custom code and "
383: + "data that references templates without a group id.");
384:
385: List templates = journalTemplatePersistence
386: .findByTemplateId(templateId);
387:
388: if (templates.size() == 0) {
389: throw new NoSuchTemplateException(
390: "No JournalTemplate exists with the template id "
391: + templateId);
392: } else {
393: return (JournalTemplate) templates.get(0);
394: }
395: } else {
396: return journalTemplatePersistence.findByG_T(groupId,
397: templateId);
398: }
399: }
400:
401: public JournalTemplate getTemplateBySmallImageId(long smallImageId)
402: throws PortalException, SystemException {
403:
404: return journalTemplatePersistence
405: .findBySmallImageId(smallImageId);
406: }
407:
408: public List getTemplates() throws SystemException {
409: return journalTemplatePersistence.findAll();
410: }
411:
412: public List getTemplates(long groupId) throws SystemException {
413: return journalTemplatePersistence.findByGroupId(groupId);
414: }
415:
416: public List getTemplates(long groupId, int begin, int end)
417: throws SystemException {
418:
419: return journalTemplatePersistence.findByGroupId(groupId, begin,
420: end);
421: }
422:
423: public int getTemplatesCount(long groupId) throws SystemException {
424: return journalTemplatePersistence.countByGroupId(groupId);
425: }
426:
427: public boolean hasTemplate(long groupId, String templateId)
428: throws SystemException {
429:
430: try {
431: getTemplate(groupId, templateId);
432:
433: return true;
434: } catch (PortalException pe) {
435: return false;
436: }
437: }
438:
439: public List search(long companyId, long groupId, String keywords,
440: String structureId, String structureIdComparator,
441: int begin, int end, OrderByComparator obc)
442: throws SystemException {
443:
444: return journalTemplateFinder.findByKeywords(companyId, groupId,
445: keywords, structureId, structureIdComparator, begin,
446: end, obc);
447: }
448:
449: public List search(long companyId, long groupId, String templateId,
450: String structureId, String structureIdComparator,
451: String name, String description, boolean andOperator,
452: int begin, int end, OrderByComparator obc)
453: throws SystemException {
454:
455: return journalTemplateFinder.findByC_G_T_S_N_D(companyId,
456: groupId, templateId, structureId,
457: structureIdComparator, name, description, andOperator,
458: begin, end, obc);
459: }
460:
461: public int searchCount(long companyId, long groupId,
462: String keywords, String structureId,
463: String structureIdComparator) throws SystemException {
464:
465: return journalTemplateFinder.countByKeywords(companyId,
466: groupId, keywords, structureId, structureIdComparator);
467: }
468:
469: public int searchCount(long companyId, long groupId,
470: String templateId, String structureId,
471: String structureIdComparator, String name,
472: String description, boolean andOperator)
473: throws SystemException {
474:
475: return journalTemplateFinder.countByC_G_T_S_N_D(companyId,
476: groupId, templateId, structureId,
477: structureIdComparator, name, description, andOperator);
478: }
479:
480: public JournalTemplate updateTemplate(long groupId,
481: String templateId, String structureId, String name,
482: String description, String xsl, boolean formatXsl,
483: String langType, boolean cacheable, boolean smallImage,
484: String smallImageURL, File smallFile)
485: throws PortalException, SystemException {
486:
487: // Template
488:
489: templateId = templateId.trim().toUpperCase();
490:
491: try {
492: if (formatXsl) {
493: if (langType.equals(JournalTemplateImpl.LANG_TYPE_VM)) {
494: xsl = JournalUtil.formatVM(xsl);
495: } else {
496: xsl = JournalUtil.formatXML(xsl);
497: }
498: }
499: } catch (DocumentException de) {
500: throw new TemplateXslException();
501: } catch (IOException ioe) {
502: throw new TemplateXslException();
503: }
504:
505: byte[] smallBytes = null;
506:
507: try {
508: smallBytes = FileUtil.getBytes(smallFile);
509: } catch (IOException ioe) {
510: }
511:
512: validate(name, description, xsl, smallImage, smallImageURL,
513: smallFile, smallBytes);
514:
515: JournalTemplate template = journalTemplatePersistence
516: .findByG_T(groupId, templateId);
517:
518: template.setModifiedDate(new Date());
519:
520: if (Validator.isNull(template.getStructureId())
521: && Validator.isNotNull(structureId)) {
522:
523: // Allow users to set the structure if and only if it currently
524: // does not have one. Otherwise, you can have bad data because there
525: // may be an existing article that has chosen to use a structure and
526: // template combination that no longer exists.
527:
528: template.setStructureId(structureId);
529: }
530:
531: template.setName(name);
532: template.setDescription(description);
533: template.setXsl(xsl);
534: template.setLangType(langType);
535: template.setCacheable(cacheable);
536: template.setSmallImage(smallImage);
537: template.setSmallImageURL(smallImageURL);
538:
539: journalTemplatePersistence.update(template);
540:
541: // Small image
542:
543: saveImages(smallImage, template.getSmallImageId(), smallFile,
544: smallBytes);
545:
546: return template;
547: }
548:
549: protected void saveImages(boolean smallImage, long smallImageId,
550: File smallFile, byte[] smallBytes) throws SystemException {
551:
552: if (smallImage) {
553: if ((smallFile != null) && (smallBytes != null)) {
554: ImageLocalUtil.updateImage(smallImageId, smallBytes);
555: }
556: } else {
557: ImageLocalUtil.deleteImage(smallImageId);
558: }
559: }
560:
561: protected void validate(long groupId, String templateId,
562: boolean autoTemplateId, String name, String description,
563: String xsl, boolean smallImage, String smallImageURL,
564: File smallFile, byte[] smallBytes) throws PortalException,
565: SystemException {
566:
567: if (!autoTemplateId) {
568: if ((Validator.isNull(templateId))
569: || (Validator.isNumber(templateId))
570: || (templateId.indexOf(StringPool.SPACE) != -1)) {
571:
572: throw new TemplateIdException();
573: }
574:
575: try {
576: journalTemplatePersistence.findByG_T(groupId,
577: templateId);
578:
579: throw new DuplicateTemplateIdException();
580: } catch (NoSuchTemplateException nste) {
581: }
582: }
583:
584: validate(name, description, xsl, smallImage, smallImageURL,
585: smallFile, smallBytes);
586: }
587:
588: protected void validate(String name, String description,
589: String xsl, boolean smallImage, String smallImageURL,
590: File smallFile, byte[] smallBytes) throws PortalException {
591:
592: if (Validator.isNull(name)) {
593: throw new TemplateNameException();
594: } else if (Validator.isNull(description)) {
595: throw new TemplateDescriptionException();
596: } else if (Validator.isNull(xsl)) {
597: throw new TemplateXslException();
598: }
599:
600: String[] imageExtensions = PropsUtil
601: .getArray(PropsUtil.JOURNAL_IMAGE_EXTENSIONS);
602:
603: if (smallImage && Validator.isNull(smallImageURL)
604: && smallFile != null && smallBytes != null) {
605:
606: String smallImageName = smallFile.getName();
607:
608: if (smallImageName != null) {
609: boolean validSmallImageExtension = false;
610:
611: for (int i = 0; i < imageExtensions.length; i++) {
612: if (StringPool.STAR.equals(imageExtensions[i])
613: || StringUtil.endsWith(smallImageName,
614: imageExtensions[i])) {
615:
616: validSmallImageExtension = true;
617:
618: break;
619: }
620: }
621:
622: if (!validSmallImageExtension) {
623: throw new TemplateSmallImageNameException(
624: smallImageName);
625: }
626: }
627:
628: long smallImageMaxSize = GetterUtil.getLong(PropsUtil
629: .get(PropsUtil.JOURNAL_IMAGE_SMALL_MAX_SIZE));
630:
631: if ((smallImageMaxSize > 0)
632: && ((smallBytes == null) || (smallBytes.length > smallImageMaxSize))) {
633:
634: throw new TemplateSmallImageSizeException();
635: }
636: }
637: }
638:
639: private static Log _log = LogFactory
640: .getLog(JournalTemplateLocalServiceImpl.class);
641:
642: }
|