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