001    /**
002     * Copyright (c) 2000-2012 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            public InputStream getContentStream(boolean incrementCounter)
044                    throws PortalException, SystemException {
045    
046                    return DLFileEntryLocalServiceUtil.getFileAsStream(
047                            PrincipalThreadLocal.getUserId(), getFileEntryId(), getVersion(),
048                            incrementCounter);
049            }
050    
051            @Override
052            public ExpandoBridge getExpandoBridge() {
053                    if (_expandoBridge == null) {
054                            _expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
055                                    getCompanyId(), DLFileEntry.class.getName(), getPrimaryKey());
056                    }
057    
058                    return _expandoBridge;
059            }
060    
061            @Override
062            public String getExtraSettings() {
063                    if (_extraSettingsProperties == null) {
064                            return super.getExtraSettings();
065                    }
066                    else {
067                            return _extraSettingsProperties.toString();
068                    }
069            }
070    
071            public UnicodeProperties getExtraSettingsProperties() {
072                    if (_extraSettingsProperties == null) {
073                            _extraSettingsProperties = new UnicodeProperties(true);
074    
075                            try {
076                                    _extraSettingsProperties.load(super.getExtraSettings());
077                            }
078                            catch (IOException ioe) {
079                                    _log.error(ioe, ioe);
080                            }
081                    }
082    
083                    return _extraSettingsProperties;
084            }
085    
086            public DLFileEntry getFileEntry() throws PortalException, SystemException {
087                    return DLFileEntryLocalServiceUtil.getFileEntry(getFileEntryId());
088            }
089    
090            public DLFolder getFolder() {
091                    DLFolder dlFolder = null;
092    
093                    if (getFolderId() > 0) {
094                            try {
095                                    dlFolder = DLFolderLocalServiceUtil.getFolder(getFolderId());
096                            }
097                            catch (Exception e) {
098                                    dlFolder = new DLFolderImpl();
099    
100                                    _log.error(e, e);
101                            }
102                    }
103                    else {
104                            dlFolder = new DLFolderImpl();
105                    }
106    
107                    return dlFolder;
108            }
109    
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            public void setExtraSettingsProperties(
122                    UnicodeProperties extraSettingsProperties) {
123    
124                    _extraSettingsProperties = extraSettingsProperties;
125    
126                    super.setExtraSettings(_extraSettingsProperties.toString());
127            }
128    
129            private static Log _log = LogFactoryUtil.getLog(DLFileVersionImpl.class);
130    
131            private ExpandoBridge _expandoBridge;
132            private UnicodeProperties _extraSettingsProperties;
133    
134    }