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.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            }
069    
070            @Override
071            public String getAllRowsCheckBox() {
072                    if (_documentLibraryDisplayPortlet) {
073                            return getAllRowsCheckbox(null, getAllRowIds(), getEntryRowIds());
074                    }
075    
076                    return null;
077            }
078    
079            @Override
080            public String getAllRowsCheckBox(HttpServletRequest request) {
081                    if (_documentLibraryDisplayPortlet) {
082                            return getAllRowsCheckbox(
083                                    request, getAllRowIds(), getEntryRowIds());
084                    }
085    
086                    return null;
087            }
088    
089            @Override
090            public String getRowCheckBox(
091                    HttpServletRequest request, boolean checked, boolean disabled,
092                    String primaryKey) {
093    
094                    DLFileShortcut dlFileShortcut = null;
095                    FileEntry fileEntry = null;
096                    Folder folder = null;
097    
098                    long entryId = GetterUtil.getLong(primaryKey);
099    
100                    try {
101                            fileEntry = DLAppServiceUtil.getFileEntry(entryId);
102                    }
103                    catch (Exception e1) {
104                            if (e1 instanceof NoSuchFileEntryException ||
105                                    e1 instanceof NoSuchRepositoryEntryException) {
106    
107                                    try {
108                                            dlFileShortcut = DLAppServiceUtil.getFileShortcut(entryId);
109                                    }
110                                    catch (Exception e2) {
111                                            if (e2 instanceof NoSuchFileShortcutException) {
112                                                    try {
113                                                            folder = DLAppServiceUtil.getFolder(entryId);
114                                                    }
115                                                    catch (Exception e3) {
116                                                            return StringPool.BLANK;
117                                                    }
118                                            }
119                                            else {
120                                                    return StringPool.BLANK;
121                                            }
122                                    }
123                            }
124                            else {
125                                    return StringPool.BLANK;
126                            }
127                    }
128    
129                    boolean showInput = false;
130    
131                    String name = null;
132    
133                    if (fileEntry != null) {
134                            name = FileEntry.class.getSimpleName();
135    
136                            try {
137                                    if (DLFileEntryPermission.contains(
138                                                    _permissionChecker, fileEntry, ActionKeys.DELETE) ||
139                                            DLFileEntryPermission.contains(
140                                                    _permissionChecker, fileEntry, ActionKeys.UPDATE)) {
141    
142                                            showInput = true;
143                                    }
144                            }
145                            catch (Exception e) {
146                            }
147                    }
148                    else if (dlFileShortcut != null) {
149                            name = DLFileShortcut.class.getSimpleName();
150    
151                            try {
152                                    if (DLFileShortcutPermission.contains(
153                                                    _permissionChecker, dlFileShortcut,
154                                                    ActionKeys.DELETE) ||
155                                            DLFileShortcutPermission.contains(
156                                                    _permissionChecker, dlFileShortcut,
157                                                    ActionKeys.UPDATE)) {
158    
159                                            showInput = true;
160                                    }
161                            }
162                            catch (Exception e) {
163                            }
164                    }
165                    else if (folder != null) {
166                            name = Folder.class.getSimpleName();
167    
168                            try {
169                                    if (DLFolderPermission.contains(
170                                                    _permissionChecker, folder, ActionKeys.DELETE) ||
171                                            DLFolderPermission.contains(
172                                                    _permissionChecker, folder, ActionKeys.UPDATE)) {
173    
174                                            showInput = true;
175                                    }
176                            }
177                            catch (Exception e) {
178                            }
179                    }
180    
181                    if (!showInput) {
182                            return StringPool.BLANK;
183                    }
184    
185                    String checkBoxRowIds = getEntryRowIds();
186    
187                    String checkBoxAllRowIds = StringPool.BLANK;
188                    String checkBoxPostOnClick = StringPool.BLANK;
189    
190                    if (_documentLibraryDisplayPortlet) {
191                            checkBoxAllRowIds = "'" + getAllRowIds() + "'";
192                    }
193                    else {
194                            checkBoxAllRowIds = "'#" + getAllRowIds() + "Checkbox'";
195                            checkBoxPostOnClick =
196                                    _liferayPortletResponse.getNamespace() +
197                                            "toggleActionsButton();";
198                    }
199    
200                    return getRowCheckBox(
201                            request, checked, disabled,
202                            _liferayPortletResponse.getNamespace() + RowChecker.ROW_IDS +
203                                    name + "Checkbox",
204                            primaryKey, checkBoxRowIds, checkBoxAllRowIds, checkBoxPostOnClick);
205            }
206    
207            protected String getEntryRowIds() {
208                    StringBundler sb = new StringBundler(13);
209    
210                    sb.append("['");
211                    sb.append(_liferayPortletResponse.getNamespace());
212                    sb.append(RowChecker.ROW_IDS);
213                    sb.append(Folder.class.getSimpleName());
214                    sb.append("Checkbox', '");
215                    sb.append(_liferayPortletResponse.getNamespace());
216                    sb.append(RowChecker.ROW_IDS);
217                    sb.append(DLFileShortcut.class.getSimpleName());
218                    sb.append("Checkbox', '");
219                    sb.append(_liferayPortletResponse.getNamespace());
220                    sb.append(RowChecker.ROW_IDS);
221                    sb.append(FileEntry.class.getSimpleName());
222                    sb.append("Checkbox']");
223    
224                    return sb.toString();
225            }
226    
227            private boolean _documentLibraryDisplayPortlet;
228            private LiferayPortletResponse _liferayPortletResponse;
229            private PermissionChecker _permissionChecker;
230    
231    }