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 getFolderId(long groupId, long repositoryId) {
47          if (groupId != repositoryId) {
48              return repositoryId;
49          }
50          else {
51              return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
52          }
53      }
54  
55      public static long getRepositoryId(long groupId, long folderId) {
56          if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
57              return groupId;
58          }
59          else {
60              return folderId;
61          }
62      }
63  
64      public DLFileEntryImpl() {
65      }
66  
67      public String getExtraSettings() {
68          if (_extraSettingsProperties == null) {
69              return super.getExtraSettings();
70          }
71          else {
72              return _extraSettingsProperties.toString();
73          }
74      }
75  
76      public UnicodeProperties getExtraSettingsProperties() {
77          if (_extraSettingsProperties == null) {
78              _extraSettingsProperties = new UnicodeProperties(true);
79  
80              try {
81                  _extraSettingsProperties.load(super.getExtraSettings());
82              }
83              catch (IOException ioe) {
84                  _log.error(ioe);
85              }
86          }
87  
88          return _extraSettingsProperties;
89      }
90  
91      public DLFolder getFolder() {
92          DLFolder folder = null;
93  
94          if (getFolderId() > 0) {
95              try {
96                  folder = DLFolderLocalServiceUtil.getFolder(getFolderId());
97              }
98              catch (Exception e) {
99                  folder = new DLFolderImpl();
100 
101                 _log.error(e);
102             }
103         }
104         else {
105             folder = new DLFolderImpl();
106         }
107 
108         return folder;
109     }
110 
111     public Lock getLock() {
112         try {
113             String lockId = DLUtil.getLockId(
114                 getGroupId(), getFolderId(), getName());
115 
116             return LockLocalServiceUtil.getLock(
117                 DLFileEntry.class.getName(), lockId);
118         }
119         catch (Exception e) {
120         }
121 
122         return null;
123     }
124 
125     public String getLuceneProperties() {
126         UnicodeProperties extraSettingsProps = getExtraSettingsProperties();
127 
128         Iterator<Map.Entry<String, String>> itr =
129             extraSettingsProps.entrySet().iterator();
130 
131         StringBundler sb = new StringBundler(
132             extraSettingsProps.entrySet().size() + 4);
133 
134         sb.append(FileUtil.stripExtension(getTitle()));
135         sb.append(StringPool.SPACE);
136         sb.append(getDescription());
137         sb.append(StringPool.SPACE);
138 
139         while (itr.hasNext()) {
140             Map.Entry<String, String> entry = itr.next();
141 
142             String value = GetterUtil.getString(entry.getValue());
143 
144             sb.append(value);
145         }
146 
147         return sb.toString();
148     }
149 
150     public long getRepositoryId() {
151         return getRepositoryId(getGroupId(), getFolderId());
152     }
153 
154     public boolean hasLock(long userId) {
155         try {
156             String lockId = DLUtil.getLockId(
157                 getGroupId(), getFolderId(), getName());
158 
159             return LockLocalServiceUtil.hasLock(
160                 userId, DLFileEntry.class.getName(), lockId);
161         }
162         catch (Exception e) {
163         }
164 
165         return false;
166     }
167 
168     public boolean isLocked() {
169         try {
170             String lockId = DLUtil.getLockId(
171                 getGroupId(), getFolderId(), getName());
172 
173             return LockLocalServiceUtil.isLocked(
174                 DLFileEntry.class.getName(), lockId);
175         }
176         catch (Exception e) {
177         }
178 
179         return false;
180     }
181 
182     public void setExtraSettings(String extraSettings) {
183         _extraSettingsProperties = null;
184 
185         super.setExtraSettings(extraSettings);
186     }
187 
188     public void setExtraSettingsProperties(
189         UnicodeProperties extraSettingsProperties) {
190 
191         _extraSettingsProperties = extraSettingsProperties;
192 
193         super.setExtraSettings(_extraSettingsProperties.toString());
194     }
195 
196     private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
197 
198     private UnicodeProperties _extraSettingsProperties = null;
199 
200 }