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