fineract-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nazeer1100...@apache.org
Subject [3/6] fineract git commit: Conditional ad hoc job service
Date Mon, 11 Dec 2017 10:29:26 GMT
Conditional ad hoc job service


Project: http://git-wip-us.apache.org/repos/asf/fineract/repo
Commit: http://git-wip-us.apache.org/repos/asf/fineract/commit/87e0c59f
Tree: http://git-wip-us.apache.org/repos/asf/fineract/tree/87e0c59f
Diff: http://git-wip-us.apache.org/repos/asf/fineract/diff/87e0c59f

Branch: refs/heads/develop
Commit: 87e0c59fb336e96ec7221749496b2c70cfd5a080
Parents: f47e0fb
Author: Konstantin Golub <key.offecka@runbox.com>
Authored: Thu Oct 5 11:28:18 2017 -0300
Committer: Konstantin Golub <key.offecka@runbox.com>
Committed: Thu Oct 5 11:28:18 2017 -0300

----------------------------------------------------------------------
 .../adhocquery/api/AdHocJsonInputParams.java    |  2 +-
 .../fineract/adhocquery/data/AdHocData.java     | 19 ++++--
 .../fineract/adhocquery/domain/AdHoc.java       |  4 +-
 .../adhocquery/domain/ReportRunFrequency.java   | 18 ++++-
 .../adhocquery/service/AdHocDataValidator.java  |  4 +-
 .../service/AdHocReadPlatformServiceImpl.java   |  5 +-
 .../AdHocScheduledJobRunnerServiceImpl.java     | 72 ++++++++++++++++----
 7 files changed, 96 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/fineract/blob/87e0c59f/fineract-provider/src/main/java/org/apache/fineract/adhocquery/api/AdHocJsonInputParams.java
----------------------------------------------------------------------
diff --git a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/api/AdHocJsonInputParams.java
b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/api/AdHocJsonInputParams.java
index e8c1420..a797a0f 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/api/AdHocJsonInputParams.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/api/AdHocJsonInputParams.java
@@ -25,7 +25,7 @@ import java.util.Set;
  * Enum of all parameters passed in while creating/updating a AdHocQuery 
  ***/
 public enum AdHocJsonInputParams {
-    ID("id"), NAME("name"),QUERY("query"),TABLENAME("tableName"),TABLEFIELD("tableFields"),
ISACTIVE("isActive"),
+    ID("id"), NAME("name"),QUERY("query"),TABLENAME("tableName"), TABLEFIELDS("tableFields"),
ISACTIVE("isActive"),
     REPORT_RUN_FREQUENCY("reportRunFrequency"),
     REPORT_RUN_EVERY("reportRunEvery"),
     EMAIL("email");

http://git-wip-us.apache.org/repos/asf/fineract/blob/87e0c59f/fineract-provider/src/main/java/org/apache/fineract/adhocquery/data/AdHocData.java
----------------------------------------------------------------------
diff --git a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/data/AdHocData.java
b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/data/AdHocData.java
index bd5421d..81cfc91 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/data/AdHocData.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/data/AdHocData.java
@@ -18,14 +18,14 @@
  */
 package org.apache.fineract.adhocquery.data;
 
-import java.util.Arrays;
-import java.util.List;
-import java.util.stream.Collectors;
-
 import org.apache.fineract.adhocquery.domain.ReportRunFrequency;
 import org.apache.fineract.infrastructure.core.data.EnumOptionData;
 import org.joda.time.DateTime;
 
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
 /**
  * Immutable data object represent note or case information AdHocData
  * 
@@ -65,10 +65,13 @@ public class AdHocData {
 
     private final Long reportRunEvery;
 
+    private final DateTime lastRun;
+
     public AdHocData(final Long id, final String name, final String query, final String tableName,
final String tableFields,
                      final boolean isActive, final DateTime createdOn, final Long createdById,
final Long updatedById,
                      final DateTime updatedOn, final String createdBy, final String email,
-                     final List<EnumOptionData> reportRunFrequencies, final Long reportRunFrequency,
final Long reportRunEvery) {
+                     final List<EnumOptionData> reportRunFrequencies, final Long reportRunFrequency,
final Long reportRunEvery,
+                     final DateTime lastRun) {
         this.id = id;
         this.name=name;
         this.query=query;
@@ -84,13 +87,14 @@ public class AdHocData {
 	    this.reportRunFrequencies = reportRunFrequencies;
 	    this.reportRunFrequency = reportRunFrequency;
 	    this.reportRunEvery = reportRunEvery;
+	    this.lastRun = lastRun;
     }
     public static AdHocData template() {
 	    List<EnumOptionData> reportRunFrequencies = Arrays.stream(ReportRunFrequency.values()).map(rrf
-> new EnumOptionData(
 		    (long) rrf.getValue(), rrf.getCode(), rrf.getCode()
 	    )).collect(Collectors.toList());
 
-	    AdHocData adHocData = new AdHocData(null,null,null,null,null,false,null,null,null,null,null,null,
reportRunFrequencies, null, null);
+	    AdHocData adHocData = new AdHocData(null,null,null,null,null,false,null,null,null,null,null,null,
reportRunFrequencies, null, null, null);
 		return adHocData;
     }
     public Long getId() {
@@ -138,4 +142,7 @@ public class AdHocData {
 	public Long getReportRunEvery() {
 		return this.reportRunEvery;
 	}
+	public DateTime getLastRun() {
+		return this.lastRun;
+	}
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/fineract/blob/87e0c59f/fineract-provider/src/main/java/org/apache/fineract/adhocquery/domain/AdHoc.java
----------------------------------------------------------------------
diff --git a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/domain/AdHoc.java
b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/domain/AdHoc.java
index 93fa73f..54f98f3 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/domain/AdHoc.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/domain/AdHoc.java
@@ -82,7 +82,7 @@ public class AdHoc extends AbstractAuditableCustom<AppUser, Long>
{
         SQLInjectionValidator.validateAdhocQuery(commandQuery);
         final String query = commandQuery;
         final String tableName = command.stringValueOfParameterNamed(AdHocJsonInputParams.TABLENAME.getValue());
-        final String tableFields = command.stringValueOfParameterNamed(AdHocJsonInputParams.TABLEFIELD.getValue());
+        final String tableFields = command.stringValueOfParameterNamed(AdHocJsonInputParams.TABLEFIELDS.getValue());
         final String email = command.stringValueOfParameterNamed(AdHocJsonInputParams.EMAIL.getValue());
         final Long reportRunFrequency = command.longValueOfParameterNamed(AdHocJsonInputParams.REPORT_RUN_FREQUENCY.getValue());
         final Long reportRunEvery = command.longValueOfParameterNamed(AdHocJsonInputParams.REPORT_RUN_EVERY.getValue());
@@ -113,7 +113,7 @@ public class AdHoc extends AbstractAuditableCustom<AppUser, Long>
{
             actualChanges.put(tableName, newValue);
             this.tableName = newValue;
         }
-        final String tableField = "tableField";
+        final String tableField = "tableFields";
         if (command.isChangeInStringParameterNamed(tableField, this.tableFields)) {
             final String newValue = command.stringValueOfParameterNamed(tableField);
             actualChanges.put(tableField, newValue);

http://git-wip-us.apache.org/repos/asf/fineract/blob/87e0c59f/fineract-provider/src/main/java/org/apache/fineract/adhocquery/domain/ReportRunFrequency.java
----------------------------------------------------------------------
diff --git a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/domain/ReportRunFrequency.java
b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/domain/ReportRunFrequency.java
index 78bd011..fc02d4b 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/domain/ReportRunFrequency.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/domain/ReportRunFrequency.java
@@ -18,6 +18,10 @@
  */
 package org.apache.fineract.adhocquery.domain;
 
+import java.util.Arrays;
+import java.util.Map;
+import java.util.stream.Collectors;
+
 public enum ReportRunFrequency {
     DAILY(1, "reportRunFrequency.daily"),
     WEEKLY(2, "reportRunFrequency.weekly"),
@@ -25,19 +29,27 @@ public enum ReportRunFrequency {
     YEARLY(4, "reportRunFrequency.yearly"),
     CUSTOM(5, "reportRunFrequency.custom");
 
-    private final int value;
+    private static final Map<Long, ReportRunFrequency> MAP = Arrays.stream(ReportRunFrequency.values()).collect(Collectors.toMap(
+        ReportRunFrequency::getValue, e -> e
+    ));
+
+    private final long value;
     private final String code;
 
-    private ReportRunFrequency(final int value, final String code) {
+    private ReportRunFrequency(final long value, final String code) {
         this.value = value;
         this.code = code;
     }
 
-    public int getValue() {
+    public long getValue() {
         return this.value;
     }
 
     public String getCode() {
         return this.code;
     }
+
+    public static ReportRunFrequency fromId(final long id) {
+        return ReportRunFrequency.MAP.get(id);
+    }
 }

http://git-wip-us.apache.org/repos/asf/fineract/blob/87e0c59f/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocDataValidator.java
----------------------------------------------------------------------
diff --git a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocDataValidator.java
b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocDataValidator.java
index bb4434b..9cb13e8 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocDataValidator.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocDataValidator.java
@@ -85,7 +85,7 @@ public final class AdHocDataValidator {
         final Long reportRunFrequencyCode = this.fromApiJsonHelper.extractLongNamed("reportRunFrequency",
element);
         if (reportRunFrequencyCode != null) {
             baseDataValidator.reset().parameter("reportRunFrequency").value(reportRunFrequencyCode)
-                .inMinMaxRange(ReportRunFrequency.DAILY.getValue(), ReportRunFrequency.CUSTOM.getValue());
+                .inMinMaxRange((int) ReportRunFrequency.DAILY.getValue(), (int) ReportRunFrequency.CUSTOM.getValue());
         }
 
         final Long reportRunEvery = this.fromApiJsonHelper.extractLongNamed("reportRunEvery",
element);
@@ -131,7 +131,7 @@ public final class AdHocDataValidator {
         if (this.fromApiJsonHelper.parameterExists("reportRunFrequency", element)) {
             final Long reportRunFrequencyCode = this.fromApiJsonHelper.extractLongNamed("reportRunFrequency",
element);
             baseDataValidator.reset().parameter("reportRunFrequency").value(reportRunFrequencyCode)
-                .inMinMaxRange(ReportRunFrequency.DAILY.getValue(), ReportRunFrequency.CUSTOM.getValue());
+                .inMinMaxRange((int) ReportRunFrequency.DAILY.getValue(), (int) ReportRunFrequency.CUSTOM.getValue());
         }
         if (this.fromApiJsonHelper.parameterExists("reportRunEvery", element)) {
             final Long reportRunEvery = this.fromApiJsonHelper.extractLongNamed("reportRunEvery",
element);

http://git-wip-us.apache.org/repos/asf/fineract/blob/87e0c59f/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocReadPlatformServiceImpl.java
----------------------------------------------------------------------
diff --git a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocReadPlatformServiceImpl.java
b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocReadPlatformServiceImpl.java
index 2ea6cfd..c5c7a0a 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocReadPlatformServiceImpl.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocReadPlatformServiceImpl.java
@@ -90,13 +90,14 @@ public class AdHocReadPlatformServiceImpl implements AdHocReadPlatformService
{
             final String email=rs.getString("email");
             final Long reportRunFrequency=JdbcSupport.getLong(rs, "report_run_frequency_code");
             final Long reportRunEvery=JdbcSupport.getLong(rs, "report_run_every");
+            final DateTime lastRun = JdbcSupport.getDateTime(rs, "last_run");
 
-            return new AdHocData(id,name,query, tableName,tableFields,isActive,createdDate,createdById,updatedById,updatedOn,createdByUsername,email,
AdHocData.template().getReportRunFrequencies(), reportRunFrequency, reportRunEvery);
+            return new AdHocData(id,name,query, tableName,tableFields,isActive,createdDate,createdById,updatedById,updatedOn,createdByUsername,email,
AdHocData.template().getReportRunFrequencies(), reportRunFrequency, reportRunEvery, lastRun);
         }
 
         public String schema() {
             return " r.id as id, r.name as name, r.query as query, r.table_name as tableName,r.table_fields
as tableField ,r.IsActive as isActive ,r.email as email ,"
-                    + " r.report_run_frequency_code, r.report_run_every, "
+                    + " r.report_run_frequency_code, r.report_run_every, r.last_run, "
             		+ " r.created_date as createdDate, r.createdby_id as createdById,cb.username
as createdBy,r.lastmodifiedby_id as updatedById ,r.lastmodified_date as updatedOn "
                     + " from m_adhoc r left join m_appuser cb on cb.id=r.createdby_id left
join m_appuser mb on mb.id=r.lastmodifiedby_id";
             		        

http://git-wip-us.apache.org/repos/asf/fineract/blob/87e0c59f/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocScheduledJobRunnerServiceImpl.java
----------------------------------------------------------------------
diff --git a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocScheduledJobRunnerServiceImpl.java
b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocScheduledJobRunnerServiceImpl.java
index d10cddd..c92121c 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocScheduledJobRunnerServiceImpl.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/adhocquery/service/AdHocScheduledJobRunnerServiceImpl.java
@@ -18,13 +18,17 @@
  */
 package org.apache.fineract.adhocquery.service;
 
-import java.util.Collection;
-
 import org.apache.fineract.adhocquery.data.AdHocData;
+import org.apache.fineract.adhocquery.domain.ReportRunFrequency;
 import org.apache.fineract.infrastructure.core.service.RoutingDataSource;
 import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
 import org.apache.fineract.infrastructure.jobs.annotation.CronTarget;
 import org.apache.fineract.infrastructure.jobs.service.JobName;
+import org.joda.time.DateTime;
+import org.joda.time.Days;
+import org.joda.time.LocalDate;
+import org.joda.time.Months;
+import org.joda.time.Years;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -32,6 +36,9 @@ import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.Collection;
+import java.util.Date;
+
 @Service(value = "adHocScheduledJobRunnerService")
 public class AdHocScheduledJobRunnerServiceImpl implements AdHocScheduledJobRunnerService
{
 
@@ -47,6 +54,7 @@ public class AdHocScheduledJobRunnerServiceImpl implements AdHocScheduledJobRunn
         this.adHocReadPlatformService = adHocReadPlatformService;
        
     }
+
     @Transactional
     @Override
     @CronTarget(jobName = JobName.GENERATE_ADHOCCLIENT_SCEHDULE)
@@ -54,16 +62,56 @@ public class AdHocScheduledJobRunnerServiceImpl implements AdHocScheduledJobRunn
     	final Collection<AdHocData> adhocs = this.adHocReadPlatformService.retrieveAllActiveAdHocQuery();
         if(adhocs.size()>0){
         	adhocs.forEach(adhoc->{
-            	//jdbcTemplate.execute("truncate table "+adhoc.getTableName());
-            	final StringBuilder insertSqlBuilder = new StringBuilder(900);
-                insertSqlBuilder
-                        .append("INSERT INTO ")
-                        .append(adhoc.getTableName()+"(")
-                		.append(adhoc.getTableFields()+") ")
-                		.append(adhoc.getQuery());
-                if (insertSqlBuilder.length() > 0) {
-                	final int result = this.jdbcTemplate.update(insertSqlBuilder.toString());
-                	logger.info(ThreadLocalContextUtil.getTenant().getName() + ": Results affected
by inserted: " + result);
+        	    boolean run = true;
+                LocalDate next = null;
+        	    if (adhoc.getReportRunFrequency() != null) {
+                    if (adhoc.getLastRun() != null) {
+                        LocalDate start = adhoc.getLastRun().toLocalDate();
+                        LocalDate end = new DateTime().toLocalDate();
+                        switch (ReportRunFrequency.fromId(adhoc.getReportRunFrequency()))
{
+                            case DAILY:
+                                next = start.plusDays(1);
+                                run = Days.daysBetween(start, end).getDays() >= 1;
+                                break;
+                            case WEEKLY:
+                                next = start.plusDays(7);
+                                run = Days.daysBetween(start, end).getDays() >= 7;
+                                break;
+                            case MONTHLY:
+                                next = start.plusMonths(1);
+                                run = Months.monthsBetween(start, end).getMonths() >=
1;
+                                break;
+                            case YEARLY:
+                                next = start.plusYears(1);
+                                run = Years.yearsBetween(start, end).getYears() >= 1;
+                                break;
+                            case CUSTOM:
+                                next = start.plusDays((int) (long) adhoc.getReportRunEvery());
+                                run = Days.daysBetween(start, end).getDays() >= adhoc.getReportRunEvery();
+                                break;
+                            default:
+                                throw new IllegalStateException();
+                        }
+
+                    }
+                }
+
+                if (run) {
+                    //jdbcTemplate.execute("truncate table "+adhoc.getTableName());
+                    final StringBuilder insertSqlBuilder = new StringBuilder(900);
+                    insertSqlBuilder
+                            .append("INSERT INTO ")
+                            .append(adhoc.getTableName()+"(")
+                            .append(adhoc.getTableFields()+") ")
+                            .append(adhoc.getQuery());
+                    if (insertSqlBuilder.length() > 0) {
+                        final int result = this.jdbcTemplate.update(insertSqlBuilder.toString());
+                        logger.info(ThreadLocalContextUtil.getTenant().getName() + ": Results
affected by inserted: " + result);
+
+                        this.jdbcTemplate.update("UPDATE m_adhoc SET last_run=? WHERE id=?",
new Date(), adhoc.getId());
+                    }
+                } else {
+                    logger.info(ThreadLocalContextUtil.getTenant().getName() + ": Skipping
execution of " + adhoc.getName() + ", scheduled for execution on " + next);
                 }
             });	
         }else{


Mime
View raw message