Java tutorial
/** * Copyright (C) 2005-2014 Rivet Logic Corporation. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; version 2 * of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ package com.rivetlogic.portlet; import static com.rivetlogic.portlet.AnnouncerPortlet.ARTICLES_RAW; import static com.rivetlogic.portlet.AnnouncerPortlet.ARTICLE_ID; import static com.rivetlogic.portlet.AnnouncerPortlet.ARTICLE_ID_CONSECUTIVE; import static com.rivetlogic.portlet.AnnouncerPortlet.ARTICLE_ID_WITH_VERSION; import static com.rivetlogic.portlet.AnnouncerPortlet.ARTICLE_RAW; import static com.rivetlogic.portlet.AnnouncerPortlet.LR_EMPTY_VALUE; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.json.JSONObject; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.servlet.HttpHeaders; import com.liferay.portal.kernel.util.DateUtil; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.theme.ThemeDisplay; import com.liferay.portal.util.PortalUtil; import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil; import com.rivetlogic.portlet.model.Completed; import com.rivetlogic.portlet.model.NotCompleted; import com.rivetlogic.portlet.service.CompletedLocalServiceUtil; import com.rivetlogic.portlet.service.NotCompletedLocalServiceUtil; import com.rivetlogic.portlet.service.persistence.CompletedPK; import com.rivetlogic.portlet.service.persistence.NotCompletedPK; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import javax.portlet.PortletPreferences; import javax.portlet.PortletResponse; import javax.portlet.ReadOnlyException; import javax.portlet.ValidatorException; import javax.servlet.http.HttpServletResponse; /** * The Class AnnouncerTools. */ public class AnnouncerTools { /** The log. */ private static Log LOG = LogFactoryUtil.getLog(AnnouncerTools.class); /** The Constant ONE_DAY. */ private static final long ONE_DAY = 1; /** * Checks if is completed. * * @param userId the user id * @param layoutPK the layout pk * @return true, if is completed * @throws SystemException the system exception */ private static boolean isCompleted(String userId, String layoutPK) throws SystemException { if (CompletedLocalServiceUtil.userExist(userId, layoutPK)) { return true; } return false; } /** * Checks if is not completed. * * @param userId the user id * @param layoutPK the layout pk * @return true, if is not completed * @throws SystemException the system exception */ private static boolean isNotCompleted(String userId, String layoutPK) throws SystemException { if (NotCompletedLocalServiceUtil.userExist(userId, layoutPK)) { return true; } return false; } /** * Checks if is same articles set id. * * @param userId the user id * @param layoutPK the layout pk * @param articlesSetId the articles set id * @return true, if is same articles set id */ private static boolean isSameArticlesSetId(String userId, String layoutPK, String articlesSetId) { try { CompletedPK userLayoutPKC = new CompletedPK(); userLayoutPKC.setLAYOUT_PK(layoutPK); userLayoutPKC.setUSER_ID(userId); Completed user = CompletedLocalServiceUtil.getCompleted(userLayoutPKC); if (!user.getARTICLES_SET_ID().equals(articlesSetId)) { return false; } } catch (PortalException e) { LOG.error(e); } catch (SystemException e) { LOG.error(e); } return true; } /** * Show not completed. * * @param userId the user id * @param layoutPK the layout pk * @return true, if successful */ private static boolean showNotCompleted(String userId, String layoutPK) { Date currentDate = null; Date notCompletedDate = null; NotCompletedPK userLayoutPKNC = new NotCompletedPK(); userLayoutPKNC.setLAYOUT_PK(layoutPK); userLayoutPKNC.setUSER_ID(userId); try { NotCompleted user = NotCompletedLocalServiceUtil.getNotCompleted(userLayoutPKNC); notCompletedDate = user.getPANEL_CLOSE_DATE(); currentDate = new Date(); int diff = DateUtil.getDaysBetween(currentDate, notCompletedDate); if (diff >= ONE_DAY) { return true; } } catch (PortalException e) { LOG.error(e); } catch (SystemException e) { LOG.error(e); } return false; } /* * This method decides if it's necessary to show the portlet will not be * shown if: userId is in Completed table, userId is in NonCompleted table * but it has been no longer than a day, since it was last shown to the user */ /** * Show announcer. * * @param userId the user id * @param layoutPK the layout pk * @param articlesSetId the articles set id * @return true, if successful * @throws SystemException the system exception */ public static boolean showAnnouncer(String userId, String layoutPK, String articlesSetId) throws SystemException { if (isCompleted(userId, layoutPK)) { if (isSameArticlesSetId(userId, layoutPK, articlesSetId)) { return false; } else { return true; } } else { if (isNotCompleted(userId, layoutPK)) { if (showNotCompleted(userId, layoutPK)) { return true; } else { return false; } } else { // userId is not on Completed or NotCompleted tables return true; } } } /** * Adds the to completed. * * @param userId the user id * @param layoutPK the layout pk * @param articlesSetId the articles set id */ public static void addToCompleted(String userId, String layoutPK, String articlesSetId) { CompletedPK userLayoutPKC = new CompletedPK(); userLayoutPKC.setUSER_ID(userId); userLayoutPKC.setLAYOUT_PK(layoutPK); Completed newCompleted = CompletedLocalServiceUtil.createCompleted(userLayoutPKC); newCompleted.setARTICLES_SET_ID(articlesSetId); try { if (isCompleted(userId, layoutPK)) { // Delete to update new // completed deleteCompleted(userId, layoutPK); } CompletedLocalServiceUtil.addCompleted(newCompleted); if (isNotCompleted(userId, layoutPK)) { // If userId exist on // NotCompleted table, // delete it deleteNotCompleted(userId, layoutPK); } } catch (SystemException e) { LOG.error(e); } } /** * Adds the to not completed. * * @param userId the user id * @param layoutPK the layout pk * @param panelCloseDate the panel close date */ public static void addToNotCompleted(String userId, String layoutPK, Date panelCloseDate) { NotCompletedPK userLayoutPKNC = new NotCompletedPK(); userLayoutPKNC.setUSER_ID(userId); userLayoutPKNC.setLAYOUT_PK(layoutPK); NotCompleted newNotCompleted = NotCompletedLocalServiceUtil.createNotCompleted(userLayoutPKNC); newNotCompleted.setPANEL_CLOSE_DATE(panelCloseDate); try { if (isNotCompleted(userId, layoutPK)) { // Delete to update new not // completed deleteNotCompleted(userId, layoutPK); } NotCompletedLocalServiceUtil.addNotCompleted(newNotCompleted); if (isCompleted(userId, layoutPK)) { // If userId exist on Completed // table, delete it deleteCompleted(userId, layoutPK); // Can happen if article set // id has change } // and user does not complete the new offer } catch (SystemException e) { LOG.error(e); } } /** * Delete completed. * * @param userId the user id * @param layoutPK the layout pk */ private static void deleteCompleted(String userId, String layoutPK) { CompletedPK userLayoutPKC = new CompletedPK(); userLayoutPKC.setUSER_ID(userId); userLayoutPKC.setLAYOUT_PK(layoutPK); try { CompletedLocalServiceUtil.deleteCompleted(userLayoutPKC); } catch (PortalException e) { LOG.error(e); } catch (SystemException e) { LOG.error(e); } } /** * Delete not completed. * * @param userId the user id * @param layoutPK the layout pk */ private static void deleteNotCompleted(String userId, String layoutPK) { NotCompletedPK userLayoutPKNC = new NotCompletedPK(); userLayoutPKNC.setUSER_ID(userId); userLayoutPKNC.setLAYOUT_PK(layoutPK); try { NotCompletedLocalServiceUtil.deleteNotCompleted(userLayoutPKNC); } catch (PortalException e) { LOG.error(e); } catch (SystemException e) { LOG.error(e); } } /** * Removes the article. * * @param pref the pref * @param themeDisplay the theme display * @param articleId the article id */ public static void removeArticle(PortletPreferences pref, ThemeDisplay themeDisplay, String articleId) { String articles = pref.getValue(ARTICLE_RAW, LR_EMPTY_VALUE); articles = articles.replace(String.valueOf(articleId), StringPool.BLANK); // remove id articles = articles.replaceAll("\\s+", StringPool.SPACE); String articleIds = articles.trim().replaceAll("\\s", StringPool.COMMA); String articleWithVersionPref = LR_EMPTY_VALUE; String articleConsecutive = LR_EMPTY_VALUE; String articleWithVersion = LR_EMPTY_VALUE; articleWithVersionPref = pref.getValue(ARTICLE_ID_WITH_VERSION, LR_EMPTY_VALUE); // articleId1:version // ... articleConsecutive = pref.getValue(ARTICLE_ID_CONSECUTIVE, LR_EMPTY_VALUE); boolean updatedArticleIds = false, updatedArticleVersions = false; updatedArticleIds = !pref.getValue(ARTICLE_ID, LR_EMPTY_VALUE).equals(articleIds); long groupId = themeDisplay.getScopeGroupId(); articleWithVersion = getArticleIdsWithVersion(groupId, articleIds); // Check if the articles version is still the same updatedArticleVersions = !articleWithVersion.equals(articleWithVersionPref); if (updatedArticleIds || updatedArticleVersions) { articleConsecutive = String.valueOf((int) (Double.valueOf(articleConsecutive) + 1)); } try { pref.setValue(ARTICLES_RAW, articles); pref.setValue(ARTICLE_ID, articleIds); pref.setValue(ARTICLE_ID_WITH_VERSION, articleWithVersion); pref.setValue(ARTICLE_ID_CONSECUTIVE, articleConsecutive); pref.store(); } catch (ReadOnlyException e) { LOG.error(e); } catch (ValidatorException e) { LOG.error(e); } catch (IOException e) { LOG.error(e); } } /** * Gets a string in the form of articleId1:version,articleId2:version ... * * @param groupId the group id * @param articleIds the article ids * @return the article ids with version */ public static String getArticleIdsWithVersion(long groupId, String articleIds) { StringBuilder articleWithVersionBuilder = new StringBuilder(); for (String articleId : articleIds.split(StringPool.COMMA)) { double version; try { version = JournalArticleLocalServiceUtil.getLatestVersion(groupId, articleId); articleWithVersionBuilder.append(StringPool.COMMA); articleWithVersionBuilder.append(articleId); articleWithVersionBuilder.append(StringPool.COLON); articleWithVersionBuilder.append(version); } catch (PortalException e) { LOG.error(e); } catch (SystemException e) { LOG.error(e); } } articleWithVersionBuilder.deleteCharAt(0); // remove the initial comma return articleWithVersionBuilder.toString(); } /** * Verify if the specified web content article exists. * @param groupId * @param articleId * @return Returns true if the specified web content article exists. */ public static boolean isArticle(long groupId, String articleId) { boolean exist = false; try { exist = JournalArticleLocalServiceUtil.hasArticle(groupId, articleId); } catch (SystemException e) { LOG.error(e); } return exist; } public static void returnJSONObject(PortletResponse response, JSONObject jsonObj) { HttpServletResponse servletResponse = PortalUtil.getHttpServletResponse(response); servletResponse.setHeader(HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE); servletResponse.setHeader(HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_NO_CACHE_VALUE); servletResponse.setHeader(HttpHeaders.EXPIRES, String.valueOf(AnnouncerPortlet.LR_EMPTY_VALUE)); PrintWriter pw; try { pw = servletResponse.getWriter(); pw.write(jsonObj.toString()); pw.close(); } catch (IOException e) { LOG.error("Error while returning json", e); } } }