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