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