001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
019    import com.liferay.portal.kernel.lar.StagedModelType;
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.Lock;
029    import com.liferay.portal.model.Repository;
030    import com.liferay.portal.service.LockLocalServiceUtil;
031    import com.liferay.portal.service.RepositoryLocalServiceUtil;
032    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
033    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
034    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
035    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
036    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
037    import com.liferay.portlet.documentlibrary.model.DLFolder;
038    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
039    import com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalServiceUtil;
040    import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
041    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
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.model.DDMStructure;
047    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
048    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
049    import com.liferay.portlet.expando.model.ExpandoBridge;
050    import com.liferay.portlet.trash.model.TrashEntry;
051    import com.liferay.portlet.trash.service.TrashEntryLocalServiceUtil;
052    
053    import java.io.IOException;
054    import java.io.InputStream;
055    
056    import java.util.HashMap;
057    import java.util.List;
058    import java.util.Map;
059    
060    /**
061     * @author Brian Wing Shun Chan
062     * @author Alexander Chow
063     */
064    public class DLFileEntryImpl extends DLFileEntryBaseImpl {
065    
066            public DLFileEntryImpl() {
067            }
068    
069            @Override
070            public String buildTreePath() throws PortalException, SystemException {
071                    DLFolder dlFolder = getFolder();
072    
073                    return dlFolder.buildTreePath();
074            }
075    
076            @Override
077            public InputStream getContentStream()
078                    throws PortalException, SystemException {
079    
080                    return getContentStream(getVersion());
081            }
082    
083            @Override
084            public InputStream getContentStream(String version)
085                    throws PortalException, SystemException {
086    
087                    return DLFileEntryServiceUtil.getFileAsStream(
088                            getFileEntryId(), version);
089            }
090    
091            @Override
092            public long getDataRepositoryId() {
093                    return DLFolderConstants.getDataRepositoryId(
094                            getGroupId(), getFolderId());
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, SystemException {
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 =
153                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);
154    
155                    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
156    
157                    for (DDMStructure ddmStructure : ddmStructures) {
158                            DLFileEntryMetadata dlFileEntryMetadata =
159                                    DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(
160                                            ddmStructure.getStructureId(), fileVersionId);
161    
162                            Fields fields = StorageEngineUtil.getFields(
163                                    dlFileEntryMetadata.getDDMStorageId());
164    
165                            fieldsMap.put(ddmStructure.getStructureKey(), fields);
166                    }
167    
168                    return fieldsMap;
169            }
170    
171            @Override
172            public DLFileVersion getFileVersion()
173                    throws PortalException, SystemException {
174    
175                    return getFileVersion(getVersion());
176            }
177    
178            @Override
179            public DLFileVersion getFileVersion(String version)
180                    throws PortalException, SystemException {
181    
182                    return DLFileVersionLocalServiceUtil.getFileVersion(
183                            getFileEntryId(), version);
184            }
185    
186            @Override
187            public List<DLFileVersion> getFileVersions(int status)
188                    throws SystemException {
189    
190                    return DLFileVersionLocalServiceUtil.getFileVersions(
191                            getFileEntryId(), status);
192            }
193    
194            @Override
195            public int getFileVersionsCount(int status) throws SystemException {
196                    return DLFileVersionLocalServiceUtil.getFileVersionsCount(
197                            getFileEntryId(), status);
198            }
199    
200            @Override
201            public DLFolder getFolder() throws PortalException, SystemException {
202                    if (getFolderId() <= 0) {
203                            return new DLFolderImpl();
204                    }
205    
206                    return DLFolderLocalServiceUtil.getFolder(getFolderId());
207            }
208    
209            @Override
210            public String getIcon() {
211                    return DLUtil.getFileIcon(getExtension());
212            }
213    
214            @Override
215            public DLFileVersion getLatestFileVersion(boolean trusted)
216                    throws PortalException, SystemException {
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            @Override
282            public long getVersionUserId() {
283                    long versionUserId = 0;
284    
285                    try {
286                            DLFileVersion dlFileVersion = getFileVersion();
287    
288                            versionUserId = dlFileVersion.getUserId();
289                    }
290                    catch (Exception e) {
291                            _log.error(e, e);
292                    }
293    
294                    return versionUserId;
295            }
296    
297            /**
298             * @deprecated As of 6.2.0, replaced by {@link DLFileVersion#getUserName()}
299             */
300            @Override
301            public String getVersionUserName() {
302                    String versionUserName = StringPool.BLANK;
303    
304                    try {
305                            DLFileVersion dlFileVersion = getFileVersion();
306    
307                            versionUserName = dlFileVersion.getUserName();
308                    }
309                    catch (Exception e) {
310                            _log.error(e, e);
311                    }
312    
313                    return versionUserName;
314            }
315    
316            /**
317             * @deprecated As of 6.2.0, replaced by {@link DLFileVersion#getUserUuid()}
318             */
319            @Override
320            public String getVersionUserUuid() {
321                    String versionUserUuid = StringPool.BLANK;
322    
323                    try {
324                            DLFileVersion dlFileVersion = getFileVersion();
325    
326                            versionUserUuid = dlFileVersion.getUserUuid();
327                    }
328                    catch (Exception e) {
329                            _log.error(e, e);
330                    }
331    
332                    return versionUserUuid;
333            }
334    
335            @Override
336            public boolean hasLock() {
337                    try {
338                            return DLFileEntryServiceUtil.hasFileEntryLock(getFileEntryId());
339                    }
340                    catch (Exception e) {
341                    }
342    
343                    return false;
344            }
345    
346            @Override
347            public boolean isCheckedOut() {
348                    try {
349                            return DLFileEntryServiceUtil.isFileEntryCheckedOut(
350                                    getFileEntryId());
351                    }
352                    catch (Exception e) {
353                    }
354    
355                    return false;
356            }
357    
358            @Override
359            public boolean isInHiddenFolder() {
360                    try {
361                            long repositoryId = getRepositoryId();
362    
363                            Repository repository = RepositoryLocalServiceUtil.getRepository(
364                                    repositoryId);
365    
366                            long dlFolderId = repository.getDlFolderId();
367    
368                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(dlFolderId);
369    
370                            return dlFolder.isHidden();
371                    }
372                    catch (Exception e) {
373                    }
374    
375                    return false;
376            }
377    
378            @Override
379            public boolean isInTrash() {
380                    if (getStatus() == WorkflowConstants.STATUS_IN_TRASH) {
381                            return true;
382                    }
383                    else {
384                            return false;
385                    }
386            }
387    
388            @Override
389            public boolean isInTrashExplicitly() throws SystemException {
390                    if (!isInTrash()) {
391                            return false;
392                    }
393    
394                    TrashEntry trashEntry = TrashEntryLocalServiceUtil.fetchEntry(
395                            getModelClassName(), getTrashEntryClassPK());
396    
397                    if (trashEntry != null) {
398                            return true;
399                    }
400    
401                    return false;
402            }
403    
404            @Override
405            public void setExtraSettings(String extraSettings) {
406                    _extraSettingsProperties = null;
407    
408                    super.setExtraSettings(extraSettings);
409            }
410    
411            @Override
412            public void setExtraSettingsProperties(
413                    UnicodeProperties extraSettingsProperties) {
414    
415                    _extraSettingsProperties = extraSettingsProperties;
416    
417                    super.setExtraSettings(_extraSettingsProperties.toString());
418            }
419    
420            private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
421    
422            private UnicodeProperties _extraSettingsProperties;
423    
424    }