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