001
014
015 package com.liferay.portlet.social.util;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.GetterUtil;
020 import com.liferay.portal.kernel.util.ListUtil;
021 import com.liferay.portal.kernel.util.ProxyFactory;
022 import com.liferay.portal.kernel.util.Tuple;
023 import com.liferay.portal.kernel.util.UniqueList;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.kernel.xml.Document;
026 import com.liferay.portal.kernel.xml.Element;
027 import com.liferay.portal.kernel.xml.SAXReaderUtil;
028 import com.liferay.portal.util.JavaFieldsParser;
029 import com.liferay.portlet.social.model.SocialAchievement;
030 import com.liferay.portlet.social.model.SocialActivityCounterConstants;
031 import com.liferay.portlet.social.model.SocialActivityCounterDefinition;
032 import com.liferay.portlet.social.model.SocialActivityDefinition;
033 import com.liferay.portlet.social.model.SocialActivityProcessor;
034
035 import java.util.ArrayList;
036 import java.util.Collections;
037 import java.util.HashMap;
038 import java.util.List;
039 import java.util.Map;
040 import java.util.Set;
041
042
045 public class SocialConfigurationUtil {
046
047 public static List<String> getActivityCounterNames() {
048 return getActivityCounterNames(
049 SocialActivityCounterConstants.TYPE_ALL, false);
050 }
051
052 public static List<String> getActivityCounterNames(
053 boolean transientCounter) {
054
055 return getActivityCounterNames(
056 SocialActivityCounterConstants.TYPE_ALL, transientCounter);
057 }
058
059 public static List<String> getActivityCounterNames(int ownerType) {
060 return getActivityCounterNames(ownerType, false);
061 }
062
063 public static List<String> getActivityCounterNames(
064 int ownerType, boolean transientCounter) {
065
066 List<String> activityCounterNames = new UniqueList<String>();
067
068 for (Map<Integer, SocialActivityDefinition> activityDefinitions :
069 _activityDefinitions.values()) {
070
071 for (SocialActivityDefinition activityDefinition :
072 activityDefinitions.values()) {
073
074 for (SocialActivityCounterDefinition activityCounterDefinition :
075 activityDefinition.getActivityCounterDefinitions()) {
076
077 if ((activityCounterDefinition.isTransient() ==
078 transientCounter) &&
079 ((ownerType ==
080 SocialActivityCounterConstants.TYPE_ALL) ||
081 (ownerType ==
082 activityCounterDefinition.getOwnerType()))) {
083
084 activityCounterNames.add(
085 activityCounterDefinition.getName());
086 }
087 }
088 }
089 }
090
091 return activityCounterNames;
092 }
093
094 public static SocialActivityDefinition getActivityDefinition(
095 String modelName, int activityType) {
096
097 Map<Integer, SocialActivityDefinition> activityDefinitions =
098 _activityDefinitions.get(modelName);
099
100 if (activityDefinitions == null) {
101 return null;
102 }
103
104 return activityDefinitions.get(activityType);
105 }
106
107 public static List<SocialActivityDefinition> getActivityDefinitions(
108 String modelName) {
109
110 Map<Integer, SocialActivityDefinition> activityDefinitions =
111 _activityDefinitions.get(modelName);
112
113 if (activityDefinitions == null) {
114 return Collections.emptyList();
115 }
116
117 return ListUtil.fromCollection(activityDefinitions.values());
118 }
119
120 public static String[] getActivityModelNames() {
121 Set<String> activityModelNames = _activityDefinitions.keySet();
122
123 return activityModelNames.toArray(
124 new String[activityModelNames.size()]);
125 }
126
127 public static List<Object> read(ClassLoader classLoader, String[] xmls)
128 throws Exception {
129
130 List<Object> objects = new ArrayList<Object>();
131
132 for (String xml : xmls) {
133 _read(classLoader, xml, objects);
134 }
135
136 return objects;
137 }
138
139 public static void removeActivityDefinition(
140 SocialActivityDefinition activityDefinition) {
141
142 Map<Integer, SocialActivityDefinition> activityDefinitions =
143 _activityDefinitions.get(activityDefinition.getModelName());
144
145 if (activityDefinitions != null) {
146 activityDefinitions.remove(activityDefinition.getActivityType());
147 }
148 }
149
150 private static void _read(
151 ClassLoader classLoader, String xml, List<Object> objects)
152 throws Exception {
153
154 if (xml == null) {
155 return;
156 }
157
158 xml = JavaFieldsParser.parse(classLoader, xml);
159
160 Document document = SAXReaderUtil.read(xml);
161
162 Element rootElement = document.getRootElement();
163
164 List<Element> activityElements = rootElement.elements("activity");
165
166 for (Element activityElement : activityElements) {
167 _readActivity(classLoader, objects, activityElement);
168 }
169 }
170
171 private static void _readAchievement(
172 ClassLoader classLoader, List<Object> objects,
173 SocialActivityDefinition activityDefinition,
174 Element achievementElement)
175 throws Exception {
176
177 String achievementClassName = GetterUtil.getString(
178 achievementElement.elementText("achievement-class"));
179
180 SocialAchievement achievement =
181 (SocialAchievement)ProxyFactory.newInstance(
182 classLoader, SocialAchievement.class, achievementClassName);
183
184 String name = GetterUtil.getString(
185 achievementElement.elementText("name"));
186
187 achievement.setName(name);
188
189 String icon = GetterUtil.getString(
190 achievementElement.elementText("icon"));
191
192 achievement.setIcon(icon);
193
194 List<Element> propertyElements = achievementElement.elements(
195 "property");
196
197 for (Element propertyElement : propertyElements) {
198 _readAchievementProperty(achievement, propertyElement);
199 }
200
201 achievement.initialize(activityDefinition);
202
203 List<SocialAchievement> achievements =
204 activityDefinition.getAchievements();
205
206 achievements.add(achievement);
207
208 Tuple tuple = new Tuple(activityDefinition, achievement);
209
210 objects.add(tuple);
211 }
212
213 private static void _readAchievementProperty(
214 SocialAchievement achievement, Element propertyElement) {
215
216 String name = GetterUtil.getString(propertyElement.elementText("name"));
217 String value = GetterUtil.getString(
218 propertyElement.elementText("value"));
219
220 achievement.setProperty(name, value);
221 }
222
223 private static void _readActivity(
224 ClassLoader classLoader, Element activityElement,
225 SocialActivityDefinition activityDefinition)
226 throws Exception {
227
228 String languageKey = GetterUtil.getString(
229 activityElement.elementText("language-key"));
230
231 activityDefinition.setLanguageKey(languageKey);
232
233 boolean logActivity = GetterUtil.getBoolean(
234 activityElement.elementText("log-activity"));
235
236 activityDefinition.setLogActivity(logActivity);
237
238 String processorClassName = GetterUtil.getString(
239 activityElement.elementText("processor-class"));
240
241 if (Validator.isNotNull(processorClassName)) {
242 SocialActivityProcessor activityProcessor =
243 (SocialActivityProcessor)ProxyFactory.newInstance(
244 classLoader, SocialActivityProcessor.class,
245 processorClassName);
246
247 activityDefinition.setActivityProcessor(activityProcessor);
248 }
249
250 _readActivityContribution(activityElement, activityDefinition);
251 _readActivityParticipation(activityElement, activityDefinition);
252 }
253
254 private static void _readActivity(
255 ClassLoader classLoader, List<Object> objects,
256 Element activityElement)
257 throws Exception {
258
259 String modelName = GetterUtil.getString(
260 activityElement.elementText("model-name"));
261
262 Map<Integer, SocialActivityDefinition> activityDefinitions =
263 _activityDefinitions.get(modelName);
264
265 if (activityDefinitions == null) {
266 activityDefinitions =
267 new HashMap<Integer, SocialActivityDefinition>();
268
269 _activityDefinitions.put(modelName, activityDefinitions);
270 }
271
272 int activityType = GetterUtil.getInteger(
273 activityElement.elementText("activity-type"));
274
275 SocialActivityDefinition activityDefinition = activityDefinitions.get(
276 activityType);
277
278 if (activityDefinition == null) {
279 activityDefinition = new SocialActivityDefinition();
280
281 activityDefinition.setModelName(modelName);
282 activityDefinition.setActivityType(activityType);
283
284 _readActivity(classLoader, activityElement, activityDefinition);
285
286 activityDefinitions.put(activityType, activityDefinition);
287
288 objects.add(activityDefinition);
289 }
290
291 List<Element> counterElements = activityElement.elements("counter");
292
293 for (Element counterElement : counterElements) {
294 _readCounter(objects, activityDefinition, counterElement);
295 }
296
297 List<Element> achievementElements = activityElement.elements(
298 "achievement");
299
300 for (Element achievementElement : achievementElements) {
301 _readAchievement(
302 classLoader, objects, activityDefinition, achievementElement);
303 }
304 }
305
306 private static void _readActivityContribution(
307 Element activityElement, SocialActivityDefinition activityDefinition) {
308
309 Element contributionValueElement = activityElement.element(
310 "contribution-value");
311 Element contributionLimitElement = activityElement.element(
312 "contribution-limit");
313
314 if ((contributionValueElement == null) &&
315 (contributionLimitElement == null)) {
316
317 return;
318 }
319
320 SocialActivityCounterDefinition activityCounterDefinition =
321 new SocialActivityCounterDefinition();
322
323 activityCounterDefinition.setName(
324 SocialActivityCounterConstants.NAME_CONTRIBUTION);
325 activityCounterDefinition.setOwnerType(
326 SocialActivityCounterConstants.TYPE_CREATOR);
327
328 int increment = 0;
329
330 if (contributionValueElement != null) {
331 increment = GetterUtil.getInteger(
332 contributionValueElement.getText());
333 }
334
335 activityCounterDefinition.setIncrement(increment);
336
337 if (contributionLimitElement != null) {
338 String limitEnabled = contributionLimitElement.attributeValue(
339 "enabled");
340
341 if (Validator.isNotNull(limitEnabled)) {
342 activityCounterDefinition.setLimitEnabled(
343 GetterUtil.getBoolean(limitEnabled));
344 }
345
346 String limitPeriod = contributionLimitElement.attributeValue(
347 "period");
348
349 if (Validator.isNotNull(limitPeriod)) {
350 activityCounterDefinition.setLimitPeriod(limitPeriod);
351 }
352
353 int limitValue = GetterUtil.getInteger(
354 contributionLimitElement.getText());
355
356 activityCounterDefinition.setLimitValue(limitValue);
357 }
358
359 activityDefinition.addCounter(activityCounterDefinition);
360
361 SocialActivityCounterDefinition popularityActivityCounterDefinition =
362 new SocialActivityCounterDefinition();
363
364 popularityActivityCounterDefinition.setName(
365 SocialActivityCounterConstants.NAME_POPULARITY);
366 popularityActivityCounterDefinition.setOwnerType(
367 SocialActivityCounterConstants.TYPE_ASSET);
368 popularityActivityCounterDefinition.setIncrement(
369 activityCounterDefinition.getIncrement());
370 popularityActivityCounterDefinition.setLimitEnabled(
371 activityCounterDefinition.isLimitEnabled());
372 popularityActivityCounterDefinition.setLimitPeriod(
373 activityCounterDefinition.getLimitPeriod());
374 popularityActivityCounterDefinition.setLimitValue(
375 activityCounterDefinition.getLimitValue());
376
377 activityDefinition.addCounter(popularityActivityCounterDefinition);
378 }
379
380 private static void _readActivityParticipation(
381 Element activityElement, SocialActivityDefinition activityDefinition) {
382
383 Element participationValueElement = activityElement.element(
384 "participation-value");
385 Element participationLimitElement = activityElement.element(
386 "participation-limit");
387
388 if ((participationValueElement == null) &&
389 (participationLimitElement == null)) {
390
391 return;
392 }
393
394 SocialActivityCounterDefinition activityCounterDefinition =
395 new SocialActivityCounterDefinition();
396
397 activityCounterDefinition.setName(
398 SocialActivityCounterConstants.NAME_PARTICIPATION);
399 activityCounterDefinition.setOwnerType(
400 SocialActivityCounterConstants.TYPE_ACTOR);
401
402 int increment = 0;
403
404 if (participationValueElement != null) {
405 increment = GetterUtil.getInteger(
406 participationValueElement.getText());
407 }
408
409 activityCounterDefinition.setIncrement(increment);
410
411 if (participationLimitElement != null) {
412 String limitEnabled = participationLimitElement.attributeValue(
413 "enabled");
414
415 if (Validator.isNotNull(limitEnabled)) {
416 activityCounterDefinition.setLimitEnabled(
417 GetterUtil.getBoolean(limitEnabled));
418 }
419
420 String limitPeriod = participationLimitElement.attributeValue(
421 "period");
422
423 if (Validator.isNotNull(limitPeriod)) {
424 activityCounterDefinition.setLimitPeriod(limitPeriod);
425 }
426
427 int limitValue = GetterUtil.getInteger(
428 participationLimitElement.getText());
429
430 activityCounterDefinition.setLimitValue(limitValue);
431 }
432
433 activityDefinition.addCounter(activityCounterDefinition);
434 }
435
436 private static void _readCounter(
437 List<Object> objects, SocialActivityDefinition activityDefinition,
438 Element counterElement) {
439
440 SocialActivityCounterDefinition activityCounterDefinition =
441 new SocialActivityCounterDefinition();
442
443 int increment = GetterUtil.getInteger(
444 counterElement.elementText("increment"), 1);
445
446 activityCounterDefinition.setIncrement(increment);
447
448 boolean enabled = GetterUtil.getBoolean(
449 counterElement.elementText("enabled"), true);
450
451 activityCounterDefinition.setEnabled(enabled);
452
453 String name = GetterUtil.getString(counterElement.elementText("name"));
454
455 activityCounterDefinition.setName(name);
456
457 String ownerType = GetterUtil.getString(
458 counterElement.elementText("owner-type"));
459
460 activityCounterDefinition.setOwnerType(ownerType);
461
462 if (activityCounterDefinition.getOwnerType() == 0) {
463 if (_log.isWarnEnabled()) {
464 _log.warn(
465 "Invalid owner type " + ownerType + " for model " +
466 activityDefinition.getModelName());
467 }
468
469 return;
470 }
471
472 activityDefinition.addCounter(activityCounterDefinition);
473
474 Tuple tuple = new Tuple(activityDefinition, activityCounterDefinition);
475
476 objects.add(tuple);
477 }
478
479 private static Log _log = LogFactoryUtil.getLog(
480 SocialConfigurationUtil.class);
481
482 private static Map<String, Map<Integer, SocialActivityDefinition>>
483 _activityDefinitions =
484 new HashMap<String, Map<Integer, SocialActivityDefinition>>();
485
486 }