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.model.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            public DLFileVersionImpl() {
046            }
047    
048            @Override
049            public String buildTreePath() throws PortalException {
050                    if (getFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
051                            return StringPool.SLASH;
052                    }
053    
054                    DLFolder dlFolder = getFolder();
055    
056                    return dlFolder.buildTreePath();
057            }
058    
059            @Override
060            public InputStream getContentStream(boolean incrementCounter)
061                    throws PortalException {
062    
063                    return DLFileEntryLocalServiceUtil.getFileAsStream(
064                            getFileEntryId(), getVersion(), incrementCounter);
065            }
066    
067            @Override
068            public List<DDMStructure> getDDMStructures() throws PortalException {
069                    DLFileEntryType dlFileEntryType =
070                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(
071                                    getFileEntryTypeId());
072    
073                    return dlFileEntryType.getDDMStructures();
074            }
075    
076            @Override
077            public DLFileEntryType getDLFileEntryType() throws PortalException {
078                    return DLFileEntryTypeLocalServiceUtil.getFileEntryType(
079                            getFileEntryTypeId());
080            }
081    
082            @Override
083            public ExpandoBridge getExpandoBridge() {
084                    if (_expandoBridge == null) {
085                            _expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
086                                    getCompanyId(), DLFileEntry.class.getName(), getPrimaryKey());
087                    }
088    
089                    return _expandoBridge;
090            }
091    
092            @Override
093            public String getExtraSettings() {
094                    if (_extraSettingsProperties == null) {
095                            return super.getExtraSettings();
096                    }
097                    else {
098                            return _extraSettingsProperties.toString();
099                    }
100            }
101    
102            @Override
103            public UnicodeProperties getExtraSettingsProperties() {
104                    if (_extraSettingsProperties == null) {
105                            _extraSettingsProperties = new UnicodeProperties(true);
106    
107                            try {
108                                    _extraSettingsProperties.load(super.getExtraSettings());
109                            }
110                            catch (IOException ioe) {
111                                    _log.error(ioe, ioe);
112                            }
113                    }
114    
115                    return _extraSettingsProperties;
116            }
117    
118            @Override
119            public DLFileEntry getFileEntry() throws PortalException {
120                    return DLFileEntryLocalServiceUtil.getFileEntry(getFileEntryId());
121            }
122    
123            @Override
124            public DLFolder getFolder() throws PortalException {
125                    if (getFolderId() <= 0) {
126                            return new DLFolderImpl();
127                    }
128    
129                    return DLFolderLocalServiceUtil.getFolder(getFolderId());
130            }
131    
132            @Override
133            public String getIcon() {
134                    return DLUtil.getFileIcon(getExtension());
135            }
136    
137            @Override
138            public void setExtraSettings(String extraSettings) {
139                    _extraSettingsProperties = null;
140    
141                    super.setExtraSettings(extraSettings);
142            }
143    
144            @Override
145            public void setExtraSettingsProperties(
146                    UnicodeProperties extraSettingsProperties) {
147    
148                    _extraSettingsProperties = extraSettingsProperties;
149    
150                    super.setExtraSettings(_extraSettingsProperties.toString());
151            }
152    
153            private static final Log _log = LogFactoryUtil.getLog(
154                    DLFileVersionImpl.class);
155    
156            private transient ExpandoBridge _expandoBridge;
157            private UnicodeProperties _extraSettingsProperties;
158    
159    }