1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.social.service.impl;
16  
17  import com.liferay.ibm.icu.util.Calendar;
18  import com.liferay.ibm.icu.util.GregorianCalendar;
19  import com.liferay.portal.NoSuchUserException;
20  import com.liferay.portal.kernel.exception.PortalException;
21  import com.liferay.portal.kernel.exception.SystemException;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.util.PropsValues;
25  import com.liferay.portlet.asset.NoSuchEntryException;
26  import com.liferay.portlet.asset.model.AssetEntry;
27  import com.liferay.portlet.social.NoSuchEquityAssetEntryException;
28  import com.liferay.portlet.social.model.SocialEquityAssetEntry;
29  import com.liferay.portlet.social.model.SocialEquityLog;
30  import com.liferay.portlet.social.model.SocialEquitySetting;
31  import com.liferay.portlet.social.model.SocialEquitySettingConstants;
32  import com.liferay.portlet.social.service.base.SocialEquityLogLocalServiceBaseImpl;
33  import com.liferay.util.dao.orm.CustomSQLUtil;
34  
35  import java.util.Date;
36  import java.util.List;
37  
38  /**
39   * <a href="SocialEquityLogLocalServiceImpl.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Zsolt Berentey
43   * @author Brian Wing Shun Chan
44   */
45  public class SocialEquityLogLocalServiceImpl
46      extends SocialEquityLogLocalServiceBaseImpl {
47  
48      public void addEquityLogs(
49              long userId, long assetEntryId, String actionId)
50          throws PortalException, SystemException {
51  
52          if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
53              return;
54          }
55  
56          List<SocialEquityLog> equityLogs =
57              socialEquityLogPersistence.findByU_AEI_A_A(
58                  userId, assetEntryId, actionId, true);
59  
60          if (!equityLogs.isEmpty()) {
61              return;
62          }
63  
64          User user = userPersistence.findByPrimaryKey(userId);
65  
66          AssetEntry assetEntry = assetEntryPersistence.findByPrimaryKey(
67              assetEntryId);
68  
69          User assetEntryUser = null;
70  
71          try {
72              assetEntryUser = userPersistence.findByPrimaryKey(
73                  assetEntry.getUserId());
74          }
75          catch (NoSuchUserException nsue) {
76          }
77  
78          List<SocialEquitySetting> equitySettings =
79              socialEquitySettingLocalService.getEquitySettings(
80                  assetEntry.getGroupId(), assetEntry.getClassNameId(), actionId);
81  
82          for (SocialEquitySetting equitySetting : equitySettings) {
83              addEquityLog(user, assetEntry, assetEntryUser, equitySetting);
84          }
85      }
86  
87      public void addEquityLogs(
88              long userId, String className, long classPK, String actionId)
89          throws PortalException, SystemException {
90  
91          if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
92              return;
93          }
94  
95          AssetEntry assetEntry = null;
96  
97          try {
98              assetEntry = assetEntryLocalService.getEntry(
99                  className, classPK);
100         }
101         catch (NoSuchEntryException nsee) {
102             return;
103         }
104 
105         addEquityLogs(userId, assetEntry.getEntryId(), actionId);
106     }
107 
108     public void checkEquityLogs() throws SystemException {
109         int validity = getEquityDate();
110 
111         if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
112             return;
113         }
114 
115         runCheckSQL(_CHECK_SOCIAL_EQUITY_ASSET_ENTRY_IQ_1, validity);
116         runCheckSQL(_CHECK_SOCIAL_EQUITY_ASSET_ENTRY_IQ_2, validity);
117         runCheckSQL(_CHECK_ASSET_ENTRY, validity);
118 
119         assetEntryPersistence.clearCache();
120 
121         runCheckSQL(_CHECK_SOCIAL_EQUITY_USER, validity);
122         runCheckSQL(_CHECK_SOCIAL_EQUITY_USER_CQ, validity);
123         runCheckSQL(_CHECK_SOCIAL_EQUITY_USER_PQ_1, validity);
124         runCheckSQL(_CHECK_SOCIAL_EQUITY_USER_PQ_2, validity);
125         runCheckSQL(_CHECK_SOCIAL_EQUITY_USER_PEQ, validity);
126         runCheckSQL(_CHECK_USER_CQ_PQ, validity);
127         runCheckSQL(_CHECK_USER_PEQ, validity);
128 
129         userPersistence.clearCache();
130 
131         runCheckSQL(_CHECK_SOCIAL_EQUITY_LOGS, validity);
132 
133         socialEquityLogPersistence.clearCache();
134     }
135 
136     public void deactivateEquityLogs(long assetEntryId) throws SystemException {
137         if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
138             return;
139         }
140 
141         SocialEquityAssetEntry equityAssetEntry = null;
142 
143         try {
144             equityAssetEntry =
145                 socialEquityAssetEntryPersistence.findByAssetEntryId(
146                     assetEntryId);
147 
148             socialEquityAssetEntryPersistence.removeByAssetEntryId(
149                 assetEntryId);
150         }
151         catch (NoSuchEquityAssetEntryException nseaee) {
152             return;
153         }
154 
155         User user = null;
156 
157         try {
158             user = userPersistence.findByPrimaryKey(
159                 equityAssetEntry.getUserId());
160 
161             if (!user.isDefaultUser()) {
162                 updateSocialEquityUser_CQ(
163                     equityAssetEntry.getGroupId(), user.getUserId(),
164                     -equityAssetEntry.getInformationEquity());
165                 updateUser_CQ(
166                     user, -equityAssetEntry.getInformationEquity());
167             }
168         }
169         catch (NoSuchUserException nsue) {
170         }
171 
172         List<SocialEquityLog> equityLogs =
173             socialEquityLogPersistence.findByAEI_T_A(
174                 assetEntryId, SocialEquitySettingConstants.TYPE_INFORMATION,
175                 true);
176 
177         for (SocialEquityLog equityLog : equityLogs) {
178             equityLog.setActive(false);
179 
180             socialEquityLogPersistence.update(equityLog, false);
181         }
182     }
183 
184     protected void addEquityLog(
185             User user, AssetEntry assetEntry, User assetEntryUser,
186             SocialEquitySetting equitySetting)
187         throws SystemException {
188 
189         int actionDate = getEquityDate();
190 
191         double k = calculateK(
192             equitySetting.getValue(), equitySetting.getValidity());
193         double b = calculateB(
194             actionDate, equitySetting.getValue(), equitySetting.getValidity());
195 
196         if (equitySetting.getType() ==
197             SocialEquitySettingConstants.TYPE_INFORMATION) {
198 
199             int count = socialEquityAssetEntryPersistence.countByAssetEntryId(
200                 assetEntry.getEntryId());
201 
202             if (count == 0) {
203                 addSocialEquityAssetEntry(assetEntry);
204             }
205 
206             updateSocialEquityAssetEntry_IQ(
207                 assetEntry.getEntryId(), actionDate, k, b);
208 
209             double informationEquity = calculateEquity(actionDate, k, b);
210 
211             updateAssetEntry(assetEntry, informationEquity);
212 
213             if ((assetEntryUser != null) && !assetEntryUser.isDefaultUser()) {
214                 updateSocialEquityUser_CQ(
215                     assetEntry.getGroupId(), assetEntryUser.getUserId(),
216                     informationEquity);
217 
218                 updateUser_CQ(assetEntryUser, informationEquity);
219             }
220         }
221         else if (equitySetting.getType() ==
222             SocialEquitySettingConstants.TYPE_PARTICIPATION) {
223 
224             int count = socialEquityUserPersistence.countByG_U(
225                 assetEntry.getGroupId(), user.getUserId());
226 
227             if (count == 0) {
228                 addSocialEquityUser(assetEntry.getGroupId(), user);
229             }
230 
231             if (!user.isDefaultUser()) {
232                 updateSocialEquityUser_PQ(
233                     assetEntry.getGroupId(), user.getUserId(), actionDate, k,
234                     b);
235 
236                 double participationEquity = calculateEquity(actionDate, k, b);
237 
238                 updateUser_PQ(user, participationEquity);
239             }
240         }
241 
242         long equityLogId = counterLocalService.increment();
243 
244         SocialEquityLog equityLog = socialEquityLogPersistence.create(
245             equityLogId);
246 
247         equityLog.setGroupId(assetEntry.getGroupId());
248         equityLog.setCompanyId(user.getCompanyId());
249         equityLog.setUserId(user.getUserId());
250         equityLog.setAssetEntryId(assetEntry.getEntryId());
251         equityLog.setActionId(equitySetting.getActionId());
252         equityLog.setActionDate(actionDate);
253         equityLog.setType(equitySetting.getType());
254         equityLog.setValue(equitySetting.getValue());
255         equityLog.setValidity(actionDate + equitySetting.getValidity());
256         equityLog.setActive(true);
257 
258         socialEquityLogPersistence.update(equityLog, false);
259     }
260 
261     protected void addSocialEquityAssetEntry(AssetEntry assetEntry)
262         throws SystemException {
263 
264         String sql = CustomSQLUtil.get(_ADD_SOCIAL_EQUITY_ASSET_ENTRY);
265 
266         sql = StringUtil.replace(
267             sql,
268             new String[] {
269                 "[$ASSET_ENTRY_ID$]",
270                 "[$COMPANY_ID$]",
271                 "[$EQUITY_ASSET_ENTRY_ID$]",
272                 "[$GROUP_ID$]",
273                 "[$USER_ID$]"
274             },
275             new String[] {
276                 String.valueOf(assetEntry.getEntryId()),
277                 String.valueOf(assetEntry.getCompanyId()),
278                 String.valueOf(counterLocalService.increment()),
279                 String.valueOf(assetEntry.getGroupId()),
280                 String.valueOf(assetEntry.getUserId())
281             });
282 
283         runSQL(sql);
284     }
285 
286     protected void addSocialEquityUser(long groupId, User user)
287         throws SystemException {
288 
289         String sql = CustomSQLUtil.get(_ADD_SOCIAL_EQUITY_USER);
290 
291         sql = StringUtil.replace(
292             sql,
293             new String[] {
294                 "[$COMPANY_ID$]",
295                 "[$EQUITY_USER_ID$]",
296                 "[$GROUP_ID$]",
297                 "[$USER_ID$]"
298             },
299             new String[] {
300                 String.valueOf(user.getCompanyId()),
301                 String.valueOf(counterLocalService.increment()),
302                 String.valueOf(groupId),
303                 String.valueOf(user.getUserId())
304             });
305 
306         runSQL(sql);
307     }
308 
309     protected double calculateB(int actionDate, int value, int validity) {
310         return calculateK(value, validity) * (actionDate + validity) * -1;
311     }
312 
313     protected double calculateEquity(int actionDate, double k, double b) {
314         return k * actionDate + b;
315     }
316 
317     protected double calculateK(int value, int validity) {
318         if (validity == 0) {
319             return 0;
320         }
321 
322         return ((double)value / validity) * -1;
323     }
324 
325     protected int getEquityDate() {
326         return getEquityDate(new Date());
327     }
328 
329     protected int getEquityDate(Date date) {
330         Calendar calendar = new GregorianCalendar(2010, Calendar.JANUARY, 1);
331 
332         return calendar.fieldDifference(date, Calendar.DATE);
333     }
334 
335     protected void removeEquityLogs(long assetEntryId)  throws SystemException {
336         SocialEquityAssetEntry equityAssetEntry = null;
337 
338         try {
339             equityAssetEntry =
340                 socialEquityAssetEntryPersistence.findByAssetEntryId(
341                     assetEntryId);
342 
343             socialEquityAssetEntryPersistence.removeByAssetEntryId(
344                 assetEntryId);
345         }
346         catch (NoSuchEquityAssetEntryException nseaee) {
347             return;
348         }
349 
350         User user = null;
351 
352         try {
353             user = userPersistence.findByPrimaryKey(
354                 equityAssetEntry.getUserId());
355 
356             if (!user.isDefaultUser()) {
357                 updateSocialEquityUser_CQ(
358                     equityAssetEntry.getGroupId(), user.getUserId(),
359                     -equityAssetEntry.getInformationEquity());
360                 updateUser_CQ(
361                     user, -equityAssetEntry.getInformationEquity());
362             }
363         }
364         catch (NoSuchUserException nsue) {
365         }
366 
367         List<SocialEquityLog> equityLogs =
368             socialEquityLogPersistence.findByAEI_T_A(
369                 assetEntryId, SocialEquitySettingConstants.TYPE_INFORMATION,
370                 true);
371 
372         for (SocialEquityLog equityLog : equityLogs) {
373             equityLog.setActive(false);
374 
375             socialEquityLogPersistence.update(equityLog, false);
376         }
377     }
378 
379     protected void runCheckSQL(String sqlId, int validity)
380         throws SystemException {
381 
382         String sql = CustomSQLUtil.get(sqlId);
383 
384         sql = StringUtil.replace(
385             sql,
386             new String[] {
387                 "[$TYPE_INFORMATION$]",
388                 "[$TYPE_PARTICIPATION$]",
389                 "[$VALIDITY$]"
390             },
391             new String[] {
392                 String.valueOf(SocialEquitySettingConstants.TYPE_INFORMATION),
393                 String.valueOf(SocialEquitySettingConstants.TYPE_PARTICIPATION),
394                 String.valueOf(validity)
395             });
396 
397         runSQL(sql);
398     }
399 
400     protected void updateAssetEntry(
401             AssetEntry assetEntry, double informationEquity)
402         throws SystemException {
403 
404         assetEntry.setSocialInformationEquity(
405             assetEntry.getSocialInformationEquity() + informationEquity);
406 
407         assetEntryPersistence.update(assetEntry, true);
408     }
409 
410     protected void updateSocialEquityAssetEntry_IQ(
411             long assetEntryId, int activityDate, double k, double b)
412         throws SystemException {
413 
414         String sql = CustomSQLUtil.get(_UPDATE_SOCIAL_EQUITY_ASSET_ENTRY_IQ);
415 
416         sql = StringUtil.replace(
417             sql,
418             new String[] {
419                 "[$ACTIVITY_DATE$]",
420                 "[$ASSET_ENTRY_ID$]",
421                 "[$INFORMATION_B$]",
422                 "[$INFORMATION_K$]"
423             },
424             new String[] {
425                 String.valueOf(activityDate),
426                 String.valueOf(assetEntryId),
427                 String.valueOf(b),
428                 String.valueOf(k)
429             });
430 
431         runSQL(sql);
432     }
433 
434     protected void updateSocialEquityUser_CQ(
435             long groupId, long userId, double newInformationEquity)
436         throws SystemException {
437 
438         String sql = CustomSQLUtil.get(_UPDATE_SOCIAL_EQUITY_USER_CQ);
439 
440         sql = StringUtil.replace(
441             sql,
442             new String[] {
443                 "[$GROUP_ID$]",
444                 "[$USER_ID$]",
445                 "[$NEW_IQ$]"
446             },
447             new String[] {
448                 String.valueOf(groupId),
449                 String.valueOf(userId),
450                 String.valueOf(newInformationEquity)
451             });
452 
453         runSQL(sql);
454     }
455 
456     protected void updateSocialEquityUser_PQ(
457             long groupId, long userId, int activityDate, double k, double b)
458         throws SystemException {
459 
460         String sql = CustomSQLUtil.get(_UPDATE_SOCIAL_EQUITY_USER_PQ);
461 
462         sql = StringUtil.replace(
463             sql,
464             new String[] {
465                 "[$ACTIVITY_DATE$]",
466                 "[$GROUP_ID$]",
467                 "[$PARTICIPATION_B$]",
468                 "[$PARTICIPATION_K$]",
469                 "[$USER_ID$]"
470             },
471             new String[] {
472                 String.valueOf(activityDate),
473                 String.valueOf(groupId),
474                 String.valueOf(b),
475                 String.valueOf(k),
476                 String.valueOf(userId)
477             });
478 
479         runSQL(sql);
480     }
481 
482     protected void updateUser_CQ(User user, double newInformationEquity)
483         throws SystemException {
484 
485         user.setSocialContributionEquity(
486             user.getSocialContributionEquity() + newInformationEquity);
487         user.setSocialPersonalEquity(
488             user.getSocialContributionEquity() +
489             user.getSocialParticipationEquity());
490 
491         userPersistence.update(user, false);
492     }
493 
494     protected void updateUser_PQ(User user, double newParticipationEquity)
495         throws SystemException {
496 
497         user.setSocialParticipationEquity(
498             user.getSocialParticipationEquity() + newParticipationEquity);
499         user.setSocialPersonalEquity(
500             user.getSocialContributionEquity() +
501             user.getSocialParticipationEquity());
502 
503         userPersistence.update(user, false);
504     }
505 
506     private static final String _ADD_SOCIAL_EQUITY_ASSET_ENTRY =
507         SocialEquityLogLocalServiceImpl.class.getName() +
508             ".addSocialEquityAssetEntry";
509 
510     private static final String _ADD_SOCIAL_EQUITY_USER =
511         SocialEquityLogLocalServiceImpl.class.getName() +
512             ".addSocialEquityUser";
513 
514     private static final String _CHECK_ASSET_ENTRY =
515         SocialEquityLogLocalServiceImpl.class.getName() + ".checkAssetEntry";
516 
517     private static final String _CHECK_SOCIAL_EQUITY_ASSET_ENTRY_IQ_1 =
518         SocialEquityLogLocalServiceImpl.class.getName() +
519             ".checkSocialEquityAssetEntry_IQ_1";
520 
521     private static final String _CHECK_SOCIAL_EQUITY_ASSET_ENTRY_IQ_2 =
522         SocialEquityLogLocalServiceImpl.class.getName() +
523             ".checkSocialEquityAssetEntry_IQ_2";
524 
525     private static final String _CHECK_SOCIAL_EQUITY_LOGS =
526         SocialEquityLogLocalServiceImpl.class.getName() +
527             ".checkSocialEquityLogs";
528 
529     private static final String _CHECK_SOCIAL_EQUITY_USER =
530         SocialEquityLogLocalServiceImpl.class.getName() +
531             ".checkSocialEquityUser";
532 
533     private static final String _CHECK_SOCIAL_EQUITY_USER_CQ =
534         SocialEquityLogLocalServiceImpl.class.getName() +
535             ".checkSocialEquityUser_CQ";
536 
537     private static final String _CHECK_SOCIAL_EQUITY_USER_PEQ =
538         SocialEquityLogLocalServiceImpl.class.getName() +
539             ".checkSocialEquityUser_PEQ";
540 
541     private static final String _CHECK_SOCIAL_EQUITY_USER_PQ_1 =
542         SocialEquityLogLocalServiceImpl.class.getName() +
543             ".checkSocialEquityUser_PQ_1";
544 
545     private static final String _CHECK_SOCIAL_EQUITY_USER_PQ_2 =
546         SocialEquityLogLocalServiceImpl.class.getName() +
547             ".checkSocialEquityUser_PQ_2";
548 
549     private static final String _CHECK_USER_CQ_PQ =
550         SocialEquityLogLocalServiceImpl.class.getName() + ".checkUser_CQ_PQ";
551 
552     private static final String _CHECK_USER_PEQ =
553         SocialEquityLogLocalServiceImpl.class.getName() + ".checkUser_PEQ";
554 
555     private static final String _UPDATE_SOCIAL_EQUITY_ASSET_ENTRY_IQ =
556         SocialEquityLogLocalServiceImpl.class.getName() +
557             ".updateSocialEquityAssetEntry_IQ";
558 
559     private static final String _UPDATE_SOCIAL_EQUITY_USER_CQ =
560         SocialEquityLogLocalServiceImpl.class.getName() +
561             ".updateSocialEquityUser_CQ";
562 
563     private static final String _UPDATE_SOCIAL_EQUITY_USER_PQ =
564         SocialEquityLogLocalServiceImpl.class.getName() +
565             ".updateSocialEquityUser_PQ";
566 
567 }