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.model.CompanyConstants;
023    import com.liferay.portal.security.auth.PrincipalThreadLocal;
024    import com.liferay.portlet.documentlibrary.DuplicateDirectoryException;
025    import com.liferay.portlet.documentlibrary.NoSuchFileException;
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.store.DLStoreUtil;
031    import com.liferay.portlet.documentlibrary.util.DLUtil;
032    import com.liferay.portlet.expando.model.ExpandoBridge;
033    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
034    
035    import java.io.IOException;
036    import java.io.InputStream;
037    
038    /**
039     * @author Jorge Ferrer
040     * @author Alexander Chow
041     */
042    public class DLFileVersionImpl extends DLFileVersionBaseImpl {
043    
044            public DLFileVersionImpl() {
045            }
046    
047            @Override
048            public String getChecksum() throws PortalException, SystemException {
049                    String dirName = "checksum/" + getFileEntryId();
050                    String fileName = dirName + "/" + getFileVersionId() + ".hash";
051    
052                    String checkSum = null;
053    
054                    try {
055                            byte[] bytes = DLStoreUtil.getFileAsBytes(
056                                    getCompanyId(), CompanyConstants.SYSTEM, fileName);
057    
058                            checkSum = new String(bytes);
059                    }
060                    catch (NoSuchFileException nsfe) {
061                    }
062    
063                    return checkSum;
064            }
065    
066            @Override
067            public InputStream getContentStream(boolean incrementCounter)
068                    throws PortalException, SystemException {
069    
070                    return DLFileEntryLocalServiceUtil.getFileAsStream(
071                            PrincipalThreadLocal.getUserId(), getFileEntryId(), getVersion(),
072                            incrementCounter);
073            }
074    
075            @Override
076            public ExpandoBridge getExpandoBridge() {
077                    if (_expandoBridge == null) {
078                            _expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
079                                    getCompanyId(), DLFileEntry.class.getName(), getPrimaryKey());
080                    }
081    
082                    return _expandoBridge;
083            }
084    
085            @Override
086            public String getExtraSettings() {
087                    if (_extraSettingsProperties == null) {
088                            return super.getExtraSettings();
089                    }
090                    else {
091                            return _extraSettingsProperties.toString();
092                    }
093            }
094    
095            @Override
096            public UnicodeProperties getExtraSettingsProperties() {
097                    if (_extraSettingsProperties == null) {
098                            _extraSettingsProperties = new UnicodeProperties(true);
099    
100                            try {
101                                    _extraSettingsProperties.load(super.getExtraSettings());
102                            }
103                            catch (IOException ioe) {
104                                    _log.error(ioe, ioe);
105                            }
106                    }
107    
108                    return _extraSettingsProperties;
109            }
110    
111            @Override
112            public DLFileEntry getFileEntry() throws PortalException, SystemException {
113                    return DLFileEntryLocalServiceUtil.getFileEntry(getFileEntryId());
114            }
115    
116            @Override
117            public DLFolder getFolder() {
118                    DLFolder dlFolder = null;
119    
120                    if (getFolderId() > 0) {
121                            try {
122                                    dlFolder = DLFolderLocalServiceUtil.getFolder(getFolderId());
123                            }
124                            catch (Exception e) {
125                                    dlFolder = new DLFolderImpl();
126    
127                                    _log.error(e, e);
128                            }
129                    }
130                    else {
131                            dlFolder = new DLFolderImpl();
132                    }
133    
134                    return dlFolder;
135            }
136    
137            @Override
138            public String getIcon() {
139                    return DLUtil.getFileIcon(getExtension());
140            }
141    
142            @Override
143            public void setChecksum(String checksum)
144                    throws PortalException, SystemException {
145    
146                    String dirName = "checksum/" + getFileEntryId();
147                    String fileName = dirName + "/" + getFileVersionId() + ".hash";
148    
149                    try {
150                            DLStoreUtil.addDirectory(
151                                    getCompanyId(), CompanyConstants.SYSTEM, dirName);
152                    }
153                    catch (DuplicateDirectoryException dde) {
154                    }
155    
156                    try {
157                            DLStoreUtil.deleteFile(
158                                    getCompanyId(), CompanyConstants.SYSTEM, fileName);
159                    }
160                    catch (Exception e) {
161                            if (_log.isWarnEnabled()) {
162                                    _log.warn(e, e);
163                            }
164                    }
165    
166                    DLStoreUtil.addFile(
167                            getCompanyId(), CompanyConstants.SYSTEM, fileName,
168                            checksum.getBytes());
169            }
170    
171            @Override
172            public void setExtraSettings(String extraSettings) {
173                    _extraSettingsProperties = null;
174    
175                    super.setExtraSettings(extraSettings);
176            }
177    
178            @Override
179            public void setExtraSettingsProperties(
180                    UnicodeProperties extraSettingsProperties) {
181    
182                    _extraSettingsProperties = extraSettingsProperties;
183    
184                    super.setExtraSettings(_extraSettingsProperties.toString());
185            }
186    
187            private static Log _log = LogFactoryUtil.getLog(DLFileVersionImpl.class);
188    
189            private transient ExpandoBridge _expandoBridge;
190            private UnicodeProperties _extraSettingsProperties;
191    
192    }