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.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.security.auth.PrincipalThreadLocal;
025    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
026    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
027    import com.liferay.portlet.documentlibrary.model.DLFolder;
028    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
029    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
030    import com.liferay.portlet.documentlibrary.util.DLUtil;
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    /**
038     * @author Jorge Ferrer
039     * @author Alexander Chow
040     */
041    public class DLFileVersionImpl extends DLFileVersionBaseImpl {
042    
043            public DLFileVersionImpl() {
044            }
045    
046            @Override
047            public String buildTreePath() throws PortalException, SystemException {
048                    StringBundler sb = new StringBundler();
049    
050                    buildTreePath(sb, getFolder());
051    
052                    return sb.toString();
053            }
054    
055            @Override
056            public InputStream getContentStream(boolean incrementCounter)
057                    throws PortalException, SystemException {
058    
059                    return DLFileEntryLocalServiceUtil.getFileAsStream(
060                            PrincipalThreadLocal.getUserId(), getFileEntryId(), getVersion(),
061                            incrementCounter);
062            }
063    
064            @Override
065            public ExpandoBridge getExpandoBridge() {
066                    if (_expandoBridge == null) {
067                            _expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
068                                    getCompanyId(), DLFileEntry.class.getName(), getPrimaryKey());
069                    }
070    
071                    return _expandoBridge;
072            }
073    
074            @Override
075            public String getExtraSettings() {
076                    if (_extraSettingsProperties == null) {
077                            return super.getExtraSettings();
078                    }
079                    else {
080                            return _extraSettingsProperties.toString();
081                    }
082            }
083    
084            @Override
085            public UnicodeProperties getExtraSettingsProperties() {
086                    if (_extraSettingsProperties == null) {
087                            _extraSettingsProperties = new UnicodeProperties(true);
088    
089                            try {
090                                    _extraSettingsProperties.load(super.getExtraSettings());
091                            }
092                            catch (IOException ioe) {
093                                    _log.error(ioe, ioe);
094                            }
095                    }
096    
097                    return _extraSettingsProperties;
098            }
099    
100            @Override
101            public DLFileEntry getFileEntry() throws PortalException, SystemException {
102                    return DLFileEntryLocalServiceUtil.getFileEntry(getFileEntryId());
103            }
104    
105            @Override
106            public DLFolder getFolder() throws PortalException, SystemException {
107                    if (getFolderId() <= 0) {
108                            return new DLFolderImpl();
109                    }
110    
111                    return DLFolderLocalServiceUtil.getFolder(getFolderId());
112            }
113    
114            @Override
115            public String getIcon() {
116                    return DLUtil.getFileIcon(getExtension());
117            }
118    
119            @Override
120            public DLFolder getTrashContainer()
121                    throws PortalException, SystemException {
122    
123                    DLFolder dlFolder = null;
124    
125                    try {
126                            dlFolder = getFolder();
127                    }
128                    catch (NoSuchFolderException nsfe) {
129                            return null;
130                    }
131    
132                    if (dlFolder.isInTrash()) {
133                            return dlFolder;
134                    }
135    
136                    return dlFolder.getTrashContainer();
137            }
138    
139            @Override
140            public boolean isInTrashContainer()
141                    throws PortalException, SystemException {
142    
143                    if (getTrashContainer() != null) {
144                            return true;
145                    }
146                    else {
147                            return false;
148                    }
149            }
150    
151            @Override
152            public void setExtraSettings(String extraSettings) {
153                    _extraSettingsProperties = null;
154    
155                    super.setExtraSettings(extraSettings);
156            }
157    
158            @Override
159            public void setExtraSettingsProperties(
160                    UnicodeProperties extraSettingsProperties) {
161    
162                    _extraSettingsProperties = extraSettingsProperties;
163    
164                    super.setExtraSettings(_extraSettingsProperties.toString());
165            }
166    
167            protected void buildTreePath(StringBundler sb, DLFolder dlFolder)
168                    throws PortalException, SystemException {
169    
170                    if (dlFolder == null) {
171                            sb.append(StringPool.SLASH);
172                    }
173                    else {
174                            buildTreePath(sb, dlFolder.getParentFolder());
175    
176                            sb.append(dlFolder.getFolderId());
177                            sb.append(StringPool.SLASH);
178                    }
179            }
180    
181            private static Log _log = LogFactoryUtil.getLog(DLFileVersionImpl.class);
182    
183            private transient ExpandoBridge _expandoBridge;
184            private UnicodeProperties _extraSettingsProperties;
185    
186    }