001    /**
002     * Copyright (c) 2000-2012 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.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.model.Lock;
027    import com.liferay.portal.model.Repository;
028    import com.liferay.portal.service.LockLocalServiceUtil;
029    import com.liferay.portal.service.RepositoryLocalServiceUtil;
030    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
031    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
032    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
033    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
034    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
035    import com.liferay.portlet.documentlibrary.model.DLFolder;
036    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
037    import com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalServiceUtil;
038    import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
039    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
040    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
041    import com.liferay.portlet.documentlibrary.service.DLFileVersionServiceUtil;
042    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
043    import com.liferay.portlet.documentlibrary.util.DLUtil;
044    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
045    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
046    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
047    import com.liferay.portlet.expando.model.ExpandoBridge;
048    
049    import java.io.IOException;
050    import java.io.InputStream;
051    
052    import java.util.HashMap;
053    import java.util.List;
054    import java.util.Map;
055    
056    /**
057     * @author Brian Wing Shun Chan
058     * @author Alexander Chow
059     */
060    public class DLFileEntryImpl extends DLFileEntryBaseImpl {
061    
062            public DLFileEntryImpl() {
063            }
064    
065            public InputStream getContentStream()
066                    throws PortalException, SystemException {
067    
068                    return getContentStream(getVersion());
069            }
070    
071            public InputStream getContentStream(String version)
072                    throws PortalException, SystemException {
073    
074                    return DLFileEntryServiceUtil.getFileAsStream(
075                            getFileEntryId(), version);
076            }
077    
078            public long getDataRepositoryId() {
079                    return DLFolderConstants.getDataRepositoryId(
080                            getGroupId(), getFolderId());
081            }
082    
083            @Override
084            public ExpandoBridge getExpandoBridge() {
085                    try {
086                            DLFileVersion dlFileVersion = getFileVersion();
087    
088                            return dlFileVersion.getExpandoBridge();
089                    }
090                    catch (Exception e) {
091                            _log.error(e, e);
092                    }
093    
094                    return null;
095            }
096    
097            @Override
098            public String getExtraSettings() {
099                    if (_extraSettingsProperties == null) {
100                            return super.getExtraSettings();
101                    }
102                    else {
103                            return _extraSettingsProperties.toString();
104                    }
105            }
106    
107            public UnicodeProperties getExtraSettingsProperties() {
108                    if (_extraSettingsProperties == null) {
109                            _extraSettingsProperties = new UnicodeProperties(true);
110    
111                            try {
112                                    _extraSettingsProperties.load(super.getExtraSettings());
113                            }
114                            catch (IOException ioe) {
115                                    _log.error(ioe, ioe);
116                            }
117                    }
118    
119                    return _extraSettingsProperties;
120            }
121    
122            public Map<String, Fields> getFieldsMap(long fileVersionId)
123                    throws PortalException, SystemException {
124    
125                    Map<String, Fields> fieldsMap = new HashMap<String, Fields>();
126    
127                    DLFileVersion dlFileVersion =
128                            DLFileVersionLocalServiceUtil.getFileVersion(fileVersionId);
129    
130                    long fileEntryTypeId = dlFileVersion.getFileEntryTypeId();
131    
132                    if (fileEntryTypeId <= 0) {
133                            return fieldsMap;
134                    }
135    
136                    DLFileEntryType dlFileEntryType =
137                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);
138    
139                    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
140    
141                    for (DDMStructure ddmStructure : ddmStructures) {
142                            DLFileEntryMetadata dlFileEntryMetadata =
143                                    DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(
144                                            ddmStructure.getStructureId(), fileVersionId);
145    
146                            Fields fields = StorageEngineUtil.getFields(
147                                    dlFileEntryMetadata.getDDMStorageId());
148    
149                            fieldsMap.put(ddmStructure.getStructureKey(), fields);
150                    }
151    
152                    return fieldsMap;
153            }
154    
155            public DLFileVersion getFileVersion()
156                    throws PortalException, SystemException {
157    
158                    return getFileVersion(getVersion());
159            }
160    
161            public DLFileVersion getFileVersion(String version)
162                    throws PortalException, SystemException {
163    
164                    return DLFileVersionLocalServiceUtil.getFileVersion(
165                            getFileEntryId(), version);
166            }
167    
168            public List<DLFileVersion> getFileVersions(int status)
169                    throws SystemException {
170    
171                    return DLFileVersionLocalServiceUtil.getFileVersions(
172                            getFileEntryId(), status);
173            }
174    
175            public int getFileVersionsCount(int status) throws SystemException {
176                    return DLFileVersionLocalServiceUtil.getFileVersionsCount(
177                            getFileEntryId(), status);
178            }
179    
180            public DLFolder getFolder() {
181                    DLFolder dlFolder = new DLFolderImpl();
182    
183                    if (getFolderId() > 0) {
184                            try {
185                                    dlFolder = DLFolderLocalServiceUtil.getFolder(getFolderId());
186                            }
187                            catch (NoSuchFolderException nsfe) {
188                                    try {
189                                            DLFileVersion dlFileVersion = getLatestFileVersion(true);
190    
191                                            if (!dlFileVersion.isInTrash()) {
192                                                    _log.error(nsfe, nsfe);
193                                            }
194                                    }
195                                    catch (Exception e) {
196                                            _log.error(e, e);
197                                    }
198                            }
199                            catch (Exception e) {
200                                    _log.error(e, e);
201                            }
202                    }
203    
204                    return dlFolder;
205            }
206    
207            public String getIcon() {
208                    return DLUtil.getFileIcon(getExtension());
209            }
210    
211            public DLFileVersion getLatestFileVersion(boolean trusted)
212                    throws PortalException, SystemException {
213    
214                    if (trusted) {
215                            return DLFileVersionLocalServiceUtil.getLatestFileVersion(
216                                    getFileEntryId(), false);
217                    }
218                    else {
219                            return DLFileVersionServiceUtil.getLatestFileVersion(
220                                    getFileEntryId());
221                    }
222            }
223    
224            public Lock getLock() {
225                    try {
226                            return LockLocalServiceUtil.getLock(
227                                    DLFileEntry.class.getName(), getFileEntryId());
228                    }
229                    catch (Exception e) {
230                    }
231    
232                    return null;
233            }
234    
235            public String getLuceneProperties() {
236                    UnicodeProperties extraSettingsProps = getExtraSettingsProperties();
237    
238                    StringBundler sb = new StringBundler(
239                            extraSettingsProps.entrySet().size() + 4);
240    
241                    sb.append(FileUtil.stripExtension(getTitle()));
242                    sb.append(StringPool.SPACE);
243                    sb.append(getDescription());
244                    sb.append(StringPool.SPACE);
245    
246                    for (Map.Entry<String, String> entry : extraSettingsProps.entrySet()) {
247                            String value = GetterUtil.getString(entry.getValue());
248    
249                            sb.append(value);
250                    }
251    
252                    return sb.toString();
253            }
254    
255            public DLFolder getTrashContainer() {
256                    DLFolder dlFolder = getFolder();
257    
258                    if (dlFolder.isInTrash()) {
259                            return dlFolder;
260                    }
261    
262                    return dlFolder.getTrashContainer();
263            }
264    
265            public boolean hasLock() {
266                    try {
267                            return DLFileEntryServiceUtil.hasFileEntryLock(getFileEntryId());
268                    }
269                    catch (Exception e) {
270                    }
271    
272                    return false;
273            }
274    
275            public boolean isCheckedOut() {
276                    try {
277                            return DLFileEntryServiceUtil.isFileEntryCheckedOut(
278                                    getFileEntryId());
279                    }
280                    catch (Exception e) {
281                    }
282    
283                    return false;
284            }
285    
286            public boolean isInHiddenFolder() {
287                    try {
288                            long repositoryId = getRepositoryId();
289    
290                            Repository repository = RepositoryLocalServiceUtil.getRepository(
291                                    repositoryId);
292    
293                            long dlFolderId = repository.getDlFolderId();
294    
295                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(dlFolderId);
296    
297                            return dlFolder.isHidden();
298                    }
299                    catch (Exception e) {
300                    }
301    
302                    return false;
303            }
304    
305            public boolean isInTrashContainer() {
306                    if (getTrashContainer() != null) {
307                            return true;
308                    }
309                    else {
310                            return false;
311                    }
312            }
313    
314            @Override
315            public void setExtraSettings(String extraSettings) {
316                    _extraSettingsProperties = null;
317    
318                    super.setExtraSettings(extraSettings);
319            }
320    
321            public void setExtraSettingsProperties(
322                    UnicodeProperties extraSettingsProperties) {
323    
324                    _extraSettingsProperties = extraSettingsProperties;
325    
326                    super.setExtraSettings(_extraSettingsProperties.toString());
327            }
328    
329            private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
330    
331            private UnicodeProperties _extraSettingsProperties;
332    
333    }