1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.documentlibrary.model.impl;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.PropertiesUtil;
30  import com.liferay.portal.kernel.util.SafeProperties;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.Lock;
33  import com.liferay.portal.service.LockLocalServiceUtil;
34  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
35  import com.liferay.portlet.documentlibrary.model.DLFolder;
36  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
37  import com.liferay.portlet.documentlibrary.util.DLUtil;
38  
39  import java.io.IOException;
40  
41  import java.util.Iterator;
42  import java.util.Map;
43  import java.util.Properties;
44  
45  /**
46   * <a href="DLFileEntryImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Alexander Chow
50   */
51  public class DLFileEntryImpl
52      extends DLFileEntryModelImpl implements DLFileEntry {
53  
54      public static String stripExtension(String name, String title) {
55          String extension = FileUtil.getExtension(name);
56  
57          if (extension == null) {
58              return title;
59          }
60  
61          int pos = title.toLowerCase().lastIndexOf(
62              StringPool.PERIOD + extension);
63  
64          if (pos > 0) {
65              title = title.substring(0, pos);
66          }
67  
68          return title;
69      }
70  
71      public DLFileEntryImpl() {
72      }
73  
74      public String getExtraSettings() {
75          if (_extraSettingsProperties == null) {
76              return super.getExtraSettings();
77          }
78          else {
79              return PropertiesUtil.toString(_extraSettingsProperties);
80          }
81      }
82  
83      public Properties getExtraSettingsProperties() {
84          if (_extraSettingsProperties == null) {
85              _extraSettingsProperties = new SafeProperties();
86  
87              try {
88                  PropertiesUtil.load(
89                      _extraSettingsProperties, super.getExtraSettings());
90              }
91              catch (IOException ioe) {
92                  _log.error(ioe);
93              }
94          }
95  
96          return _extraSettingsProperties;
97      }
98  
99      public DLFolder getFolder() {
100         DLFolder folder = null;
101 
102         try {
103             folder = DLFolderLocalServiceUtil.getFolder(getFolderId());
104         }
105         catch (Exception e) {
106             folder = new DLFolderImpl();
107 
108             _log.error(e);
109         }
110 
111         return folder;
112     }
113 
114     public Lock getLock() {
115         try {
116             String lockId = DLUtil.getLockId(getFolderId(), getName());
117 
118             return LockLocalServiceUtil.getLock(
119                 DLFileEntry.class.getName(), lockId);
120         }
121         catch (Exception e) {
122         }
123 
124         return null;
125     }
126 
127     public String getLuceneProperties() {
128         StringBuilder sb = new StringBuilder();
129 
130         sb.append(getTitle());
131         sb.append(StringPool.SPACE);
132         sb.append(getDescription());
133         sb.append(StringPool.SPACE);
134 
135         Properties extraSettingsProps = getExtraSettingsProperties();
136 
137         Iterator<Map.Entry<Object, Object>> itr =
138             extraSettingsProps.entrySet().iterator();
139 
140         while (itr.hasNext()) {
141             Map.Entry<Object, Object> entry = itr.next();
142 
143             String value = GetterUtil.getString((String)entry.getValue());
144 
145             sb.append(value);
146         }
147 
148         return sb.toString();
149     }
150 
151     public String getTitleWithExtension() {
152         StringBuilder sb = new StringBuilder();
153 
154         sb.append(getTitle());
155         sb.append(StringPool.PERIOD);
156         sb.append(FileUtil.getExtension(getName()));
157 
158         return sb.toString();
159     }
160 
161     public boolean hasLock(long userId) {
162         try {
163             String lockId = DLUtil.getLockId(getFolderId(), getName());
164 
165             return LockLocalServiceUtil.hasLock(
166                 userId, DLFileEntry.class.getName(), lockId);
167         }
168         catch (Exception e) {
169         }
170 
171         return false;
172     }
173 
174     public boolean isLocked() {
175         try {
176             String lockId = DLUtil.getLockId(getFolderId(), getName());
177 
178             return LockLocalServiceUtil.isLocked(
179                 DLFileEntry.class.getName(), lockId);
180         }
181         catch (Exception e) {
182         }
183 
184         return false;
185     }
186 
187     public void setExtraSettings(String extraSettings) {
188         _extraSettingsProperties = null;
189 
190         super.setExtraSettings(extraSettings);
191     }
192 
193     public void setExtraSettingsProperties(Properties extraSettingsProperties) {
194         _extraSettingsProperties = extraSettingsProperties;
195 
196         super.setExtraSettings(
197             PropertiesUtil.toString(_extraSettingsProperties));
198     }
199 
200     private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
201 
202     private Properties _extraSettingsProperties = null;
203 
204 }