001    /**
002     * Copyright (c) 2000-present 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.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.UnicodeProperties;
022    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
023    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
024    import com.liferay.portlet.documentlibrary.model.DLFolder;
025    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
026    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
027    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
028    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
029    import com.liferay.portlet.documentlibrary.util.DLUtil;
030    import com.liferay.portlet.dynamicdatamapping.DDMStructure;
031    import com.liferay.portlet.expando.model.ExpandoBridge;
032    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
033    
034    import java.io.IOException;
035    import java.io.InputStream;
036    
037    import java.util.List;
038    
039    /**
040     * @author Jorge Ferrer
041     * @author Alexander Chow
042     */
043    public class DLFileVersionImpl extends DLFileVersionBaseImpl {
044    
045            @Override
046            public String buildTreePath() throws PortalException {
047                    if (getFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
048                            return StringPool.SLASH;
049                    }
050    
051                    DLFolder dlFolder = getFolder();
052    
053                    return dlFolder.buildTreePath();
054            }
055    
056            @Override
057            public InputStream getContentStream(boolean incrementCounter)
058                    throws PortalException {
059    
060                    return DLFileEntryLocalServiceUtil.getFileAsStream(
061                            getFileEntryId(), getVersion(), incrementCounter);
062            }
063    
064            @Override
065            public List<DDMStructure> getDDMStructures() throws PortalException {
066                    DLFileEntryType dlFileEntryType =
067                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(
068                                    getFileEntryTypeId());
069    
070                    return dlFileEntryType.getDDMStructures();
071            }
072    
073            @Override
074            public DLFileEntryType getDLFileEntryType() throws PortalException {
075                    return DLFileEntryTypeLocalServiceUtil.getFileEntryType(
076                            getFileEntryTypeId());
077            }
078    
079            @Override
080            public ExpandoBridge getExpandoBridge() {
081                    if (_expandoBridge == null) {
082                            _expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
083                                    getCompanyId(), DLFileEntry.class.getName(), getPrimaryKey());
084                    }
085    
086                    return _expandoBridge;
087            }
088    
089            @Override
090            public String getExtraSettings() {
091                    if (_extraSettingsProperties == null) {
092                            return super.getExtraSettings();
093                    }
094                    else {
095                            return _extraSettingsProperties.toString();
096                    }
097            }
098    
099            @Override
100            public UnicodeProperties getExtraSettingsProperties() {
101                    if (_extraSettingsProperties == null) {
102                            _extraSettingsProperties = new UnicodeProperties(true);
103    
104                            try {
105                                    _extraSettingsProperties.load(super.getExtraSettings());
106                            }
107                            catch (IOException ioe) {
108                                    _log.error(ioe, ioe);
109                            }
110                    }
111    
112                    return _extraSettingsProperties;
113            }
114    
115            @Override
116            public DLFileEntry getFileEntry() throws PortalException {
117                    return DLFileEntryLocalServiceUtil.getFileEntry(getFileEntryId());
118            }
119    
120            @Override
121            public DLFolder getFolder() throws PortalException {
122                    if (getFolderId() <= 0) {
123                            return new DLFolderImpl();
124                    }
125    
126                    return DLFolderLocalServiceUtil.getFolder(getFolderId());
127            }
128    
129            @Override
130            public String getIcon() {
131                    return DLUtil.getFileIcon(getExtension());
132            }
133    
134            @Override
135            public void setExtraSettings(String extraSettings) {
136                    _extraSettingsProperties = null;
137    
138                    super.setExtraSettings(extraSettings);
139            }
140    
141            @Override
142            public void setExtraSettingsProperties(
143                    UnicodeProperties extraSettingsProperties) {
144    
145                    _extraSettingsProperties = extraSettingsProperties;
146    
147                    super.setExtraSettings(_extraSettingsProperties.toString());
148            }
149    
150            private static final Log _log = LogFactoryUtil.getLog(
151                    DLFileVersionImpl.class);
152    
153            private transient ExpandoBridge _expandoBridge;
154            private UnicodeProperties _extraSettingsProperties;
155    
156    }