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.service.ClassNameLocalServiceUtil;
29 import com.liferay.portal.util.PropsKeys;
30 import com.liferay.portal.util.PropsUtil;
31 import com.liferay.portal.xml.ElementImpl;
32
33 import java.io.StringReader;
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 import org.dom4j.Document;
46 import org.dom4j.Element;
47 import org.dom4j.io.SAXReader;
48
49
55 public class ModelHintsImpl implements ModelHints {
56
57 public ModelHintsImpl() {
58 _hintCollections = new HashMap<String, Map<String, String>>();
59 _defaultHints = new HashMap<String, Map<String, String>>();
60 _modelFields = new HashMap();
61 _models = new TreeSet<String>();
62
63 try {
64 ClassLoader classLoader = getClass().getClassLoader();
65
66 String[] configs = StringUtil.split(
67 PropsUtil.get(PropsKeys.MODEL_HINTS_CONFIGS));
68
69 for (int i = 0; i < configs.length; i++) {
70 read(classLoader, configs[i]);
71 }
72 }
73 catch (Exception e) {
74 _log.error(e, e);
75 }
76 }
77
78 public Map<String, String> getDefaultHints(String model) {
79 return _defaultHints.get(model);
80 }
81
82 public com.liferay.portal.kernel.xml.Element getFieldsEl(
83 String model, String field) {
84
85 Map fields = (Map)_modelFields.get(model);
86
87 if (fields == null) {
88 return null;
89 }
90 else {
91 Element fieldsEl = (Element)fields.get(field + _ELEMENTS_SUFFIX);
92
93 if (fieldsEl == null) {
94 return null;
95 }
96 else {
97 return new ElementImpl(fieldsEl);
98 }
99 }
100 }
101
102 public List<String> getModels() {
103 return ListUtil.fromCollection(_models);
104 }
105
106 public String getType(String model, String field) {
107 Map fields = (Map)_modelFields.get(model);
108
109 if (fields == null) {
110 return null;
111 }
112 else {
113 return (String)fields.get(field + _TYPE_SUFFIX);
114 }
115 }
116
117 public Map getHints(String model, String field) {
118 Map fields = (Map)_modelFields.get(model);
119
120 if (fields == null) {
121 return null;
122 }
123 else {
124 return (Map)fields.get(field + _HINTS_SUFFIX);
125 }
126 }
127
128 public void read(ClassLoader classLoader, String source) throws Exception {
129 String xml = null;
130
131 try {
132 xml = StringUtil.read(classLoader, source);
133 }
134 catch (Exception e) {
135 _log.warn("Cannot load " + source);
136 }
137
138 if (xml == null) {
139 return;
140 }
141
142 if (_log.isDebugEnabled()) {
143 _log.debug("Loading " + source);
144 }
145
146 SAXReader reader = new SAXReader();
147
148 Document doc = reader.read(new StringReader(xml));
149
150 Element root = doc.getRootElement();
151
152 Iterator<Element> itr1 = root.elements("hint-collection").iterator();
153
154 while (itr1.hasNext()) {
155 Element hintCollection = itr1.next();
156
157 String name = hintCollection.attributeValue("name");
158
159 Map<String, String> hints = _hintCollections.get(name);
160
161 if (hints == null) {
162 hints = new HashMap<String, String>();
163
164 _hintCollections.put(name, hints);
165 }
166
167 Iterator<Element> itr2 = hintCollection.elements("hint").iterator();
168
169 while (itr2.hasNext()) {
170 Element hint = itr2.next();
171
172 String hintName = hint.attributeValue("name");
173 String hintValue = hint.getText();
174
175 hints.put(hintName, hintValue);
176 }
177 }
178
179 itr1 = root.elements("model").iterator();
180
181 while (itr1.hasNext()) {
182 Element model = itr1.next();
183
184 String name = model.attributeValue("name");
185
186 if (classLoader != ModelHintsImpl.class.getClassLoader()) {
187 ClassNameLocalServiceUtil.getClassName(name);
188 }
189
190 Map<String, String> defaultHints = new HashMap<String, String>();
191
192 _defaultHints.put(name, defaultHints);
193
194 Element defaultHintsEl = model.element("default-hints");
195
196 if (defaultHintsEl != null) {
197 Iterator<Element> itr2 = defaultHintsEl.elements(
198 "hint").iterator();
199
200 while (itr2.hasNext()) {
201 Element hint = itr2.next();
202
203 String hintName = hint.attributeValue("name");
204 String hintValue = hint.getText();
205
206 defaultHints.put(hintName, hintValue);
207 }
208 }
209
210 Map fields = (Map)_modelFields.get(name);
211
212 if (fields == null) {
213 fields = new HashMap();
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 String trimString(String model, String field, String value) {
263 if (value == null) {
264 return value;
265 }
266
267 Map<String, String> hints = getHints(model, field);
268
269 if (hints == null) {
270 return value;
271 }
272
273 int maxLength = GetterUtil.getInteger(
274 ModelHintsConstants.TEXT_MAX_LENGTH);
275
276 maxLength = GetterUtil.getInteger(hints.get("max-length"), maxLength);
277
278 if (value.length() > maxLength) {
279 return value.substring(0, maxLength);
280 }
281 else {
282 return value;
283 }
284 }
285
286 private static final String _ELEMENTS_SUFFIX = "_ELEMENTS";
287
288 private static final String _TYPE_SUFFIX = "_TYPE";
289
290 private static final String _HINTS_SUFFIX = "_HINTS";
291
292 private static Log _log = LogFactory.getLog(ModelHintsImpl.class);
293
294 private Map<String, Map<String, String>> _hintCollections;
295 private Map<String, Map<String, String>> _defaultHints;
296 private Map _modelFields;
297 private Set<String> _models;
298
299 }