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