001    /**
002     * Copyright (c) 2000-2010 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.FileUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.UnicodeProperties;
026    import com.liferay.portal.model.Lock;
027    import com.liferay.portal.service.LockLocalServiceUtil;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
029    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
030    import com.liferay.portlet.documentlibrary.model.DLFolder;
031    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
032    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
033    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
034    import com.liferay.portlet.documentlibrary.util.DLUtil;
035    
036    import java.io.IOException;
037    
038    import java.util.Iterator;
039    import java.util.Map;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Alexander Chow
044     */
045    public class DLFileEntryImpl
046            extends DLFileEntryModelImpl implements DLFileEntry {
047    
048            public static long getFolderId(long groupId, long repositoryId) {
049                    if (groupId != repositoryId) {
050                            return repositoryId;
051                    }
052                    else {
053                            return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
054                    }
055            }
056    
057            public static long getRepositoryId(long groupId, long folderId) {
058                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
059                            return groupId;
060                    }
061                    else {
062                            return folderId;
063                    }
064            }
065    
066            public DLFileEntryImpl() {
067            }
068    
069            public String getExtraSettings() {
070                    if (_extraSettingsProperties == null) {
071                            return super.getExtraSettings();
072                    }
073                    else {
074                            return _extraSettingsProperties.toString();
075                    }
076            }
077    
078            public UnicodeProperties getExtraSettingsProperties() {
079                    if (_extraSettingsProperties == null) {
080                            _extraSettingsProperties = new UnicodeProperties(true);
081    
082                            try {
083                                    _extraSettingsProperties.load(super.getExtraSettings());
084                            }
085                            catch (IOException ioe) {
086                                    _log.error(ioe, ioe);
087                            }
088                    }
089    
090                    return _extraSettingsProperties;
091            }
092    
093            public DLFileVersion getFileVersion()
094                    throws PortalException, SystemException {
095    
096                    return DLFileVersionLocalServiceUtil.getFileVersion(
097                            getGroupId(), getFolderId(), getName(), getVersion());
098            }
099    
100            public DLFolder getFolder() {
101                    DLFolder folder = null;
102    
103                    if (getFolderId() > 0) {
104                            try {
105                                    folder = DLFolderLocalServiceUtil.getFolder(getFolderId());
106                            }
107                            catch (Exception e) {
108                                    folder = new DLFolderImpl();
109    
110                                    _log.error(e, e);
111                            }
112                    }
113                    else {
114                            folder = new DLFolderImpl();
115                    }
116    
117                    return folder;
118            }
119    
120            public String getIcon() {
121                    return DLUtil.getFileIcon(getExtension());
122            }
123    
124            public DLFileVersion getLatestFileVersion()
125                    throws PortalException, SystemException {
126    
127                    return DLFileVersionLocalServiceUtil.getLatestFileVersion(
128                            getGroupId(), getFolderId(), getName());
129            }
130    
131            public Lock getLock() {
132                    try {
133                            String lockId = DLUtil.getLockId(
134                                    getGroupId(), getFolderId(), getName());
135    
136                            return LockLocalServiceUtil.getLock(
137                                    DLFileEntry.class.getName(), lockId);
138                    }
139                    catch (Exception e) {
140                    }
141    
142                    return null;
143            }
144    
145            public String getLuceneProperties() {
146                    UnicodeProperties extraSettingsProps = getExtraSettingsProperties();
147    
148                    Iterator<Map.Entry<String, String>> itr =
149                            extraSettingsProps.entrySet().iterator();
150    
151                    StringBundler sb = new StringBundler(
152                            extraSettingsProps.entrySet().size() + 4);
153    
154                    sb.append(FileUtil.stripExtension(getTitle()));
155                    sb.append(StringPool.SPACE);
156                    sb.append(getDescription());
157                    sb.append(StringPool.SPACE);
158    
159                    while (itr.hasNext()) {
160                            Map.Entry<String, String> entry = itr.next();
161    
162                            String value = GetterUtil.getString(entry.getValue());
163    
164                            sb.append(value);
165                    }
166    
167                    return sb.toString();
168            }
169    
170            public long getRepositoryId() {
171                    return getRepositoryId(getGroupId(), getFolderId());
172            }
173    
174            public boolean hasLock(long userId) {
175                    try {
176                            String lockId = DLUtil.getLockId(
177                                    getGroupId(), getFolderId(), getName());
178    
179                            return LockLocalServiceUtil.hasLock(
180                                    userId, DLFileEntry.class.getName(), lockId);
181                    }
182                    catch (Exception e) {
183                    }
184    
185                    return false;
186            }
187    
188            public boolean isLocked() {
189                    try {
190                            String lockId = DLUtil.getLockId(
191                                    getGroupId(), getFolderId(), getName());
192    
193                            return LockLocalServiceUtil.isLocked(
194                                    DLFileEntry.class.getName(), lockId);
195                    }
196                    catch (Exception e) {
197                    }
198    
199                    return false;
200            }
201    
202            public void setExtraSettings(String extraSettings) {
203                    _extraSettingsProperties = null;
204    
205                    super.setExtraSettings(extraSettings);
206            }
207    
208            public void setExtraSettingsProperties(
209                    UnicodeProperties extraSettingsProperties) {
210    
211                    _extraSettingsProperties = extraSettingsProperties;
212    
213                    super.setExtraSettings(_extraSettingsProperties.toString());
214            }
215    
216            private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
217    
218            private UnicodeProperties _extraSettingsProperties = null;
219    
220    }