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.display.context.logic;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.repository.model.FileEntry;
019    import com.liferay.portal.security.permission.ActionKeys;
020    import com.liferay.portal.security.permission.PermissionChecker;
021    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
022    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
023    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
024    
025    /**
026     * @author Iv??n Zaera
027     */
028    public class FileEntryDisplayContextHelper {
029    
030            public FileEntryDisplayContextHelper(
031                    PermissionChecker permissionChecker, FileEntry fileEntry) {
032    
033                    _permissionChecker = permissionChecker;
034                    _fileEntry = fileEntry;
035    
036                    if (_fileEntry == null) {
037                            _setValuesForNullFileEntry();
038                    }
039            }
040    
041            public DLFileEntryType getDLFileEntryType() throws PortalException {
042                    if (isDLFileEntry()) {
043                            DLFileEntry dlFileEntry = (DLFileEntry)_fileEntry.getModel();
044    
045                            return dlFileEntry.getDLFileEntryType();
046                    }
047    
048                    return null;
049            }
050    
051            public FileEntry getFileEntry() {
052                    return _fileEntry;
053            }
054    
055            public boolean hasDeletePermission() throws PortalException {
056                    if (_hasDeletePermission == null) {
057                            _hasDeletePermission = DLFileEntryPermission.contains(
058                                    _permissionChecker, _fileEntry, ActionKeys.DELETE);
059                    }
060    
061                    return _hasDeletePermission;
062            }
063    
064            public boolean hasLock() {
065                    if (_hasLock == null) {
066                            _hasLock = _fileEntry.hasLock();
067                    }
068    
069                    return _hasLock;
070            }
071    
072            public boolean hasOverrideCheckoutPermission() throws PortalException {
073                    if (_hasOverrideCheckoutPermission == null) {
074                            _hasOverrideCheckoutPermission = DLFileEntryPermission.contains(
075                                    _permissionChecker, _fileEntry, ActionKeys.OVERRIDE_CHECKOUT);
076                    }
077    
078                    return _hasOverrideCheckoutPermission;
079            }
080    
081            public boolean hasPermissionsPermission() throws PortalException {
082                    if (_hasPermissionsPermission == null) {
083                            _hasPermissionsPermission = DLFileEntryPermission.contains(
084                                    _permissionChecker, _fileEntry, ActionKeys.PERMISSIONS);
085                    }
086    
087                    return _hasPermissionsPermission;
088            }
089    
090            public boolean hasUpdatePermission() throws PortalException {
091                    if (_hasUpdatePermission == null) {
092                            _hasUpdatePermission = DLFileEntryPermission.contains(
093                                    _permissionChecker, _fileEntry, ActionKeys.UPDATE);
094                    }
095    
096                    return _hasUpdatePermission;
097            }
098    
099            public boolean hasViewPermission() throws PortalException {
100                    if (_hasViewPermission == null) {
101                            _hasViewPermission = DLFileEntryPermission.contains(
102                                    _permissionChecker, _fileEntry, ActionKeys.VIEW);
103                    }
104    
105                    return _hasViewPermission;
106            }
107    
108            public boolean isCancelCheckoutDocumentActionAvailable()
109                    throws PortalException {
110    
111                    if (isCheckinActionAvailable() ||
112                            (isCheckedOut() && hasOverrideCheckoutPermission())) {
113    
114                            return true;
115                    }
116    
117                    return false;
118            }
119    
120            public boolean isCheckedOut() {
121                    if (_checkedOut == null) {
122                            _checkedOut = _fileEntry.isCheckedOut();
123                    }
124    
125                    return _checkedOut;
126            }
127    
128            public boolean isCheckedOutByMe() {
129                    if (isCheckedOut() && isLockedByMe()) {
130                            return true;
131                    }
132    
133                    return false;
134            }
135    
136            public boolean isCheckedOutByOther() {
137                    if (isCheckedOut() && !isLockedByMe()) {
138                            return true;
139                    }
140    
141                    return false;
142            }
143    
144            public boolean isCheckinActionAvailable() throws PortalException {
145                    if (hasUpdatePermission() && isLockedByMe() && isSupportsLocking()) {
146                            return true;
147                    }
148    
149                    return false;
150            }
151    
152            public boolean isCheckoutDocumentActionAvailable() throws PortalException {
153                    if (hasUpdatePermission() && !isCheckedOut() && isSupportsLocking()) {
154                            return true;
155                    }
156    
157                    return false;
158            }
159    
160            public boolean isDLFileEntry() {
161                    if (_dlFileEntry == null) {
162                            if (_fileEntry.getModel() instanceof DLFileEntry) {
163                                    _dlFileEntry = true;
164                            }
165                            else {
166                                    _dlFileEntry = false;
167                            }
168                    }
169    
170                    return _dlFileEntry;
171            }
172    
173            public boolean isDownloadActionAvailable() throws PortalException {
174                    return hasViewPermission();
175            }
176    
177            public boolean isEditActionAvailable() throws PortalException {
178                    return isUpdatable();
179            }
180    
181            public boolean isFileEntryDeletable() throws PortalException {
182                    if (hasDeletePermission() && !isCheckedOutByOther()) {
183                            return true;
184                    }
185    
186                    return false;
187            }
188    
189            public boolean isLockedByMe() {
190                    if (hasLock()) {
191                            return true;
192                    }
193    
194                    return false;
195            }
196    
197            public boolean isMoveActionAvailable() throws PortalException {
198                    return isUpdatable();
199            }
200    
201            public boolean isPermissionsButtonVisible() throws PortalException {
202                    return hasPermissionsPermission();
203            }
204    
205            public boolean isSupportsLocking() {
206                    if (_supportsLocking == null) {
207                            _supportsLocking = _fileEntry.isSupportsLocking();
208                    }
209    
210                    return _supportsLocking;
211            }
212    
213            public boolean isUpdatable() throws PortalException {
214                    if (hasUpdatePermission() && !isCheckedOutByOther()) {
215                            return true;
216                    }
217    
218                    return false;
219            }
220    
221            private void _setValuesForNullFileEntry() {
222                    _checkedOut = false;
223                    _dlFileEntry = true;
224                    _hasDeletePermission = false;
225                    _hasLock = false;
226                    _hasOverrideCheckoutPermission = false;
227                    _hasPermissionsPermission = true;
228                    _hasUpdatePermission = true;
229                    _hasViewPermission = false;
230                    _supportsLocking = false;
231            }
232    
233            private Boolean _checkedOut;
234            private Boolean _dlFileEntry;
235            private final FileEntry _fileEntry;
236            private Boolean _hasDeletePermission;
237            private Boolean _hasLock;
238            private Boolean _hasOverrideCheckoutPermission;
239            private Boolean _hasPermissionsPermission;
240            private Boolean _hasUpdatePermission;
241            private Boolean _hasViewPermission;
242            private final PermissionChecker _permissionChecker;
243            private Boolean _supportsLocking;
244    
245    }