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