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