13
13
import java .math .BigDecimal ;
14
14
import java .math .RoundingMode ;
15
15
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 .*;
20
17
21
18
import static com .google .common .collect .Lists .newArrayList ;
22
19
import static com .topcoder .onlinereview .component .util .CommonUtils .executeSqlWithParam ;
@@ -294,7 +291,7 @@ public class DefaultProjectPaymentCalculator implements ProjectPaymentCalculator
294
291
* </p>
295
292
*/
296
293
public static final String DEFAULT_CONFIG_NAMESPACE =
297
- "com.topcoder.management.payment.calculator.impl.DefaultProjectPaymentCalculator" ;
294
+ "com.topcoder.management.payment.calculator.impl.DefaultProjectPaymentCalculator" ;
298
295
299
296
/**
300
297
* <p>
@@ -466,8 +463,18 @@ public class DefaultProjectPaymentCalculator implements ProjectPaymentCalculator
466
463
* </p>
467
464
*/
468
465
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 + "'" ;
471
478
472
479
/**
473
480
* <p>
@@ -476,30 +483,50 @@ public class DefaultProjectPaymentCalculator implements ProjectPaymentCalculator
476
483
* </p>
477
484
*/
478
485
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" ;
498
505
499
506
@ Autowired
500
507
@ Qualifier ("tcsJdbcTemplate" )
501
508
private JdbcTemplate jdbcTemplate ;
502
509
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
+
503
530
/**
504
531
* <p>
505
532
* This method is a concrete implementation of the namesake method defined in the interface.
@@ -539,33 +566,29 @@ public class DefaultProjectPaymentCalculator implements ProjectPaymentCalculator
539
566
* If any error occurred during the operation.
540
567
*/
541
568
public Map <Long , BigDecimal > getDefaultPayments (long projectId , List <Long > resourceRoleIDs )
542
- throws ProjectPaymentCalculatorException {
569
+ throws ProjectPaymentCalculatorException {
543
570
String signature = DEFAULT_CONFIG_NAMESPACE + "#getDefaultPayments(long, List<Long>)" ;
544
571
Helper .logEntrance (log , signature , new String [] {"projectId" , "resourceRoleIDs" },
545
- new Object [] {projectId , resourceRoleIDs });
572
+ new Object [] {projectId , resourceRoleIDs });
546
573
547
574
// 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" );
555
578
556
579
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 ));
560
584
Map <Long , BigDecimal > defaultPaymentsMap = new HashMap <Long , BigDecimal >();
561
-
562
585
// Iterate through the supported resource roles IDs and compute the default payment for each one of the
563
586
// requested resource role
564
587
for (Map <String , Object > row : resultSet ) {
565
588
long roleId = getLong (row , RESOURCE_ROLE_ID_COLUMN );
566
- if (resourceRoleIDs .contains (roleId )) {
589
+ if (resourceRoleIDs .contains (roleId ) && !( skipPayments && reviewerRoles . contains ( roleId )) ) {
567
590
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 );
569
592
float baseCoefficient = ofNullable (getFloat (row , BASE_COEFFICIENT_COLUMN )).orElse (0F );
570
593
float incrementalCoefficient = ofNullable (getFloat (row , INCREMENTAL_COEFFICIENT_COLUMN )).orElse (0F );
571
594
float prize = ofNullable (getFloat (row , PRIZE_COLUMN )).orElse (0F );
@@ -575,22 +598,42 @@ public Map<Long, BigDecimal> getDefaultPayments(long projectId, List<Long> resou
575
598
576
599
// calculate the payment
577
600
BigDecimal augend =
578
- BigDecimal .valueOf ((baseCoefficient + incrementalCoefficient * submissionsCount ) * prize );
579
-
601
+ BigDecimal .valueOf ((baseCoefficient + incrementalCoefficient * submissionsCount ) * prize );
580
602
BigDecimal payment = fixedAmount .add (augend .setScale (2 , RoundingMode .HALF_UP ));
581
603
582
604
// put into the map
583
605
defaultPaymentsMap .put (roleId , payment );
584
606
}
585
607
}
586
-
587
608
Helper .logExit (log , signature , new Object [] {defaultPaymentsMap });
588
-
589
609
return defaultPaymentsMap ;
590
610
} catch (SQLException e ) {
591
611
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;
593
635
}
636
+ return false ;
594
637
}
595
638
596
639
/**
@@ -633,11 +676,11 @@ public Map<Long, BigDecimal> getDefaultPayments(long projectId, List<Long> resou
633
676
* If any error occurred during the operation.
634
677
*/
635
678
public BigDecimal getDefaultPayment (long projectCategoryId , long resourceRoleId , BigDecimal prize ,
636
- int submissionsCount ) throws ProjectPaymentCalculatorException {
679
+ int submissionsCount ) throws ProjectPaymentCalculatorException {
637
680
String signature = DEFAULT_CONFIG_NAMESPACE + "#getDefaultPayment(long, long, BigDecimal, int)" ;
638
681
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 });
641
684
642
685
// arguments checking
643
686
try {
@@ -668,9 +711,9 @@ public BigDecimal getDefaultPayment(long projectCategoryId, long resourceRoleId,
668
711
669
712
BigDecimal scaledPrize = prize .setScale (2 , RoundingMode .HALF_UP );
670
713
BigDecimal multiplicandCoefficient =
671
- BigDecimal .valueOf (baseCoefficient + incrementalCoefficient * count );
714
+ BigDecimal .valueOf (baseCoefficient + incrementalCoefficient * count );
672
715
payment =
673
- fixedAmount .add (scaledPrize .multiply (multiplicandCoefficient )).setScale (2 , RoundingMode .HALF_UP );
716
+ fixedAmount .add (scaledPrize .multiply (multiplicandCoefficient )).setScale (2 , RoundingMode .HALF_UP );
674
717
}
675
718
Helper .logExit (log , signature , new Object [] {payment });
676
719
@@ -701,8 +744,8 @@ private static int getSubmissionsCount(Map<String, Object> resultSet, long resou
701
744
}
702
745
703
746
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 ) {
706
749
submissionsCount = getInt (resultSet , "passed_contest_submissions" );
707
750
}
708
751
@@ -740,12 +783,12 @@ private static int getSubmissionsCount(Map<String, Object> resultSet, long resou
740
783
*/
741
784
private static boolean isSubmissionRequired (long resourceRoleId ) {
742
785
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 );
749
792
}
750
793
751
794
0 commit comments