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