1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.PropertiesUtil;
30  import com.liferay.portal.kernel.util.SafeProperties;
31  import com.liferay.portal.kernel.util.StringPool;
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  /**
43   * <a href="DLFileEntryImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Alexander Chow
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 DLFolder getFolder() {
72          DLFolder folder = null;
73  
74          try {
75              folder = DLFolderLocalServiceUtil.getFolder(getFolderId());
76          }
77          catch (Exception e) {
78              folder = new DLFolderImpl();
79  
80              _log.error(e);
81          }
82  
83          return folder;
84      }
85  
86      public String getTitleWithExtension() {
87          StringBuilder sb = new StringBuilder();
88  
89          sb.append(getTitle());
90          sb.append(StringPool.PERIOD);
91          sb.append(FileUtil.getExtension(getName()));
92  
93          return sb.toString();
94      }
95  
96      public String getExtraSettings() {
97          if (_extraSettingsProperties == null) {
98              return super.getExtraSettings();
99          }
100         else {
101             return PropertiesUtil.toString(_extraSettingsProperties);
102         }
103     }
104 
105     public void setExtraSettings(String extraSettings) {
106         _extraSettingsProperties = null;
107 
108         super.setExtraSettings(extraSettings);
109     }
110 
111     public Properties getExtraSettingsProperties() {
112         if (_extraSettingsProperties == null) {
113             _extraSettingsProperties = new SafeProperties();
114 
115             try {
116                 PropertiesUtil.load(
117                     _extraSettingsProperties, super.getExtraSettings());
118             }
119             catch (IOException ioe) {
120                 _log.error(ioe);
121             }
122         }
123 
124         return _extraSettingsProperties;
125     }
126 
127     public void setExtraSettingsProperties(Properties extraSettingsProperties) {
128         _extraSettingsProperties = extraSettingsProperties;
129 
130         super.setExtraSettings(
131             PropertiesUtil.toString(_extraSettingsProperties));
132     }
133 
134     public String getLuceneProperties() {
135         StringBuilder sb = new StringBuilder();
136 
137         sb.append(getTitle());
138         sb.append(StringPool.SPACE);
139         sb.append(getDescription());
140         sb.append(StringPool.SPACE);
141 
142         Properties extraSettingsProps = getExtraSettingsProperties();
143 
144         Iterator<Map.Entry<Object, Object>> itr =
145             extraSettingsProps.entrySet().iterator();
146 
147         while (itr.hasNext()) {
148             Map.Entry<Object, Object> entry = itr.next();
149 
150             String value = GetterUtil.getString((String)entry.getValue());
151 
152             sb.append(value);
153         }
154 
155         return sb.toString();
156     }
157 
158     private static Log _log = LogFactoryUtil.getLog(DLFileEntryImpl.class);
159 
160     private Properties _extraSettingsProperties = null;
161 
162 }