1
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.util.PropsKeys;
29 import com.liferay.portal.util.PropsUtil;
30 import com.liferay.portal.xml.ElementImpl;
31
32 import java.io.StringReader;
33
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.TreeSet;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 import org.dom4j.Document;
45 import org.dom4j.Element;
46 import org.dom4j.io.SAXReader;
47
48
54 public class ModelHintsImpl implements ModelHints {
55
56 public ModelHintsImpl() {
57 _hintCollections = new HashMap<String, Map<String, String>>();
58 _defaultHints = new HashMap<String, Map<String, String>>();
59 _modelFields = new HashMap();
60 _models = new TreeSet<String>();
61
62 try {
63 ClassLoader classLoader = getClass().getClassLoader();
64
65 String[] configs = StringUtil.split(
66 PropsUtil.get(PropsKeys.MODEL_HINTS_CONFIGS));
67
68 for (int i = 0; i < configs.length; i++) {
69 read(classLoader, configs[i]);
70 }
71 }
72 catch (Exception e) {
73 _log.error(e, e);
74 }
75 }
76
77 public Map<String, String> getDefaultHints(String model) {
78 return _defaultHints.get(model);
79 }
80
81 public com.liferay.portal.kernel.xml.Element getFieldsEl(
82 String model, String field) {
83
84 Map fields = (Map)_modelFields.get(model);
85
86 if (fields == null) {
87 return null;
88 }
89 else {
90 Element fieldsEl = (Element)fields.get(field + _ELEMENTS_SUFFIX);
91
92 if (fieldsEl == null) {
93 return null;
94 }
95 else {
96 return new ElementImpl(fieldsEl);
97 }
98 }
99 }
100
101 public List<String> getModels() {
102 return ListUtil.fromCollection(_models);
103 }
104
105 public String getType(String model, String field) {
106 Map fields = (Map)_modelFields.get(model);
107
108 if (fields == null) {
109 return null;
110 }
111 else {
112 return (String)fields.get(field + _TYPE_SUFFIX);
113 }
114 }
115
116 public Map getHints(String model, String field) {
117 Map fields = (Map)_modelFields.get(model);
118
119 if (fields == null) {
120 return null;
121 }
122 else {
123 return (Map)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 SAXReader reader = new SAXReader();
146
147 Document doc = reader.read(new StringReader(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 Map<String, String> defaultHints = new HashMap<String, String>();
186
187 _defaultHints.put(name, defaultHints);
188
189 Element defaultHintsEl = model.element("default-hints");
190
191 if (defaultHintsEl != null) {
192 Iterator<Element> itr2 = defaultHintsEl.elements(
193 "hint").iterator();
194
195 while (itr2.hasNext()) {
196 Element hint = itr2.next();
197
198 String hintName = hint.attributeValue("name");
199 String hintValue = hint.getText();
200
201 defaultHints.put(hintName, hintValue);
202 }
203 }
204
205 Map fields = (Map)_modelFields.get(name);
206
207 if (fields == null) {
208 fields = new HashMap();
209
210 _modelFields.put(name, fields);
211 }
212
213 _models.add(name);
214
215 Iterator<Element> itr2 = model.elements("field").iterator();
216
217 while (itr2.hasNext()) {
218 Element field = itr2.next();
219
220 String fieldName = field.attributeValue("name");
221 String fieldType = field.attributeValue("type");
222
223 Map<String, String> fieldHints = new HashMap<String, String>();
224
225 fieldHints.putAll(defaultHints);
226
227 Iterator<Element> itr3 = field.elements(
228 "hint-collection").iterator();
229
230 while (itr3.hasNext()) {
231 Element hintCollection = itr3.next();
232
233 Map<String, String> hints = _hintCollections.get(
234 hintCollection.attributeValue("name"));
235
236 fieldHints.putAll(hints);
237 }
238
239 itr3 = field.elements("hint").iterator();
240
241 while (itr3.hasNext()) {
242 Element hint = itr3.next();
243
244 String hintName = hint.attributeValue("name");
245 String hintValue = hint.getText();
246
247 fieldHints.put(hintName, hintValue);
248 }
249
250 fields.put(fieldName + _ELEMENTS_SUFFIX, field);
251 fields.put(fieldName + _TYPE_SUFFIX, fieldType);
252 fields.put(fieldName + _HINTS_SUFFIX, fieldHints);
253 }
254 }
255 }
256
257 public String trimString(String model, String field, String value) {
258 if (value == null) {
259 return value;
260 }
261
262 Map<String, String> hints = getHints(model, field);
263
264 if (hints == null) {
265 return value;
266 }
267
268 int maxLength = GetterUtil.getInteger(
269 ModelHintsConstants.TEXT_MAX_LENGTH);
270
271 maxLength = GetterUtil.getInteger(hints.get("max-length"), maxLength);
272
273 if (value.length() > maxLength) {
274 return value.substring(0, maxLength);
275 }
276 else {
277 return value;
278 }
279 }
280
281 private static final String _ELEMENTS_SUFFIX = "_ELEMENTS";
282
283 private static final String _TYPE_SUFFIX = "_TYPE";
284
285 private static final String _HINTS_SUFFIX = "_HINTS";
286
287 private static Log _log = LogFactory.getLog(ModelHintsImpl.class);
288
289 private Map<String, Map<String, String>> _hintCollections;
290 private Map<String, Map<String, String>> _defaultHints;
291 private Map _modelFields;
292 private Set<String> _models;
293
294 }