Skip to content

Commit 1ba8c4a

Browse files
authored
Merge pull request #63 from topcoder-platform/prod2172_try2
uploading non-project changes to new branch
2 parents 4b7d088 + da83144 commit 1ba8c4a

File tree

1 file changed

+102
-59
lines changed

1 file changed

+102
-59
lines changed

src/main/java/com/topcoder/onlinereview/component/project/payment/calculator/DefaultProjectPaymentCalculator.java

Lines changed: 102 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
import java.math.BigDecimal;
1414
import java.math.RoundingMode;
1515
import java.sql.SQLException;
16-
import java.util.HashMap;
17-
import java.util.List;
18-
import java.util.Map;
19-
import java.util.Optional;
16+
import java.util.*;
2017

2118
import static com.google.common.collect.Lists.newArrayList;
2219
import static com.topcoder.onlinereview.component.util.CommonUtils.executeSqlWithParam;
@@ -294,7 +291,7 @@ public class DefaultProjectPaymentCalculator implements ProjectPaymentCalculator
294291
* </p>
295292
*/
296293
public static final String DEFAULT_CONFIG_NAMESPACE =
297-
"com.topcoder.management.payment.calculator.impl.DefaultProjectPaymentCalculator";
294+
"com.topcoder.management.payment.calculator.impl.DefaultProjectPaymentCalculator";
298295

299296
/**
300297
* <p>
@@ -466,8 +463,18 @@ public class DefaultProjectPaymentCalculator implements ProjectPaymentCalculator
466463
* </p>
467464
*/
468465
private static final String GET_DEFAULT_PAYMENT_QUERY =
469-
"SELECT fixed_amount, base_coefficient, incremental_coefficient " + "FROM default_project_payment "
470-
+ "WHERE project_category_id = ? and resource_role_id = ?";
466+
"SELECT fixed_amount, base_coefficient, incremental_coefficient " + "FROM default_project_payment "
467+
+ "WHERE project_category_id = ? and resource_role_id = ?";
468+
469+
/**
470+
* <p>
471+
* The SQL query to retrieve metadata for a challenge to determine if it should skip payments calc
472+
* </p>
473+
*/
474+
private static final String SKIP_PAYMENTS_FIELD = "skip_payments";
475+
private static final String SKIP_PAYMENTS_METADATA_TYPE_ID = "skip_OR_payment_calcs"; // shared with challenge-api
476+
private static final String GET_METADATA_QUERY =
477+
"SELECT pi.value " + SKIP_PAYMENTS_FIELD + " FROM project_info pi WHERE pi.project_id=? AND pi.project_info_type_id='" + SKIP_PAYMENTS_METADATA_TYPE_ID + "'";
471478

472479
/**
473480
* <p>
@@ -476,30 +483,50 @@ public class DefaultProjectPaymentCalculator implements ProjectPaymentCalculator
476483
* </p>
477484
*/
478485
private static final String GET_DEFAULT_PAYMENTS_QUERY =
479-
"SELECT dpp.resource_role_id, dpp.fixed_amount, dpp.base_coefficient, dpp.incremental_coefficient,"
480-
+ "max(pr.prize_amount) as prize,"
481-
+ "sum(case when s.submission_type_id = 1 then 1 else 0 end) as total_contest_submissions,"
482-
+ "sum(case when s.submission_type_id = 1 and s.submission_status_id != 2 then 1 else 0 end) "
483-
+ "as passed_contest_submissions,"
484-
+ "sum(case when s.submission_type_id = 3 then 1 else 0 end) as total_checkpoint_submissions,"
485-
+ "sum(case when s.submission_type_id = 3 and s.submission_status_id != 6 then 1 else 0 end) "
486-
+ "as passed_checkpoint_submissions, "
487-
+ "sum(case when s.submission_type_id = 1 and exists (select 1 from review r "
488-
+ "where r.submission_id = s.submission_id and r.committed = 1) then 1 else 0 end) "
489-
+ "as total_reviewed_contest_submissions "
490-
+ "FROM default_project_payment dpp "
491-
+ "INNER JOIN project p ON dpp.project_category_id = p.project_category_id and p.project_id=? "
492-
+ "LEFT OUTER JOIN prize pr ON pr.project_id=p.project_id and pr.prize_type_id=15 and pr.place=1 "
493-
+ "LEFT OUTER JOIN upload u ON u.project_id = p.project_id and u.upload_type_id = 1 "
494-
+ "LEFT OUTER JOIN submission s ON s.submission_type_id in (1,3) and s.upload_id = u.upload_id "
495-
+ "and s.submission_status_id in (1,2,3,4,6,7) "
496-
+ "WHERE dpp.resource_role_id in (2,4,5,6,7,8,9,14,18,19,20,21) "
497-
+ "GROUP BY dpp.resource_role_id, dpp.fixed_amount, dpp.base_coefficient, dpp.incremental_coefficient";
486+
"SELECT dpp.resource_role_id, dpp.fixed_amount, dpp.base_coefficient, dpp.incremental_coefficient,"
487+
+ "max(pr.prize_amount) as prize,"
488+
+ "sum(case when s.submission_type_id = 1 then 1 else 0 end) as total_contest_submissions,"
489+
+ "sum(case when s.submission_type_id = 1 and s.submission_status_id != 2 then 1 else 0 end) "
490+
+ "as passed_contest_submissions,"
491+
+ "sum(case when s.submission_type_id = 3 then 1 else 0 end) as total_checkpoint_submissions,"
492+
+ "sum(case when s.submission_type_id = 3 and s.submission_status_id != 6 then 1 else 0 end) "
493+
+ "as passed_checkpoint_submissions, "
494+
+ "sum(case when s.submission_type_id = 1 and exists (select 1 from review r "
495+
+ "where r.submission_id = s.submission_id and r.committed = 1) then 1 else 0 end) "
496+
+ "as total_reviewed_contest_submissions "
497+
+ "FROM default_project_payment dpp "
498+
+ "INNER JOIN project p ON dpp.project_category_id = p.project_category_id and p.project_id=? "
499+
+ "LEFT OUTER JOIN prize pr ON pr.project_id=p.project_id and pr.prize_type_id=15 and pr.place=1 "
500+
+ "LEFT OUTER JOIN upload u ON u.project_id = p.project_id and u.upload_type_id = 1 "
501+
+ "LEFT OUTER JOIN submission s ON s.submission_type_id in (1,3) and s.upload_id = u.upload_id "
502+
+ "and s.submission_status_id in (1,2,3,4,6,7) "
503+
+ "WHERE dpp.resource_role_id in (2,4,5,6,7,8,9,14,18,19,20,21) "
504+
+ "GROUP BY dpp.resource_role_id, dpp.fixed_amount, dpp.base_coefficient, dpp.incremental_coefficient";
498505

499506
@Autowired
500507
@Qualifier("tcsJdbcTemplate")
501508
private JdbcTemplate jdbcTemplate;
502509

510+
/**
511+
* This contains a list of roles for which payment calculation should be skipped
512+
* when the skip payment flag is set by the self-service app.
513+
*/
514+
private List<Long> reviewerRoles;
515+
516+
public DefaultProjectPaymentCalculator() {
517+
super();
518+
// create list of reviewer roles
519+
reviewerRoles = new ArrayList<Long>();
520+
reviewerRoles.add(REVIEWER_RESOURCE_ROLE_ID);
521+
reviewerRoles.add(ACCURACY_REVIEWER_RESOURCE_ROLE_ID);
522+
reviewerRoles.add(FAILURE_REVIEWER_RESOURCE_ROLE_ID);
523+
reviewerRoles.add(STRESS_REVIEWER_RESOURCE_ROLE_ID);
524+
reviewerRoles.add(FINAL_REVIEWER_RESOURCE_ROLE_ID);
525+
reviewerRoles.add(SPECIFICATION_REVIEWER_RESOURCE_ROLE_ID);
526+
reviewerRoles.add(CHECKPOINT_REVIEWER_RESOURCE_ROLE_ID);
527+
reviewerRoles.add(ITERATIVE_REVIEWER_RESOURCE_ROLE_ID);
528+
}
529+
503530
/**
504531
* <p>
505532
* This method is a concrete implementation of the namesake method defined in the interface.
@@ -539,33 +566,29 @@ public class DefaultProjectPaymentCalculator implements ProjectPaymentCalculator
539566
* If any error occurred during the operation.
540567
*/
541568
public Map<Long, BigDecimal> getDefaultPayments(long projectId, List<Long> resourceRoleIDs)
542-
throws ProjectPaymentCalculatorException {
569+
throws ProjectPaymentCalculatorException {
543570
String signature = DEFAULT_CONFIG_NAMESPACE + "#getDefaultPayments(long, List<Long>)";
544571
Helper.logEntrance(log, signature, new String[] {"projectId", "resourceRoleIDs"},
545-
new Object[] {projectId, resourceRoleIDs});
572+
new Object[] {projectId, resourceRoleIDs});
546573

547574
// arguments checking
548-
try {
549-
Helper.checkPositive(projectId, "projectId");
550-
Helper.checkNotNullNorEmpty(resourceRoleIDs, "resourceRoleIDs");
551-
Helper.checkNotNullElements(resourceRoleIDs, "resourceRoleIDs");
552-
} catch (IllegalArgumentException e) {
553-
throw Helper.logException(log, signature, e);
554-
}
575+
Helper.checkPositive(projectId, "projectId");
576+
Helper.checkNotNullNorEmpty(resourceRoleIDs, "resourceRoleIDs");
577+
Helper.checkNotNullElements(resourceRoleIDs, "resourceRoleIDs");
555578

556579
try {
557-
// Execute the query and get the result.
558-
List<Map<String, Object>> resultSet = executeSqlWithParam(jdbcTemplate, GET_DEFAULT_PAYMENTS_QUERY, newArrayList(projectId));
559-
580+
// get skip payments flag for project
581+
boolean skipPayments = isSkipPaymentsFlagPresentForProject(projectId);
582+
// Execute the query and get the result.
583+
List<Map<String, Object>> resultSet = executeSqlWithParam(jdbcTemplate, GET_DEFAULT_PAYMENTS_QUERY, newArrayList(projectId));
560584
Map<Long, BigDecimal> defaultPaymentsMap = new HashMap<Long, BigDecimal>();
561-
562585
// Iterate through the supported resource roles IDs and compute the default payment for each one of the
563586
// requested resource role
564587
for (Map<String, Object> row: resultSet) {
565588
long roleId = getLong(row, RESOURCE_ROLE_ID_COLUMN);
566-
if (resourceRoleIDs.contains(roleId)) {
589+
if (resourceRoleIDs.contains(roleId) && !(skipPayments && reviewerRoles.contains(roleId))) {
567590
BigDecimal fixedAmount =
568-
new BigDecimal(getDouble(row, FIXED_AMOUNT_COLUMN)).setScale(2, RoundingMode.HALF_UP);
591+
new BigDecimal(getDouble(row, FIXED_AMOUNT_COLUMN)).setScale(2, RoundingMode.HALF_UP);
569592
float baseCoefficient = ofNullable(getFloat(row, BASE_COEFFICIENT_COLUMN)).orElse(0F);
570593
float incrementalCoefficient = ofNullable(getFloat(row, INCREMENTAL_COEFFICIENT_COLUMN)).orElse(0F);
571594
float prize = ofNullable(getFloat(row, PRIZE_COLUMN)).orElse(0F);
@@ -575,22 +598,42 @@ public Map<Long, BigDecimal> getDefaultPayments(long projectId, List<Long> resou
575598

576599
// calculate the payment
577600
BigDecimal augend =
578-
BigDecimal.valueOf((baseCoefficient + incrementalCoefficient * submissionsCount) * prize);
579-
601+
BigDecimal.valueOf((baseCoefficient + incrementalCoefficient * submissionsCount) * prize);
580602
BigDecimal payment = fixedAmount.add(augend.setScale(2, RoundingMode.HALF_UP));
581603

582604
// put into the map
583605
defaultPaymentsMap.put(roleId, payment);
584606
}
585607
}
586-
587608
Helper.logExit(log, signature, new Object[] {defaultPaymentsMap});
588-
589609
return defaultPaymentsMap;
590610
} catch (SQLException e) {
591611
throw Helper.logException(log, signature, new ProjectPaymentCalculatorException(
592-
"Fails to query project payments from database", e));
612+
"Fails to query project payments from database", e));
613+
}
614+
}
615+
616+
/**
617+
* Queries the project attributes table for the payment skipped flag (challenge metadata in v5)
618+
*
619+
* @param projectId
620+
* @return true if payments are skipped, false, otherwise
621+
*/
622+
private boolean isSkipPaymentsFlagPresentForProject(long projectId) {
623+
List<Map<String, Object>> resultSet = executeSqlWithParam(jdbcTemplate, GET_METADATA_QUERY, newArrayList(projectId));
624+
if (resultSet == null || resultSet.size() == 0) { return false; }
625+
String skipPayments = resultSet.get(0).get(SKIP_PAYMENTS_FIELD).toString();
626+
if (skipPayments == null) { return false; }
627+
// assumed format
628+
if ("true".equalsIgnoreCase(skipPayments.trim())) { return true; }
629+
// maybe not in assumed format, try to parseBool
630+
try {
631+
Boolean b = Boolean.parseBoolean(skipPayments);
632+
return b;
633+
} catch (Throwable t) {
634+
// don't care, return false;
593635
}
636+
return false;
594637
}
595638

596639
/**
@@ -633,11 +676,11 @@ public Map<Long, BigDecimal> getDefaultPayments(long projectId, List<Long> resou
633676
* If any error occurred during the operation.
634677
*/
635678
public BigDecimal getDefaultPayment(long projectCategoryId, long resourceRoleId, BigDecimal prize,
636-
int submissionsCount) throws ProjectPaymentCalculatorException {
679+
int submissionsCount) throws ProjectPaymentCalculatorException {
637680
String signature = DEFAULT_CONFIG_NAMESPACE + "#getDefaultPayment(long, long, BigDecimal, int)";
638681
Helper.logEntrance(log, signature, new String[] {"projectCategoryId", "resourceRoleId",
639-
PRIZE_COLUMN, "submissionsCount"},
640-
new Object[] {projectCategoryId, resourceRoleId, prize, submissionsCount});
682+
PRIZE_COLUMN, "submissionsCount"},
683+
new Object[] {projectCategoryId, resourceRoleId, prize, submissionsCount});
641684

642685
// arguments checking
643686
try {
@@ -668,9 +711,9 @@ public BigDecimal getDefaultPayment(long projectCategoryId, long resourceRoleId,
668711

669712
BigDecimal scaledPrize = prize.setScale(2, RoundingMode.HALF_UP);
670713
BigDecimal multiplicandCoefficient =
671-
BigDecimal.valueOf(baseCoefficient + incrementalCoefficient * count);
714+
BigDecimal.valueOf(baseCoefficient + incrementalCoefficient * count);
672715
payment =
673-
fixedAmount.add(scaledPrize.multiply(multiplicandCoefficient)).setScale(2, RoundingMode.HALF_UP);
716+
fixedAmount.add(scaledPrize.multiply(multiplicandCoefficient)).setScale(2, RoundingMode.HALF_UP);
674717
}
675718
Helper.logExit(log, signature, new Object[] {payment});
676719

@@ -701,8 +744,8 @@ private static int getSubmissionsCount(Map<String, Object> resultSet, long resou
701744
}
702745

703746
if (resourceRoleId == REVIEWER_RESOURCE_ROLE_ID || resourceRoleId == ACCURACY_REVIEWER_RESOURCE_ROLE_ID
704-
|| resourceRoleId == FAILURE_REVIEWER_RESOURCE_ROLE_ID
705-
|| resourceRoleId == STRESS_REVIEWER_RESOURCE_ROLE_ID) {
747+
|| resourceRoleId == FAILURE_REVIEWER_RESOURCE_ROLE_ID
748+
|| resourceRoleId == STRESS_REVIEWER_RESOURCE_ROLE_ID) {
706749
submissionsCount = getInt(resultSet, "passed_contest_submissions");
707750
}
708751

@@ -740,12 +783,12 @@ private static int getSubmissionsCount(Map<String, Object> resultSet, long resou
740783
*/
741784
private static boolean isSubmissionRequired(long resourceRoleId) {
742785
return (resourceRoleId == PRIMARY_SCREENER_RESOURCE_ROLE_ID || resourceRoleId == REVIEWER_RESOURCE_ROLE_ID
743-
|| resourceRoleId == ACCURACY_REVIEWER_RESOURCE_ROLE_ID
744-
|| resourceRoleId == FAILURE_REVIEWER_RESOURCE_ROLE_ID
745-
|| resourceRoleId == STRESS_REVIEWER_RESOURCE_ROLE_ID
746-
|| resourceRoleId == CHECKPOINT_SCREENER_RESOURCE_ROLE_ID
747-
|| resourceRoleId == CHECKPOINT_REVIEWER_RESOURCE_ROLE_ID
748-
|| resourceRoleId == ITERATIVE_REVIEWER_RESOURCE_ROLE_ID);
786+
|| resourceRoleId == ACCURACY_REVIEWER_RESOURCE_ROLE_ID
787+
|| resourceRoleId == FAILURE_REVIEWER_RESOURCE_ROLE_ID
788+
|| resourceRoleId == STRESS_REVIEWER_RESOURCE_ROLE_ID
789+
|| resourceRoleId == CHECKPOINT_SCREENER_RESOURCE_ROLE_ID
790+
|| resourceRoleId == CHECKPOINT_REVIEWER_RESOURCE_ROLE_ID
791+
|| resourceRoleId == ITERATIVE_REVIEWER_RESOURCE_ROLE_ID);
749792
}
750793

751794

0 commit comments

Comments
 (0)