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.UPDATE)) {
104    
105                                            showInput = true;
106                                    }
107                            }
108                            catch (Exception e) {
109                            }
110                    }
111                    else if (folder != null) {
112                            name = JournalFolder.class.getSimpleName();
113    
114                            try {
115                                    if (JournalFolderPermission.contains(
116                                                    _permissionChecker, folder, ActionKeys.DELETE)) {
117    
118                                            showInput = true;
119                                    }
120                            }
121                            catch (Exception e) {
122                            }
123                    }
124    
125                    if (!showInput) {
126                            return StringPool.BLANK;
127                    }
128    
129                    StringBundler sb = new StringBundler();
130    
131                    sb.append("['");
132                    sb.append(_liferayPortletResponse.getNamespace());
133                    sb.append(RowChecker.ROW_IDS);
134                    sb.append(JournalFolder.class.getSimpleName());
135                    sb.append("Checkbox', '");
136                    sb.append(_liferayPortletResponse.getNamespace());
137                    sb.append(RowChecker.ROW_IDS);
138                    sb.append(JournalArticle.class.getSimpleName());
139                    sb.append("Checkbox']");
140    
141                    String checkBoxRowIds = sb.toString();
142    
143                    return getRowCheckBox(
144                            checked, disabled,
145                            _liferayPortletResponse.getNamespace() + RowChecker.ROW_IDS +
146                                    name + "Checkbox",
147                            primaryKey, checkBoxRowIds, "'#" + getAllRowIds() + "Checkbox'",
148                            StringPool.BLANK);
149            }
150    
151            private LiferayPortletResponse _liferayPortletResponse;
152            private PermissionChecker _permissionChecker;
153    
154    }