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