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