001    /**
002     * Copyright (c) 2000-present 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.documentlibrary.search;
016    
017    import com.liferay.portal.NoSuchRepositoryEntryException;
018    import com.liferay.portal.kernel.dao.search.RowChecker;
019    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
020    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.Folder;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.WebKeys;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.security.permission.PermissionChecker;
029    import com.liferay.portal.theme.PortletDisplay;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
033    import com.liferay.portlet.documentlibrary.NoSuchFileShortcutException;
034    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
035    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
036    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
037    import com.liferay.portlet.documentlibrary.service.permission.DLFileShortcutPermission;
038    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
039    
040    import javax.servlet.http.HttpServletRequest;
041    
042    /**
043     * @author Sergio Gonz??lez
044     */
045    public class EntriesChecker extends RowChecker {
046    
047            public EntriesChecker(
048                    LiferayPortletRequest liferayPortletRequest,
049                    LiferayPortletResponse liferayPortletResponse) {
050    
051                    super(liferayPortletResponse);
052    
053                    _liferayPortletResponse = liferayPortletResponse;
054    
055                    ThemeDisplay themeDisplay =
056                            (ThemeDisplay)liferayPortletRequest.getAttribute(
057                                    WebKeys.THEME_DISPLAY);
058    
059                    _permissionChecker = themeDisplay.getPermissionChecker();
060    
061                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
062    
063                    String portletName = portletDisplay.getPortletName();
064    
065                    if (portletName.equals(PortletKeys.DOCUMENT_LIBRARY_DISPLAY)) {
066                            _documentLibraryDisplayPortlet = true;
067                    }
068                    else {
069                            _documentLibraryDisplayPortlet = false;
070                    }
071            }
072    
073            @Override
074            public String getAllRowsCheckBox() {
075                    if (_documentLibraryDisplayPortlet) {
076                            return getAllRowsCheckbox(null, getAllRowIds(), getEntryRowIds());
077                    }
078    
079                    return null;
080            }
081    
082            @Override
083            public String getAllRowsCheckBox(HttpServletRequest request) {
084                    if (_documentLibraryDisplayPortlet) {
085                            return getAllRowsCheckbox(
086                                    request, getAllRowIds(), getEntryRowIds());
087                    }
088    
089                    return null;
090            }
091    
092            @Override
093            public String getRowCheckBox(
094                    HttpServletRequest request, boolean checked, boolean disabled,
095                    String primaryKey) {
096    
097                    DLFileShortcut dlFileShortcut = null;
098                    FileEntry fileEntry = null;
099                    Folder folder = null;
100    
101                    long entryId = GetterUtil.getLong(primaryKey);
102    
103                    try {
104                            fileEntry = DLAppServiceUtil.getFileEntry(entryId);
105                    }
106                    catch (Exception e1) {
107                            if (e1 instanceof NoSuchFileEntryException ||
108                                    e1 instanceof NoSuchRepositoryEntryException) {
109    
110                                    try {
111                                            dlFileShortcut = DLAppServiceUtil.getFileShortcut(entryId);
112                                    }
113                                    catch (Exception e2) {
114                                            if (e2 instanceof NoSuchFileShortcutException) {
115                                                    try {
116                                                            folder = DLAppServiceUtil.getFolder(entryId);
117                                                    }
118                                                    catch (Exception e3) {
119                                                            return StringPool.BLANK;
120                                                    }
121                                            }
122                                            else {
123                                                    return StringPool.BLANK;
124                                            }
125                                    }
126                            }
127                            else {
128                                    return StringPool.BLANK;
129                            }
130                    }
131    
132                    boolean showInput = false;
133    
134                    String name = null;
135    
136                    if (fileEntry != null) {
137                            name = FileEntry.class.getSimpleName();
138    
139                            try {
140                                    if (DLFileEntryPermission.contains(
141                                                    _permissionChecker, fileEntry, ActionKeys.DELETE) ||
142                                            DLFileEntryPermission.contains(
143                                                    _permissionChecker, fileEntry, ActionKeys.UPDATE)) {
144    
145                                            showInput = true;
146                                    }
147                            }
148                            catch (Exception e) {
149                            }
150                    }
151                    else if (dlFileShortcut != null) {
152                            name = DLFileShortcut.class.getSimpleName();
153    
154                            try {
155                                    if (DLFileShortcutPermission.contains(
156                                                    _permissionChecker, dlFileShortcut,
157                                                    ActionKeys.DELETE) ||
158                                            DLFileShortcutPermission.contains(
159                                                    _permissionChecker, dlFileShortcut,
160                                                    ActionKeys.UPDATE)) {
161    
162                                            showInput = true;
163                                    }
164                            }
165                            catch (Exception e) {
166                            }
167                    }
168                    else if (folder != null) {
169                            name = Folder.class.getSimpleName();
170    
171                            try {
172                                    if (DLFolderPermission.contains(
173                                                    _permissionChecker, folder, ActionKeys.DELETE) ||
174                                            DLFolderPermission.contains(
175                                                    _permissionChecker, folder, ActionKeys.UPDATE)) {
176    
177                                            showInput = true;
178                                    }
179                            }
180                            catch (Exception e) {
181                            }
182                    }
183    
184                    if (!showInput) {
185                            return StringPool.BLANK;
186                    }
187    
188                    String checkBoxRowIds = getEntryRowIds();
189    
190                    String checkBoxAllRowIds = StringPool.BLANK;
191                    String checkBoxPostOnClick = StringPool.BLANK;
192    
193                    if (_documentLibraryDisplayPortlet) {
194                            checkBoxAllRowIds = "'" + getAllRowIds() + "'";
195                    }
196                    else {
197                            checkBoxAllRowIds = "'#" + getAllRowIds() + "'";
198                            checkBoxPostOnClick =
199                                    _liferayPortletResponse.getNamespace() +
200                                            "toggleActionsButton();";
201                    }
202    
203                    return getRowCheckBox(
204                            request, checked, disabled,
205                            _liferayPortletResponse.getNamespace() + RowChecker.ROW_IDS + name,
206                            primaryKey, checkBoxRowIds, checkBoxAllRowIds, checkBoxPostOnClick);
207            }
208    
209            protected String getEntryRowIds() {
210                    StringBundler sb = new StringBundler(13);
211    
212                    sb.append("['");
213                    sb.append(_liferayPortletResponse.getNamespace());
214                    sb.append(RowChecker.ROW_IDS);
215                    sb.append(Folder.class.getSimpleName());
216                    sb.append("', '");
217                    sb.append(_liferayPortletResponse.getNamespace());
218                    sb.append(RowChecker.ROW_IDS);
219                    sb.append(DLFileShortcut.class.getSimpleName());
220                    sb.append("', '");
221                    sb.append(_liferayPortletResponse.getNamespace());
222                    sb.append(RowChecker.ROW_IDS);
223                    sb.append(FileEntry.class.getSimpleName());
224                    sb.append("']");
225    
226                    return sb.toString();
227            }
228    
229            private final boolean _documentLibraryDisplayPortlet;
230            private final LiferayPortletResponse _liferayPortletResponse;
231            private final PermissionChecker _permissionChecker;
232    
233    }