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