0001: /**
0002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
0003: *
0004: * Permission is hereby granted, free of charge, to any person obtaining a copy
0005: * of this software and associated documentation files (the "Software"), to deal
0006: * in the Software without restriction, including without limitation the rights
0007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008: * copies of the Software, and to permit persons to whom the Software is
0009: * furnished to do so, subject to the following conditions:
0010: *
0011: * The above copyright notice and this permission notice shall be included in
0012: * all copies or substantial portions of the Software.
0013: *
0014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0020: * SOFTWARE.
0021: */package com.liferay.portlet.journal.service.impl;
0022:
0023: import com.liferay.portal.PortalException;
0024: import com.liferay.portal.SystemException;
0025: import com.liferay.portal.kernel.mail.MailMessage;
0026: import com.liferay.portal.kernel.search.Hits;
0027: import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
0028: import com.liferay.portal.kernel.util.ContentTypes;
0029: import com.liferay.portal.kernel.util.GetterUtil;
0030: import com.liferay.portal.kernel.util.LocaleUtil;
0031: import com.liferay.portal.kernel.util.OrderByComparator;
0032: import com.liferay.portal.kernel.util.StringMaker;
0033: import com.liferay.portal.kernel.util.StringPool;
0034: import com.liferay.portal.kernel.util.StringUtil;
0035: import com.liferay.portal.kernel.util.Validator;
0036: import com.liferay.portal.lucene.LuceneFields;
0037: import com.liferay.portal.lucene.LuceneUtil;
0038: import com.liferay.portal.model.Company;
0039: import com.liferay.portal.model.Image;
0040: import com.liferay.portal.model.User;
0041: import com.liferay.portal.model.impl.ResourceImpl;
0042: import com.liferay.portal.service.impl.ImageLocalUtil;
0043: import com.liferay.portal.servlet.filters.layoutcache.LayoutCacheUtil;
0044: import com.liferay.portal.theme.ThemeDisplay;
0045: import com.liferay.portal.util.PortalUtil;
0046: import com.liferay.portal.util.PortletKeys;
0047: import com.liferay.portal.util.PropsUtil;
0048: import com.liferay.portlet.journal.ArticleContentException;
0049: import com.liferay.portlet.journal.ArticleDisplayDateException;
0050: import com.liferay.portlet.journal.ArticleExpirationDateException;
0051: import com.liferay.portlet.journal.ArticleIdException;
0052: import com.liferay.portlet.journal.ArticleReviewDateException;
0053: import com.liferay.portlet.journal.ArticleSmallImageNameException;
0054: import com.liferay.portlet.journal.ArticleSmallImageSizeException;
0055: import com.liferay.portlet.journal.ArticleTitleException;
0056: import com.liferay.portlet.journal.ArticleTypeException;
0057: import com.liferay.portlet.journal.DuplicateArticleIdException;
0058: import com.liferay.portlet.journal.NoSuchArticleException;
0059: import com.liferay.portlet.journal.NoSuchArticleResourceException;
0060: import com.liferay.portlet.journal.NoSuchTemplateException;
0061: import com.liferay.portlet.journal.StructureXsdException;
0062: import com.liferay.portlet.journal.job.CheckArticleJob;
0063: import com.liferay.portlet.journal.model.JournalArticle;
0064: import com.liferay.portlet.journal.model.JournalArticleDisplay;
0065: import com.liferay.portlet.journal.model.JournalStructure;
0066: import com.liferay.portlet.journal.model.JournalTemplate;
0067: import com.liferay.portlet.journal.model.impl.JournalArticleDisplayImpl;
0068: import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
0069: import com.liferay.portlet.journal.service.base.JournalArticleLocalServiceBaseImpl;
0070: import com.liferay.portlet.journal.util.Indexer;
0071: import com.liferay.portlet.journal.util.JournalUtil;
0072: import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
0073: import com.liferay.portlet.journalcontent.util.JournalContentUtil;
0074: import com.liferay.util.FileUtil;
0075: import com.liferay.util.Html;
0076: import com.liferay.util.LocalizationUtil;
0077: import com.liferay.util.MathUtil;
0078: import com.liferay.util.lucene.HitsImpl;
0079:
0080: import java.io.File;
0081: import java.io.IOException;
0082: import java.io.StringReader;
0083:
0084: import java.util.Collections;
0085: import java.util.Date;
0086: import java.util.HashSet;
0087: import java.util.Iterator;
0088: import java.util.List;
0089: import java.util.Map;
0090: import java.util.Set;
0091:
0092: import javax.mail.internet.InternetAddress;
0093:
0094: import javax.portlet.PortletPreferences;
0095:
0096: import org.apache.commons.logging.Log;
0097: import org.apache.commons.logging.LogFactory;
0098: import org.apache.lucene.index.IndexWriter;
0099: import org.apache.lucene.search.BooleanClause;
0100: import org.apache.lucene.search.BooleanQuery;
0101: import org.apache.lucene.search.Searcher;
0102: import org.apache.lucene.search.Sort;
0103: import org.apache.lucene.search.SortField;
0104:
0105: import org.dom4j.Document;
0106: import org.dom4j.DocumentException;
0107: import org.dom4j.DocumentFactory;
0108: import org.dom4j.DocumentHelper;
0109: import org.dom4j.Element;
0110: import org.dom4j.XPath;
0111: import org.dom4j.io.SAXReader;
0112:
0113: /**
0114: * <a href="JournalArticleLocalServiceImpl.java.html"><b><i>View Source</i></b>
0115: * </a>
0116: *
0117: * @author Brian Wing Shun Chan
0118: *
0119: */
0120: public class JournalArticleLocalServiceImpl extends
0121: JournalArticleLocalServiceBaseImpl {
0122:
0123: public JournalArticle addArticle(long userId, String articleId,
0124: boolean autoArticleId, long plid, String title,
0125: String description, String content, String type,
0126: String structureId, String templateId,
0127: int displayDateMonth, int displayDateDay,
0128: int displayDateYear, int displayDateHour,
0129: int displayDateMinute, int expirationDateMonth,
0130: int expirationDateDay, int expirationDateYear,
0131: int expirationDateHour, int expirationDateMinute,
0132: boolean neverExpire, int reviewDateMonth,
0133: int reviewDateDay, int reviewDateYear, int reviewDateHour,
0134: int reviewDateMinute, boolean neverReview,
0135: boolean indexable, boolean smallImage,
0136: String smallImageURL, File smallFile, Map images,
0137: String articleURL, PortletPreferences prefs,
0138: String[] tagsEntries, boolean addCommunityPermissions,
0139: boolean addGuestPermissions) throws PortalException,
0140: SystemException {
0141:
0142: return addArticle(null, userId, articleId, autoArticleId, plid,
0143: title, description, content, type, structureId,
0144: templateId, displayDateMonth, displayDateDay,
0145: displayDateYear, displayDateHour, displayDateMinute,
0146: expirationDateMonth, expirationDateDay,
0147: expirationDateYear, expirationDateHour,
0148: expirationDateMinute, neverExpire, reviewDateMonth,
0149: reviewDateDay, reviewDateYear, reviewDateHour,
0150: reviewDateMinute, neverReview, indexable, smallImage,
0151: smallImageURL, smallFile, images, articleURL, prefs,
0152: tagsEntries, Boolean.valueOf(addCommunityPermissions),
0153: Boolean.valueOf(addGuestPermissions), null, null);
0154: }
0155:
0156: public JournalArticle addArticle(String uuid, long userId,
0157: String articleId, boolean autoArticleId, long plid,
0158: String title, String description, String content,
0159: String type, String structureId, String templateId,
0160: int displayDateMonth, int displayDateDay,
0161: int displayDateYear, int displayDateHour,
0162: int displayDateMinute, int expirationDateMonth,
0163: int expirationDateDay, int expirationDateYear,
0164: int expirationDateHour, int expirationDateMinute,
0165: boolean neverExpire, int reviewDateMonth,
0166: int reviewDateDay, int reviewDateYear, int reviewDateHour,
0167: int reviewDateMinute, boolean neverReview,
0168: boolean indexable, boolean smallImage,
0169: String smallImageURL, File smallFile, Map images,
0170: String articleURL, PortletPreferences prefs,
0171: String[] tagsEntries, boolean addCommunityPermissions,
0172: boolean addGuestPermissions) throws PortalException,
0173: SystemException {
0174:
0175: return addArticle(uuid, userId, articleId, autoArticleId, plid,
0176: title, description, content, type, structureId,
0177: templateId, displayDateMonth, displayDateDay,
0178: displayDateYear, displayDateHour, displayDateMinute,
0179: expirationDateMonth, expirationDateDay,
0180: expirationDateYear, expirationDateHour,
0181: expirationDateMinute, neverExpire, reviewDateMonth,
0182: reviewDateDay, reviewDateYear, reviewDateHour,
0183: reviewDateMinute, neverReview, indexable, smallImage,
0184: smallImageURL, smallFile, images, articleURL, prefs,
0185: tagsEntries, Boolean.valueOf(addCommunityPermissions),
0186: Boolean.valueOf(addGuestPermissions), null, null);
0187: }
0188:
0189: public JournalArticle addArticle(long userId, String articleId,
0190: boolean autoArticleId, long plid, String title,
0191: String description, String content, String type,
0192: String structureId, String templateId,
0193: int displayDateMonth, int displayDateDay,
0194: int displayDateYear, int displayDateHour,
0195: int displayDateMinute, int expirationDateMonth,
0196: int expirationDateDay, int expirationDateYear,
0197: int expirationDateHour, int expirationDateMinute,
0198: boolean neverExpire, int reviewDateMonth,
0199: int reviewDateDay, int reviewDateYear, int reviewDateHour,
0200: int reviewDateMinute, boolean neverReview,
0201: boolean indexable, boolean smallImage,
0202: String smallImageURL, File smallFile, Map images,
0203: String articleURL, PortletPreferences prefs,
0204: String[] tagsEntries, String[] communityPermissions,
0205: String[] guestPermissions) throws PortalException,
0206: SystemException {
0207:
0208: return addArticle(null, userId, articleId, autoArticleId, plid,
0209: title, description, content, type, structureId,
0210: templateId, displayDateMonth, displayDateDay,
0211: displayDateYear, displayDateHour, displayDateMinute,
0212: expirationDateMonth, expirationDateDay,
0213: expirationDateYear, expirationDateHour,
0214: expirationDateMinute, neverExpire, reviewDateMonth,
0215: reviewDateDay, reviewDateYear, reviewDateHour,
0216: reviewDateMinute, neverReview, indexable, smallImage,
0217: smallImageURL, smallFile, images, articleURL, prefs,
0218: tagsEntries, null, null, communityPermissions,
0219: guestPermissions);
0220: }
0221:
0222: public JournalArticle addArticle(String uuid, long userId,
0223: String articleId, boolean autoArticleId, long plid,
0224: String title, String description, String content,
0225: String type, String structureId, String templateId,
0226: int displayDateMonth, int displayDateDay,
0227: int displayDateYear, int displayDateHour,
0228: int displayDateMinute, int expirationDateMonth,
0229: int expirationDateDay, int expirationDateYear,
0230: int expirationDateHour, int expirationDateMinute,
0231: boolean neverExpire, int reviewDateMonth,
0232: int reviewDateDay, int reviewDateYear, int reviewDateHour,
0233: int reviewDateMinute, boolean neverReview,
0234: boolean indexable, boolean smallImage,
0235: String smallImageURL, File smallFile, Map images,
0236: String articleURL, PortletPreferences prefs,
0237: String[] tagsEntries, Boolean addCommunityPermissions,
0238: Boolean addGuestPermissions, String[] communityPermissions,
0239: String[] guestPermissions) throws PortalException,
0240: SystemException {
0241:
0242: long groupId = PortalUtil.getPortletGroupId(plid);
0243:
0244: return addArticleToGroup(uuid, userId, articleId,
0245: autoArticleId, groupId, title, description, content,
0246: type, structureId, templateId, displayDateMonth,
0247: displayDateDay, displayDateYear, displayDateHour,
0248: displayDateMinute, expirationDateMonth,
0249: expirationDateDay, expirationDateYear,
0250: expirationDateHour, expirationDateMinute, neverExpire,
0251: reviewDateMonth, reviewDateDay, reviewDateYear,
0252: reviewDateHour, reviewDateMinute, neverReview,
0253: indexable, smallImage, smallImageURL, smallFile,
0254: images, articleURL, prefs, tagsEntries,
0255: addCommunityPermissions, addGuestPermissions,
0256: communityPermissions, guestPermissions);
0257: }
0258:
0259: public JournalArticle addArticleToGroup(String uuid, long userId,
0260: String articleId, boolean autoArticleId, long groupId,
0261: String title, String description, String content,
0262: String type, String structureId, String templateId,
0263: int displayDateMonth, int displayDateDay,
0264: int displayDateYear, int displayDateHour,
0265: int displayDateMinute, int expirationDateMonth,
0266: int expirationDateDay, int expirationDateYear,
0267: int expirationDateHour, int expirationDateMinute,
0268: boolean neverExpire, int reviewDateMonth,
0269: int reviewDateDay, int reviewDateYear, int reviewDateHour,
0270: int reviewDateMinute, boolean neverReview,
0271: boolean indexable, boolean smallImage,
0272: String smallImageURL, File smallFile, Map images,
0273: String articleURL, PortletPreferences prefs,
0274: String[] tagsEntries, Boolean addCommunityPermissions,
0275: Boolean addGuestPermissions, String[] communityPermissions,
0276: String[] guestPermissions) throws PortalException,
0277: SystemException {
0278:
0279: // Article
0280:
0281: User user = userPersistence.findByPrimaryKey(userId);
0282: articleId = articleId.trim().toUpperCase();
0283: Date now = new Date();
0284:
0285: Date displayDate = PortalUtil.getDate(displayDateMonth,
0286: displayDateDay, displayDateYear, displayDateHour,
0287: displayDateMinute, user.getTimeZone(),
0288: new ArticleDisplayDateException());
0289:
0290: Date expirationDate = null;
0291:
0292: if (!neverExpire) {
0293: expirationDate = PortalUtil.getDate(expirationDateMonth,
0294: expirationDateDay, expirationDateYear,
0295: expirationDateHour, expirationDateMinute, user
0296: .getTimeZone(),
0297: new ArticleExpirationDateException());
0298: }
0299:
0300: Date reviewDate = null;
0301:
0302: if (!neverReview) {
0303: reviewDate = PortalUtil.getDate(reviewDateMonth,
0304: reviewDateDay, reviewDateYear, reviewDateHour,
0305: reviewDateMinute, user.getTimeZone(),
0306: new ArticleReviewDateException());
0307: }
0308:
0309: byte[] smallBytes = null;
0310:
0311: try {
0312: smallBytes = FileUtil.getBytes(smallFile);
0313: } catch (IOException ioe) {
0314: }
0315:
0316: validate(groupId, articleId, autoArticleId, title, content,
0317: type, structureId, templateId, smallImage,
0318: smallImageURL, smallFile, smallBytes);
0319:
0320: if (autoArticleId) {
0321: articleId = String.valueOf(counterLocalService.increment());
0322: }
0323:
0324: long id = counterLocalService.increment();
0325:
0326: long resourcePrimKey = journalArticleResourceLocalService
0327: .getArticleResourcePrimKey(groupId, articleId);
0328:
0329: JournalArticle article = journalArticlePersistence.create(id);
0330:
0331: content = format(groupId, articleId, article.getVersion(),
0332: false, content, structureId, images);
0333:
0334: article.setUuid(uuid);
0335: article.setResourcePrimKey(resourcePrimKey);
0336: article.setGroupId(groupId);
0337: article.setCompanyId(user.getCompanyId());
0338: article.setUserId(user.getUserId());
0339: article.setUserName(user.getFullName());
0340: article.setCreateDate(now);
0341: article.setModifiedDate(now);
0342: article.setArticleId(articleId);
0343: article.setVersion(JournalArticleImpl.DEFAULT_VERSION);
0344: article.setTitle(title);
0345: article.setDescription(description);
0346: article.setContent(content);
0347: article.setType(type);
0348: article.setStructureId(structureId);
0349: article.setTemplateId(templateId);
0350: article.setDisplayDate(displayDate);
0351: article.setApproved(false);
0352:
0353: if ((expirationDate == null) || expirationDate.after(now)) {
0354: article.setExpired(false);
0355: } else {
0356: article.setExpired(true);
0357: }
0358:
0359: article.setExpirationDate(expirationDate);
0360: article.setReviewDate(reviewDate);
0361: article.setIndexable(indexable);
0362: article.setSmallImage(smallImage);
0363: article.setSmallImageId(counterLocalService.increment());
0364: article.setSmallImageURL(smallImageURL);
0365:
0366: journalArticlePersistence.update(article);
0367:
0368: // Small image
0369:
0370: saveImages(smallImage, article.getSmallImageId(), smallFile,
0371: smallBytes);
0372:
0373: // Resources
0374:
0375: if ((addCommunityPermissions != null)
0376: && (addGuestPermissions != null)) {
0377:
0378: addArticleResources(article, addCommunityPermissions
0379: .booleanValue(), addGuestPermissions.booleanValue());
0380: } else {
0381: addArticleResources(article, communityPermissions,
0382: guestPermissions);
0383: }
0384:
0385: // Tags
0386:
0387: updateTagsAsset(userId, article, tagsEntries);
0388:
0389: // Email
0390:
0391: sendEmail(article, articleURL, prefs, "requested");
0392:
0393: return article;
0394: }
0395:
0396: public void addArticleResources(long groupId, String articleId,
0397: boolean addCommunityPermissions, boolean addGuestPermissions)
0398: throws PortalException, SystemException {
0399:
0400: JournalArticle article = getLatestArticle(groupId, articleId);
0401:
0402: addArticleResources(article, addCommunityPermissions,
0403: addGuestPermissions);
0404: }
0405:
0406: public void addArticleResources(JournalArticle article,
0407: boolean addCommunityPermissions, boolean addGuestPermissions)
0408: throws PortalException, SystemException {
0409:
0410: resourceLocalService.addResources(article.getCompanyId(),
0411: article.getGroupId(), article.getUserId(),
0412: JournalArticle.class.getName(), article
0413: .getResourcePrimKey(), false,
0414: addCommunityPermissions, addGuestPermissions);
0415: }
0416:
0417: public void addArticleResources(long groupId, String articleId,
0418: String[] communityPermissions, String[] guestPermissions)
0419: throws PortalException, SystemException {
0420:
0421: JournalArticle article = getLatestArticle(groupId, articleId);
0422:
0423: addArticleResources(article, communityPermissions,
0424: guestPermissions);
0425: }
0426:
0427: public void addArticleResources(JournalArticle article,
0428: String[] communityPermissions, String[] guestPermissions)
0429: throws PortalException, SystemException {
0430:
0431: resourceLocalService.addModelResources(article.getCompanyId(),
0432: article.getGroupId(), article.getUserId(),
0433: JournalArticle.class.getName(), article
0434: .getResourcePrimKey(), communityPermissions,
0435: guestPermissions);
0436: }
0437:
0438: public JournalArticle approveArticle(long userId, long groupId,
0439: String articleId, double version, String articleURL,
0440: PortletPreferences prefs) throws PortalException,
0441: SystemException {
0442:
0443: // Article
0444:
0445: User user = userPersistence.findByPrimaryKey(userId);
0446: Date now = new Date();
0447:
0448: JournalArticle article = journalArticlePersistence.findByG_A_V(
0449: groupId, articleId, version);
0450:
0451: article.setModifiedDate(now);
0452: article.setApproved(true);
0453: article.setApprovedByUserId(user.getUserId());
0454: article.setApprovedByUserName(user.getFullName());
0455: article.setApprovedDate(now);
0456:
0457: journalArticlePersistence.update(article);
0458:
0459: // Email
0460:
0461: sendEmail(article, articleURL, prefs, "granted");
0462:
0463: // Lucene
0464:
0465: try {
0466: if (article.isIndexable()) {
0467: String[] tagsEntries = tagsEntryLocalService
0468: .getEntryNames(JournalArticle.class.getName(),
0469: article.getResourcePrimKey());
0470:
0471: Indexer.updateArticle(article.getCompanyId(), article
0472: .getGroupId(), article.getArticleId(), article
0473: .getVersion(), article.getTitle(), article
0474: .getDescription(), article.getContent(),
0475: article.getType(), article.getDisplayDate(),
0476: tagsEntries);
0477: }
0478: } catch (IOException ioe) {
0479: _log.error("Indexing " + article.getId(), ioe);
0480: }
0481:
0482: return article;
0483: }
0484:
0485: public JournalArticle checkArticleResourcePrimKey(long groupId,
0486: String articleId, double version) throws PortalException,
0487: SystemException {
0488:
0489: JournalArticle article = journalArticlePersistence.findByG_A_V(
0490: groupId, articleId, version);
0491:
0492: if (article.getResourcePrimKey() > 0) {
0493: return article;
0494: }
0495:
0496: long resourcePrimKey = journalArticleResourceLocalService
0497: .getArticleResourcePrimKey(groupId, articleId);
0498:
0499: article.setResourcePrimKey(resourcePrimKey);
0500:
0501: journalArticlePersistence.update(article);
0502:
0503: return article;
0504: }
0505:
0506: public void checkArticles() throws PortalException, SystemException {
0507: Date now = new Date();
0508:
0509: List articles = journalArticleFinder.findByExpirationDate(
0510: Boolean.FALSE, now, new Date(now.getTime()
0511: - CheckArticleJob.INTERVAL));
0512:
0513: if (_log.isDebugEnabled()) {
0514: _log.debug("Expiring " + articles.size() + " articles");
0515: }
0516:
0517: Set companies = new HashSet();
0518:
0519: for (int i = 0; i < articles.size(); i++) {
0520: JournalArticle article = (JournalArticle) articles.get(i);
0521:
0522: article.setApproved(false);
0523: article.setExpired(true);
0524:
0525: journalArticlePersistence.update(article);
0526:
0527: try {
0528: if (article.isIndexable()) {
0529: Indexer.deleteArticle(article.getCompanyId(),
0530: article.getArticleId());
0531: }
0532: } catch (IOException ioe) {
0533: _log.error("Removing index " + article.getId(), ioe);
0534: }
0535:
0536: JournalContentUtil.clearCache(article.getGroupId(), article
0537: .getArticleId(), article.getTemplateId());
0538:
0539: companies.add(new Long(article.getCompanyId()));
0540: }
0541:
0542: Iterator itr = companies.iterator();
0543:
0544: while (itr.hasNext()) {
0545: long companyId = ((Long) itr.next()).longValue();
0546:
0547: LayoutCacheUtil.clearCache(companyId);
0548: }
0549:
0550: articles = journalArticleFinder.findByReviewDate(now, new Date(
0551: now.getTime() - CheckArticleJob.INTERVAL));
0552:
0553: if (_log.isDebugEnabled()) {
0554: _log.debug("Sending review notifications for "
0555: + articles.size() + " articles");
0556: }
0557:
0558: for (int i = 0; i < articles.size(); i++) {
0559: JournalArticle article = (JournalArticle) articles.get(i);
0560:
0561: Date reviewDate = article.getReviewDate();
0562:
0563: if (reviewDate != null) {
0564: long diff = reviewDate.getTime() - now.getTime();
0565:
0566: if ((diff > 0) && (diff < CheckArticleJob.INTERVAL)) {
0567: String articleURL = StringPool.BLANK;
0568:
0569: long ownerId = article.getGroupId();
0570: int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
0571: long plid = PortletKeys.PREFS_PLID_SHARED;
0572: String portletId = PortletKeys.JOURNAL;
0573:
0574: PortletPreferences prefs = portletPreferencesLocalService
0575: .getPreferences(article.getCompanyId(),
0576: ownerId, ownerType, plid, portletId);
0577:
0578: sendEmail(article, articleURL, prefs, "review");
0579: }
0580: }
0581: }
0582: }
0583:
0584: public void checkNewLine(long groupId, String articleId,
0585: double version) throws PortalException, SystemException {
0586:
0587: JournalArticle article = journalArticlePersistence.findByG_A_V(
0588: groupId, articleId, version);
0589:
0590: String content = GetterUtil.getString(article.getContent());
0591:
0592: if (content.indexOf("\\n") != -1) {
0593: content = StringUtil.replace(content, new String[] { "\\n",
0594: "\\r" }, new String[] { "\n", "\r" });
0595:
0596: article.setContent(content);
0597:
0598: journalArticlePersistence.update(article);
0599: }
0600: }
0601:
0602: public void checkStructure(long groupId, String articleId,
0603: double version) throws PortalException, SystemException {
0604:
0605: JournalArticle article = journalArticlePersistence.findByG_A_V(
0606: groupId, articleId, version);
0607:
0608: if (Validator.isNull(article.getStructureId())) {
0609: return;
0610: }
0611:
0612: try {
0613: checkStructure(article);
0614: } catch (DocumentException de) {
0615: _log.error(de, de);
0616: }
0617: }
0618:
0619: public void deleteArticle(long groupId, String articleId,
0620: double version, String articleURL, PortletPreferences prefs)
0621: throws PortalException, SystemException {
0622:
0623: JournalArticle article = journalArticlePersistence.findByG_A_V(
0624: groupId, articleId, version);
0625:
0626: deleteArticle(article, articleURL, prefs);
0627: }
0628:
0629: public void deleteArticle(JournalArticle article,
0630: String articleURL, PortletPreferences prefs)
0631: throws PortalException, SystemException {
0632:
0633: // Lucene
0634:
0635: try {
0636: if (article.isApproved() && article.isIndexable()) {
0637: Indexer.deleteArticle(article.getCompanyId(), article
0638: .getArticleId());
0639: }
0640: } catch (IOException ioe) {
0641: _log
0642: .error("Deleting index " + article.getPrimaryKey(),
0643: ioe);
0644: }
0645:
0646: // Email
0647:
0648: if ((prefs != null)
0649: && !article.isApproved()
0650: && isLatestVersion(article.getGroupId(), article
0651: .getArticleId(), article.getVersion())) {
0652:
0653: sendEmail(article, articleURL, prefs, "denied");
0654: }
0655:
0656: // Tags
0657:
0658: tagsAssetLocalService.deleteAsset(JournalArticle.class
0659: .getName(), article.getResourcePrimKey());
0660:
0661: // Ratings
0662:
0663: ratingsStatsLocalService.deleteStats(JournalArticle.class
0664: .getName(), article.getResourcePrimKey());
0665:
0666: // Message boards
0667:
0668: mbMessageLocalService.deleteDiscussionMessages(
0669: JournalArticle.class.getName(), article
0670: .getResourcePrimKey());
0671:
0672: // Content searches
0673:
0674: journalContentSearchLocalService.deleteArticleContentSearches(
0675: article.getGroupId(), article.getArticleId());
0676:
0677: // Images
0678:
0679: journalArticleImageLocalService.deleteImages(article
0680: .getGroupId(), article.getArticleId(), article
0681: .getVersion());
0682:
0683: // Small image
0684:
0685: ImageLocalUtil.deleteImage(article.getSmallImageId());
0686:
0687: // Resources
0688:
0689: if (journalArticlePersistence.countByG_A(article.getGroupId(),
0690: article.getArticleId()) == 1) {
0691:
0692: resourceLocalService.deleteResource(article.getCompanyId(),
0693: JournalArticle.class.getName(),
0694: ResourceImpl.SCOPE_INDIVIDUAL, article
0695: .getResourcePrimKey());
0696: }
0697:
0698: // Resource
0699:
0700: if (journalArticlePersistence.countByG_A(article.getGroupId(),
0701: article.getArticleId()) == 1) {
0702:
0703: try {
0704: journalArticleResourceLocalService
0705: .deleteArticleResource(article.getGroupId(),
0706: article.getArticleId());
0707: } catch (NoSuchArticleResourceException nsare) {
0708: }
0709: }
0710:
0711: // Article
0712:
0713: journalArticlePersistence.remove(article.getPrimaryKey());
0714: }
0715:
0716: public void deleteArticles(long groupId) throws PortalException,
0717: SystemException {
0718:
0719: Iterator itr = journalArticlePersistence.findByGroupId(groupId)
0720: .iterator();
0721:
0722: while (itr.hasNext()) {
0723: JournalArticle article = (JournalArticle) itr.next();
0724:
0725: deleteArticle(article, null, null);
0726: }
0727: }
0728:
0729: public void expireArticle(long groupId, String articleId,
0730: double version, String articleURL, PortletPreferences prefs)
0731: throws PortalException, SystemException {
0732:
0733: JournalArticle article = journalArticlePersistence.findByG_A_V(
0734: groupId, articleId, version);
0735:
0736: expireArticle(article, articleURL, prefs);
0737: }
0738:
0739: public void expireArticle(JournalArticle article,
0740: String articleURL, PortletPreferences prefs)
0741: throws PortalException, SystemException {
0742:
0743: // Email
0744:
0745: if ((prefs != null)
0746: && !article.isApproved()
0747: && isLatestVersion(article.getGroupId(), article
0748: .getArticleId(), article.getVersion())) {
0749:
0750: sendEmail(article, articleURL, prefs, "denied");
0751: }
0752:
0753: // Article
0754:
0755: article.setExpirationDate(new Date());
0756:
0757: article.setApproved(false);
0758: article.setExpired(true);
0759:
0760: journalArticlePersistence.update(article);
0761:
0762: // Lucene
0763:
0764: try {
0765: if (article.isIndexable()) {
0766: Indexer.deleteArticle(article.getCompanyId(), article
0767: .getArticleId());
0768: }
0769: } catch (IOException ioe) {
0770: _log.error("Removing index " + article.getId(), ioe);
0771: }
0772: }
0773:
0774: public JournalArticle getArticle(long id) throws PortalException,
0775: SystemException {
0776:
0777: return journalArticlePersistence.findByPrimaryKey(id);
0778: }
0779:
0780: public JournalArticle getArticle(long groupId, String articleId)
0781: throws PortalException, SystemException {
0782:
0783: // Get the latest article that is approved, if none are approved, get
0784: // the latest unapproved article
0785:
0786: try {
0787: return getLatestArticle(groupId, articleId, Boolean.TRUE);
0788: } catch (NoSuchArticleException nsae) {
0789: return getLatestArticle(groupId, articleId, Boolean.FALSE);
0790: }
0791: }
0792:
0793: public JournalArticle getArticle(long groupId, String articleId,
0794: double version) throws PortalException, SystemException {
0795:
0796: return journalArticlePersistence.findByG_A_V(groupId,
0797: articleId, version);
0798: }
0799:
0800: public String getArticleContent(long groupId, String articleId,
0801: String languageId, ThemeDisplay themeDisplay)
0802: throws PortalException, SystemException {
0803:
0804: return getArticleContent(groupId, articleId, null, languageId,
0805: themeDisplay);
0806: }
0807:
0808: public String getArticleContent(long groupId, String articleId,
0809: String templateId, String languageId,
0810: ThemeDisplay themeDisplay) throws PortalException,
0811: SystemException {
0812:
0813: JournalArticleDisplay articleDisplay = getArticleDisplay(
0814: groupId, articleId, templateId, languageId,
0815: themeDisplay);
0816:
0817: return articleDisplay.getContent();
0818: }
0819:
0820: public String getArticleContent(long groupId, String articleId,
0821: double version, String languageId, ThemeDisplay themeDisplay)
0822: throws PortalException, SystemException {
0823:
0824: return getArticleContent(groupId, articleId, version, null,
0825: languageId, themeDisplay);
0826: }
0827:
0828: public String getArticleContent(long groupId, String articleId,
0829: double version, String templateId, String languageId,
0830: ThemeDisplay themeDisplay) throws PortalException,
0831: SystemException {
0832:
0833: JournalArticleDisplay articleDisplay = getArticleDisplay(
0834: groupId, articleId, version, templateId, languageId,
0835: themeDisplay);
0836:
0837: if (articleDisplay == null) {
0838: return StringPool.BLANK;
0839: } else {
0840: return articleDisplay.getContent();
0841: }
0842: }
0843:
0844: public JournalArticleDisplay getArticleDisplay(long groupId,
0845: String articleId, String languageId,
0846: ThemeDisplay themeDisplay) throws PortalException,
0847: SystemException {
0848:
0849: return getArticleDisplay(groupId, articleId, null, languageId,
0850: themeDisplay);
0851: }
0852:
0853: public JournalArticleDisplay getArticleDisplay(long groupId,
0854: String articleId, String languageId, int page,
0855: String xmlRequest, ThemeDisplay themeDisplay)
0856: throws PortalException, SystemException {
0857:
0858: return getArticleDisplay(groupId, articleId, null, languageId,
0859: page, xmlRequest, themeDisplay);
0860: }
0861:
0862: public JournalArticleDisplay getArticleDisplay(long groupId,
0863: String articleId, String templateId, String languageId,
0864: ThemeDisplay themeDisplay) throws PortalException,
0865: SystemException {
0866:
0867: JournalArticle article = getDisplayArticle(groupId, articleId);
0868:
0869: return getArticleDisplay(groupId, articleId, article
0870: .getVersion(), templateId, languageId, themeDisplay);
0871: }
0872:
0873: public JournalArticleDisplay getArticleDisplay(long groupId,
0874: String articleId, String templateId, String languageId,
0875: int page, String xmlRequest, ThemeDisplay themeDisplay)
0876: throws PortalException, SystemException {
0877:
0878: JournalArticle article = getDisplayArticle(groupId, articleId);
0879:
0880: return getArticleDisplay(groupId, articleId, article
0881: .getVersion(), templateId, languageId, page,
0882: xmlRequest, themeDisplay);
0883: }
0884:
0885: public JournalArticleDisplay getArticleDisplay(long groupId,
0886: String articleId, double version, String templateId,
0887: String languageId, ThemeDisplay themeDisplay)
0888: throws PortalException, SystemException {
0889:
0890: return getArticleDisplay(groupId, articleId, version,
0891: templateId, languageId, 1, null, themeDisplay);
0892: }
0893:
0894: public JournalArticleDisplay getArticleDisplay(long groupId,
0895: String articleId, double version, String templateId,
0896: String languageId, int page, String xmlRequest,
0897: ThemeDisplay themeDisplay) throws PortalException,
0898: SystemException {
0899:
0900: String content = null;
0901:
0902: Date now = new Date();
0903:
0904: JournalArticle article = journalArticlePersistence.findByG_A_V(
0905: groupId, articleId, version);
0906:
0907: if (article.isExpired()) {
0908: Date expirationDate = article.getExpirationDate();
0909:
0910: if ((expirationDate != null) && expirationDate.before(now)) {
0911: return null;
0912: }
0913: }
0914:
0915: if (article.getDisplayDate().after(now)) {
0916: return null;
0917: }
0918:
0919: if (page < 1) {
0920: page = 1;
0921: }
0922:
0923: int numberOfPages = 1;
0924: boolean paginate = false;
0925: boolean pageFlow = false;
0926:
0927: boolean cacheable = true;
0928:
0929: Map tokens = JournalUtil.getTokens(groupId, themeDisplay);
0930:
0931: tokens.put("article_resource_pk", String.valueOf(article
0932: .getResourcePrimKey()));
0933:
0934: String xml = article.getContent();
0935:
0936: try {
0937: Document doc = null;
0938:
0939: Element root = null;
0940:
0941: if (article.isTemplateDriven()) {
0942: SAXReader reader = new SAXReader();
0943:
0944: doc = reader.read(new StringReader(xml));
0945:
0946: root = doc.getRootElement();
0947:
0948: Document request = null;
0949:
0950: if (Validator.isNotNull(xmlRequest)) {
0951: request = reader.read(new StringReader(xmlRequest));
0952: }
0953:
0954: List pages = root.elements("page");
0955:
0956: if (pages.size() > 0) {
0957: pageFlow = true;
0958:
0959: String targetPage = request
0960: .valueOf("/request/parameters/parameter[name='targetPage']/"
0961: + "value");
0962:
0963: Element pageEl = null;
0964:
0965: if (Validator.isNotNull(targetPage)) {
0966: XPath xpathSelector = DocumentHelper
0967: .createXPath("/root/page[@id = '"
0968: + targetPage + "']");
0969:
0970: pageEl = (Element) xpathSelector
0971: .selectSingleNode(doc);
0972: }
0973:
0974: DocumentFactory docFactory = DocumentFactory
0975: .getInstance();
0976:
0977: if (pageEl != null) {
0978: doc = docFactory.createDocument(pageEl);
0979:
0980: root = doc.getRootElement();
0981:
0982: numberOfPages = pages.size();
0983: } else {
0984: if (page > pages.size()) {
0985: page = 1;
0986: }
0987:
0988: pageEl = (Element) pages.get(page - 1);
0989:
0990: doc = docFactory.createDocument(pageEl);
0991:
0992: root = doc.getRootElement();
0993:
0994: numberOfPages = pages.size();
0995: paginate = true;
0996: }
0997: }
0998:
0999: if (request != null) {
1000: root.add(request.getRootElement().createCopy());
1001: }
1002:
1003: JournalUtil.addAllReservedEls(root, tokens, article);
1004:
1005: xml = JournalUtil.formatXML(doc);
1006: }
1007: } catch (DocumentException de) {
1008: throw new SystemException(de);
1009: } catch (IOException ioe) {
1010: throw new SystemException(ioe);
1011: }
1012:
1013: try {
1014: if (_log.isDebugEnabled()) {
1015: _log.debug("Transforming " + articleId + " " + version
1016: + " " + languageId);
1017: }
1018:
1019: String script = null;
1020: String langType = null;
1021:
1022: if (article.isTemplateDriven()) {
1023:
1024: // Try with specified template first. If a template is not
1025: // specified, use the default one. If the specified template
1026: // does not exit, use the default one. If the default one does
1027: // not exist, throw an exception.
1028:
1029: String defaultTemplateId = article.getTemplateId();
1030:
1031: if (Validator.isNull(templateId)) {
1032: templateId = defaultTemplateId;
1033: }
1034:
1035: JournalTemplate template = null;
1036:
1037: try {
1038: template = journalTemplatePersistence.findByG_T(
1039: groupId, templateId);
1040: } catch (NoSuchTemplateException nste) {
1041: if (!defaultTemplateId.equals(templateId)) {
1042: template = journalTemplatePersistence
1043: .findByG_T(groupId, defaultTemplateId);
1044: } else {
1045: throw nste;
1046: }
1047: }
1048:
1049: script = template.getXsl();
1050: langType = template.getLangType();
1051: cacheable = template.isCacheable();
1052: }
1053:
1054: content = JournalUtil.transform(tokens, languageId, xml,
1055: script, langType);
1056:
1057: if (!pageFlow) {
1058: String[] pieces = StringUtil.split(content,
1059: _TOKEN_PAGE_BREAK);
1060:
1061: if (pieces.length > 1) {
1062: if (page > pieces.length) {
1063: page = 1;
1064: }
1065:
1066: content = pieces[page - 1];
1067: numberOfPages = pieces.length;
1068: paginate = true;
1069: }
1070: }
1071: } catch (Exception e) {
1072: throw new SystemException(e);
1073: }
1074:
1075: return new JournalArticleDisplayImpl(article.getId(), article
1076: .getResourcePrimKey(), article.getGroupId(), article
1077: .getUserId(), article.getArticleId(), article
1078: .getVersion(), article.getTitle(), article
1079: .getDescription(), article.getAvailableLocales(),
1080: content, article.getType(), article.getStructureId(),
1081: templateId, article.isSmallImage(), article
1082: .getSmallImageId(), article.getSmallImageURL(),
1083: numberOfPages, page, paginate, cacheable);
1084: }
1085:
1086: public List getArticles() throws SystemException {
1087: return journalArticlePersistence.findAll();
1088: }
1089:
1090: public List getArticles(long groupId) throws SystemException {
1091: return journalArticlePersistence.findByGroupId(groupId);
1092: }
1093:
1094: public List getArticles(long groupId, int begin, int end)
1095: throws SystemException {
1096:
1097: return journalArticlePersistence.findByGroupId(groupId, begin,
1098: end);
1099: }
1100:
1101: public List getArticles(long groupId, int begin, int end,
1102: OrderByComparator obc) throws SystemException {
1103:
1104: return journalArticlePersistence.findByGroupId(groupId, begin,
1105: end, obc);
1106: }
1107:
1108: public List getArticlesBySmallImageId(long smallImageId)
1109: throws SystemException {
1110:
1111: return journalArticlePersistence
1112: .findBySmallImageId(smallImageId);
1113: }
1114:
1115: public int getArticlesCount(long groupId) throws SystemException {
1116: return journalArticlePersistence.countByGroupId(groupId);
1117: }
1118:
1119: public JournalArticle getDisplayArticle(long groupId,
1120: String articleId) throws PortalException, SystemException {
1121:
1122: List articles = journalArticlePersistence.findByG_A_A(groupId,
1123: articleId, true);
1124:
1125: if (articles.size() == 0) {
1126: throw new NoSuchArticleException();
1127: }
1128:
1129: Collections.sort(articles, new ArticleDisplayDateComparator());
1130:
1131: Date now = new Date();
1132:
1133: for (int i = 0; i < articles.size(); i++) {
1134: JournalArticle article = (JournalArticle) articles.get(i);
1135:
1136: if (article.getDisplayDate().before(now)) {
1137: return article;
1138: }
1139: }
1140:
1141: return (JournalArticle) articles.get(0);
1142: }
1143:
1144: public JournalArticle getLatestArticle(long groupId,
1145: String articleId) throws PortalException, SystemException {
1146:
1147: return getLatestArticle(groupId, articleId, null);
1148: }
1149:
1150: public JournalArticle getLatestArticle(long groupId,
1151: String articleId, Boolean approved) throws PortalException,
1152: SystemException {
1153:
1154: List articles = null;
1155:
1156: if (approved == null) {
1157: articles = journalArticlePersistence.findByG_A(groupId,
1158: articleId, 0, 1);
1159: } else {
1160: articles = journalArticlePersistence.findByG_A_A(groupId,
1161: articleId, approved.booleanValue(), 0, 1);
1162: }
1163:
1164: if (articles.size() == 0) {
1165: throw new NoSuchArticleException();
1166: }
1167:
1168: return (JournalArticle) articles.get(0);
1169: }
1170:
1171: public double getLatestVersion(long groupId, String articleId)
1172: throws PortalException, SystemException {
1173:
1174: JournalArticle article = getLatestArticle(groupId, articleId);
1175:
1176: return article.getVersion();
1177: }
1178:
1179: public double getLatestVersion(long groupId, String articleId,
1180: Boolean approved) throws PortalException, SystemException {
1181:
1182: JournalArticle article = getLatestArticle(groupId, articleId,
1183: approved);
1184:
1185: return article.getVersion();
1186: }
1187:
1188: public List getStructureArticles(long groupId, String structureId)
1189: throws SystemException {
1190:
1191: return journalArticlePersistence
1192: .findByG_S(groupId, structureId);
1193: }
1194:
1195: public List getStructureArticles(long groupId, String structureId,
1196: int begin, int end, OrderByComparator obc)
1197: throws SystemException {
1198:
1199: return journalArticlePersistence.findByG_S(groupId,
1200: structureId, begin, end, obc);
1201: }
1202:
1203: public int getStructureArticlesCount(long groupId,
1204: String structureId) throws SystemException {
1205:
1206: return journalArticlePersistence.countByG_S(groupId,
1207: structureId);
1208: }
1209:
1210: public List getTemplateArticles(long groupId, String templateId)
1211: throws SystemException {
1212:
1213: return journalArticlePersistence.findByG_T(groupId, templateId);
1214: }
1215:
1216: public List getTemplateArticles(long groupId, String templateId,
1217: int begin, int end, OrderByComparator obc)
1218: throws SystemException {
1219:
1220: return journalArticlePersistence.findByG_T(groupId, templateId,
1221: begin, end, obc);
1222: }
1223:
1224: public int getTemplateArticlesCount(long groupId, String templateId)
1225: throws SystemException {
1226:
1227: return journalArticlePersistence
1228: .countByG_T(groupId, templateId);
1229: }
1230:
1231: public boolean hasArticle(long groupId, String articleId)
1232: throws SystemException {
1233:
1234: try {
1235: getArticle(groupId, articleId);
1236:
1237: return true;
1238: } catch (PortalException pe) {
1239: return false;
1240: }
1241: }
1242:
1243: public boolean isLatestVersion(long groupId, String articleId,
1244: double version) throws PortalException, SystemException {
1245:
1246: if (getLatestVersion(groupId, articleId) == version) {
1247: return true;
1248: } else {
1249: return false;
1250: }
1251: }
1252:
1253: public boolean isLatestVersion(long groupId, String articleId,
1254: double version, Boolean active) throws PortalException,
1255: SystemException {
1256:
1257: if (getLatestVersion(groupId, articleId, active) == version) {
1258: return true;
1259: } else {
1260: return false;
1261: }
1262: }
1263:
1264: public void reIndex(String[] ids) throws SystemException {
1265: if (LuceneUtil.INDEX_READ_ONLY) {
1266: return;
1267: }
1268:
1269: long companyId = GetterUtil.getLong(ids[0]);
1270:
1271: IndexWriter writer = null;
1272:
1273: try {
1274: writer = LuceneUtil.getWriter(companyId);
1275:
1276: Iterator itr = journalArticlePersistence.findByCompanyId(
1277: companyId).iterator();
1278:
1279: while (itr.hasNext()) {
1280: JournalArticle article = (JournalArticle) itr.next();
1281:
1282: if (article.isApproved() && article.isIndexable()) {
1283: long resourcePrimKey = article.getResourcePrimKey();
1284: long groupId = article.getGroupId();
1285: String articleId = article.getArticleId();
1286: double version = article.getVersion();
1287: String title = article.getTitle();
1288: String description = article.getDescription();
1289: String content = article.getContent();
1290: String type = article.getType();
1291: Date displayDate = article.getDisplayDate();
1292:
1293: String[] tagsEntries = tagsEntryLocalService
1294: .getEntryNames(JournalArticle.class
1295: .getName(), resourcePrimKey);
1296:
1297: try {
1298: org.apache.lucene.document.Document doc = Indexer
1299: .getAddArticleDocument(companyId,
1300: groupId, articleId, version,
1301: title, description, content,
1302: type, displayDate, tagsEntries);
1303:
1304: writer.addDocument(doc);
1305: } catch (Exception e1) {
1306: _log.error("Reindexing " + article.getId(), e1);
1307: }
1308: }
1309: }
1310: } catch (SystemException se) {
1311: throw se;
1312: } catch (Exception e2) {
1313: throw new SystemException(e2);
1314: } finally {
1315: try {
1316: if (writer != null) {
1317: LuceneUtil.write(companyId);
1318: }
1319: } catch (Exception e) {
1320: _log.error(e);
1321: }
1322: }
1323: }
1324:
1325: public JournalArticle removeArticleLocale(long groupId,
1326: String articleId, double version, String languageId)
1327: throws PortalException, SystemException {
1328:
1329: JournalArticle article = journalArticlePersistence.findByG_A_V(
1330: groupId, articleId, version);
1331:
1332: String content = article.getContent();
1333:
1334: if (article.isTemplateDriven()) {
1335: content = JournalUtil.removeArticleLocale(content,
1336: languageId);
1337: } else {
1338: content = LocalizationUtil.removeLocalization(content,
1339: "static-content", languageId);
1340: }
1341:
1342: article.setContent(content);
1343:
1344: journalArticlePersistence.update(article);
1345:
1346: return article;
1347: }
1348:
1349: public Hits search(long companyId, long groupId, String keywords)
1350: throws SystemException {
1351:
1352: return search(companyId, groupId, keywords, "displayDate");
1353: }
1354:
1355: public Hits search(long companyId, long groupId, String keywords,
1356: String sortField) throws SystemException {
1357:
1358: Searcher searcher = null;
1359:
1360: try {
1361: HitsImpl hits = new HitsImpl();
1362:
1363: BooleanQuery contextQuery = new BooleanQuery();
1364:
1365: LuceneUtil.addRequiredTerm(contextQuery,
1366: LuceneFields.PORTLET_ID, Indexer.PORTLET_ID);
1367:
1368: if (groupId > 0) {
1369: LuceneUtil.addRequiredTerm(contextQuery,
1370: LuceneFields.GROUP_ID, groupId);
1371: }
1372:
1373: BooleanQuery searchQuery = new BooleanQuery();
1374:
1375: if (Validator.isNotNull(keywords)) {
1376: LuceneUtil.addTerm(searchQuery, LuceneFields.TITLE,
1377: keywords);
1378: LuceneUtil.addTerm(searchQuery, LuceneFields.CONTENT,
1379: keywords);
1380: LuceneUtil.addTerm(searchQuery,
1381: LuceneFields.DESCRIPTION, keywords);
1382: }
1383:
1384: BooleanQuery fullQuery = new BooleanQuery();
1385:
1386: fullQuery.add(contextQuery, BooleanClause.Occur.MUST);
1387:
1388: if (searchQuery.clauses().size() > 0) {
1389: fullQuery.add(searchQuery, BooleanClause.Occur.MUST);
1390: }
1391:
1392: searcher = LuceneUtil.getSearcher(companyId);
1393:
1394: Sort sort = new Sort(new SortField(sortField, true));
1395:
1396: try {
1397: hits.recordHits(searcher.search(fullQuery, sort),
1398: searcher);
1399: } catch (RuntimeException re) {
1400:
1401: // Trying to sort on a field when there are no results throws a
1402: // RuntimeException that should not be rethrown
1403:
1404: String msg = GetterUtil.getString(re.getMessage());
1405:
1406: if (!msg.endsWith("does not appear to be indexed")) {
1407: throw re;
1408: }
1409: }
1410:
1411: return hits;
1412: } catch (Exception e) {
1413: return LuceneUtil.closeSearcher(searcher, keywords, e);
1414: }
1415: }
1416:
1417: public List search(long companyId, long groupId, String keywords,
1418: Double version, String type, String structureId,
1419: String templateId, Date displayDateGT, Date displayDateLT,
1420: Boolean approved, Boolean expired, Date reviewDate,
1421: int begin, int end, OrderByComparator obc)
1422: throws SystemException {
1423:
1424: return journalArticleFinder.findByKeywords(companyId, groupId,
1425: keywords, version, type, structureId, templateId,
1426: displayDateGT, displayDateLT, approved, expired,
1427: reviewDate, begin, end, obc);
1428: }
1429:
1430: public List search(long companyId, long groupId, String articleId,
1431: Double version, String title, String description,
1432: String content, String type, String structureId,
1433: String templateId, Date displayDateGT, Date displayDateLT,
1434: Boolean approved, Boolean expired, Date reviewDate,
1435: boolean andOperator, int begin, int end,
1436: OrderByComparator obc) throws SystemException {
1437:
1438: return journalArticleFinder.findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
1439: companyId, groupId, articleId, version, title,
1440: description, content, type, structureId, templateId,
1441: displayDateGT, displayDateLT, approved, expired,
1442: reviewDate, andOperator, begin, end, obc);
1443: }
1444:
1445: public List search(long companyId, long groupId, String articleId,
1446: Double version, String title, String description,
1447: String content, String type, String[] structureIds,
1448: String[] templateIds, Date displayDateGT,
1449: Date displayDateLT, Boolean approved, Boolean expired,
1450: Date reviewDate, boolean andOperator, int begin, int end,
1451: OrderByComparator obc) throws SystemException {
1452:
1453: return journalArticleFinder.findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
1454: companyId, groupId, articleId, version, title,
1455: description, content, type, structureIds, templateIds,
1456: displayDateGT, displayDateLT, approved, expired,
1457: reviewDate, andOperator, begin, end, obc);
1458: }
1459:
1460: public int searchCount(long companyId, long groupId,
1461: String keywords, Double version, String type,
1462: String structureId, String templateId, Date displayDateGT,
1463: Date displayDateLT, Boolean approved, Boolean expired,
1464: Date reviewDate) throws SystemException {
1465:
1466: return journalArticleFinder.countByKeywords(companyId, groupId,
1467: keywords, version, type, structureId, templateId,
1468: displayDateGT, displayDateLT, approved, expired,
1469: reviewDate);
1470: }
1471:
1472: public int searchCount(long companyId, long groupId,
1473: String articleId, Double version, String title,
1474: String description, String content, String type,
1475: String structureId, String templateId, Date displayDateGT,
1476: Date displayDateLT, Boolean approved, Boolean expired,
1477: Date reviewDate, boolean andOperator)
1478: throws SystemException {
1479:
1480: return journalArticleFinder.countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
1481: companyId, groupId, articleId, version, title,
1482: description, content, type, structureId, templateId,
1483: displayDateGT, displayDateLT, approved, expired,
1484: reviewDate, andOperator);
1485: }
1486:
1487: public int searchCount(long companyId, long groupId,
1488: String articleId, Double version, String title,
1489: String description, String content, String type,
1490: String[] structureIds, String[] templateIds,
1491: Date displayDateGT, Date displayDateLT, Boolean approved,
1492: Boolean expired, Date reviewDate, boolean andOperator)
1493: throws SystemException {
1494:
1495: return journalArticleFinder.countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
1496: companyId, groupId, articleId, version, title,
1497: description, content, type, structureIds, templateIds,
1498: displayDateGT, displayDateLT, approved, expired,
1499: reviewDate, andOperator);
1500: }
1501:
1502: public JournalArticle updateArticle(long userId, long groupId,
1503: String articleId, double version, boolean incrementVersion,
1504: String title, String description, String content,
1505: String type, String structureId, String templateId,
1506: int displayDateMonth, int displayDateDay,
1507: int displayDateYear, int displayDateHour,
1508: int displayDateMinute, int expirationDateMonth,
1509: int expirationDateDay, int expirationDateYear,
1510: int expirationDateHour, int expirationDateMinute,
1511: boolean neverExpire, int reviewDateMonth,
1512: int reviewDateDay, int reviewDateYear, int reviewDateHour,
1513: int reviewDateMinute, boolean neverReview,
1514: boolean indexable, boolean smallImage,
1515: String smallImageURL, File smallFile, Map images,
1516: String articleURL, PortletPreferences prefs,
1517: String[] tagsEntries) throws PortalException,
1518: SystemException {
1519:
1520: // Article
1521:
1522: User user = userPersistence.findByPrimaryKey(userId);
1523: articleId = articleId.trim().toUpperCase();
1524: Date now = new Date();
1525:
1526: Date displayDate = PortalUtil.getDate(displayDateMonth,
1527: displayDateDay, displayDateYear, displayDateHour,
1528: displayDateMinute, user.getTimeZone(),
1529: new ArticleDisplayDateException());
1530:
1531: Date expirationDate = null;
1532:
1533: if (!neverExpire) {
1534: expirationDate = PortalUtil.getDate(expirationDateMonth,
1535: expirationDateDay, expirationDateYear,
1536: expirationDateHour, expirationDateMinute, user
1537: .getTimeZone(),
1538: new ArticleExpirationDateException());
1539: }
1540:
1541: Date reviewDate = null;
1542:
1543: if (!neverReview) {
1544: reviewDate = PortalUtil.getDate(reviewDateMonth,
1545: reviewDateDay, reviewDateYear, reviewDateHour,
1546: reviewDateMinute, user.getTimeZone(),
1547: new ArticleReviewDateException());
1548: }
1549:
1550: byte[] smallBytes = null;
1551:
1552: try {
1553: smallBytes = FileUtil.getBytes(smallFile);
1554: } catch (IOException ioe) {
1555: }
1556:
1557: validate(groupId, title, content, type, structureId,
1558: templateId, smallImage, smallImageURL, smallFile,
1559: smallBytes);
1560:
1561: JournalArticle oldArticle = journalArticlePersistence
1562: .findByG_A_V(groupId, articleId, version);
1563:
1564: JournalArticle article = null;
1565:
1566: if (incrementVersion) {
1567: double latestVersion = getLatestVersion(groupId, articleId);
1568:
1569: long id = counterLocalService.increment();
1570:
1571: article = journalArticlePersistence.create(id);
1572:
1573: article.setResourcePrimKey(oldArticle.getResourcePrimKey());
1574: article.setGroupId(oldArticle.getGroupId());
1575: article.setCompanyId(user.getCompanyId());
1576: article.setUserId(user.getUserId());
1577: article.setUserName(user.getFullName());
1578: article.setCreateDate(now);
1579: article.setArticleId(articleId);
1580: article.setVersion(MathUtil.format(latestVersion + 0.1, 1,
1581: 1));
1582: article.setSmallImageId(oldArticle.getSmallImageId());
1583: } else {
1584: article = oldArticle;
1585: }
1586:
1587: content = format(groupId, articleId, article.getVersion(),
1588: incrementVersion, content, structureId, images);
1589:
1590: boolean approved = oldArticle.isApproved();
1591:
1592: if (incrementVersion) {
1593: approved = false;
1594: }
1595:
1596: article.setModifiedDate(now);
1597: article.setTitle(title);
1598: article.setDescription(description);
1599: article.setContent(content);
1600: article.setType(type);
1601: article.setStructureId(structureId);
1602: article.setTemplateId(templateId);
1603: article.setDisplayDate(displayDate);
1604: article.setApproved(approved);
1605:
1606: if ((expirationDate == null) || expirationDate.after(now)) {
1607: article.setExpired(false);
1608: } else {
1609: article.setExpired(true);
1610: }
1611:
1612: article.setExpirationDate(expirationDate);
1613: article.setReviewDate(reviewDate);
1614: article.setIndexable(indexable);
1615: article.setSmallImage(smallImage);
1616:
1617: if (article.getSmallImageId() == 0) {
1618: article.setSmallImageId(counterLocalService.increment());
1619: }
1620:
1621: article.setSmallImageURL(smallImageURL);
1622:
1623: journalArticlePersistence.update(article);
1624:
1625: // Small image
1626:
1627: saveImages(smallImage, article.getSmallImageId(), smallFile,
1628: smallBytes);
1629:
1630: // Tags
1631:
1632: updateTagsAsset(userId, article, tagsEntries);
1633:
1634: // Email
1635:
1636: if (incrementVersion) {
1637: sendEmail(article, articleURL, prefs, "requested");
1638: }
1639:
1640: // Lucene
1641:
1642: try {
1643: if (article.isIndexable()) {
1644: if (article.isApproved()) {
1645: Indexer.updateArticle(article.getCompanyId(),
1646: article.getGroupId(), article
1647: .getArticleId(), article
1648: .getVersion(), article.getTitle(),
1649: article.getDescription(), article
1650: .getContent(), article.getType(),
1651: article.getDisplayDate(), tagsEntries);
1652: } else {
1653: Indexer.deleteArticle(article.getCompanyId(),
1654: article.getArticleId());
1655: }
1656: }
1657: } catch (IOException ioe) {
1658: _log.error("Indexing " + article.getPrimaryKey(), ioe);
1659: }
1660:
1661: return article;
1662: }
1663:
1664: public JournalArticle updateContent(long groupId, String articleId,
1665: double version, String content) throws PortalException,
1666: SystemException {
1667:
1668: JournalArticle article = journalArticlePersistence.findByG_A_V(
1669: groupId, articleId, version);
1670:
1671: article.setContent(content);
1672:
1673: journalArticlePersistence.update(article);
1674:
1675: return article;
1676: }
1677:
1678: public void updateTagsAsset(long userId, JournalArticle article,
1679: String[] tagsEntries) throws PortalException,
1680: SystemException {
1681:
1682: boolean updateAsset = true;
1683:
1684: if (article.getVersion() == 1) {
1685: } else if (!article.isApproved()) {
1686: updateAsset = false;
1687: } else if (!isLatestVersion(article.getGroupId(), article
1688: .getArticleId(), article.getVersion(), Boolean.TRUE)) {
1689:
1690: updateAsset = false;
1691: }
1692:
1693: if (updateAsset) {
1694: tagsAssetLocalService.updateAsset(userId, article
1695: .getGroupId(), JournalArticle.class.getName(),
1696: article.getResourcePrimKey(), tagsEntries, null,
1697: null, article.getDisplayDate(), article
1698: .getExpirationDate(),
1699: ContentTypes.TEXT_HTML, article.getTitle(), article
1700: .getDescription(), null, null, 0, 0, null,
1701: false);
1702: }
1703: }
1704:
1705: protected void checkStructure(JournalArticle article)
1706: throws DocumentException, PortalException, SystemException {
1707:
1708: JournalStructure structure = journalStructurePersistence
1709: .findByG_S(article.getGroupId(), article
1710: .getStructureId());
1711:
1712: String content = GetterUtil.getString(article.getContent());
1713:
1714: SAXReader reader = new SAXReader();
1715:
1716: Document contentDoc = reader.read(new StringReader(content));
1717: Document xsdDoc = reader.read(new StringReader(structure
1718: .getXsd()));
1719:
1720: try {
1721: checkStructure(contentDoc, xsdDoc.getRootElement());
1722: } catch (StructureXsdException sxsde) {
1723: long groupId = article.getGroupId();
1724: String articleId = article.getArticleId();
1725: double version = article.getVersion();
1726:
1727: _log
1728: .error("Article {groupId="
1729: + groupId
1730: + ", articleId="
1731: + articleId
1732: + ", version="
1733: + version
1734: + "} has content that does not match its structure: "
1735: + sxsde.getMessage());
1736: }
1737: }
1738:
1739: protected void checkStructure(Document contentDoc, Element root)
1740: throws PortalException {
1741:
1742: Iterator itr = root.elements().iterator();
1743:
1744: while (itr.hasNext()) {
1745: Element el = (Element) itr.next();
1746:
1747: checkStructureField(el, contentDoc);
1748:
1749: checkStructure(contentDoc, el);
1750: }
1751: }
1752:
1753: protected void checkStructureField(Element el, Document contentDoc)
1754: throws PortalException {
1755:
1756: StringMaker elPath = new StringMaker();
1757:
1758: elPath.append(el.attributeValue("name"));
1759:
1760: Element elParent = (Element) el.getParent();
1761:
1762: for (;;) {
1763: if ((elParent == null)
1764: || (elParent.getName().equals("root"))) {
1765:
1766: break;
1767: }
1768:
1769: elPath.insert(0, elParent.attributeValue("name")
1770: + StringPool.COMMA);
1771:
1772: elParent = (Element) elParent.getParent();
1773: }
1774:
1775: String[] elPathNames = StringUtil.split(elPath.toString());
1776:
1777: Element contentEl = contentDoc.getRootElement();
1778:
1779: for (int i = 0; i < elPathNames.length; i++) {
1780: boolean foundEl = false;
1781:
1782: Iterator itr = contentEl.elements().iterator();
1783:
1784: while (itr.hasNext()) {
1785: Element tempEl = (Element) itr.next();
1786:
1787: if (elPathNames[i].equals(tempEl.attributeValue("name",
1788: StringPool.BLANK))) {
1789:
1790: contentEl = tempEl;
1791: foundEl = true;
1792:
1793: break;
1794: }
1795: }
1796:
1797: if (!foundEl) {
1798: String elType = contentEl.attributeValue("type",
1799: StringPool.BLANK);
1800:
1801: if (!elType.equals("list")
1802: && !elType.equals("multi-list")) {
1803: throw new StructureXsdException(elPath.toString());
1804: }
1805:
1806: break;
1807: }
1808: }
1809: }
1810:
1811: protected String format(long groupId, String articleId,
1812: double version, boolean incrementVersion, String content,
1813: String structureId, Map images) throws SystemException {
1814:
1815: if (Validator.isNotNull(structureId)) {
1816: SAXReader reader = new SAXReader();
1817:
1818: Document doc = null;
1819:
1820: try {
1821: doc = reader.read(new StringReader(content));
1822:
1823: Element root = doc.getRootElement();
1824:
1825: format(groupId, articleId, version, incrementVersion,
1826: root, images);
1827:
1828: content = JournalUtil.formatXML(doc);
1829: } catch (DocumentException de) {
1830: _log.error(de);
1831: } catch (IOException ioe) {
1832: _log.error(ioe);
1833: }
1834: }
1835:
1836: content = Html.replaceMsWordCharacters(content);
1837:
1838: return content;
1839: }
1840:
1841: protected void format(long groupId, String articleId,
1842: double version, boolean incrementVersion, Element root,
1843: Map images) throws SystemException {
1844:
1845: Iterator itr = root.elements().iterator();
1846:
1847: while (itr.hasNext()) {
1848: Element el = (Element) itr.next();
1849:
1850: String elName = el.attributeValue("name", StringPool.BLANK);
1851: String elType = el.attributeValue("type", StringPool.BLANK);
1852:
1853: if (elType.equals("image")) {
1854: formatImage(groupId, articleId, version,
1855: incrementVersion, el, elName, images);
1856: }
1857: /*else if (elType.equals("text_area")) {
1858: Element dynamicContent = el.element("dynamic-content");
1859:
1860: String text = dynamicContent.getText();
1861:
1862: // LEP-1594
1863:
1864: try {
1865: text = ParserUtils.trimTags(
1866: text, new String[] {"script"}, false, true);
1867: }
1868: catch (ParserException pe) {
1869: text = pe.getLocalizedMessage();
1870: }
1871: catch (UnsupportedEncodingException uee) {
1872: text = uee.getLocalizedMessage();
1873: }
1874:
1875: dynamicContent.setText(text);
1876: }*/
1877:
1878: format(groupId, articleId, version, incrementVersion, el,
1879: images);
1880: }
1881: }
1882:
1883: protected void formatImage(long groupId, String articleId,
1884: double version, boolean incrementVersion, Element el,
1885: String elName, Map images) throws SystemException {
1886:
1887: List imageContents = el.elements("dynamic-content");
1888:
1889: Iterator itr = imageContents.listIterator();
1890:
1891: while (itr.hasNext()) {
1892: Element dynamicContent = (Element) itr.next();
1893:
1894: String elLanguage = dynamicContent.attributeValue(
1895: "language-id", StringPool.BLANK);
1896:
1897: if (!elLanguage.equals(StringPool.BLANK)) {
1898: elLanguage = "_" + elLanguage;
1899: }
1900:
1901: long imageId = journalArticleImageLocalService
1902: .getArticleImageId(groupId, articleId, version,
1903: elName, elLanguage);
1904:
1905: double oldVersion = MathUtil.format(version - 0.1, 1, 1);
1906:
1907: long oldImageId = 0;
1908:
1909: if (oldVersion >= 1) {
1910: oldImageId = journalArticleImageLocalService
1911: .getArticleImageId(groupId, articleId,
1912: oldVersion, elName, elLanguage);
1913: }
1914:
1915: String elContent = "/image/journal/article?img_id="
1916: + imageId + "&t="
1917: + ImageServletTokenUtil.getToken(imageId);
1918:
1919: if (dynamicContent.getText().equals("delete")) {
1920: dynamicContent.setText(StringPool.BLANK);
1921:
1922: ImageLocalUtil.deleteImage(imageId);
1923:
1924: String defaultElLanguage = "";
1925:
1926: if (!Validator.isNotNull(elLanguage)) {
1927: defaultElLanguage = "_"
1928: + LocaleUtil.toLanguageId(LocaleUtil
1929: .getDefault());
1930: }
1931:
1932: long defaultImageId = journalArticleImageLocalService
1933: .getArticleImageId(groupId, articleId, version,
1934: elName, defaultElLanguage);
1935:
1936: ImageLocalUtil.deleteImage(defaultImageId);
1937:
1938: continue;
1939: }
1940:
1941: byte[] bytes = (byte[]) images.get(elName + elLanguage);
1942:
1943: if (bytes != null && (bytes.length > 0)) {
1944: dynamicContent.setText(elContent);
1945: dynamicContent.addAttribute("id", String
1946: .valueOf(imageId));
1947:
1948: ImageLocalUtil.updateImage(imageId, bytes);
1949:
1950: continue;
1951: }
1952:
1953: if ((version > JournalArticleImpl.DEFAULT_VERSION)
1954: && (incrementVersion)) {
1955:
1956: Image oldImage = null;
1957:
1958: if (oldImageId > 0) {
1959: oldImage = ImageLocalUtil.getImage(oldImageId);
1960: }
1961:
1962: if (oldImage != null) {
1963: dynamicContent.setText(elContent);
1964: dynamicContent.addAttribute("id", String
1965: .valueOf(imageId));
1966:
1967: bytes = oldImage.getTextObj();
1968:
1969: ImageLocalUtil.updateImage(imageId, bytes);
1970: }
1971:
1972: continue;
1973: }
1974:
1975: Image image = ImageLocalUtil.getImage(imageId);
1976:
1977: if (image != null) {
1978: dynamicContent.setText(elContent);
1979: dynamicContent.addAttribute("id", String
1980: .valueOf(imageId));
1981:
1982: continue;
1983: }
1984:
1985: String defaultElLanguage = "";
1986:
1987: if (!Validator.isNotNull(elLanguage)) {
1988: defaultElLanguage = "_"
1989: + LocaleUtil.toLanguageId(LocaleUtil
1990: .getDefault());
1991: }
1992:
1993: long defaultImageId = journalArticleImageLocalService
1994: .getArticleImageId(groupId, articleId, version,
1995: elName, defaultElLanguage);
1996:
1997: Image defaultImage = ImageLocalUtil
1998: .getImage(defaultImageId);
1999:
2000: if (defaultImage != null) {
2001: dynamicContent.setText(elContent);
2002: dynamicContent.addAttribute("id", String
2003: .valueOf(imageId));
2004:
2005: bytes = defaultImage.getTextObj();
2006:
2007: ImageLocalUtil.updateImage(imageId, bytes);
2008:
2009: continue;
2010: }
2011:
2012: dynamicContent.setText(StringPool.BLANK);
2013: }
2014: }
2015:
2016: protected void saveImages(boolean smallImage, long smallImageId,
2017: File smallFile, byte[] smallBytes) throws SystemException {
2018:
2019: if (smallImage) {
2020: if ((smallFile != null) && (smallBytes != null)) {
2021: ImageLocalUtil.updateImage(smallImageId, smallBytes);
2022: }
2023: } else {
2024: ImageLocalUtil.deleteImage(smallImageId);
2025: }
2026: }
2027:
2028: protected void sendEmail(JournalArticle article, String articleURL,
2029: PortletPreferences prefs, String emailType)
2030: throws PortalException, SystemException {
2031:
2032: try {
2033: if (prefs == null) {
2034: return;
2035: } else if (emailType.equals("denied")
2036: && JournalUtil
2037: .getEmailArticleApprovalDeniedEnabled(prefs)) {
2038: } else if (emailType.equals("granted")
2039: && JournalUtil
2040: .getEmailArticleApprovalGrantedEnabled(prefs)) {
2041: } else if (emailType.equals("requested")
2042: && JournalUtil
2043: .getEmailArticleApprovalRequestedEnabled(prefs)) {
2044: } else if (emailType.equals("review")
2045: && JournalUtil.getEmailArticleReviewEnabled(prefs)) {
2046: } else {
2047: return;
2048: }
2049:
2050: Company company = companyPersistence
2051: .findByPrimaryKey(article.getCompanyId());
2052:
2053: User user = userPersistence.findByPrimaryKey(article
2054: .getUserId());
2055:
2056: articleURL += "&groupId=" + article.getGroupId()
2057: + "&articleId=" + article.getArticleId()
2058: + "&version=" + article.getVersion();
2059:
2060: String portletName = PortalUtil.getPortletTitle(
2061: PortletKeys.JOURNAL, user);
2062:
2063: String fromName = JournalUtil.getEmailFromName(prefs);
2064: String fromAddress = JournalUtil.getEmailFromAddress(prefs);
2065:
2066: String toName = user.getFullName();
2067: String toAddress = user.getEmailAddress();
2068:
2069: if (emailType.equals("requested")
2070: || emailType.equals("review")) {
2071:
2072: String tempToName = fromName;
2073: String tempToAddress = fromAddress;
2074:
2075: fromName = toName;
2076: fromAddress = toAddress;
2077:
2078: toName = tempToName;
2079: toAddress = tempToAddress;
2080: }
2081:
2082: String subject = null;
2083: String body = null;
2084:
2085: if (emailType.equals("denied")) {
2086: subject = JournalUtil
2087: .getEmailArticleApprovalDeniedSubject(prefs);
2088: body = JournalUtil
2089: .getEmailArticleApprovalDeniedBody(prefs);
2090: } else if (emailType.equals("granted")) {
2091: subject = JournalUtil
2092: .getEmailArticleApprovalGrantedSubject(prefs);
2093: body = JournalUtil
2094: .getEmailArticleApprovalGrantedBody(prefs);
2095: } else if (emailType.equals("requested")) {
2096: subject = JournalUtil
2097: .getEmailArticleApprovalRequestedSubject(prefs);
2098: body = JournalUtil
2099: .getEmailArticleApprovalRequestedBody(prefs);
2100: } else if (emailType.equals("review")) {
2101: subject = JournalUtil
2102: .getEmailArticleReviewSubject(prefs);
2103: body = JournalUtil.getEmailArticleReviewBody(prefs);
2104: }
2105:
2106: subject = StringUtil.replace(subject, new String[] {
2107: "[$ARTICLE_ID$]", "[$ARTICLE_TITLE$]",
2108: "[$ARTICLE_URL$]", "[$ARTICLE_VERSION$]",
2109: "[$FROM_ADDRESS$]", "[$FROM_NAME$]",
2110: "[$PORTAL_URL$]", "[$PORTLET_NAME$]",
2111: "[$TO_ADDRESS$]", "[$TO_NAME$]" }, new String[] {
2112: article.getArticleId(), article.getTitle(),
2113: articleURL, String.valueOf(article.getVersion()),
2114: fromAddress, fromName, company.getVirtualHost(),
2115: portletName, toAddress, toName, });
2116:
2117: body = StringUtil.replace(body, new String[] {
2118: "[$ARTICLE_ID$]", "[$ARTICLE_TITLE$]",
2119: "[$ARTICLE_URL$]", "[$ARTICLE_VERSION$]",
2120: "[$FROM_ADDRESS$]", "[$FROM_NAME$]",
2121: "[$PORTAL_URL$]", "[$PORTLET_NAME$]",
2122: "[$TO_ADDRESS$]", "[$TO_NAME$]" }, new String[] {
2123: article.getArticleId(), article.getTitle(),
2124: articleURL, String.valueOf(article.getVersion()),
2125: fromAddress, fromName, company.getVirtualHost(),
2126: portletName, toAddress, toName, });
2127:
2128: InternetAddress from = new InternetAddress(fromAddress,
2129: fromName);
2130:
2131: InternetAddress to = new InternetAddress(toAddress, toName);
2132:
2133: MailMessage message = new MailMessage(from, to, subject,
2134: body, true);
2135:
2136: mailService.sendEmail(message);
2137: } catch (IOException ioe) {
2138: throw new SystemException(ioe);
2139: } catch (PortalException pe) {
2140: throw pe;
2141: }
2142: }
2143:
2144: protected void validate(long groupId, String articleId,
2145: boolean autoArticleId, String title, String content,
2146: String type, String structureId, String templateId,
2147: boolean smallImage, String smallImageURL, File smallFile,
2148: byte[] smallBytes) throws PortalException, SystemException {
2149:
2150: if (!autoArticleId) {
2151: if ((Validator.isNull(articleId))
2152: || (Validator.isNumber(articleId))
2153: || (articleId.indexOf(StringPool.SPACE) != -1)) {
2154:
2155: throw new ArticleIdException();
2156: }
2157:
2158: try {
2159: journalArticlePersistence.findByG_A_V(groupId,
2160: articleId, JournalArticleImpl.DEFAULT_VERSION);
2161:
2162: throw new DuplicateArticleIdException();
2163: } catch (NoSuchArticleException nste) {
2164: }
2165: }
2166:
2167: validate(groupId, title, content, type, structureId,
2168: templateId, smallImage, smallImageURL, smallFile,
2169: smallBytes);
2170: }
2171:
2172: protected void validate(long groupId, String title, String content,
2173: String type, String structureId, String templateId,
2174: boolean smallImage, String smallImageURL, File smallFile,
2175: byte[] smallBytes) throws PortalException, SystemException {
2176:
2177: if (Validator.isNull(title)) {
2178: throw new ArticleTitleException();
2179: } else if (Validator.isNull(content)) {
2180: throw new ArticleContentException();
2181: } else if (Validator.isNull(type)) {
2182: throw new ArticleTypeException();
2183: }
2184:
2185: if (Validator.isNotNull(structureId)) {
2186: journalStructurePersistence.findByG_S(groupId, structureId);
2187:
2188: JournalTemplate template = journalTemplatePersistence
2189: .findByG_T(groupId, templateId);
2190:
2191: if (!template.getStructureId().equals(structureId)) {
2192: throw new NoSuchTemplateException();
2193: }
2194: }
2195:
2196: String[] imageExtensions = PropsUtil
2197: .getArray(PropsUtil.JOURNAL_IMAGE_EXTENSIONS);
2198:
2199: if (smallImage && Validator.isNull(smallImageURL)
2200: && smallFile != null && smallBytes != null) {
2201:
2202: String smallImageName = smallFile.getName();
2203:
2204: if (smallImageName != null) {
2205: boolean validSmallImageExtension = false;
2206:
2207: for (int i = 0; i < imageExtensions.length; i++) {
2208: if (StringPool.STAR.equals(imageExtensions[i])
2209: || StringUtil.endsWith(smallImageName,
2210: imageExtensions[i])) {
2211:
2212: validSmallImageExtension = true;
2213:
2214: break;
2215: }
2216: }
2217:
2218: if (!validSmallImageExtension) {
2219: throw new ArticleSmallImageNameException(
2220: smallImageName);
2221: }
2222: }
2223:
2224: long smallImageMaxSize = GetterUtil.getLong(PropsUtil
2225: .get(PropsUtil.JOURNAL_IMAGE_SMALL_MAX_SIZE));
2226:
2227: if ((smallImageMaxSize > 0)
2228: && ((smallBytes == null) || (smallBytes.length > smallImageMaxSize))) {
2229:
2230: throw new ArticleSmallImageSizeException();
2231: }
2232: }
2233: }
2234:
2235: private static final String _TOKEN_PAGE_BREAK = PropsUtil
2236: .get(PropsUtil.JOURNAL_ARTICLE_TOKEN_PAGE_BREAK);
2237:
2238: private static Log _log = LogFactory
2239: .getLog(JournalArticleLocalServiceImpl.class);
2240:
2241: }
|