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             _log.warn("Cannot load " + source);
135         }
136 
137         if (xml == null) {
138             return;
139         }
140 
141         if (_log.isDebugEnabled()) {
142             _log.debug("Loading " + source);
143         }
144 
145         Document doc = _saxReader.read(xml);
146 
147         Element root = doc.getRootElement();
148 
149         Iterator<Element> itr1 = root.elements("hint-collection").iterator();
150 
151         while (itr1.hasNext()) {
152             Element hintCollection = itr1.next();
153 
154             String name = hintCollection.attributeValue("name");
155 
156             Map<String, String> hints = _hintCollections.get(name);
157 
158             if (hints == null) {
159                 hints = new HashMap<String, String>();
160 
161                 _hintCollections.put(name, hints);
162             }
163 
164             Iterator<Element> itr2 = hintCollection.elements("hint").iterator();
165 
166             while (itr2.hasNext()) {
167                 Element hint = itr2.next();
168 
169                 String hintName = hint.attributeValue("name");
170                 String hintValue = hint.getText();
171 
172                 hints.put(hintName, hintValue);
173             }
174         }
175 
176         itr1 = root.elements("model").iterator();
177 
178         while (itr1.hasNext()) {
179             Element model = itr1.next();
180 
181             String name = model.attributeValue("name");
182 
183             if (classLoader != ModelHintsImpl.class.getClassLoader()) {
184                 ClassNameLocalServiceUtil.getClassName(name);
185             }
186 
187             Map<String, String> defaultHints = new HashMap<String, String>();
188 
189             _defaultHints.put(name, defaultHints);
190 
191             Element defaultHintsEl = model.element("default-hints");
192 
193             if (defaultHintsEl != null) {
194                 Iterator<Element> itr2 = defaultHintsEl.elements(
195                     "hint").iterator();
196 
197                 while (itr2.hasNext()) {
198                     Element hint = itr2.next();
199 
200                     String hintName = hint.attributeValue("name");
201                     String hintValue = hint.getText();
202 
203                     defaultHints.put(hintName, hintValue);
204                 }
205             }
206 
207             Map<String, Object> fields =
208                 (Map<String, Object>)_modelFields.get(name);
209 
210             if (fields == null) {
211                 fields = new HashMap<String, Object>();
212 
213                 _modelFields.put(name, fields);
214             }
215 
216             _models.add(name);
217 
218             Iterator<Element> itr2 = model.elements("field").iterator();
219 
220             while (itr2.hasNext()) {
221                 Element field = itr2.next();
222 
223                 String fieldName = field.attributeValue("name");
224                 String fieldType = field.attributeValue("type");
225 
226                 Map<String, String> fieldHints = new HashMap<String, String>();
227 
228                 fieldHints.putAll(defaultHints);
229 
230                 Iterator<Element> itr3 = field.elements(
231                     "hint-collection").iterator();
232 
233                 while (itr3.hasNext()) {
234                     Element hintCollection = itr3.next();
235 
236                     Map<String, String> hints = _hintCollections.get(
237                         hintCollection.attributeValue("name"));
238 
239                     fieldHints.putAll(hints);
240                 }
241 
242                 itr3 = field.elements("hint").iterator();
243 
244                 while (itr3.hasNext()) {
245                     Element hint = itr3.next();
246 
247                     String hintName = hint.attributeValue("name");
248                     String hintValue = hint.getText();
249 
250                     fieldHints.put(hintName, hintValue);
251                 }
252 
253                 fields.put(fieldName + _ELEMENTS_SUFFIX, field);
254                 fields.put(fieldName + _TYPE_SUFFIX, fieldType);
255                 fields.put(fieldName + _HINTS_SUFFIX, fieldHints);
256             }
257         }
258     }
259 
260     public void setSAXReader(SAXReader saxReader) {
261         _saxReader = saxReader;
262     }
263 
264     public String trimString(String model, String field, String value) {
265         if (value == null) {
266             return value;
267         }
268 
269         Map<String, String> hints = getHints(model, field);
270 
271         if (hints == null) {
272             return value;
273         }
274 
275         int maxLength = GetterUtil.getInteger(
276             ModelHintsConstants.TEXT_MAX_LENGTH);
277 
278         maxLength = GetterUtil.getInteger(hints.get("max-length"), maxLength);
279 
280         if (value.length() > maxLength) {
281             return value.substring(0, maxLength);
282         }
283         else {
284             return value;
285         }
286     }
287 
288     private static final String _ELEMENTS_SUFFIX = "_ELEMENTS";
289 
290     private static final String _TYPE_SUFFIX = "_TYPE";
291 
292     private static final String _HINTS_SUFFIX = "_HINTS";
293 
294     private static Log _log = LogFactory.getLog(ModelHintsImpl.class);
295 
296     private Map<String, Map<String, String>> _hintCollections;
297     private Map<String, Map<String, String>> _defaultHints;
298     private Map<String, Object> _modelFields;
299     private Set<String> _models;
300     private SAXReader _saxReader;
301 
302 }