1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.SystemException;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.PropertiesUtil;
29  import com.liferay.portal.kernel.util.SafeProperties;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
33  import com.liferay.portlet.documentlibrary.model.DLFolder;
34  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
35  
36  import java.io.IOException;
37  
38  import java.util.Iterator;
39  import java.util.Map;
40  import java.util.Properties;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
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   */
52  public class DLFileEntryImpl
53      extends DLFileEntryModelImpl implements DLFileEntry {
54  
55      public static final double DEFAULT_VERSION = 1.0;
56  
57      public static final int DEFAULT_READ_COUNT = 0;
58  
59      public static String stripExtension(String name, String title) {
60          String extension = FileUtil.getExtension(name);
61  
62          if (extension == null) {
63              return title;
64          }
65  
66          int pos = title.toLowerCase().lastIndexOf(
67              StringPool.PERIOD + extension);
68  
69          if (pos > 0) {
70              title = title.substring(0, pos);
71          }
72  
73          return title;
74      }
75  
76      public DLFileEntryImpl() {
77      }
78  
79      public String getUserUuid() throws SystemException {
80          return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
81      }
82  
83      public void setUserUuid(String userUuid) {
84          _userUuid = userUuid;
85      }
86  
87      public DLFolder getFolder() {
88          DLFolder folder = null;
89  
90          try {
91              folder = DLFolderLocalServiceUtil.getFolder(getFolderId());
92          }
93          catch (Exception e) {
94              folder = new DLFolderImpl();
95  
96              _log.error(e);
97          }
98  
99          return folder;
100     }
101 
102     public String getTitleWithExtension() {
103         StringBuilder sb = new StringBuilder();
104 
105         sb.append(getTitle());
106         sb.append(StringPool.PERIOD);
107         sb.append(FileUtil.getExtension(getName()));
108 
109         return sb.toString();
110     }
111 
112     public String getExtraSettings() {
113         if (_extraSettingsProperties == null) {
114             return super.getExtraSettings();
115         }
116         else {
117             return PropertiesUtil.toString(_extraSettingsProperties);
118         }
119     }
120 
121     public void setExtraSettings(String extraSettings) {
122         _extraSettingsProperties = null;
123 
124         super.setExtraSettings(extraSettings);
125     }
126 
127     public Properties getExtraSettingsProperties() {
128         if (_extraSettingsProperties == null) {
129             _extraSettingsProperties = new SafeProperties();
130 
131             try {
132                 PropertiesUtil.load(
133                     _extraSettingsProperties, super.getExtraSettings());
134             }
135             catch (IOException ioe) {
136                 _log.error(ioe);
137             }
138         }
139 
140         return _extraSettingsProperties;
141     }
142 
143     public void setExtraSettingsProperties(Properties extraSettingsProperties) {
144         _extraSettingsProperties = extraSettingsProperties;
145 
146         super.setExtraSettings(
147             PropertiesUtil.toString(_extraSettingsProperties));
148     }
149 
150     public String getLuceneProperties() {
151         StringBuilder sb = new StringBuilder();
152 
153         sb.append(getTitle());
154         sb.append(StringPool.SPACE);
155         sb.append(getDescription());
156         sb.append(StringPool.SPACE);
157 
158         Properties extraSettingsProps = getExtraSettingsProperties();
159 
160         Iterator<Map.Entry<Object, Object>> itr =
161             extraSettingsProps.entrySet().iterator();
162 
163         while (itr.hasNext()) {
164             Map.Entry<Object, Object> entry = itr.next();
165 
166             String value = GetterUtil.getString((String)entry.getValue());
167 
168             sb.append(value);
169         }
170 
171         return sb.toString();
172     }
173 
174     private static Log _log = LogFactory.getLog(DLFileEntryImpl.class);
175 
176     private Properties _extraSettingsProperties = null;
177     private String _userUuid;
178 
179 }