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