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.context.helper;
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 isCancelCheckoutDocumentButtonVisible()
109                    throws PortalException {
110    
111                    if (isCheckinButtonVisible() ||
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 isCheckinButtonVisible() throws PortalException {
145                    if (hasUpdatePermission() && isLockedByMe() && isSupportsLocking()) {
146                            return true;
147                    }
148    
149                    return false;
150            }
151    
152            public boolean isCheckoutDocumentButtonVisible() 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 isFileEntryDeletable() throws PortalException {
174                    if (hasDeletePermission() && !isCheckedOutByOther()) {
175                            return true;
176                    }
177    
178                    return false;
179            }
180    
181            public boolean isLockedByMe() {
182                    if (hasLock()) {
183                            return true;
184                    }
185    
186                    return false;
187            }
188    
189            public boolean isSupportsLocking() {
190                    if (_supportsLocking == null) {
191                            _supportsLocking = _fileEntry.isSupportsLocking();
192                    }
193    
194                    return _supportsLocking;
195            }
196    
197            public boolean isUpdatable() throws PortalException {
198                    if (hasUpdatePermission() && !isCheckedOutByOther()) {
199                            return true;
200                    }
201    
202                    return false;
203            }
204    
205            private void _setValuesForNullFileEntry() {
206                    _checkedOut = false;
207                    _dlFileEntry = true;
208                    _hasDeletePermission = false;
209                    _hasLock = false;
210                    _hasOverrideCheckoutPermission = false;
211                    _hasPermissionsPermission = true;
212                    _hasUpdatePermission = true;
213                    _hasViewPermission = false;
214                    _supportsLocking = false;
215            }
216    
217            private Boolean _checkedOut;
218            private Boolean _dlFileEntry;
219            private final FileEntry _fileEntry;
220            private Boolean _hasDeletePermission;
221            private Boolean _hasLock;
222            private Boolean _hasOverrideCheckoutPermission;
223            private Boolean _hasPermissionsPermission;
224            private Boolean _hasUpdatePermission;
225            private Boolean _hasViewPermission;
226            private final PermissionChecker _permissionChecker;
227            private Boolean _supportsLocking;
228    
229    }