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