1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.PropertiesUtil;
31  import com.liferay.portal.kernel.util.SafeProperties;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.util.PortalUtil;
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  
38  import java.io.IOException;
39  
40  import java.util.Iterator;
41  import java.util.Map;
42  import java.util.Properties;
43  
44  /**
45   * <a href="DLFileEntryImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Alexander Chow
49   *
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 getUserUuid() throws SystemException {
75          return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
76      }
77  
78      public void setUserUuid(String userUuid) {
79          _userUuid = userUuid;
80      }
81  
82      public DLFolder getFolder() {
83          DLFolder folder = null;
84  
85          try {
86              folder = DLFolderLocalServiceUtil.getFolder(getFolderId());
87          }
88          catch (Exception e) {
89              folder = new DLFolderImpl();
90  
91              _log.error(e);
92          }
93  
94          return folder;
95      }
96  
97      public String getTitleWithExtension() {
98          StringBuilder sb = new StringBuilder();
99  
100         sb.append(getTitle());
101         sb.append(StringPool.PERIOD);
102         sb.append(FileUtil.getExtension(getName()));
103 
104         return sb.toString();
105     }
106 
107     public String getExtraSettings() {
108         if (_extraSettingsProperties == null) {
109             return super.getExtraSettings();
110         }
111         else {
112             return PropertiesUtil.toString(_extraSettingsProperties);
113         }
114     }
115 
116     public void setExtraSettings(String extraSettings) {
117         _extraSettingsProperties = null;
118 
119         super.setExtraSettings(extraSettings);
120     }
121 
122     public Properties getExtraSettingsProperties() {
123         if (_extraSettingsProperties == null) {
124             _extraSettingsProperties = new SafeProperties();
125 
126             try {
127                 PropertiesUtil.load(
128                     _extraSettingsProperties, super.getExtraSettings());
129             }
130             catch (IOException ioe) {
131                 _log.error(ioe);
132             }
133         }
134 
135         return _extraSettingsProperties;
136     }
137 
138     public void setExtraSettingsProperties(Properties extraSettingsProperties) {
139         _extraSettingsProperties = extraSettingsProperties;
140 
141         super.setExtraSettings(
142             PropertiesUtil.toString(_extraSettingsProperties));
143     }
144 
145     public String getLuceneProperties() {
146         StringBuilder sb = new StringBuilder();
147 
148         sb.append(getTitle());
149         sb.append(StringPool.SPACE);
150         sb.append(getDescription());
151         sb.append(StringPool.SPACE);
152 
153         Properties extraSettingsProps = getExtraSettingsProperties();
154 
155         Iterator<Map.Entry<Object, Object>> itr =
156             extraSettingsProps.entrySet().iterator();
157 
158         while (itr.hasNext()) {
159             Map.Entry<Object, Object> entry = itr.next();
160 
161             String value = GetterUtil.getString((String)entry.getValue());
162 
163             sb.append(value);
164         }
165 
166         return sb.toString();
167     }
168 
169     private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
170 
171     private Properties _extraSettingsProperties = null;
172     private String _userUuid;
173 
174 }