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.ThemeDisplay;
030    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
031    import com.liferay.portlet.documentlibrary.NoSuchFileShortcutException;
032    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
033    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
034    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
035    import com.liferay.portlet.documentlibrary.service.permission.DLFileShortcutPermission;
036    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
037    
038    import javax.servlet.http.HttpServletRequest;
039    
040    /**
041     * @author Sergio González
042     */
043    public class EntriesChecker extends RowChecker {
044    
045            public EntriesChecker(
046                    LiferayPortletRequest liferayPortletRequest,
047                    LiferayPortletResponse liferayPortletResponse) {
048    
049                    super(liferayPortletResponse);
050    
051                    _liferayPortletResponse = liferayPortletResponse;
052    
053                    ThemeDisplay themeDisplay =
054                            (ThemeDisplay)liferayPortletRequest.getAttribute(
055                                    WebKeys.THEME_DISPLAY);
056    
057                    _permissionChecker = themeDisplay.getPermissionChecker();
058            }
059    
060            @Override
061            public String getAllRowsCheckBox() {
062                    return null;
063            }
064    
065            @Override
066            public String getRowCheckBox(
067                    HttpServletRequest request, boolean checked, boolean disabled,
068                    String primaryKey) {
069    
070                    DLFileShortcut dlFileShortcut = null;
071                    FileEntry fileEntry = null;
072                    Folder folder = null;
073    
074                    long entryId = GetterUtil.getLong(primaryKey);
075    
076                    try {
077                            fileEntry = DLAppServiceUtil.getFileEntry(entryId);
078                    }
079                    catch (Exception e1) {
080                            if (e1 instanceof NoSuchFileEntryException ||
081                                    e1 instanceof NoSuchRepositoryEntryException) {
082    
083                                    try {
084                                            dlFileShortcut = DLAppServiceUtil.getFileShortcut(entryId);
085                                    }
086                                    catch (Exception e2) {
087                                            if (e2 instanceof NoSuchFileShortcutException) {
088                                                    try {
089                                                            folder = DLAppServiceUtil.getFolder(entryId);
090                                                    }
091                                                    catch (Exception e3) {
092                                                            return StringPool.BLANK;
093                                                    }
094                                            }
095                                            else {
096                                                    return StringPool.BLANK;
097                                            }
098                                    }
099                            }
100                            else {
101                                    return StringPool.BLANK;
102                            }
103                    }
104    
105                    boolean showInput = false;
106    
107                    String name = null;
108    
109                    if (fileEntry != null) {
110                            name = FileEntry.class.getSimpleName();
111    
112                            try {
113                                    if (DLFileEntryPermission.contains(
114                                                    _permissionChecker, fileEntry, ActionKeys.DELETE) ||
115                                            DLFileEntryPermission.contains(
116                                                    _permissionChecker, fileEntry, ActionKeys.UPDATE)) {
117    
118                                            showInput = true;
119                                    }
120                            }
121                            catch (Exception e) {
122                            }
123                    }
124                    else if (dlFileShortcut != null) {
125                            name = DLFileShortcut.class.getSimpleName();
126    
127                            try {
128                                    if (DLFileShortcutPermission.contains(
129                                                    _permissionChecker, dlFileShortcut,
130                                                    ActionKeys.DELETE) ||
131                                            DLFileShortcutPermission.contains(
132                                                    _permissionChecker, dlFileShortcut,
133                                                    ActionKeys.UPDATE)) {
134    
135                                            showInput = true;
136                                    }
137                            }
138                            catch (Exception e) {
139                            }
140                    }
141                    else if (folder != null) {
142                            name = Folder.class.getSimpleName();
143    
144                            try {
145                                    if (DLFolderPermission.contains(
146                                                    _permissionChecker, folder, ActionKeys.DELETE) ||
147                                            DLFolderPermission.contains(
148                                                    _permissionChecker, folder, ActionKeys.UPDATE)) {
149    
150                                            showInput = true;
151                                    }
152                            }
153                            catch (Exception e) {
154                            }
155                    }
156    
157                    if (!showInput) {
158                            return StringPool.BLANK;
159                    }
160    
161                    StringBundler sb = new StringBundler();
162    
163                    sb.append("['");
164                    sb.append(_liferayPortletResponse.getNamespace());
165                    sb.append(RowChecker.ROW_IDS);
166                    sb.append(Folder.class.getSimpleName());
167                    sb.append("Checkbox', '");
168                    sb.append(_liferayPortletResponse.getNamespace());
169                    sb.append(RowChecker.ROW_IDS);
170                    sb.append(DLFileShortcut.class.getSimpleName());
171                    sb.append("Checkbox', '");
172                    sb.append(_liferayPortletResponse.getNamespace());
173                    sb.append(RowChecker.ROW_IDS);
174                    sb.append(FileEntry.class.getSimpleName());
175                    sb.append("Checkbox']");
176    
177                    String checkBoxRowIds = sb.toString();
178    
179                    return getRowCheckBox(
180                            checked, disabled,
181                            _liferayPortletResponse.getNamespace() + RowChecker.ROW_IDS +
182                                    name + "Checkbox",
183                            primaryKey, checkBoxRowIds, "'#" + getAllRowIds() + "Checkbox'",
184                            _liferayPortletResponse.getNamespace() + "toggleActionsButton();");
185            }
186    
187            private LiferayPortletResponse _liferayPortletResponse;
188            private PermissionChecker _permissionChecker;
189    
190    }