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.portal.model;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.ListUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.SAXReader;
31  import com.liferay.portal.service.ClassNameLocalServiceUtil;
32  import com.liferay.portal.util.PropsKeys;
33  import com.liferay.portal.util.PropsUtil;
34  
35  import java.util.HashMap;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.Set;
40  import java.util.TreeSet;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="ModelHintsImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class ModelHintsImpl implements ModelHints {
52  
53      public void afterPropertiesSet() {
54          _hintCollections = new HashMap<String, Map<String, String>>();
55          _defaultHints = new HashMap<String, Map<String, String>>();
56          _modelFields = new HashMap<String, Object>();
57          _models = new TreeSet<String>();
58  
59          try {
60              ClassLoader classLoader = getClass().getClassLoader();
61  
62              String[] configs = StringUtil.split(
63                  PropsUtil.get(PropsKeys.MODEL_HINTS_CONFIGS));
64  
65              for (int i = 0; i < configs.length; i++) {
66                  read(classLoader, configs[i]);
67              }
68          }
69          catch (Exception e) {
70              _log.error(e, e);
71          }
72      }
73  
74      public Map<String, String> getDefaultHints(String model) {
75          return _defaultHints.get(model);
76      }
77  
78      public com.liferay.portal.kernel.xml.Element getFieldsEl(
79          String model, String field) {
80  
81          Map<String, Object> fields =
82              (Map<String, Object>)_modelFields.get(model);
83  
84          if (fields == null) {
85              return null;
86          }
87          else {
88              Element fieldsEl = (Element)fields.get(field + _ELEMENTS_SUFFIX);
89  
90              if (fieldsEl == null) {
91                  return null;
92              }
93              else {
94                  return fieldsEl;
95              }
96          }
97      }
98  
99      public List<String> getModels() {
100         return ListUtil.fromCollection(_models);
101     }
102 
103     public String getType(String model, String field) {
104         Map<String, Object> fields =
105             (Map<String, Object>)_modelFields.get(model);
106 
107         if (fields == null) {
108             return null;
109         }
110         else {
111             return (String)fields.get(field + _TYPE_SUFFIX);
112         }
113     }
114 
115     public Map<String, String> getHints(String model, String field) {
116         Map<String, Object> fields =
117             (Map<String, Object>)_modelFields.get(model);
118 
119         if (fields == null) {
120             return null;
121         }
122         else {
123             return (Map<String, String>)fields.get(field + _HINTS_SUFFIX);
124         }
125     }
126 
127     public void read(ClassLoader classLoader, String source) throws Exception {
128         String xml = null;
129 
130         try {
131             xml = StringUtil.read(classLoader, source);
132         }
133         catch (Exception e) {
134             if (_log.isWarnEnabled()) {
135                 _log.warn("Cannot load " + source);
136             }
137         }
138 
139         if (xml == null) {
140             return;
141         }
142 
143         if (_log.isDebugEnabled()) {
144             _log.debug("Loading " + source);
145         }
146 
147         Document doc = _saxReader.read(xml);
148 
149         Element root = doc.getRootElement();
150 
151         Iterator<Element> itr1 = root.elements("hint-collection").iterator();
152 
153         while (itr1.hasNext()) {
154             Element hintCollection = itr1.next();
155 
156             String name = hintCollection.attributeValue("name");
157 
158             Map<String, String> hints = _hintCollections.get(name);
159 
160             if (hints == null) {
161                 hints = new HashMap<String, String>();
162 
163                 _hintCollections.put(name, hints);
164             }
165 
166             Iterator<Element> itr2 = hintCollection.elements("hint").iterator();
167 
168             while (itr2.hasNext()) {
169                 Element hint = itr2.next();
170 
171                 String hintName = hint.attributeValue("name");
172                 String hintValue = hint.getText();
173 
174                 hints.put(hintName, hintValue);
175             }
176         }
177 
178         itr1 = root.elements("model").iterator();
179 
180         while (itr1.hasNext()) {
181             Element model = itr1.next();
182 
183             String name = model.attributeValue("name");
184 
185             if (classLoader != ModelHintsImpl.class.getClassLoader()) {
186                 ClassNameLocalServiceUtil.getClassName(name);
187             }
188 
189             Map<String, String> defaultHints = new HashMap<String, String>();
190 
191             _defaultHints.put(name, defaultHints);
192 
193             Element defaultHintsEl = model.element("default-hints");
194 
195             if (defaultHintsEl != null) {
196                 Iterator<Element> itr2 = defaultHintsEl.elements(
197                     "hint").iterator();
198 
199                 while (itr2.hasNext()) {
200                     Element hint = itr2.next();
201 
202                     String hintName = hint.attributeValue("name");
203                     String hintValue = hint.getText();
204 
205                     defaultHints.put(hintName, hintValue);
206                 }
207             }
208 
209             Map<String, Object> fields =
210                 (Map<String, Object>)_modelFields.get(name);
211 
212             if (fields == null) {
213                 fields = new HashMap<String, Object>();
214 
215                 _modelFields.put(name, fields);
216             }
217 
218             _models.add(name);
219 
220             Iterator<Element> itr2 = model.elements("field").iterator();
221 
222             while (itr2.hasNext()) {
223                 Element field = itr2.next();
224 
225                 String fieldName = field.attributeValue("name");
226                 String fieldType = field.attributeValue("type");
227 
228                 Map<String, String> fieldHints = new HashMap<String, String>();
229 
230                 fieldHints.putAll(defaultHints);
231 
232                 Iterator<Element> itr3 = field.elements(
233                     "hint-collection").iterator();
234 
235                 while (itr3.hasNext()) {
236                     Element hintCollection = itr3.next();
237 
238                     Map<String, String> hints = _hintCollections.get(
239                         hintCollection.attributeValue("name"));
240 
241                     fieldHints.putAll(hints);
242                 }
243 
244                 itr3 = field.elements("hint").iterator();
245 
246                 while (itr3.hasNext()) {
247                     Element hint = itr3.next();
248 
249                     String hintName = hint.attributeValue("name");
250                     String hintValue = hint.getText();
251 
252                     fieldHints.put(hintName, hintValue);
253                 }
254 
255                 fields.put(fieldName + _ELEMENTS_SUFFIX, field);
256                 fields.put(fieldName + _TYPE_SUFFIX, fieldType);
257                 fields.put(fieldName + _HINTS_SUFFIX, fieldHints);
258             }
259         }
260     }
261 
262     public void setSAXReader(SAXReader saxReader) {
263         _saxReader = saxReader;
264     }
265 
266     public String trimString(String model, String field, String value) {
267         if (value == null) {
268             return value;
269         }
270 
271         Map<String, String> hints = getHints(model, field);
272 
273         if (hints == null) {
274             return value;
275         }
276 
277         int maxLength = GetterUtil.getInteger(
278             ModelHintsConstants.TEXT_MAX_LENGTH);
279 
280         maxLength = GetterUtil.getInteger(hints.get("max-length"), maxLength);
281 
282         if (value.length() > maxLength) {
283             return value.substring(0, maxLength);
284         }
285         else {
286             return value;
287         }
288     }
289 
290     private static final String _ELEMENTS_SUFFIX = "_ELEMENTS";
291 
292     private static final String _TYPE_SUFFIX = "_TYPE";
293 
294     private static final String _HINTS_SUFFIX = "_HINTS";
295 
296     private static Log _log = LogFactory.getLog(ModelHintsImpl.class);
297 
298     private Map<String, Map<String, String>> _hintCollections;
299     private Map<String, Map<String, String>> _defaultHints;
300     private Map<String, Object> _modelFields;
301     private Set<String> _models;
302     private SAXReader _saxReader;
303 
304 }