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                    if (_dlFileVersion == null) {
159                            _dlFileVersion = getFileVersion(getVersion());
160                    }
161    
162                    return _dlFileVersion;
163            }
164    
165            public DLFileVersion getFileVersion(String version)
166                    throws PortalException, SystemException {
167    
168                    return DLFileVersionLocalServiceUtil.getFileVersion(
169                            getFileEntryId(), version);
170            }
171    
172            public List<DLFileVersion> getFileVersions(int status)
173                    throws SystemException {
174    
175                    return DLFileVersionLocalServiceUtil.getFileVersions(
176                            getFileEntryId(), status);
177            }
178    
179            public int getFileVersionsCount(int status) throws SystemException {
180                    return DLFileVersionLocalServiceUtil.getFileVersionsCount(
181                            getFileEntryId(), status);
182            }
183    
184            public DLFolder getFolder() {
185                    DLFolder dlFolder = new DLFolderImpl();
186    
187                    if (getFolderId() > 0) {
188                            try {
189                                    dlFolder = DLFolderLocalServiceUtil.getFolder(getFolderId());
190                            }
191                            catch (NoSuchFolderException nsfe) {
192                                    try {
193                                            DLFileVersion dlFileVersion = getLatestFileVersion(true);
194    
195                                            if (!dlFileVersion.isInTrash()) {
196                                                    _log.error(nsfe, nsfe);
197                                            }
198                                    }
199                                    catch (Exception e) {
200                                            _log.error(e, e);
201                                    }
202                            }
203                            catch (Exception e) {
204                                    _log.error(e, e);
205                            }
206                    }
207    
208                    return dlFolder;
209            }
210    
211            public String getIcon() {
212                    return DLUtil.getFileIcon(getExtension());
213            }
214    
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            public Lock getLock() {
229                    try {
230                            return LockLocalServiceUtil.getLock(
231                                    DLFileEntry.class.getName(), getFileEntryId());
232                    }
233                    catch (Exception e) {
234                    }
235    
236                    return null;
237            }
238    
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            public DLFolder getTrashFolder() {
260                    DLFolder dlFolder = getFolder();
261    
262                    if (dlFolder.isInTrash()) {
263                            return dlFolder;
264                    }
265    
266                    return dlFolder.getTrashFolder();
267            }
268    
269            public boolean hasLock() {
270                    try {
271                            return DLFileEntryServiceUtil.hasFileEntryLock(getFileEntryId());
272                    }
273                    catch (Exception e) {
274                    }
275    
276                    return false;
277            }
278    
279            public boolean isCheckedOut() {
280                    try {
281                            return DLFileEntryServiceUtil.isFileEntryCheckedOut(
282                                    getFileEntryId());
283                    }
284                    catch (Exception e) {
285                    }
286    
287                    return false;
288            }
289    
290            public boolean isInHiddenFolder() {
291                    try {
292                            long repositoryId = _dlFileVersion.getRepositoryId();
293    
294                            Repository repository = RepositoryLocalServiceUtil.getRepository(
295                                    repositoryId);
296    
297                            long dlFolderId = repository.getDlFolderId();
298    
299                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(dlFolderId);
300    
301                            return dlFolder.isHidden();
302                    }
303                    catch (Exception e) {
304                    }
305    
306                    return false;
307            }
308    
309            public boolean isInTrashFolder() {
310                    if (getTrashFolder() != null) {
311                            return true;
312                    }
313                    else {
314                            return false;
315                    }
316            }
317    
318            @Override
319            public void setExtraSettings(String extraSettings) {
320                    _extraSettingsProperties = null;
321    
322                    super.setExtraSettings(extraSettings);
323            }
324    
325            public void setExtraSettingsProperties(
326                    UnicodeProperties extraSettingsProperties) {
327    
328                    _extraSettingsProperties = extraSettingsProperties;
329    
330                    super.setExtraSettings(_extraSettingsProperties.toString());
331            }
332    
333            public void setFileVersion(DLFileVersion dlFileVersion) {
334                    _dlFileVersion = dlFileVersion;
335            }
336    
337            private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
338    
339            private DLFileVersion _dlFileVersion;
340            private UnicodeProperties _extraSettingsProperties;
341    
342    }