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 String stripExtension(String name, String title) {
52          String extension = FileUtil.getExtension(name);
53  
54          if (extension == null) {
55              return title;
56          }
57  
58          int pos = title.toLowerCase().lastIndexOf(
59              StringPool.PERIOD + extension);
60  
61          if (pos > 0) {
62              title = title.substring(0, pos);
63          }
64  
65          return title;
66      }
67  
68      public DLFileEntryImpl() {
69      }
70  
71      public String getUserUuid() throws SystemException {
72          return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
73      }
74  
75      public void setUserUuid(String userUuid) {
76          _userUuid = userUuid;
77      }
78  
79      public DLFolder getFolder() {
80          DLFolder folder = null;
81  
82          try {
83              folder = DLFolderLocalServiceUtil.getFolder(getFolderId());
84          }
85          catch (Exception e) {
86              folder = new DLFolderImpl();
87  
88              _log.error(e);
89          }
90  
91          return folder;
92      }
93  
94      public String getTitleWithExtension() {
95          StringBuilder sb = new StringBuilder();
96  
97          sb.append(getTitle());
98          sb.append(StringPool.PERIOD);
99          sb.append(FileUtil.getExtension(getName()));
100 
101         return sb.toString();
102     }
103 
104     public String getExtraSettings() {
105         if (_extraSettingsProperties == null) {
106             return super.getExtraSettings();
107         }
108         else {
109             return PropertiesUtil.toString(_extraSettingsProperties);
110         }
111     }
112 
113     public void setExtraSettings(String extraSettings) {
114         _extraSettingsProperties = null;
115 
116         super.setExtraSettings(extraSettings);
117     }
118 
119     public Properties getExtraSettingsProperties() {
120         if (_extraSettingsProperties == null) {
121             _extraSettingsProperties = new SafeProperties();
122 
123             try {
124                 PropertiesUtil.load(
125                     _extraSettingsProperties, super.getExtraSettings());
126             }
127             catch (IOException ioe) {
128                 _log.error(ioe);
129             }
130         }
131 
132         return _extraSettingsProperties;
133     }
134 
135     public void setExtraSettingsProperties(Properties extraSettingsProperties) {
136         _extraSettingsProperties = extraSettingsProperties;
137 
138         super.setExtraSettings(
139             PropertiesUtil.toString(_extraSettingsProperties));
140     }
141 
142     public String getLuceneProperties() {
143         StringBuilder sb = new StringBuilder();
144 
145         sb.append(getTitle());
146         sb.append(StringPool.SPACE);
147         sb.append(getDescription());
148         sb.append(StringPool.SPACE);
149 
150         Properties extraSettingsProps = getExtraSettingsProperties();
151 
152         Iterator<Map.Entry<Object, Object>> itr =
153             extraSettingsProps.entrySet().iterator();
154 
155         while (itr.hasNext()) {
156             Map.Entry<Object, Object> entry = itr.next();
157 
158             String value = GetterUtil.getString((String)entry.getValue());
159 
160             sb.append(value);
161         }
162 
163         return sb.toString();
164     }
165 
166     private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
167 
168     private Properties _extraSettingsProperties = null;
169     private String _userUuid;
170 
171 }