1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.service.DLFolderLocalServiceUtil;
29  import com.liferay.portlet.documentlibrary.util.DLUtil;
30  
31  import java.io.IOException;
32  
33  import java.util.Iterator;
34  import java.util.Map;
35  
36  /**
37   * <a href="DLFileEntryImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Alexander Chow
41   */
42  public class DLFileEntryImpl
43      extends DLFileEntryModelImpl implements DLFileEntry {
44  
45      public static String stripExtension(String name, String title) {
46          String extension = FileUtil.getExtension(name);
47  
48          if (extension == null) {
49              return title;
50          }
51  
52          int pos = title.toLowerCase().lastIndexOf(
53              StringPool.PERIOD + extension);
54  
55          if (pos > 0) {
56              title = title.substring(0, pos);
57          }
58  
59          return title;
60      }
61  
62      public DLFileEntryImpl() {
63      }
64  
65      public String getExtraSettings() {
66          if (_extraSettingsProperties == null) {
67              return super.getExtraSettings();
68          }
69          else {
70              return _extraSettingsProperties.toString();
71          }
72      }
73  
74      public UnicodeProperties getExtraSettingsProperties() {
75          if (_extraSettingsProperties == null) {
76              _extraSettingsProperties = new UnicodeProperties(true);
77  
78              try {
79                  _extraSettingsProperties.load(super.getExtraSettings());
80              }
81              catch (IOException ioe) {
82                  _log.error(ioe);
83              }
84          }
85  
86          return _extraSettingsProperties;
87      }
88  
89      public DLFolder getFolder() {
90          DLFolder folder = null;
91  
92          try {
93              folder = DLFolderLocalServiceUtil.getFolder(getFolderId());
94          }
95          catch (Exception e) {
96              folder = new DLFolderImpl();
97  
98              _log.error(e);
99          }
100 
101         return folder;
102     }
103 
104     public Lock getLock() {
105         try {
106             String lockId = DLUtil.getLockId(getFolderId(), getName());
107 
108             return LockLocalServiceUtil.getLock(
109                 DLFileEntry.class.getName(), lockId);
110         }
111         catch (Exception e) {
112         }
113 
114         return null;
115     }
116 
117     public String getLuceneProperties() {
118         UnicodeProperties extraSettingsProps = getExtraSettingsProperties();
119 
120         Iterator<Map.Entry<String, String>> itr =
121             extraSettingsProps.entrySet().iterator();
122 
123         StringBundler sb = new StringBundler(
124             extraSettingsProps.entrySet().size() + 4);
125 
126         sb.append(getTitle());
127         sb.append(StringPool.SPACE);
128         sb.append(getDescription());
129         sb.append(StringPool.SPACE);
130 
131         while (itr.hasNext()) {
132             Map.Entry<String, String> entry = itr.next();
133 
134             String value = GetterUtil.getString(entry.getValue());
135 
136             sb.append(value);
137         }
138 
139         return sb.toString();
140     }
141 
142     public String getTitleWithExtension() {
143         StringBuilder sb = new StringBuilder();
144 
145         sb.append(getTitle());
146         sb.append(StringPool.PERIOD);
147         sb.append(FileUtil.getExtension(getName()));
148 
149         return sb.toString();
150     }
151 
152     public boolean hasLock(long userId) {
153         try {
154             String lockId = DLUtil.getLockId(getFolderId(), getName());
155 
156             return LockLocalServiceUtil.hasLock(
157                 userId, DLFileEntry.class.getName(), lockId);
158         }
159         catch (Exception e) {
160         }
161 
162         return false;
163     }
164 
165     public boolean isLocked() {
166         try {
167             String lockId = DLUtil.getLockId(getFolderId(), getName());
168 
169             return LockLocalServiceUtil.isLocked(
170                 DLFileEntry.class.getName(), lockId);
171         }
172         catch (Exception e) {
173         }
174 
175         return false;
176     }
177 
178     public void setExtraSettings(String extraSettings) {
179         _extraSettingsProperties = null;
180 
181         super.setExtraSettings(extraSettings);
182     }
183 
184     public void setExtraSettingsProperties(
185         UnicodeProperties extraSettingsProperties) {
186 
187         _extraSettingsProperties = extraSettingsProperties;
188 
189         super.setExtraSettings(_extraSettingsProperties.toString());
190     }
191 
192     private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
193 
194     private UnicodeProperties _extraSettingsProperties = null;
195 
196 }