1   /**
2    * Copyright (c) 2000-2007 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.util.GetterUtil;
26  import com.liferay.portal.kernel.util.NullSafeProperties;
27  import com.liferay.portal.kernel.util.PropertiesUtil;
28  import com.liferay.portal.kernel.util.StringMaker;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
31  import com.liferay.portlet.documentlibrary.model.DLFolder;
32  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
33  import com.liferay.util.FileUtil;
34  
35  import java.io.IOException;
36  
37  import java.util.Iterator;
38  import java.util.Map;
39  import java.util.Properties;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="DLFileEntryImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class DLFileEntryImpl
51      extends DLFileEntryModelImpl implements DLFileEntry {
52  
53      public static final double DEFAULT_VERSION = 1.0;
54  
55      public static final int DEFAULT_READ_COUNT = 0;
56  
57      public DLFileEntryImpl() {
58      }
59  
60      public DLFolder getFolder() {
61          DLFolder folder = null;
62  
63          try {
64              folder = DLFolderLocalServiceUtil.getFolder(getFolderId());
65          }
66          catch (Exception e) {
67              folder = new DLFolderImpl();
68  
69              _log.error(e);
70          }
71  
72          return folder;
73      }
74  
75      public String getTitleWithExtension() {
76          String titleWithExtension = getTitle();
77  
78          if (FileUtil.getExtension(titleWithExtension) == null) {
79              titleWithExtension +=
80                  StringPool.PERIOD + FileUtil.getExtension(getName());
81          }
82  
83          return titleWithExtension;
84      }
85  
86      public String getExtraSettings() {
87          if (_extraSettingsProperties == null) {
88              return super.getExtraSettings();
89          }
90          else {
91              return PropertiesUtil.toString(_extraSettingsProperties);
92          }
93      }
94  
95      public void setExtraSettings(String extraSettings) {
96          _extraSettingsProperties = null;
97  
98          super.setExtraSettings(extraSettings);
99      }
100 
101     public Properties getExtraSettingsProperties() {
102         if (_extraSettingsProperties == null) {
103             _extraSettingsProperties = new NullSafeProperties();
104 
105             try {
106                 PropertiesUtil.load(
107                     _extraSettingsProperties, super.getExtraSettings());
108             }
109             catch (IOException ioe) {
110                 _log.error(ioe);
111             }
112         }
113 
114         return _extraSettingsProperties;
115     }
116 
117     public void setExtraSettingsProperties(Properties extraSettingsProperties) {
118         _extraSettingsProperties = extraSettingsProperties;
119 
120         super.setExtraSettings(
121             PropertiesUtil.toString(_extraSettingsProperties));
122     }
123 
124     public String getLuceneProperties() {
125         StringMaker sm = new StringMaker();
126 
127         sm.append(getTitle());
128         sm.append(StringPool.SPACE);
129         sm.append(getDescription());
130         sm.append(StringPool.SPACE);
131 
132         Properties extraSettingsProps = getExtraSettingsProperties();
133 
134         Iterator itr = (Iterator)extraSettingsProps.entrySet().iterator();
135 
136         while (itr.hasNext()) {
137             Map.Entry entry = (Map.Entry)itr.next();
138 
139             String value = GetterUtil.getString((String)entry.getValue());
140 
141             sm.append(value);
142         }
143 
144         return sm.toString();
145     }
146 
147     private static Log _log = LogFactory.getLog(DLFileEntryImpl.class);
148 
149     private Properties _extraSettingsProperties = null;
150 
151 }