1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.model.impl;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.PropertiesUtil;
28  import com.liferay.portal.kernel.util.SafeProperties;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
32  import com.liferay.portlet.documentlibrary.model.DLFolder;
33  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
34  
35  import java.io.IOException;
36  
37  import java.util.Iterator;
38  import java.util.Map;
39  import java.util.Properties;
40  
41  /**
42   * <a href="DLFileEntryImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Alexander Chow
46   *
47   */
48  public class DLFileEntryImpl
49      extends DLFileEntryModelImpl implements DLFileEntry {
50  
51      public static final double DEFAULT_VERSION = 1.0;
52  
53      public static final int DEFAULT_READ_COUNT = 0;
54  
55      public static String stripExtension(String name, String title) {
56          String extension = FileUtil.getExtension(name);
57  
58          if (extension == null) {
59              return title;
60          }
61  
62          int pos = title.toLowerCase().lastIndexOf(
63              StringPool.PERIOD + extension);
64  
65          if (pos > 0) {
66              title = title.substring(0, pos);
67          }
68  
69          return title;
70      }
71  
72      public DLFileEntryImpl() {
73      }
74  
75      public String getUserUuid() throws SystemException {
76          return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
77      }
78  
79      public void setUserUuid(String userUuid) {
80          _userUuid = userUuid;
81      }
82  
83      public DLFolder getFolder() {
84          DLFolder folder = null;
85  
86          try {
87              folder = DLFolderLocalServiceUtil.getFolder(getFolderId());
88          }
89          catch (Exception e) {
90              folder = new DLFolderImpl();
91  
92              _log.error(e);
93          }
94  
95          return folder;
96      }
97  
98      public String getTitleWithExtension() {
99          StringBuilder sb = new StringBuilder();
100 
101         sb.append(getTitle());
102         sb.append(StringPool.PERIOD);
103         sb.append(FileUtil.getExtension(getName()));
104 
105         return sb.toString();
106     }
107 
108     public String getExtraSettings() {
109         if (_extraSettingsProperties == null) {
110             return super.getExtraSettings();
111         }
112         else {
113             return PropertiesUtil.toString(_extraSettingsProperties);
114         }
115     }
116 
117     public void setExtraSettings(String extraSettings) {
118         _extraSettingsProperties = null;
119 
120         super.setExtraSettings(extraSettings);
121     }
122 
123     public Properties getExtraSettingsProperties() {
124         if (_extraSettingsProperties == null) {
125             _extraSettingsProperties = new SafeProperties();
126 
127             try {
128                 PropertiesUtil.load(
129                     _extraSettingsProperties, super.getExtraSettings());
130             }
131             catch (IOException ioe) {
132                 _log.error(ioe);
133             }
134         }
135 
136         return _extraSettingsProperties;
137     }
138 
139     public void setExtraSettingsProperties(Properties extraSettingsProperties) {
140         _extraSettingsProperties = extraSettingsProperties;
141 
142         super.setExtraSettings(
143             PropertiesUtil.toString(_extraSettingsProperties));
144     }
145 
146     public String getLuceneProperties() {
147         StringBuilder sb = new StringBuilder();
148 
149         sb.append(getTitle());
150         sb.append(StringPool.SPACE);
151         sb.append(getDescription());
152         sb.append(StringPool.SPACE);
153 
154         Properties extraSettingsProps = getExtraSettingsProperties();
155 
156         Iterator<Map.Entry<Object, Object>> itr =
157             extraSettingsProps.entrySet().iterator();
158 
159         while (itr.hasNext()) {
160             Map.Entry<Object, Object> entry = itr.next();
161 
162             String value = GetterUtil.getString((String)entry.getValue());
163 
164             sb.append(value);
165         }
166 
167         return sb.toString();
168     }
169 
170     private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
171 
172     private Properties _extraSettingsProperties = null;
173     private String _userUuid;
174 
175 }