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.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.lock.Lock;
019    import com.liferay.portal.kernel.lock.LockManagerUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.FileUtil;
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.UnicodeProperties;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.model.Repository;
029    import com.liferay.portal.service.RepositoryLocalServiceUtil;
030    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
032    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
033    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
034    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
035    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
036    import com.liferay.portlet.documentlibrary.model.DLFolder;
037    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
038    import com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalServiceUtil;
039    import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
040    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
041    import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
042    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
043    import com.liferay.portlet.documentlibrary.service.DLFileVersionServiceUtil;
044    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
045    import com.liferay.portlet.documentlibrary.util.DLUtil;
046    import com.liferay.portlet.dynamicdatamapping.DDMFormValues;
047    import com.liferay.portlet.dynamicdatamapping.DDMStructure;
048    import com.liferay.portlet.dynamicdatamapping.StorageEngineManagerUtil;
049    import com.liferay.portlet.expando.model.ExpandoBridge;
050    import com.liferay.portlet.exportimport.lar.StagedModelType;
051    
052    import java.io.IOException;
053    import java.io.InputStream;
054    
055    import java.util.HashMap;
056    import java.util.List;
057    import java.util.Map;
058    
059    /**
060     * @author Brian Wing Shun Chan
061     * @author Alexander Chow
062     */
063    public class DLFileEntryImpl extends DLFileEntryBaseImpl {
064    
065            @Override
066            public String buildTreePath() throws PortalException {
067                    if (getFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
068                            return StringPool.SLASH;
069                    }
070    
071                    DLFolder dlFolder = getFolder();
072    
073                    return dlFolder.buildTreePath();
074            }
075    
076            @Override
077            public InputStream getContentStream() throws PortalException {
078                    return getContentStream(getVersion());
079            }
080    
081            @Override
082            public InputStream getContentStream(String version) throws PortalException {
083                    return DLFileEntryServiceUtil.getFileAsStream(
084                            getFileEntryId(), version);
085            }
086    
087            @Override
088            public long getDataRepositoryId() {
089                    return DLFolderConstants.getDataRepositoryId(
090                            getGroupId(), getFolderId());
091            }
092    
093            @Override
094            public Map<String, DDMFormValues> getDDMFormValuesMap(long fileVersionId)
095                    throws PortalException {
096    
097                    Map<String, DDMFormValues> ddmFormValuesMap = new HashMap<>();
098    
099                    DLFileVersion dlFileVersion =
100                            DLFileVersionLocalServiceUtil.getFileVersion(fileVersionId);
101    
102                    long fileEntryTypeId = dlFileVersion.getFileEntryTypeId();
103    
104                    if (fileEntryTypeId <= 0) {
105                            return ddmFormValuesMap;
106                    }
107    
108                    DLFileEntryType dlFileEntryType = getDLFileEntryType();
109    
110                    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
111    
112                    for (DDMStructure ddmStructure : ddmStructures) {
113                            DLFileEntryMetadata dlFileEntryMetadata =
114                                    DLFileEntryMetadataLocalServiceUtil.fetchFileEntryMetadata(
115                                            ddmStructure.getStructureId(), fileVersionId);
116    
117                            if (dlFileEntryMetadata != null) {
118                                    DDMFormValues ddmFormValues =
119                                            StorageEngineManagerUtil.getDDMFormValues(
120                                                    dlFileEntryMetadata.getDDMStorageId());
121    
122                                    ddmFormValuesMap.put(
123                                            ddmStructure.getStructureKey(), ddmFormValues);
124                            }
125                    }
126    
127                    return ddmFormValuesMap;
128            }
129    
130            @Override
131            public DLFileEntryType getDLFileEntryType() throws PortalException {
132                    return DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(
133                            getFileEntryTypeId());
134            }
135    
136            @Override
137            public ExpandoBridge getExpandoBridge() {
138                    try {
139                            DLFileVersion dlFileVersion = getFileVersion();
140    
141                            return dlFileVersion.getExpandoBridge();
142                    }
143                    catch (Exception e) {
144                            if (_log.isWarnEnabled()) {
145                                    _log.warn(e, e);
146                            }
147                    }
148    
149                    return null;
150            }
151    
152            @Override
153            public String getExtraSettings() {
154                    if (_extraSettingsProperties == null) {
155                            return super.getExtraSettings();
156                    }
157                    else {
158                            return _extraSettingsProperties.toString();
159                    }
160            }
161    
162            @Override
163            public UnicodeProperties getExtraSettingsProperties() {
164                    if (_extraSettingsProperties == null) {
165                            _extraSettingsProperties = new UnicodeProperties(true);
166    
167                            try {
168                                    _extraSettingsProperties.load(super.getExtraSettings());
169                            }
170                            catch (IOException ioe) {
171                                    _log.error(ioe, ioe);
172                            }
173                    }
174    
175                    return _extraSettingsProperties;
176            }
177    
178            @Override
179            public List<DLFileShortcut> getFileShortcuts() {
180                    return DLFileShortcutLocalServiceUtil.getFileShortcuts(
181                            getFileEntryId());
182            }
183    
184            @Override
185            public DLFileVersion getFileVersion() throws PortalException {
186                    return getFileVersion(getVersion());
187            }
188    
189            @Override
190            public DLFileVersion getFileVersion(String version) throws PortalException {
191                    return DLFileVersionLocalServiceUtil.getFileVersion(
192                            getFileEntryId(), version);
193            }
194    
195            @Override
196            public List<DLFileVersion> getFileVersions(int status) {
197                    return DLFileVersionLocalServiceUtil.getFileVersions(
198                            getFileEntryId(), status);
199            }
200    
201            @Override
202            public int getFileVersionsCount(int status) {
203                    return DLFileVersionLocalServiceUtil.getFileVersionsCount(
204                            getFileEntryId(), status);
205            }
206    
207            @Override
208            public DLFolder getFolder() throws PortalException {
209                    if (getFolderId() <= 0) {
210                            return new DLFolderImpl();
211                    }
212    
213                    return DLFolderLocalServiceUtil.getFolder(getFolderId());
214            }
215    
216            @Override
217            public String getIcon() {
218                    return DLUtil.getFileIcon(getExtension());
219            }
220    
221            @Override
222            public String getIconCssClass() {
223                    return DLUtil.getFileIconCssClass(getExtension());
224            }
225    
226            @Override
227            public DLFileVersion getLatestFileVersion(boolean trusted)
228                    throws PortalException {
229    
230                    if (trusted) {
231                            return DLFileVersionLocalServiceUtil.getLatestFileVersion(
232                                    getFileEntryId(), false);
233                    }
234                    else {
235                            return DLFileVersionServiceUtil.getLatestFileVersion(
236                                    getFileEntryId());
237                    }
238            }
239    
240            @Override
241            public Lock getLock() {
242                    try {
243                            return LockManagerUtil.getLock(
244                                    DLFileEntry.class.getName(), getFileEntryId());
245                    }
246                    catch (Exception e) {
247                            if (_log.isWarnEnabled()) {
248                                    _log.warn(e, e);
249                            }
250                    }
251    
252                    return null;
253            }
254    
255            @Override
256            public String getLuceneProperties() {
257                    UnicodeProperties extraSettingsProps = getExtraSettingsProperties();
258    
259                    StringBundler sb = new StringBundler(
260                            extraSettingsProps.entrySet().size() + 4);
261    
262                    sb.append(FileUtil.stripExtension(getTitle()));
263                    sb.append(StringPool.SPACE);
264                    sb.append(getDescription());
265                    sb.append(StringPool.SPACE);
266    
267                    for (Map.Entry<String, String> entry : extraSettingsProps.entrySet()) {
268                            String value = GetterUtil.getString(entry.getValue());
269    
270                            sb.append(value);
271                    }
272    
273                    return sb.toString();
274            }
275    
276            @Override
277            public StagedModelType getStagedModelType() {
278                    return new StagedModelType(DLFileEntryConstants.getClassName());
279            }
280    
281            @Override
282            public int getStatus() {
283                    try {
284                            DLFileVersion dlFileVersion = getFileVersion();
285    
286                            return dlFileVersion.getStatus();
287                    }
288                    catch (Exception e) {
289                            return WorkflowConstants.STATUS_APPROVED;
290                    }
291            }
292    
293            @Override
294            public boolean hasLock() {
295                    try {
296                            return DLFileEntryServiceUtil.hasFileEntryLock(getFileEntryId());
297                    }
298                    catch (Exception e) {
299                            if (_log.isWarnEnabled()) {
300                                    _log.warn(e, e);
301                            }
302                    }
303    
304                    return false;
305            }
306    
307            @Override
308            public boolean isCheckedOut() {
309                    try {
310                            return DLFileEntryServiceUtil.isFileEntryCheckedOut(
311                                    getFileEntryId());
312                    }
313                    catch (Exception e) {
314                            if (_log.isWarnEnabled()) {
315                                    _log.warn(e, e);
316                            }
317                    }
318    
319                    return false;
320            }
321    
322            @Override
323            public boolean isInHiddenFolder() {
324                    try {
325                            long repositoryId = getRepositoryId();
326    
327                            Repository repository = RepositoryLocalServiceUtil.getRepository(
328                                    repositoryId);
329    
330                            long dlFolderId = repository.getDlFolderId();
331    
332                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(dlFolderId);
333    
334                            return dlFolder.isHidden();
335                    }
336                    catch (Exception e) {
337                            if (_log.isWarnEnabled()) {
338                                    _log.warn(e, e);
339                            }
340                    }
341    
342                    return false;
343            }
344    
345            @Override
346            public boolean isInTrash() {
347                    if (getStatus() == WorkflowConstants.STATUS_IN_TRASH) {
348                            return true;
349                    }
350                    else {
351                            return false;
352                    }
353            }
354    
355            @Override
356            public void setExtraSettings(String extraSettings) {
357                    _extraSettingsProperties = null;
358    
359                    super.setExtraSettings(extraSettings);
360            }
361    
362            @Override
363            public void setExtraSettingsProperties(
364                    UnicodeProperties extraSettingsProperties) {
365    
366                    _extraSettingsProperties = extraSettingsProperties;
367    
368                    super.setExtraSettings(_extraSettingsProperties.toString());
369            }
370    
371            private static final Log _log = LogFactoryUtil.getLog(
372                    DLFileEntryImpl.class);
373    
374            private UnicodeProperties _extraSettingsProperties;
375    
376    }