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.kernel.workflow.WorkflowConstants;
029    import com.liferay.portal.model.Lock;
030    import com.liferay.portal.model.Repository;
031    import com.liferay.portal.service.LockLocalServiceUtil;
032    import com.liferay.portal.service.RepositoryLocalServiceUtil;
033    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
034    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
035    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
036    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
037    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
038    import com.liferay.portlet.documentlibrary.model.DLFolder;
039    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
040    import com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalServiceUtil;
041    import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
042    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
043    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
044    import com.liferay.portlet.documentlibrary.service.DLFileVersionServiceUtil;
045    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
046    import com.liferay.portlet.documentlibrary.util.DLUtil;
047    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
048    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
049    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
050    import com.liferay.portlet.expando.model.ExpandoBridge;
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            public DLFileEntryImpl() {
066            }
067    
068            @Override
069            public String buildTreePath() throws PortalException, SystemException {
070                    StringBundler sb = new StringBundler();
071    
072                    buildTreePath(sb, getFolder());
073    
074                    return sb.toString();
075            }
076    
077            @Override
078            public InputStream getContentStream()
079                    throws PortalException, SystemException {
080    
081                    return getContentStream(getVersion());
082            }
083    
084            @Override
085            public InputStream getContentStream(String version)
086                    throws PortalException, SystemException {
087    
088                    return DLFileEntryServiceUtil.getFileAsStream(
089                            getFileEntryId(), version);
090            }
091    
092            @Override
093            public long getDataRepositoryId() {
094                    return DLFolderConstants.getDataRepositoryId(
095                            getGroupId(), getFolderId());
096            }
097    
098            @Override
099            public ExpandoBridge getExpandoBridge() {
100                    try {
101                            DLFileVersion dlFileVersion = getFileVersion();
102    
103                            return dlFileVersion.getExpandoBridge();
104                    }
105                    catch (Exception e) {
106                            _log.error(e, e);
107                    }
108    
109                    return null;
110            }
111    
112            @Override
113            public String getExtraSettings() {
114                    if (_extraSettingsProperties == null) {
115                            return super.getExtraSettings();
116                    }
117                    else {
118                            return _extraSettingsProperties.toString();
119                    }
120            }
121    
122            @Override
123            public UnicodeProperties getExtraSettingsProperties() {
124                    if (_extraSettingsProperties == null) {
125                            _extraSettingsProperties = new UnicodeProperties(true);
126    
127                            try {
128                                    _extraSettingsProperties.load(super.getExtraSettings());
129                            }
130                            catch (IOException ioe) {
131                                    _log.error(ioe, ioe);
132                            }
133                    }
134    
135                    return _extraSettingsProperties;
136            }
137    
138            @Override
139            public Map<String, Fields> getFieldsMap(long fileVersionId)
140                    throws PortalException, SystemException {
141    
142                    Map<String, Fields> fieldsMap = new HashMap<String, Fields>();
143    
144                    DLFileVersion dlFileVersion =
145                            DLFileVersionLocalServiceUtil.getFileVersion(fileVersionId);
146    
147                    long fileEntryTypeId = dlFileVersion.getFileEntryTypeId();
148    
149                    if (fileEntryTypeId <= 0) {
150                            return fieldsMap;
151                    }
152    
153                    DLFileEntryType dlFileEntryType =
154                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);
155    
156                    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
157    
158                    for (DDMStructure ddmStructure : ddmStructures) {
159                            DLFileEntryMetadata dlFileEntryMetadata =
160                                    DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(
161                                            ddmStructure.getStructureId(), fileVersionId);
162    
163                            Fields fields = StorageEngineUtil.getFields(
164                                    dlFileEntryMetadata.getDDMStorageId());
165    
166                            fieldsMap.put(ddmStructure.getStructureKey(), fields);
167                    }
168    
169                    return fieldsMap;
170            }
171    
172            @Override
173            public DLFileVersion getFileVersion()
174                    throws PortalException, SystemException {
175    
176                    return getFileVersion(getVersion());
177            }
178    
179            @Override
180            public DLFileVersion getFileVersion(String version)
181                    throws PortalException, SystemException {
182    
183                    return DLFileVersionLocalServiceUtil.getFileVersion(
184                            getFileEntryId(), version);
185            }
186    
187            @Override
188            public List<DLFileVersion> getFileVersions(int status)
189                    throws SystemException {
190    
191                    return DLFileVersionLocalServiceUtil.getFileVersions(
192                            getFileEntryId(), status);
193            }
194    
195            @Override
196            public int getFileVersionsCount(int status) throws SystemException {
197                    return DLFileVersionLocalServiceUtil.getFileVersionsCount(
198                            getFileEntryId(), status);
199            }
200    
201            @Override
202            public DLFolder getFolder() throws PortalException, SystemException {
203                    if (getFolderId() <= 0) {
204                            return new DLFolderImpl();
205                    }
206    
207                    return DLFolderLocalServiceUtil.getFolder(getFolderId());
208            }
209    
210            @Override
211            public String getIcon() {
212                    return DLUtil.getFileIcon(getExtension());
213            }
214    
215            @Override
216            public DLFileVersion getLatestFileVersion(boolean trusted)
217                    throws PortalException, SystemException {
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(FileEntry.class);
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            @Override
280            public DLFolder getTrashContainer()
281                    throws PortalException, SystemException {
282    
283                    DLFolder dlFolder = null;
284    
285                    try {
286                            dlFolder = getFolder();
287                    }
288                    catch (NoSuchFolderException nsfe) {
289                            return null;
290                    }
291    
292                    if (dlFolder.isInTrash()) {
293                            return dlFolder;
294                    }
295    
296                    return dlFolder.getTrashContainer();
297            }
298    
299            /**
300             * @deprecated As of 6.2.0, replaced by {@link DLFileVersion#getUserId()}
301             */
302            @Override
303            public long getVersionUserId() {
304                    long versionUserId = 0;
305    
306                    try {
307                            DLFileVersion dlFileVersion = getFileVersion();
308    
309                            versionUserId = dlFileVersion.getUserId();
310                    }
311                    catch (Exception e) {
312                            _log.error(e, e);
313                    }
314    
315                    return versionUserId;
316            }
317    
318            /**
319             * @deprecated As of 6.2.0, replaced by {@link DLFileVersion#getUserName()}
320             */
321            @Override
322            public String getVersionUserName() {
323                    String versionUserName = StringPool.BLANK;
324    
325                    try {
326                            DLFileVersion dlFileVersion = getFileVersion();
327    
328                            versionUserName = dlFileVersion.getUserName();
329                    }
330                    catch (Exception e) {
331                            _log.error(e, e);
332                    }
333    
334                    return versionUserName;
335            }
336    
337            /**
338             * @deprecated As of 6.2.0, replaced by {@link DLFileVersion#getUserUuid()}
339             */
340            @Override
341            public String getVersionUserUuid() {
342                    String versionUserUuid = StringPool.BLANK;
343    
344                    try {
345                            DLFileVersion dlFileVersion = getFileVersion();
346    
347                            versionUserUuid = dlFileVersion.getUserUuid();
348                    }
349                    catch (Exception e) {
350                            _log.error(e, e);
351                    }
352    
353                    return versionUserUuid;
354            }
355    
356            @Override
357            public boolean hasLock() {
358                    try {
359                            return DLFileEntryServiceUtil.hasFileEntryLock(getFileEntryId());
360                    }
361                    catch (Exception e) {
362                    }
363    
364                    return false;
365            }
366    
367            @Override
368            public boolean isCheckedOut() {
369                    try {
370                            return DLFileEntryServiceUtil.isFileEntryCheckedOut(
371                                    getFileEntryId());
372                    }
373                    catch (Exception e) {
374                    }
375    
376                    return false;
377            }
378    
379            @Override
380            public boolean isInHiddenFolder() {
381                    try {
382                            long repositoryId = getRepositoryId();
383    
384                            Repository repository = RepositoryLocalServiceUtil.getRepository(
385                                    repositoryId);
386    
387                            long dlFolderId = repository.getDlFolderId();
388    
389                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(dlFolderId);
390    
391                            return dlFolder.isHidden();
392                    }
393                    catch (Exception e) {
394                    }
395    
396                    return false;
397            }
398    
399            @Override
400            public boolean isInTrash() {
401                    if (getStatus() == WorkflowConstants.STATUS_IN_TRASH) {
402                            return true;
403                    }
404                    else {
405                            return false;
406                    }
407            }
408    
409            @Override
410            public boolean isInTrashContainer() {
411                    try {
412                            if (getTrashContainer() != null) {
413                                    return true;
414                            }
415                    }
416                    catch (Exception e) {
417                    }
418    
419                    return false;
420            }
421    
422            @Override
423            public void setExtraSettings(String extraSettings) {
424                    _extraSettingsProperties = null;
425    
426                    super.setExtraSettings(extraSettings);
427            }
428    
429            @Override
430            public void setExtraSettingsProperties(
431                    UnicodeProperties extraSettingsProperties) {
432    
433                    _extraSettingsProperties = extraSettingsProperties;
434    
435                    super.setExtraSettings(_extraSettingsProperties.toString());
436            }
437    
438            protected void buildTreePath(StringBundler sb, DLFolder dlFolder)
439                    throws PortalException, SystemException {
440    
441                    if (dlFolder == null) {
442                            sb.append(StringPool.SLASH);
443                    }
444                    else {
445                            buildTreePath(sb, dlFolder.getParentFolder());
446    
447                            sb.append(dlFolder.getFolderId());
448                            sb.append(StringPool.SLASH);
449                    }
450            }
451    
452            private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
453    
454            private UnicodeProperties _extraSettingsProperties;
455    
456    }