1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.model.impl;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.FileUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.UnicodeProperties;
24  import com.liferay.portal.model.Lock;
25  import com.liferay.portal.service.LockLocalServiceUtil;
26  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
27  import com.liferay.portlet.documentlibrary.model.DLFolder;
28  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
29  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
30  import com.liferay.portlet.documentlibrary.util.DLUtil;
31  
32  import java.io.IOException;
33  
34  import java.util.Iterator;
35  import java.util.Map;
36  
37  /**
38   * <a href="DLFileEntryImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Alexander Chow
42   */
43  public class DLFileEntryImpl
44      extends DLFileEntryModelImpl implements DLFileEntry {
45  
46      public static long getRepositoryId(long groupId, long folderId) {
47          if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
48              return groupId;
49          }
50          else {
51              return folderId;
52          }
53      }
54  
55      public DLFileEntryImpl() {
56      }
57  
58      public String getExtraSettings() {
59          if (_extraSettingsProperties == null) {
60              return super.getExtraSettings();
61          }
62          else {
63              return _extraSettingsProperties.toString();
64          }
65      }
66  
67      public UnicodeProperties getExtraSettingsProperties() {
68          if (_extraSettingsProperties == null) {
69              _extraSettingsProperties = new UnicodeProperties(true);
70  
71              try {
72                  _extraSettingsProperties.load(super.getExtraSettings());
73              }
74              catch (IOException ioe) {
75                  _log.error(ioe);
76              }
77          }
78  
79          return _extraSettingsProperties;
80      }
81  
82      public DLFolder getFolder() {
83          DLFolder folder = null;
84  
85          if (getFolderId() > 0) {
86              try {
87                  folder = DLFolderLocalServiceUtil.getFolder(getFolderId());
88              }
89              catch (Exception e) {
90                  folder = new DLFolderImpl();
91  
92                  _log.error(e);
93              }
94          }
95          else {
96              folder = new DLFolderImpl();
97          }
98  
99          return folder;
100     }
101 
102     public Lock getLock() {
103         try {
104             String lockId = DLUtil.getLockId(
105                 getGroupId(), getFolderId(), getName());
106 
107             return LockLocalServiceUtil.getLock(
108                 DLFileEntry.class.getName(), lockId);
109         }
110         catch (Exception e) {
111         }
112 
113         return null;
114     }
115 
116     public String getLuceneProperties() {
117         UnicodeProperties extraSettingsProps = getExtraSettingsProperties();
118 
119         Iterator<Map.Entry<String, String>> itr =
120             extraSettingsProps.entrySet().iterator();
121 
122         StringBundler sb = new StringBundler(
123             extraSettingsProps.entrySet().size() + 4);
124 
125         sb.append(FileUtil.stripExtension(getTitle()));
126         sb.append(StringPool.SPACE);
127         sb.append(getDescription());
128         sb.append(StringPool.SPACE);
129 
130         while (itr.hasNext()) {
131             Map.Entry<String, String> entry = itr.next();
132 
133             String value = GetterUtil.getString(entry.getValue());
134 
135             sb.append(value);
136         }
137 
138         return sb.toString();
139     }
140 
141     public long getRepositoryId() {
142         return getRepositoryId(getGroupId(), getFolderId());
143     }
144 
145     public boolean hasLock(long userId) {
146         try {
147             String lockId = DLUtil.getLockId(
148                 getGroupId(), getFolderId(), getName());
149 
150             return LockLocalServiceUtil.hasLock(
151                 userId, DLFileEntry.class.getName(), lockId);
152         }
153         catch (Exception e) {
154         }
155 
156         return false;
157     }
158 
159     public boolean isLocked() {
160         try {
161             String lockId = DLUtil.getLockId(
162                 getGroupId(), getFolderId(), getName());
163 
164             return LockLocalServiceUtil.isLocked(
165                 DLFileEntry.class.getName(), lockId);
166         }
167         catch (Exception e) {
168         }
169 
170         return false;
171     }
172 
173     public void setExtraSettings(String extraSettings) {
174         _extraSettingsProperties = null;
175 
176         super.setExtraSettings(extraSettings);
177     }
178 
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(DLFileEntryImpl.class);
188 
189     private UnicodeProperties _extraSettingsProperties = null;
190 
191 }