001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.social.service.impl;
016    
017    import com.liferay.ibm.icu.util.Calendar;
018    import com.liferay.ibm.icu.util.GregorianCalendar;
019    import com.liferay.portal.NoSuchUserException;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.increment.BufferedIncrement;
023    import com.liferay.portal.kernel.increment.SocialEquityIncrement;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portlet.asset.NoSuchEntryException;
029    import com.liferay.portlet.asset.model.AssetEntry;
030    import com.liferay.portlet.social.NoSuchEquityAssetEntryException;
031    import com.liferay.portlet.social.model.SocialEquityAssetEntry;
032    import com.liferay.portlet.social.model.SocialEquityLog;
033    import com.liferay.portlet.social.model.SocialEquitySetting;
034    import com.liferay.portlet.social.model.SocialEquitySettingConstants;
035    import com.liferay.portlet.social.model.SocialEquityValue;
036    import com.liferay.portlet.social.service.base.SocialEquityLogLocalServiceBaseImpl;
037    import com.liferay.util.dao.orm.CustomSQLUtil;
038    
039    import java.sql.PreparedStatement;
040    import java.sql.ResultSet;
041    import java.sql.SQLException;
042    
043    import java.util.ArrayList;
044    import java.util.Date;
045    import java.util.List;
046    
047    import javax.sql.DataSource;
048    
049    import org.springframework.dao.DataAccessException;
050    import org.springframework.jdbc.core.BatchPreparedStatementSetter;
051    import org.springframework.jdbc.core.JdbcTemplate;
052    import org.springframework.jdbc.core.RowCallbackHandler;
053    
054    /**
055     * @author Zsolt Berentey
056     * @author Brian Wing Shun Chan
057     */
058    public class SocialEquityLogLocalServiceImpl
059            extends SocialEquityLogLocalServiceBaseImpl {
060    
061            public void addEquityLogs(
062                            long userId, long assetEntryId, String actionId)
063                    throws PortalException, SystemException {
064    
065                    if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
066                            return;
067                    }
068    
069                    User user = userPersistence.findByPrimaryKey(userId);
070    
071                    AssetEntry assetEntry = assetEntryPersistence.findByPrimaryKey(
072                            assetEntryId);
073    
074                    User assetEntryUser = null;
075    
076                    try {
077                            assetEntryUser = userPersistence.findByPrimaryKey(
078                                    assetEntry.getUserId());
079                    }
080                    catch (NoSuchUserException nsue) {
081                    }
082    
083                    List<SocialEquitySetting> equitySettings =
084                            socialEquitySettingLocalService.getEquitySettings(
085                                    assetEntry.getGroupId(), assetEntry.getClassNameId(), actionId);
086    
087                    for (SocialEquitySetting equitySetting : equitySettings) {
088                            if (isSocialEquityEnabled(
089                                            assetEntry.getGroupId(), assetEntry.getClassName(),
090                                            equitySetting.getType())) {
091    
092                                    addEquityLog(user, assetEntry, assetEntryUser, equitySetting);
093                            }
094                    }
095            }
096    
097            public void addEquityLogs(
098                            long userId, String className, long classPK, String actionId)
099                    throws PortalException, SystemException {
100    
101                    if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
102                            return;
103                    }
104    
105                    AssetEntry assetEntry = null;
106    
107                    try {
108                            assetEntry = assetEntryLocalService.getEntry(
109                                    className, classPK);
110                    }
111                    catch (NoSuchEntryException nsee) {
112                            return;
113                    }
114    
115                    addEquityLogs(userId, assetEntry.getEntryId(), actionId);
116            }
117    
118            public void checkEquityLogs() throws SystemException {
119                    int validity = getEquityDate();
120    
121                    if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
122                            return;
123                    }
124    
125                    runCheckSQL(_CHECK_SOCIAL_EQUITY_ASSET_ENTRY_IQ, validity);
126    
127                    assetEntryPersistence.clearCache();
128    
129                    runCheckSQL(_CHECK_SOCIAL_EQUITY_USER, validity);
130                    runCheckSQL(_CHECK_SOCIAL_EQUITY_USER_CQ, validity);
131                    runCheckSQL(_CHECK_SOCIAL_EQUITY_USER_PQ, validity);
132    
133                    userPersistence.clearCache();
134    
135                    runCheckSQL(_CHECK_SOCIAL_EQUITY_LOGS, validity);
136    
137                    socialEquityLogPersistence.clearCache();
138    
139                    updateRanks();
140            }
141    
142            public void deactivateEquityLogs(long assetEntryId)
143                    throws PortalException, SystemException {
144    
145                    if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
146                            return;
147                    }
148    
149                    SocialEquityAssetEntry equityAssetEntry = null;
150    
151                    try {
152                            equityAssetEntry =
153                                    socialEquityAssetEntryPersistence.findByAssetEntryId(
154                                            assetEntryId);
155    
156                            socialEquityAssetEntryPersistence.removeByAssetEntryId(
157                                    assetEntryId);
158                    }
159                    catch (NoSuchEquityAssetEntryException nseaee) {
160                            return;
161                    }
162    
163                    User user = null;
164    
165                    try {
166                            user = userPersistence.findByPrimaryKey(
167                                    equityAssetEntry.getUserId());
168    
169                            if (!user.isDefaultUser()) {
170                                    SocialEquityValue socialEquityValue = new SocialEquityValue(
171                                            -equityAssetEntry.getInformationK(),
172                                            -equityAssetEntry.getInformationB());
173    
174                                    incrementSocialEquityUser_CQ(
175                                            equityAssetEntry.getGroupId(), user.getUserId(),
176                                            socialEquityValue);
177                            }
178                    }
179                    catch (NoSuchUserException nsue) {
180                    }
181    
182                    List<SocialEquityLog> equityLogs =
183                            socialEquityLogPersistence.findByAEI_T_A(
184                                    assetEntryId, SocialEquitySettingConstants.TYPE_INFORMATION,
185                                    true);
186    
187                    for (SocialEquityLog equityLog : equityLogs) {
188                            equityLog.setActive(false);
189    
190                            socialEquityLogPersistence.update(equityLog, false);
191                    }
192            }
193    
194            public void deactivateEquityLogs(
195                            long userId, long assetEntryId, String actionId)
196                    throws PortalException, SystemException {
197    
198                    if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
199                            return;
200                    }
201    
202                    User user = userPersistence.findByPrimaryKey(userId);
203    
204                    AssetEntry assetEntry = assetEntryPersistence.findByPrimaryKey(
205                            assetEntryId);
206    
207                    // Information Equity
208    
209                    if (isSocialEquityEnabled(
210                                    assetEntry.getGroupId(), assetEntry.getClassName(),
211                                    SocialEquitySettingConstants.TYPE_INFORMATION)) {
212    
213                            List<SocialEquityLog> equityLogs =
214                                    socialEquityLogPersistence.findByAEI_AID_A_T(
215                                            assetEntryId, actionId, true,
216                                            SocialEquitySettingConstants.TYPE_INFORMATION);
217    
218                            SocialEquityValue socialEquityValue = new SocialEquityValue(0,0);
219    
220                            for (SocialEquityLog equityLog : equityLogs) {
221                                    double k = calculateK(
222                                            equityLog.getValue(),equityLog.getLifespan());
223                                    double b = calculateB(
224                                            equityLog.getActionDate(), equityLog.getValue(),
225                                            equityLog.getLifespan());
226    
227                                    socialEquityValue.subtract(new SocialEquityValue(k,b));
228    
229                                    socialEquityLogPersistence.remove(equityLog);
230                            }
231    
232                            socialEquityLogLocalService.incrementSocialEquityAssetEntry_IQ(
233                                    assetEntryId, socialEquityValue);
234    
235                            socialEquityLogLocalService.incrementSocialEquityUser_CQ(
236                                    assetEntry.getGroupId(), assetEntry.getUserId(),
237                                    socialEquityValue);
238                    }
239    
240                    // Participation Equity
241    
242                    if (isSocialEquityEnabled(
243                                    assetEntry.getGroupId(), assetEntry.getClassName(),
244                                    SocialEquitySettingConstants.TYPE_PARTICIPATION)) {
245    
246                            List<SocialEquityLog> equityLogs =
247                                    socialEquityLogPersistence.findByU_AID_A_T(
248                                            userId, actionId, true,
249                                            SocialEquitySettingConstants.TYPE_PARTICIPATION);
250    
251                            SocialEquityValue socialEquityValue = new SocialEquityValue(0,0);
252    
253                            for (SocialEquityLog equityLog : equityLogs) {
254                                    double k = calculateK(
255                                            equityLog.getValue(),equityLog.getLifespan());
256                                    double b = calculateB(
257                                            equityLog.getActionDate(), equityLog.getValue(),
258                                            equityLog.getLifespan());
259    
260                                    socialEquityValue.subtract(new SocialEquityValue(k,b));
261    
262                                    socialEquityLogPersistence.remove(equityLog);
263                            }
264    
265                            socialEquityLogLocalService.incrementSocialEquityUser_PQ(
266                                    user.getGroup().getGroupId(), userId, socialEquityValue);
267                    }
268            }
269    
270            public void deactivateEquityLogs(
271                            long userId, String className, long classPK, String actionId)
272                    throws PortalException, SystemException {
273    
274                    if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
275                            return;
276                    }
277    
278                    AssetEntry assetEntry = null;
279    
280                    try {
281                            assetEntry = assetEntryLocalService.getEntry(
282                                    className, classPK);
283                    }
284                    catch (NoSuchEntryException nsee) {
285                            return;
286                    }
287    
288                    deactivateEquityLogs(userId, assetEntry.getEntryId(), actionId);
289            }
290    
291            @BufferedIncrement(incrementClass = SocialEquityIncrement.class)
292            public void incrementSocialEquityAssetEntry_IQ(
293                            long assetEntryId, SocialEquityValue socialEquityValue)
294                    throws SystemException {
295    
296                    AssetEntry assetEntry = assetEntryPersistence.fetchByPrimaryKey(
297                            assetEntryId);
298    
299                    assetEntry.updateSocialInformationEquity(socialEquityValue.getValue());
300    
301                    int count = socialEquityAssetEntryPersistence.countByAssetEntryId(
302                            assetEntryId);
303    
304                    if (count == 0) {
305                            addSocialEquityAssetEntry(assetEntry);
306                    }
307    
308                    String sql = CustomSQLUtil.get(_UPDATE_SOCIAL_EQUITY_ASSET_ENTRY_IQ);
309    
310                    sql = StringUtil.replace(
311                            sql,
312                            new String[] {
313                                    "[$ASSET_ENTRY_ID$]",
314                                    "[$INFORMATION_B$]",
315                                    "[$INFORMATION_K$]"
316                            },
317                            new String[] {
318                                    String.valueOf(assetEntryId),
319                                    String.valueOf(socialEquityValue.getB()),
320                                    String.valueOf(socialEquityValue.getK())
321                            });
322    
323                    runSQL(sql);
324            }
325    
326            @BufferedIncrement(incrementClass = SocialEquityIncrement.class)
327            public void incrementSocialEquityUser_CQ(
328                            long groupId, long userId, SocialEquityValue socialEquityValue)
329                    throws PortalException, SystemException {
330    
331                    User user = userLocalService.getUser(userId);
332    
333                    int count = socialEquityUserPersistence.countByG_U(groupId, userId);
334    
335                    if (count == 0) {
336                            addSocialEquityUser(groupId, user);
337                    }
338    
339                    user.updateSocialContributionEquity(socialEquityValue.getValue());
340    
341                    String sql = CustomSQLUtil.get(_UPDATE_SOCIAL_EQUITY_USER_CQ);
342    
343                    sql = StringUtil.replace(
344                            sql,
345                            new String[] {
346                                    "[$CONTRIBUTION_B$]",
347                                    "[$CONTRIBUTION_K$]",
348                                    "[$GROUP_ID$]",
349                                    "[$USER_ID$]"
350                            },
351                            new String[] {
352                                    String.valueOf(socialEquityValue.getB()),
353                                    String.valueOf(socialEquityValue.getK()),
354                                    String.valueOf(groupId),
355                                    String.valueOf(userId)
356                            });
357    
358                    runSQL(sql);
359            }
360    
361            @BufferedIncrement(incrementClass = SocialEquityIncrement.class)
362            public void incrementSocialEquityUser_PQ(
363                            long groupId, long userId, SocialEquityValue socialEquityValue)
364                    throws PortalException, SystemException {
365    
366                    User user = userLocalService.getUser(userId);
367    
368                    int count = socialEquityUserPersistence.countByG_U(groupId, userId);
369    
370                    if (count == 0) {
371                            addSocialEquityUser(groupId, user);
372                    }
373    
374                    user.updateSocialParticipationEquity(socialEquityValue.getValue());
375    
376                    String sql = CustomSQLUtil.get(_UPDATE_SOCIAL_EQUITY_USER_PQ);
377    
378                    sql = StringUtil.replace(
379                            sql,
380                            new String[] {
381                                    "[$GROUP_ID$]",
382                                    "[$PARTICIPATION_B$]",
383                                    "[$PARTICIPATION_K$]",
384                                    "[$USER_ID$]"
385                            },
386                            new String[] {
387                                    String.valueOf(groupId),
388                                    String.valueOf(socialEquityValue.getB()),
389                                    String.valueOf(socialEquityValue.getK()),
390                                    String.valueOf(userId)
391                            });
392    
393                    runSQL(sql);
394            }
395    
396            public void updateRanks() {
397                    DataSource dataSource = socialEquityLogPersistence.getDataSource();
398    
399                    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
400    
401                    UpdateRanksHandler updateRanksHandler = new UpdateRanksHandler(
402                            jdbcTemplate);
403    
404                    String sql = CustomSQLUtil.get(_FIND_SOCIAL_EQUITY_USER);
405    
406                    sql = StringUtil.replace(
407                            sql, "[$ACTION_DATE$]", String.valueOf(getEquityDate()));
408    
409                    jdbcTemplate.query(sql, updateRanksHandler);
410    
411                    updateRanksHandler.flush();
412            }
413    
414            public void updateRanks(long groupId) {
415                    DataSource dataSource = socialEquityLogPersistence.getDataSource();
416    
417                    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
418    
419                    UpdateRanksHandler updateRanksHandler = new UpdateRanksHandler(
420                            jdbcTemplate);
421    
422                    String sql = CustomSQLUtil.get(_FIND_SOCIAL_EQUITY_USER_BY_GROUP);
423    
424                    sql = StringUtil.replace(
425                            sql,
426                            new String[] {
427                                    "[$ACTION_DATE$]",
428                                    "[$GROUP_ID$]"
429                            },
430                            new String[] {
431                                    String.valueOf(getEquityDate()),
432                                    String.valueOf(groupId)
433                            });
434    
435                    jdbcTemplate.query(sql, updateRanksHandler);
436    
437                    updateRanksHandler.flush();
438            }
439    
440            protected void addEquityLog(
441                            User user, AssetEntry assetEntry, User assetEntryUser,
442                            SocialEquitySetting equitySetting)
443                    throws PortalException, SystemException {
444    
445                    if (!isAddEquityLog(
446                                    user.getUserId(), assetEntry.getEntryId(), equitySetting)) {
447    
448                            return;
449                    }
450    
451                    int actionDate = getEquityDate();
452    
453                    double k = calculateK(
454                            equitySetting.getValue(), equitySetting.getLifespan());
455                    double b = calculateB(
456                            actionDate, equitySetting.getValue(), equitySetting.getLifespan());
457    
458                    SocialEquityValue socialEquity = new SocialEquityValue(k, b);
459    
460                    if (equitySetting.getType() ==
461                                    SocialEquitySettingConstants.TYPE_INFORMATION) {
462    
463                            socialEquityLogLocalService.incrementSocialEquityAssetEntry_IQ(
464                                    assetEntry.getEntryId(), socialEquity);
465    
466                            if ((assetEntryUser != null) && !assetEntryUser.isDefaultUser()) {
467                                    socialEquityLogLocalService.incrementSocialEquityUser_CQ(
468                                            assetEntry.getGroupId(), assetEntryUser.getUserId(),
469                                            socialEquity);
470                            }
471                    }
472                    else if (equitySetting.getType() ==
473                                            SocialEquitySettingConstants.TYPE_PARTICIPATION) {
474    
475                            if (!user.isDefaultUser()) {
476                                    socialEquityLogLocalService.incrementSocialEquityUser_PQ(
477                                            assetEntry.getGroupId(), user.getUserId(), socialEquity);
478                            }
479                    }
480    
481                    long equityLogId = counterLocalService.increment();
482    
483                    SocialEquityLog equityLog = socialEquityLogPersistence.create(
484                            equityLogId);
485    
486                    equityLog.setGroupId(assetEntry.getGroupId());
487                    equityLog.setCompanyId(user.getCompanyId());
488                    equityLog.setUserId(user.getUserId());
489                    equityLog.setAssetEntryId(assetEntry.getEntryId());
490                    equityLog.setActionId(equitySetting.getActionId());
491                    equityLog.setActionDate(actionDate);
492                    equityLog.setType(equitySetting.getType());
493                    equityLog.setValue(equitySetting.getValue());
494                    equityLog.setExpiration(actionDate + equitySetting.getLifespan());
495                    equityLog.setActive(true);
496    
497                    socialEquityLogPersistence.update(equityLog, false);
498            }
499    
500            protected void addSocialEquityAssetEntry(AssetEntry assetEntry)
501                    throws SystemException {
502    
503                    String sql = CustomSQLUtil.get(_ADD_SOCIAL_EQUITY_ASSET_ENTRY);
504    
505                    sql = StringUtil.replace(
506                            sql,
507                            new String[] {
508                                    "[$ASSET_ENTRY_ID$]",
509                                    "[$COMPANY_ID$]",
510                                    "[$EQUITY_ASSET_ENTRY_ID$]",
511                                    "[$GROUP_ID$]",
512                                    "[$USER_ID$]"
513                            },
514                            new String[] {
515                                    String.valueOf(assetEntry.getEntryId()),
516                                    String.valueOf(assetEntry.getCompanyId()),
517                                    String.valueOf(counterLocalService.increment()),
518                                    String.valueOf(assetEntry.getGroupId()),
519                                    String.valueOf(assetEntry.getUserId())
520                            });
521    
522                    runSQL(sql);
523            }
524    
525            protected void addSocialEquityUser(long groupId, User user)
526                    throws SystemException {
527    
528                    String sql = CustomSQLUtil.get(_ADD_SOCIAL_EQUITY_USER);
529    
530                    sql = StringUtil.replace(
531                            sql,
532                            new String[] {
533                                    "[$COMPANY_ID$]",
534                                    "[$EQUITY_USER_ID$]",
535                                    "[$GROUP_ID$]",
536                                    "[$USER_ID$]"
537                            },
538                            new String[] {
539                                    String.valueOf(user.getCompanyId()),
540                                    String.valueOf(counterLocalService.increment()),
541                                    String.valueOf(groupId),
542                                    String.valueOf(user.getUserId())
543                            });
544    
545                    runSQL(sql);
546            }
547    
548            protected double calculateB(int actionDate, int value, int lifespan) {
549                    return calculateK(value, lifespan) * (actionDate + lifespan) * -1;
550            }
551    
552            protected double calculateEquity(int actionDate, double k, double b) {
553                    return k * actionDate + b;
554            }
555    
556            protected double calculateK(int value, int lifespan) {
557                    if (lifespan == 0) {
558                            return 0;
559                    }
560    
561                    return ((double)value / lifespan) * -1;
562            }
563    
564            protected int getEquityDate() {
565                    return getEquityDate(new Date());
566            }
567    
568            protected int getEquityDate(Date date) {
569                    Calendar calendar = new GregorianCalendar(2010, Calendar.JANUARY, 1);
570    
571                    return calendar.fieldDifference(date, Calendar.DATE);
572            }
573    
574            protected boolean isAddEquityLog(
575                            long userId, long assetEntryId, SocialEquitySetting equitySetting)
576                    throws SystemException {
577    
578                    if (equitySetting.getDailyLimit() < 0) {
579                            return false;
580                    }
581    
582                    String actionId = equitySetting.getActionId();
583                    int actionDate = getEquityDate();
584                    int type = equitySetting.getType();
585    
586                    // Duplicate
587    
588                    if (socialEquityLogPersistence.countByU_AEI_AID_AD_A_T(
589                                    userId, assetEntryId, actionId, actionDate, true, type) > 0) {
590    
591                            return false;
592                    }
593    
594                    // Unique
595    
596                    if (equitySetting.isUniqueEntry()) {
597                            int count = 0;
598    
599                            if (type == SocialEquitySettingConstants.TYPE_INFORMATION) {
600                                    count = socialEquityLogPersistence.countByAEI_AID_A_T(
601                                            assetEntryId, actionId, true, type);
602                            }
603                            else {
604                                    count = socialEquityLogPersistence.countByU_AID_A_T(
605                                            userId, actionId, true, type);
606                            }
607    
608                            if (count > 0) {
609                                    return false;
610                            }
611                    }
612    
613                    // Daily limit
614    
615                    if (equitySetting.getDailyLimit() == 0) {
616                            return true;
617                    }
618    
619                    int count = 0;
620    
621                    if (type == SocialEquitySettingConstants.TYPE_INFORMATION) {
622                            count = socialEquityLogPersistence.countByAEI_AID_AD_A_T(
623                                    assetEntryId, actionId, actionDate, true, type);
624                    }
625                    else {
626                            count = socialEquityLogPersistence.countByU_AID_AD_A_T(
627                                    userId, actionId, actionDate, true, type);
628                    }
629    
630                    if (count < equitySetting.getDailyLimit()) {
631                            return true;
632                    }
633                    else {
634                            return false;
635                    }
636            }
637    
638            protected boolean isSocialEquityEnabled(
639                            long groupId, String className, int type)
640                    throws SystemException {
641    
642                    if (!socialEquityGroupSettingLocalService.isEnabled(
643                                    groupId, Group.class.getName(), type)) {
644    
645                            return false;
646                    }
647    
648                    return socialEquityGroupSettingLocalService.isEnabled(
649                            groupId, className, type);
650            }
651    
652            protected void runCheckSQL(String sqlId, int validity)
653                    throws SystemException {
654    
655                    String sql = CustomSQLUtil.get(sqlId);
656    
657                    sql = StringUtil.replace(
658                            sql,
659                            new String[] {
660                                    "[$TYPE_INFORMATION$]",
661                                    "[$TYPE_PARTICIPATION$]",
662                                    "[$EXPIRATION$]"
663                            },
664                            new String[] {
665                                    String.valueOf(SocialEquitySettingConstants.TYPE_INFORMATION),
666                                    String.valueOf(SocialEquitySettingConstants.TYPE_PARTICIPATION),
667                                    String.valueOf(validity)
668                            });
669    
670                    runSQL(sql);
671            }
672    
673            private static final String _ADD_SOCIAL_EQUITY_ASSET_ENTRY =
674                    SocialEquityLogLocalServiceImpl.class.getName() +
675                            ".addSocialEquityAssetEntry";
676    
677            private static final String _ADD_SOCIAL_EQUITY_USER =
678                    SocialEquityLogLocalServiceImpl.class.getName() +
679                            ".addSocialEquityUser";
680    
681            private static final String _CHECK_SOCIAL_EQUITY_ASSET_ENTRY_IQ =
682                    SocialEquityLogLocalServiceImpl.class.getName() +
683                            ".checkSocialEquityAssetEntry_IQ";
684    
685            private static final String _CHECK_SOCIAL_EQUITY_LOGS =
686                    SocialEquityLogLocalServiceImpl.class.getName() +
687                            ".checkSocialEquityLogs";
688    
689            private static final String _CHECK_SOCIAL_EQUITY_USER =
690                    SocialEquityLogLocalServiceImpl.class.getName() +
691                            ".checkSocialEquityUser";
692    
693            private static final String _CHECK_SOCIAL_EQUITY_USER_CQ =
694                    SocialEquityLogLocalServiceImpl.class.getName() +
695                            ".checkSocialEquityUser_CQ";
696    
697            private static final String _CHECK_SOCIAL_EQUITY_USER_PQ =
698                    SocialEquityLogLocalServiceImpl.class.getName() +
699                            ".checkSocialEquityUser_PQ";
700    
701            private static final String _FIND_SOCIAL_EQUITY_USER =
702                    SocialEquityLogLocalServiceImpl.class.getName() +
703                            ".findSocialEquityUser";
704    
705            private static final String _FIND_SOCIAL_EQUITY_USER_BY_GROUP =
706                    SocialEquityLogLocalServiceImpl.class.getName() +
707                            ".findSocialEquityUserByGroup";
708    
709            private static final String _UPDATE_SOCIAL_EQUITY_ASSET_ENTRY_IQ =
710                    SocialEquityLogLocalServiceImpl.class.getName() +
711                            ".updateSocialEquityAssetEntry_IQ";
712    
713            private static final String _UPDATE_SOCIAL_EQUITY_USER_CQ =
714                    SocialEquityLogLocalServiceImpl.class.getName() +
715                            ".updateSocialEquityUser_CQ";
716    
717            private static final String _UPDATE_SOCIAL_EQUITY_USER_PQ =
718                    SocialEquityLogLocalServiceImpl.class.getName() +
719                            ".updateSocialEquityUser_PQ";
720    
721            private static final String _UPDATE_SOCIAL_EQUITY_USER_RANK =
722                    SocialEquityLogLocalServiceImpl.class.getName() +
723                            ".updateSocialEquityUserRank";
724    
725            private class UpdateRanksHandler implements RowCallbackHandler {
726    
727                    public UpdateRanksHandler(JdbcTemplate jdbcTemplate) {
728                            _updateRanksSetter = new UpdateRanksSetter(jdbcTemplate);
729                    }
730    
731                    public void flush() {
732                            _updateRanksSetter.flush();
733                    }
734    
735                    public void processRow(ResultSet rs) throws SQLException {
736                            long equityUserId = rs.getLong("equityUserId");
737                            long groupId = rs.getLong("groupId");
738                            double equityValue = rs.getDouble("equityValue");
739    
740                            if (groupId == _groupId) {
741                                    if (equityValue == _equityValue) {
742                                            _ties++;
743                                    }
744                                    else {
745                                            _equityValue = equityValue;
746                                            _rank = _rank + _ties + 1;
747                                            _ties = 0;
748                                    }
749                            }
750                            else {
751                                    _groupId = groupId;
752                                    _rank = 1;
753                                    _ties = 0;
754                            }
755    
756                            _updateRanksSetter.add(equityUserId, _rank);
757                    }
758    
759                    private double _equityValue;
760                    private long _groupId;
761                    private long _rank;
762                    private long _ties;
763                    private UpdateRanksSetter _updateRanksSetter;
764    
765            }
766    
767            private class UpdateRanksSetter implements BatchPreparedStatementSetter {
768    
769                    public UpdateRanksSetter(JdbcTemplate jdbcTemplate) {
770                            _jdbcTemplate = jdbcTemplate;
771                    }
772    
773                    public void add(long equityUserId, long rank) {
774                            _sqlParams.add(new Long[] {equityUserId, rank});
775    
776                            if (_sqlParams.size() >= 100) {
777                                    flush();
778                            }
779                    }
780    
781                    public int getBatchSize() {
782                            return _sqlParams.size();
783                    }
784    
785                    public void flush() {
786                            try {
787                                    _jdbcTemplate.batchUpdate(_sql, this);
788                            }
789                            catch (DataAccessException dae) {
790                                    throw dae;
791                            }
792                            finally {
793                                    _sqlParams.clear();
794                            }
795                    }
796    
797                    public void setValues(PreparedStatement ps, int index)
798                            throws SQLException {
799    
800                            Long[] sqlParams = _sqlParams.get(index);
801    
802                            long equityUserId = sqlParams[0];
803                            long rank = sqlParams[1];
804    
805                            ps.setLong(1, rank);
806                            ps.setLong(2, equityUserId);
807                            ps.setLong(3, rank);
808                    }
809    
810                    private JdbcTemplate _jdbcTemplate;
811                    private String _sql = CustomSQLUtil.get(
812                            _UPDATE_SOCIAL_EQUITY_USER_RANK);
813                    private List<Long[]> _sqlParams = new ArrayList<Long[]>();
814    
815            }
816    
817    }