001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.search;
016    
017    import com.liferay.portal.kernel.dao.search.RowChecker;
018    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
019    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.security.permission.ActionKeys;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portlet.journal.NoSuchArticleException;
028    import com.liferay.portlet.journal.model.JournalArticle;
029    import com.liferay.portlet.journal.model.JournalFolder;
030    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
031    import com.liferay.portlet.journal.service.JournalFolderServiceUtil;
032    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
033    import com.liferay.portlet.journal.service.permission.JournalFolderPermission;
034    
035    import javax.servlet.http.HttpServletRequest;
036    
037    /**
038     * @author Sergio Gonz??lez
039     */
040    public class EntriesChecker extends RowChecker {
041    
042            public EntriesChecker(
043                    LiferayPortletRequest liferayPortletRequest,
044                    LiferayPortletResponse liferayPortletResponse) {
045    
046                    super(liferayPortletResponse);
047    
048                    _liferayPortletResponse = liferayPortletResponse;
049    
050                    ThemeDisplay themeDisplay =
051                            (ThemeDisplay)liferayPortletRequest.getAttribute(
052                                    WebKeys.THEME_DISPLAY);
053    
054                    _permissionChecker = themeDisplay.getPermissionChecker();
055            }
056    
057            @Override
058            public String getAllRowsCheckBox() {
059                    return null;
060            }
061    
062            @Override
063            public String getRowCheckBox(
064                    HttpServletRequest request, boolean checked, boolean disabled,
065                    String primaryKey) {
066    
067                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
068                            WebKeys.THEME_DISPLAY);
069    
070                    JournalArticle article = null;
071                    JournalFolder folder = null;
072    
073                    String articleId = GetterUtil.getString(primaryKey);
074    
075                    try {
076                            article = JournalArticleServiceUtil.getArticle(
077                                    themeDisplay.getScopeGroupId(), articleId);
078                    }
079                    catch (Exception e1) {
080                            if (e1 instanceof NoSuchArticleException) {
081                                    try {
082                                            long folderId = GetterUtil.getLong(primaryKey);
083    
084                                            folder = JournalFolderServiceUtil.getFolder(folderId);
085                                    }
086                                    catch (Exception e2) {
087                                            return StringPool.BLANK;
088                                    }
089                            }
090                    }
091    
092                    boolean showInput = false;
093    
094                    String name = null;
095    
096                    if (article != null) {
097                            name = JournalArticle.class.getSimpleName();
098    
099                            try {
100                                    if (JournalArticlePermission.contains(
101                                                    _permissionChecker, article, ActionKeys.DELETE) ||
102                                            JournalArticlePermission.contains(
103                                                    _permissionChecker, article, ActionKeys.EXPIRE) ||
104                                            JournalArticlePermission.contains(
105                                                    _permissionChecker, article, ActionKeys.UPDATE)) {
106    
107                                            showInput = true;
108                                    }
109                            }
110                            catch (Exception e) {
111                            }
112                    }
113                    else if (folder != null) {
114                            name = JournalFolder.class.getSimpleName();
115    
116                            try {
117                                    if (JournalFolderPermission.contains(
118                                                    _permissionChecker, folder, ActionKeys.DELETE)) {
119    
120                                            showInput = true;
121                                    }
122                            }
123                            catch (Exception e) {
124                            }
125                    }
126    
127                    if (!showInput) {
128                            return StringPool.BLANK;
129                    }
130    
131                    StringBundler sb = new StringBundler();
132    
133                    sb.append("['");
134                    sb.append(_liferayPortletResponse.getNamespace());
135                    sb.append(RowChecker.ROW_IDS);
136                    sb.append(JournalFolder.class.getSimpleName());
137                    sb.append("Checkbox', '");
138                    sb.append(_liferayPortletResponse.getNamespace());
139                    sb.append(RowChecker.ROW_IDS);
140                    sb.append(JournalArticle.class.getSimpleName());
141                    sb.append("Checkbox']");
142    
143                    String checkBoxRowIds = sb.toString();
144    
145                    return getRowCheckBox(
146                            checked, disabled,
147                            _liferayPortletResponse.getNamespace() + RowChecker.ROW_IDS +
148                                    name + "Checkbox",
149                            primaryKey, checkBoxRowIds, "'#" + getAllRowIds() + "Checkbox'",
150                            StringPool.BLANK);
151            }
152    
153            private LiferayPortletResponse _liferayPortletResponse;
154            private PermissionChecker _permissionChecker;
155    
156    }