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