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