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