diff --git a/imi/src/main/java/org/motechproject/nms/imi/service/TargetFileService.java b/imi/src/main/java/org/motechproject/nms/imi/service/TargetFileService.java index 5235b8f5c..900a5cb13 100644 --- a/imi/src/main/java/org/motechproject/nms/imi/service/TargetFileService.java +++ b/imi/src/main/java/org/motechproject/nms/imi/service/TargetFileService.java @@ -16,6 +16,7 @@ public interface TargetFileService { */ HashMap generateTargetFile(boolean split); + HashMap generateTargetFileApi(boolean split); TargetFileNotification generateTargetFileWhatsApp(); diff --git a/imi/src/main/java/org/motechproject/nms/imi/service/impl/TargetFileServiceImpl.java b/imi/src/main/java/org/motechproject/nms/imi/service/impl/TargetFileServiceImpl.java index f3b75bb10..4ad1f2fcd 100644 --- a/imi/src/main/java/org/motechproject/nms/imi/service/impl/TargetFileServiceImpl.java +++ b/imi/src/main/java/org/motechproject/nms/imi/service/impl/TargetFileServiceImpl.java @@ -27,6 +27,7 @@ import org.motechproject.nms.imi.web.contract.FileProcessedStatusRequest; import org.motechproject.nms.kilkari.domain.*; import org.motechproject.nms.kilkari.domain.WhatsAppOptSMS; +import org.motechproject.nms.kilkari.dto.SubscriptionDto; import org.motechproject.nms.kilkari.repository.WhatsAppOptSMSDataService; import org.motechproject.nms.kilkari.service.CallRetryService; import org.motechproject.nms.kilkari.service.SubscriptionService; @@ -55,6 +56,7 @@ @Service("targetFileService") public class TargetFileServiceImpl implements TargetFileService { private static final String LOCAL_OBD_DIR = "imi.local_obd_dir"; + private static final String LOCAL_OBDTEST_DIR = "imi.local_obdtest_dir"; private static final String LOCAL_WHATSAPP_SMS_OBD_DIR = "imi.local_whatsapp_sms_obd_dir"; private static final String LOCAL_OBD_DIR_WHATSAPP = "imi.local_obd_dir_whatsapp"; private static final String TARGET_FILE_TIME = "imi.target_file_time"; @@ -89,6 +91,8 @@ public class TargetFileServiceImpl implements TargetFileService { private static final String non_Jh = "NON-JH"; private static final int PROGRESS_INTERVAL = 10000; + private static final int WEEKS_1 = 72; + private static final int WEEKS_2 = 48; private static final String GENERATE_TARGET_FILE_EVENT = "nms.obd.generate_target_file"; private static final String GENERATE_WHATSAPP_SMS_TARGET_FILE_EVENT = "nms.obd.generate_whatsapp_sms_target_file"; @@ -267,6 +271,19 @@ public String serviceIdFromOrigin(boolean freshCall, SubscriptionOrigin origin) throw new IllegalStateException("Unexpected SubscriptionOrigin value"); } + public String serviceIdFromOrigins(boolean freshCall, String origin) { + + if (origin.equalsIgnoreCase("MCTS_IMPORT") || origin.equalsIgnoreCase("RCH_IMPORT")) { + return freshCall ? freshCheckDND : retryCheckDND; + } + + if (origin.equalsIgnoreCase("IVR")) { + return freshCall ? freshNoCheckDND : retryNoCheckDND; + } + + throw new IllegalStateException("Unexpected SubscriptionOrigin value"); + } + public String serviceIdFromOriginJh(boolean freshCall, SubscriptionOrigin origin) { if (origin == SubscriptionOrigin.MCTS_IMPORT || origin == SubscriptionOrigin.RCH_IMPORT) { @@ -280,6 +297,19 @@ public String serviceIdFromOriginJh(boolean freshCall, SubscriptionOrigin origin throw new IllegalStateException("Unexpected SubscriptionOrigin value"); } + public String serviceIdFromOriginsJh(boolean freshCall, String origin) { + + if (origin.equalsIgnoreCase("MCTS_IMPORT") || origin.equalsIgnoreCase("RCH_IMPORT")) { + return freshCall ? freshCheckDNDJh : retryCheckDNDJh; + } + + if (origin.equalsIgnoreCase("IVR")) { + return freshCall ? freshNoCheckDNDJh : retryNoCheckDNDJh; + } + + throw new IllegalStateException("Unexpected SubscriptionOrigin value"); + } + private void scheduleWhatsAppTargetFileGeneration() { //Calculate today's fire time DateTimeFormatter fmt = DateTimeFormat.forPattern("H:m"); @@ -564,13 +594,143 @@ private void writeSubscriptionRow(String requestId, String serviceId, // NO CHEC writer.write("\n"); } - private HashMap generateFreshCalls(DateTime timestamp, int maxQueryBlock, String callFlowUrl, + private HashMap generateFreshCallsApi(DateTime timestamp, int maxQueryBlock, String callFlowUrl, HashMap wr, List subscriptionIdsJh, boolean split) throws IOException { LOGGER.info("generateFreshCallsObd({})", timestamp); int skippedrecords = 0; + DayOfTheWeek dow = DayOfTheWeek.fromDateTime(timestamp); + HashMap recordsMap = new HashMap<>(); + int recordsWritten = 0; + int recordsWrittenSpecific = 0; + int recordsWrittenJh = 0; + Long offset = 0L; + int weeks; + + List specificStateList = getSpecificStateList(); + + List timeSlots = null; + + try { + timeSlots = subscriptionTimeSlotService.findAllTimeSlots(); + + }catch (Exception e){ + LOGGER.error("Error finding time slots for subscriptions", e); + } + if(timeSlots==null){ + LOGGER.warn("No time slots available, proceeding without time slot data."); + } + + + Map timeSlotMap = new HashMap<>(); + if(timeSlots!=null){ + for (SubscriptionTimeSlot timeSlot : timeSlots) { + timeSlotMap.put(timeSlot.getSubscriptionId(), timeSlot); + }} + + + + List subscriptions = findAllSubscriptionsForDayApi(dow, maxQueryBlock); + + LOGGER.info("Subs_block_size" + subscriptions.size()); + + + for (SubscriptionDto subscription : subscriptions) { + offset = subscription.getId(); + +// Subscriber subscriber = subscription.getSubscriber(); + Long stateID = subscription.getStateId(); + + RequestId requestId = new RequestId(subscription.getSubscriptionId(), TIME_FORMATTER.print(timestamp)); + + try { +// SubscriptionPack pack = subscription.getSubscriptionPack(); + int daysIntoPack = Days.daysBetween(new DateTime(subscription.getStartDate()), timestamp).getDays(); +// LOGGER.info("this is the weeks: {}",pack.getWeeks()); + if(subscription.getSubscriptionPackIdOid()==1){ + weeks =WEEKS_1; + }else { + weeks=WEEKS_2; + } + if (daysIntoPack == weeks * 7) { + // + // Do not add subscriptions on their last day to the fresh call list since we + // will try to fetch message for current +1 week, which wouldn't exist + // See https://applab.atlassian.net/browse/NMS-301 + // + /*LOGGER.debug("Ignoring last day for subscription {} from fresh calls.", + subscription.getSubscriptionId());*/ + skippedrecords++; + continue; + } + + SubscriptionPackMessage msg = subscription.nextScheduledMessage(timestamp); +// LOGGER.info("this is the mesg we are getting : {}",msg); + + SubscriptionTimeSlot timeSlot = timeSlotMap.get(subscription.getSubscriptionId()); + String timeStamp1 = timeSlot != null ? timeSlot.getTimeStamp1() != null ? timeSlot.getTimeStamp1().toString() : "" : ""; + String timeStamp2 = timeSlot != null ? timeSlot.getTimeStamp2() != null ? timeSlot.getTimeStamp2().toString() : "" : ""; + String timeStamp3 = timeSlot != null ? timeSlot.getTimeStamp3() != null ? timeSlot.getTimeStamp3().toString() : "" : ""; + + +// LOGGER.info("this is the calling number: {}",subscriber.getCallingNumber().toString()); + + if(split && subscriptionIdsJh.contains(subscription.getSubscriptionId())) { + writeSubscriptionRow( + requestId.toString(), + serviceIdFromOriginsJh(true, subscription.getOrigin()), + subscription.getCallingNumber(), + HIGH_PRIORITY, //todo: how do we choose a priority? + callFlowUrl, + msg.getMessageFileName(), + msg.getWeekId(), + // we are happy with empty language and circle since they are optional + subscription.getLanguageCode(), + subscription.getCircleName(), + getCode(subscription.getOrigin()), + wr.get(Jh), + subscription.isNeedsWelcomeOptInForWp(), + timeStamp1, + timeStamp2, + timeStamp3); + recordsWrittenJh++; + } + else if(specificStateList.contains(stateID)){ + recordsWrittenSpecific = getRecordsWrittenApi(callFlowUrl, wr, recordsWrittenSpecific, subscription,requestId,msg, specific_non_Jh,timeStamp1,timeStamp2,timeStamp3); + } + else { + recordsWritten = getRecordsWrittenApi(callFlowUrl, wr, recordsWritten, subscription, requestId, msg, non_Jh,timeStamp1,timeStamp2,timeStamp3); + + } + } catch (IllegalStateException se) { + String message = se.toString(); + alertService.create(subscription.getSubscriptionId(), "IllegalStateException", message, + AlertType.HIGH, AlertStatus.NEW, 0, null); + LOGGER.error(message,se); + } + } + + + + LOGGER.info(WROTE+non_Jh, recordsWritten); + recordsMap.put(non_Jh, recordsWritten); + recordsMap.put(specific_non_Jh, recordsWrittenSpecific); + if(split){ + LOGGER.info(WROTE+Jh, recordsWrittenJh); + recordsMap.put(Jh, recordsWrittenJh); + } + return recordsMap; + } + + private HashMap generateFreshCalls(DateTime timestamp, int maxQueryBlock, String callFlowUrl, + HashMap wr, List subscriptionIdsJh, boolean split) throws IOException { + + LOGGER.info("generateFreshCallsObd({})", timestamp); + + int skippedrecords = 0; + DayOfTheWeek dow = DayOfTheWeek.fromDateTime(timestamp); HashMap recordsMap = new HashMap<>(); int recordsWritten = 0; @@ -595,9 +755,9 @@ private HashMap generateFreshCalls(DateTime timestamp, int maxQ try { timeSlots = subscriptionTimeSlotService.findTimeSlotsForSubscriptionsById(subscriptionIds); - }catch (Exception e){ + }catch (Exception e){ LOGGER.error("Error finding time slots for subscriptions", e); - } + } if(timeSlots==null){ continue; } @@ -642,25 +802,25 @@ private HashMap generateFreshCalls(DateTime timestamp, int maxQ if(split && subscriptionIdsJh.contains(subscription.getSubscriptionId())) { - writeSubscriptionRow( - requestId.toString(), - serviceIdFromOriginJh(true, subscription.getOrigin()), - subscriber.getCallingNumber().toString(), - HIGH_PRIORITY, //todo: how do we choose a priority? - callFlowUrl, - msg.getMessageFileName(), - msg.getWeekId(), - // we are happy with empty language and circle since they are optional - subscriber.getLanguage() == null ? "" : subscriber.getLanguage().getCode(), - subscriber.getCircle() == null ? "" : subscriber.getCircle().getName(), - subscription.getOrigin().getCode(), - wr.get(Jh), - subscription.isNeedsWelcomeOptInForWP(), - timeStamp1, - timeStamp2, - timeStamp3); - recordsWrittenJh++; - } + writeSubscriptionRow( + requestId.toString(), + serviceIdFromOriginJh(true, subscription.getOrigin()), + subscriber.getCallingNumber().toString(), + HIGH_PRIORITY, //todo: how do we choose a priority? + callFlowUrl, + msg.getMessageFileName(), + msg.getWeekId(), + // we are happy with empty language and circle since they are optional + subscriber.getLanguage() == null ? "" : subscriber.getLanguage().getCode(), + subscriber.getCircle() == null ? "" : subscriber.getCircle().getName(), + subscription.getOrigin().getCode(), + wr.get(Jh), + subscription.isNeedsWelcomeOptInForWP(), + timeStamp1, + timeStamp2, + timeStamp3); + recordsWrittenJh++; + } else if(specificStateList.contains(stateID)){ recordsWrittenSpecific = getRecordsWritten(callFlowUrl, wr, recordsWrittenSpecific, subscription, subscriber, requestId, msg, specific_non_Jh,timeStamp1,timeStamp2,timeStamp3); } @@ -710,6 +870,62 @@ private int getRecordsWritten(String callFlowUrl, HashMap wr, int recordsWrittenJh, SubscriptionDto subscription, RequestId requestId, SubscriptionPackMessage msg, String specific_non_jh,String timeStamp1,String timeStamp2,String timeStamp3) throws IOException { + writeSubscriptionRow( + requestId.toString(), + serviceIdFromOrigins(true, subscription.getOrigin()), + subscription.getCallingNumber(), + NORMAL_PRIORITY, //todo: how do we choose a priority? + callFlowUrl, + msg.getMessageFileName(), + msg.getWeekId(), + // we are happy with empty language and circle since they are optional + subscription.getLanguageCode(), + subscription.getCircleName(), + getCode(subscription.getOrigin()), + wr.get(specific_non_jh), + subscription.isNeedsWelcomeOptInForWp(), + timeStamp1, + timeStamp2, + timeStamp3); + recordsWrittenJh++; + return recordsWrittenJh; + } + + private List findAllSubscriptionsForDayApi(DayOfTheWeek dow, int batchSize) { + List allSubscriptions = new ArrayList<>(); + long offset = 0; // Start from the first record + boolean hasMoreRecords = true; + + while (hasMoreRecords) { + List batchSubscriptions = subscriptionService.findActiveSubscriptionsForDayApi(dow, offset, batchSize); + + if (batchSubscriptions.isEmpty()) { + hasMoreRecords = false; // No more records to fetch + } else { + allSubscriptions.addAll(batchSubscriptions); + offset += batchSize; // Increment offset for the next batch + } + + LOGGER.info("Fetched {} records so far.", allSubscriptions.size()); + } + + LOGGER.info("Total subscriptions fetched: {}", allSubscriptions.size()); + return allSubscriptions; + } + private void writeWhatsAppHeader(OutputStreamWriter writer) throws IOException { /* * #1 CircleId @@ -789,9 +1005,8 @@ private void writeWhatsAppSMSRow(String circleId, String contentFile, String lan } - private HashMap generateRetryCalls(DateTime timestamp, int maxQueryBlock, String callFlowUrl, - HashMap wr, List subscriptionIdsJh, boolean split) throws IOException { + HashMap wr, List subscriptionIdsJh, boolean split) throws IOException { LOGGER.info("generateRetryCallsObd({})", timestamp); int count = 0; @@ -1022,10 +1237,224 @@ else if(specificState.contains(stateCode)){ return retryCount; } + + private HashMap generateRetryCallsApi(DateTime timestamp, int maxQueryBlock, String callFlowUrl, + HashMap wr, List subscriptionIdsJh, boolean split) throws IOException { + + LOGGER.info("generateRetryCallsObd({})", timestamp); + int count = 0; + int countJh = 0; + int countSpecific = 0; + HashMap retryCount = new HashMap<>(); + List specificState = getSpecificStateList(); + + List timeSlots = null; + try { + timeSlots = subscriptionTimeSlotService.findAllTimeSlots(); + } catch (Exception e) { + LOGGER.error("Error finding time slots for subscriptions", e); + } + + if (timeSlots == null) { + LOGGER.warn("No time slots available, proceeding without time slot data."); + } + + Map timeSlotMap = new HashMap<>(); + if(timeSlots!=null){ + for (SubscriptionTimeSlot timeSlot : timeSlots) { + timeSlotMap.put(timeSlot.getSubscriptionId(), timeSlot); + }} + + for (String writer : wr.keySet()) { + if (writer.equals(specific_non_Jh)) { + Long offset = 0L; + do { + List callRetries = callRetryService.retrieveAllIVR(offset, maxQueryBlock, specificState); + + LOGGER.info("Call_Retries" + callRetries.size()); + + if (callRetries.size() == 0) { + break; + } + + + + + + + for (CallRetry callRetry : callRetries) { + offset = callRetry.getId(); + RequestId requestId = new RequestId(callRetry.getSubscriptionId(), TIME_FORMATTER.print(timestamp)); + + SubscriptionTimeSlot timeSlot = timeSlotMap.get(callRetry.getSubscriptionId()); + String timeStamp1 = timeSlot != null ? timeSlot.getTimeStamp1() != null ? timeSlot.getTimeStamp1().toString() : "" : ""; + String timeStamp2 = timeSlot != null ? timeSlot.getTimeStamp2() != null ? timeSlot.getTimeStamp2().toString() : "" : ""; + String timeStamp3 = timeSlot != null ? timeSlot.getTimeStamp3() != null ? timeSlot.getTimeStamp3().toString() : "" : ""; + + writeSubscriptionRow( + requestId.toString(), + serviceIdFromOrigin(false, callRetry.getSubscriptionOrigin()), + callRetry.getMsisdn().toString(), + NORMAL_PRIORITY, + callFlowUrl, + callRetry.getContentFileName(), + callRetry.getWeekId(), + callRetry.getLanguageLocationCode(), + callRetry.getCircle(), + callRetry.getSubscriptionOrigin().getCode(), + wr.get(specific_non_Jh), + callRetry.isOpt_in_call_eligibility(), + timeStamp1, + timeStamp2, + timeStamp3); + countSpecific++; + } + + } while (true); + } else if(writer.equals(non_Jh)){ + Long offset = 0L; + do { + // All calls are rescheduled for the next day which means that we should query for all CallRetry records + List callRetries = callRetryService.retrieveAllNonIVR(offset, maxQueryBlock,specificState); + LOGGER.info("Call_Retries" + callRetries.size()); + + if (callRetries.size() == 0) { + break; + } + + + for (CallRetry callRetry : callRetries) { + offset = callRetry.getId(); + RequestId requestId = new RequestId(callRetry.getSubscriptionId(), TIME_FORMATTER.print(timestamp)); + SubscriptionTimeSlot timeSlot = timeSlotMap.get(callRetry.getSubscriptionId()); + String timeStamp1 = timeSlot != null ? timeSlot.getTimeStamp1() != null ? timeSlot.getTimeStamp1().toString() : "" : ""; + String timeStamp2 = timeSlot != null ? timeSlot.getTimeStamp2() != null ? timeSlot.getTimeStamp2().toString() : "" : ""; + String timeStamp3 = timeSlot != null ? timeSlot.getTimeStamp3() != null ? timeSlot.getTimeStamp3().toString() : "" : ""; + + if (split && subscriptionIdsJh.contains(callRetry.getSubscriptionId())) { + writeSubscriptionRow( + requestId.toString(), + serviceIdFromOriginJh(false, callRetry.getSubscriptionOrigin()), + callRetry.getMsisdn().toString(), + HIGH_PRIORITY, + callFlowUrl, + callRetry.getContentFileName(), + callRetry.getWeekId(), + callRetry.getLanguageLocationCode(), + callRetry.getCircle(), + callRetry.getSubscriptionOrigin().getCode(), + wr.get(Jh), + callRetry.isOpt_in_call_eligibility(), + timeStamp1, + timeStamp2, + timeStamp3); + countJh++; + } else { + writeSubscriptionRow( + requestId.toString(), + serviceIdFromOrigin(false, callRetry.getSubscriptionOrigin()), + callRetry.getMsisdn().toString(), + NORMAL_PRIORITY, + callFlowUrl, + callRetry.getContentFileName(), + callRetry.getWeekId(), + callRetry.getLanguageLocationCode(), + callRetry.getCircle(), + callRetry.getSubscriptionOrigin().getCode(), + wr.get(non_Jh), + callRetry.isOpt_in_call_eligibility(), + timeStamp1, + timeStamp2, + timeStamp3); + count++; + } + } + + } while (true); + } + } + /* do { + // All calls are rescheduled for the next day which means that we should query for all CallRetry records + List callRetries = callRetryService.retrieveAll(offset, maxQueryBlock); + LOGGER.info("Call_Retries"+callRetries.size()); + + if (callRetries.size() == 0) { + break; + } + + for (CallRetry callRetry : callRetries) { + offset = callRetry.getId(); + RequestId requestId = new RequestId(callRetry.getSubscriptionId(), TIME_FORMATTER.print(timestamp)); + Subscriber subscriber = subscriptionService.getSubscription(callRetry.getSubscriptionId()).getSubscriber(); + Long stateCode = subscriber.getMother() == null ? subscriber.getChild().getState().getCode() : subscriber.getMother().getState().getCode(); + if(split && subscriptionIdsJh.contains(callRetry.getSubscriptionId())) { + writeSubscriptionRow( + requestId.toString(), + serviceIdFromOriginJh(false, callRetry.getSubscriptionOrigin()), + callRetry.getMsisdn().toString(), + HIGH_PRIORITY, + callFlowUrl, + callRetry.getContentFileName(), + callRetry.getWeekId(), + callRetry.getLanguageLocationCode(), + callRetry.getCircle(), + callRetry.getSubscriptionOrigin().getCode(), + wr.get(Jh)); + countJh++; + } + else if(specificState.contains(stateCode)){ + writeSubscriptionRow( + requestId.toString(), + serviceIdFromOrigin(false, callRetry.getSubscriptionOrigin()), + callRetry.getMsisdn().toString(), + NORMAL_PRIORITY, + callFlowUrl, + callRetry.getContentFileName(), + callRetry.getWeekId(), + callRetry.getLanguageLocationCode(), + callRetry.getCircle(), + callRetry.getSubscriptionOrigin().getCode(), + wr.get(specific_non_Jh)); + countSpecific++; + } + else { + writeSubscriptionRow( + requestId.toString(), + serviceIdFromOrigin(false, callRetry.getSubscriptionOrigin()), + callRetry.getMsisdn().toString(), + NORMAL_PRIORITY, + callFlowUrl, + callRetry.getContentFileName(), + callRetry.getWeekId(), + callRetry.getLanguageLocationCode(), + callRetry.getCircle(), + callRetry.getSubscriptionOrigin().getCode(), + wr.get(non_Jh)); + count++; + } + } + + } while (true);*/ + + LOGGER.info(WROTE+non_Jh+"Retry", count); + retryCount.put(non_Jh, count); + retryCount.put(specific_non_Jh, countSpecific); + if(split) { + LOGGER.info(WROTE+Jh+"Retry", countJh); + retryCount.put(Jh, countJh); + } + + return retryCount; + } + private File localObdDir() { return new File(settingsFacade.getProperty(LOCAL_OBD_DIR)); } + private File localObdTestDir() { + return new File(settingsFacade.getProperty(LOCAL_OBDTEST_DIR)); + } + private List getSpecificStateList(){ String locationProp = settingsFacade.getProperty(SPECIFIC_STATE_ID); if (StringUtils.isBlank(locationProp)) { @@ -1177,6 +1606,141 @@ public HashMap generateTargetFile(boolean split) } return tfn; } + + + @Transactional + public HashMap generateTargetFileApi(boolean split) { + LOGGER.info("generateTargetFile()"+split); + DateTime today = DateTime.now(); + String targetFileName = targetFileName(TIME_FORMATTER.print(today)); + String targetFileNameHungama = targetFileName(TIME_FORMATTER.print(today)+"IVR"); + File localTargetDir = localObdTestDir(); + String checksum; + String checksumHungama; + File targetFile = new File(localTargetDir, targetFileName); + File targetFileHungama = new File(localTargetDir, targetFileNameHungama); + + HashMap recordCount; + HashMap recordCountRetry; + + int maxQueryBlock = Integer.parseInt(settingsFacade.getProperty(MAX_QUERY_BLOCK)); + String callFlowUrl = settingsFacade.getProperty(TARGET_FILE_CALL_FLOW_URL); + + if (callFlowUrl == null) { + //it's ok to have an empty call flow url - the spec says the default call flow will be used + //whatever that is... + callFlowUrl = ""; + } + HashMap wr = new HashMap<>(); + HashMap tfn = new HashMap<>(); + if(split){ + String targetFileNameJh = targetFileName(TIME_FORMATTER.print(today)+"JH"); + File localTargetDirJh = localObdTestDir(); + String checksumJh; + File targetFileJh = new File(localTargetDirJh, targetFileNameJh); + + try { + FileOutputStream fos = new FileOutputStream(targetFile); + OutputStreamWriter writer = new OutputStreamWriter(fos); + + FileOutputStream fosH = new FileOutputStream(targetFileHungama); + OutputStreamWriter writerH = new OutputStreamWriter(fosH); + + FileOutputStream fosJh = new FileOutputStream(targetFileJh); + OutputStreamWriter writerJh = new OutputStreamWriter(fosJh); + + //Header + writeHeader(writer); + writeHeader(writerH); + writeHeader(writerJh); + + List subscriptionIdsJh = subscriptionService.findJhSubscriptionIds(); + LOGGER.info("JH-Subscriptions-Number"+subscriptionIdsJh.size()); + + + wr.put(Jh, writerJh); + wr.put(non_Jh, writer); + wr.put(specific_non_Jh, writerH); + + //Fresh calls + recordCount = generateFreshCallsApi(today, maxQueryBlock, callFlowUrl, wr, subscriptionIdsJh, true); + + //Retry calls + recordCountRetry = generateRetryCallsApi(today, maxQueryBlock, callFlowUrl, wr, subscriptionIdsJh, true); + + writer.close(); + fos.close(); + + writerH.close(); + fosH.close(); + + writerJh.close(); + fosJh.close(); + + checksum = ChecksumHelper.checksum(targetFile); + checksumJh = ChecksumHelper.checksum(targetFileJh); + checksumHungama = ChecksumHelper.checksum(targetFileHungama); + tfn.put(Jh, new TargetFileNotification(targetFileNameJh, checksumJh, recordCount.get(Jh)+recordCountRetry.get(Jh))); + + } catch (IOException e) { + LOGGER.error(e.getMessage(),e); + alert(targetFile.toString(), "targetFile", e.getMessage()); + fileAuditRecordDataService.create(new FileAuditRecord(FileType.TARGET_FILE, targetFile.getName(), + false, e.getMessage(), null, null)); + return null; + } + } + else{ + try { + FileOutputStream fos = new FileOutputStream(targetFile); + OutputStreamWriter writer = new OutputStreamWriter(fos); + + FileOutputStream fosH = new FileOutputStream(targetFileHungama); + OutputStreamWriter writerH = new OutputStreamWriter(fosH); + + //Header + writeHeader(writer); + writeHeader(writerH); + + wr.put(non_Jh, writer); + wr.put(specific_non_Jh,writerH); + + //Fresh calls + recordCount = generateFreshCallsApi(today, maxQueryBlock, callFlowUrl, wr, Collections.emptyList(), false); + + //Retry calls + recordCountRetry = generateRetryCallsApi(today, maxQueryBlock, callFlowUrl, wr, Collections.emptyList(), false); + + writer.close(); + writerH.close(); + + fos.close(); + fosH.close(); + + checksum = ChecksumHelper.checksum(targetFile); + checksumHungama = ChecksumHelper.checksum(targetFileHungama); + + } catch (IOException e) { + LOGGER.error(e.getMessage(),e); + alert(targetFile.toString(), "targetFile", e.getMessage()); + fileAuditRecordDataService.create(new FileAuditRecord(FileType.TARGET_FILE, targetFile.getName(), + false, e.getMessage(), null, null)); + return null; + } + } + + tfn.put(non_Jh, new TargetFileNotification(targetFileName, checksum, recordCount.get(non_Jh)+recordCountRetry.get(non_Jh))); + tfn.put(specific_non_Jh, new TargetFileNotification(targetFileNameHungama,checksumHungama, recordCount.get(specific_non_Jh)+recordCountRetry.get(specific_non_Jh))); + LOGGER.debug("TargetFileNotification = {}", tfn.toString()); + + //audit the success + for(TargetFileNotification t: tfn.values()) { + fileAuditRecordDataService.create(new FileAuditRecord(FileType.TARGET_FILE, t.getFileName(), true, + null, t.getRecordsCount(), t.getChecksum())); + } + return tfn; + } + @Transactional public HashMap generateWhatsAppSMSTargetFile() { DateTime today = DateTime.now(); diff --git a/imi/src/main/java/org/motechproject/nms/imi/web/ImiController.java b/imi/src/main/java/org/motechproject/nms/imi/web/ImiController.java index ab21a49e5..09a5758ef 100644 --- a/imi/src/main/java/org/motechproject/nms/imi/web/ImiController.java +++ b/imi/src/main/java/org/motechproject/nms/imi/web/ImiController.java @@ -218,7 +218,7 @@ public String generateTargetFile() { LOGGER.debug("/generateTargetFile (GET)"); try { - HashMap tfn = targetFileService.generateTargetFile(Boolean.parseBoolean(settingsFacade.getProperty(generateJhFile))); + HashMap tfn = targetFileService.generateTargetFileApi(Boolean.parseBoolean(settingsFacade.getProperty(generateJhFile))); LOGGER.debug("targetFileService.generateTargetFile() done"); return tfn == null ? "null" : tfn.values().toString(); diff --git a/imi/src/main/resources/imi.properties b/imi/src/main/resources/imi.properties index 7b7b99f4f..dab12c10a 100644 --- a/imi/src/main/resources/imi.properties +++ b/imi/src/main/resources/imi.properties @@ -13,6 +13,8 @@ imi.target_file_sec_interval=86400 #local directory where target files should be generated imi.local_obd_dir=/usr/local/imi/obd +imi.local_obdtest_dir=/usr/local/imi/obdtest + #remote directory where target files should be copied to imi.remote_obd_dir=/usr/local/imi/obd-remote diff --git a/imi/src/main/resources/securityRules.json b/imi/src/main/resources/securityRules.json index fd1a0f17a..8fa2c804d 100644 --- a/imi/src/main/resources/securityRules.json +++ b/imi/src/main/resources/securityRules.json @@ -74,6 +74,21 @@ "ANY" ] }, + { + "active": true, + "pattern": "/**/generateTargetFileApi/**", + "supportedSchemes": [ + "NO_SECURITY" + ], + "protocol": "HTTP", + "priority": 2, + "rest": true, + "origin": "SYSTEM_MODULE_IMI", + "version": "all", + "methodsRequired": [ + "ANY" + ] + }, { "active": true, "pattern": "/**/generateWhatsAppSMSTargetFile/**", diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/dto/SubscriptionDto.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/dto/SubscriptionDto.java new file mode 100644 index 000000000..3f2c8bca8 --- /dev/null +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/dto/SubscriptionDto.java @@ -0,0 +1,261 @@ +package org.motechproject.nms.kilkari.dto; + +import org.joda.time.DateTime; +import org.joda.time.Days; +import org.motechproject.nms.kilkari.domain.SubscriptionPackMessage; + +import java.util.Date; + +public class SubscriptionDto { + private final long id; + private final Date activationDate; + private final String deactivationReason; + private final Date endDate; + private final String firstMessageDayOfWeek; + private final boolean needsWelcomeMessageViaObd; + private final String origin; + private final String secondMessageDayOfWeek; + private final Date startDate; + private final String status; + private final long subscriberIdOid; + private final String subscriptionId; + private final long subscriptionPackIdOid; + private final Date creationDate; + private final String creator; + private final Date modificationDate; + private final String modifiedBy; + private final String owner; + private final boolean needsWelcomeOptInForWp; + private final String callingNumber; + private final String languageCode; + private final String circleName; + private final long stateId; + private final int weeks; + private final String subscriptionPackName; + private final int messagesPerWeek; + + private SubscriptionDto(Builder builder) { + this.id = builder.id; + this.activationDate = builder.activationDate; + this.deactivationReason = builder.deactivationReason; + this.endDate = builder.endDate; + this.firstMessageDayOfWeek = builder.firstMessageDayOfWeek; + this.needsWelcomeMessageViaObd = builder.needsWelcomeMessageViaObd; + this.origin = builder.origin; + this.secondMessageDayOfWeek = builder.secondMessageDayOfWeek; + this.startDate = builder.startDate; + this.status = builder.status; + this.subscriberIdOid = builder.subscriberIdOid; + this.subscriptionId = builder.subscriptionId; + this.subscriptionPackIdOid = builder.subscriptionPackIdOid; + this.creationDate = builder.creationDate; + this.creator = builder.creator; + this.modificationDate = builder.modificationDate; + this.modifiedBy = builder.modifiedBy; + this.owner = builder.owner; + this.needsWelcomeOptInForWp = builder.needsWelcomeOptInForWp; + this.callingNumber = builder.callingNumber; + this.languageCode = builder.languageCode; + this.circleName = builder.circleName; + this.stateId = builder.stateId; + this.weeks = builder.weeks; + this.subscriptionPackName = builder.subscriptionPackName; + this.messagesPerWeek = builder.messagesPerWeek; + } + + public long getId() { + return id; + } + + public Date getActivationDate() { + return activationDate; + } + + public String getDeactivationReason() { + return deactivationReason; + } + + public Date getEndDate() { + return endDate; + } + + public String getFirstMessageDayOfWeek() { + return firstMessageDayOfWeek; + } + + public boolean isNeedsWelcomeMessageViaObd() { + return needsWelcomeMessageViaObd; + } + + public String getOrigin() { + return origin; + } + + public String getSecondMessageDayOfWeek() { + return secondMessageDayOfWeek; + } + + public Date getStartDate() { + return startDate; + } + + public String getStatus() { + return status; + } + + public long getSubscriberIdOid() { + return subscriberIdOid; + } + + public String getSubscriptionId() { + return subscriptionId; + } + + public long getSubscriptionPackIdOid() { + return subscriptionPackIdOid; + } + + public Date getCreationDate() { + return creationDate; + } + + public String getCreator() { + return creator; + } + + public Date getModificationDate() { + return modificationDate; + } + + public String getModifiedBy() { + return modifiedBy; + } + + public String getOwner() { + return owner; + } + + public boolean isNeedsWelcomeOptInForWp() { + return needsWelcomeOptInForWp; + } + + public String getCallingNumber() { + return callingNumber; + } + + public String getLanguageCode() { + return languageCode; + } + + public String getCircleName() { + return circleName; + } + + public long getStateId() { + return stateId; + } + + public int getWeeks() { + return weeks; + } + + public String getSubscriptionPackName() { + return subscriptionPackName; + } + + public int getMessagesPerWeek() { + return messagesPerWeek; + } + + + public SubscriptionPackMessage nextScheduledMessage(DateTime date) { + + int daysIntoPack = Days.daysBetween(new DateTime(startDate), date).getDays(); + if (daysIntoPack < 0) { + throw new IllegalStateException( + String.format("Subscription with ID %s is not due for any scheduled message. Start date in the future", subscriptionId)); + } + + if (needsWelcomeMessageViaObd) { + return getWelcomeMessage(); + } + + int currentWeek = daysIntoPack / 7 + 1; + + return getMessageByWeekAndMessageId(currentWeek, 1); + + } + + public SubscriptionPackMessage getMessageByWeekAndMessageId(int week, int day) { + String weekId = String.format("w%d_%d", week, day); + String messageFileName = String.format("w%d_%d.wav", week, day); + + return new SubscriptionPackMessage(weekId, messageFileName, 120); // Using default 120 seconds duration + } + + private SubscriptionPackMessage getWelcomeMessage() { + return new SubscriptionPackMessage("w1_1", "w1_1.wav", 120); + } + + public static class Builder { + private long id; + private Date activationDate; + private String deactivationReason; + private Date endDate; + private String firstMessageDayOfWeek; + private boolean needsWelcomeMessageViaObd; + private String origin; + private String secondMessageDayOfWeek; + private Date startDate; + private String status; + private long subscriberIdOid; + private String subscriptionId; + private long subscriptionPackIdOid; + private Date creationDate; + private String creator; + private Date modificationDate; + private String modifiedBy; + private String owner; + private boolean needsWelcomeOptInForWp; + private String callingNumber; + private String languageCode; + private String circleName; + private long stateId; + private int weeks; + private String subscriptionPackName; + private int messagesPerWeek; + + public Builder setId(long id) { this.id = id; return this; } + public Builder setActivationDate(Date activationDate) { this.activationDate = activationDate; return this; } + public Builder setDeactivationReason(String deactivationReason) { this.deactivationReason = deactivationReason; return this; } + public Builder setEndDate(Date endDate) { this.endDate = endDate; return this; } + public Builder setFirstMessageDayOfWeek(String firstMessageDayOfWeek) { this.firstMessageDayOfWeek = firstMessageDayOfWeek; return this; } + public Builder setNeedsWelcomeMessageViaObd(boolean needsWelcomeMessageViaObd) { this.needsWelcomeMessageViaObd = needsWelcomeMessageViaObd; return this; } + public Builder setOrigin(String origin) { this.origin = origin; return this; } + public Builder setSecondMessageDayOfWeek(String secondMessageDayOfWeek) { this.secondMessageDayOfWeek = secondMessageDayOfWeek; return this; } + public Builder setStartDate(Date startDate) { this.startDate = startDate; return this; } + public Builder setStatus(String status) { this.status = status; return this; } + public Builder setSubscriberIdOid(long subscriberIdOid) { this.subscriberIdOid = subscriberIdOid; return this; } + public Builder setSubscriptionId(String subscriptionId) { this.subscriptionId = subscriptionId; return this; } + public Builder setSubscriptionPackIdOid(long subscriptionPackIdOid) { this.subscriptionPackIdOid = subscriptionPackIdOid; return this; } + public Builder setCreationDate(Date creationDate) { this.creationDate = creationDate; return this; } + public Builder setCreator(String creator) { this.creator = creator; return this; } + public Builder setModificationDate(Date modificationDate) { this.modificationDate = modificationDate; return this; } + public Builder setModifiedBy(String modifiedBy) { this.modifiedBy = modifiedBy; return this; } + public Builder setOwner(String owner) { this.owner = owner; return this; } + public Builder setNeedsWelcomeOptInForWp(boolean needsWelcomeOptInForWp) { this.needsWelcomeOptInForWp = needsWelcomeOptInForWp; return this; } + public Builder setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; return this; } + public Builder setLanguageCode(String languageCode) { this.languageCode = languageCode; return this; } + public Builder setCircleName(String circleName) { this.circleName = circleName; return this; } + public Builder setStateId(long stateId) { this.stateId = stateId; return this; } + public Builder setWeeks(int weeks) { this.weeks = weeks; return this; } + public Builder setSubscriptionPackName(String subscriptionPackName) { this.subscriptionPackName = subscriptionPackName; return this; } + public Builder setMessagesPerWeek(int messagesPerWeek) { this.messagesPerWeek = messagesPerWeek; return this; } + + + + public SubscriptionDto build() { + return new SubscriptionDto(this); + } + } +} diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java index b0d016fdb..484e39ec4 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java @@ -10,6 +10,7 @@ import org.motechproject.nms.kilkari.domain.SubscriptionOrigin; import org.motechproject.nms.kilkari.domain.SubscriptionPack; import org.motechproject.nms.kilkari.domain.SubscriptionPackType; +import org.motechproject.nms.kilkari.dto.SubscriptionDto; import org.motechproject.nms.props.domain.DayOfTheWeek; import org.motechproject.nms.region.domain.Circle; import org.motechproject.nms.region.domain.Language; @@ -151,6 +152,8 @@ Subscription createSubscription(Subscriber subscriber, long callingNumber, Langu * @param rowCount The maximum number of rows to return * @return The list of subscriptions due for a message */ + List findActiveSubscriptionsForDayApi(DayOfTheWeek dayOfTheWeek, long offset, int rowCount); + List findActiveSubscriptionsForDay(DayOfTheWeek dayOfTheWeek, long offset, int rowCount); List findJhSubscriptionIds(); diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionTimeSlotService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionTimeSlotService.java index cefbbb875..58b8e5cd4 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionTimeSlotService.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionTimeSlotService.java @@ -8,4 +8,6 @@ public interface SubscriptionTimeSlotService { List findTimeSlotsForSubscriptionsById(List subscriptionId); + List findAllTimeSlots(); + } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java index 47ec842a2..5f3985b4f 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java @@ -16,6 +16,7 @@ import org.motechproject.mds.util.InstanceSecurityRestriction; import org.motechproject.metrics.service.Timer; import org.motechproject.nms.kilkari.domain.*; +import org.motechproject.nms.kilkari.dto.SubscriptionDto; import org.motechproject.nms.kilkari.repository.*; import org.motechproject.nms.kilkari.service.CsrVerifierService; import org.motechproject.nms.kilkari.service.SubscriptionService; @@ -36,7 +37,9 @@ import javax.annotation.PostConstruct; import javax.jdo.Query; import javax.validation.ConstraintViolationException; +import java.sql.*; import java.util.*; +import java.util.Date; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -61,6 +64,10 @@ public class SubscriptionServiceImpl implements SubscriptionService { private SubscriberMsisdnTrackerDataService subscriberMsisdnTrackerDataService; public static AtomicBoolean isCapacityAvailable = new AtomicBoolean(true); private static final String HIGH_PRIORITY_BLOCK = "kilkari.highPriority.blockId"; + public static final String DB_URL_JDBC = "db.url"; + public static final String DB_USER_JDBC = "db.user"; + public static final String DB_PASSWORD_JDBC = "db.password"; + @Autowired @@ -985,6 +992,26 @@ public Subscription create(Subscription subscription) { return subscriptionDataService.create(subscription); } + +// @Override +// public List findActiveSubscriptionsForDayUsingJdo(DayOfTheWeek dow, long offset, int rowCount) { +// try { +// Query query = persistenceManager.newQuery(Subscription.class); +// query.setFilter("firstMessageDayOfWeek == :dow && status == :status && serviceStatus == :ivr || serviceStatus == :ivrAndWhatsApp"); +// query.setOrdering("id ascending"); +// +// +// return (List) query.executeWithArray( +// dow.toString(), +// SubscriptionStatus.ACTIVE, +// "IVR", +// "IVR_AND_WHATSAPP" +// ); +// } finally { +// persistenceManager.close(); +// } +// } + @Override public List findActiveSubscriptionsForDay(final DayOfTheWeek dow, final long offset, final int rowCount) { @@ -1033,6 +1060,105 @@ public List execute(Query query) { } + + + @Override + public List findActiveSubscriptionsForDayApi(DayOfTheWeek dow, long offset, int rowCount) { + List subscriptions = new ArrayList<>(); + String query = "SELECT s.id as id, activationDate, deactivationReason, endDate, firstMessageDayOfWeek, " + + "s.needsWelcomeMessageViaObd, s.origin, s.secondMessageDayOfWeek, s.startDate, " + + "s.status, s.subscriber_id_OID, s.subscriptionId, s.subscriptionPack_id_OID, " + + "s.creationDate, s.creator, s.modificationDate, s.modifiedBy, s.owner, s.needsWelcomeOptInForWP,sub.callingNumber, " + + "COALESCE(lang.code, '') AS languageCode," + + "COALESCE(circ.name, '') AS circleName," + + "COALESCE(statemother.id, statechild.id) AS stateID " + + "FROM nms_subscriptions s " + + "INNER JOIN nms_subscribers AS sub ON s.subscriber_id_OID = sub.id " + + "LEFT JOIN nms_mcts_mothers AS mother ON sub.mother_id_OID = mother.id " + + "LEFT JOIN nms_states AS statemother ON mother.state_id_OID = statemother.id " + + "LEFT JOIN nms_mcts_children AS child ON sub.child_id_OID = child.id " + + "LEFT JOIN nms_states AS statechild ON child.state_id_OID = statechild.id " + + "LEFT JOIN nms_languages AS lang ON sub.language_id_OID = lang.id " + + "LEFT JOIN nms_circles AS circ ON sub.circle_id_OID = circ.id " + + "WHERE s.firstMessageDayOfWeek = ? AND s.status = 'ACTIVE' " + + "AND serviceStatus IN ('IVR', 'IVR_AND_WHATSAPP') " + + "ORDER BY s.id LIMIT ? OFFSET ? "; + + LOGGER.debug("Executing query: {}", query); + + // ResultSet resultSet = null; + PreparedStatement stmt = null; + Connection connection =null; + + try{ connection = DriverManager.getConnection(settingsFacade.getProperty(DB_URL_JDBC), settingsFacade.getProperty(DB_USER_JDBC), settingsFacade.getProperty(DB_PASSWORD_JDBC)); + stmt = connection.prepareStatement(query) ; + + stmt.setString(1, String.valueOf(dow)); + stmt.setInt(2, rowCount); + stmt.setLong(3, offset); + + try (ResultSet resultSet = stmt.executeQuery()) { + while (resultSet.next()) { + SubscriptionDto subscriptionDto = populateSubscription(resultSet); + subscriptions.add(subscriptionDto); + } + } + + + } catch (Exception e) { + LOGGER.error("Error executing query: {}", query, e); + }finally { + try { + if (stmt != null) { + stmt.close(); + } + if (connection != null) { + connection.close(); + } + } catch (Exception e) { + LOGGER.error("Error closing the connection with the database..."); + e.printStackTrace(); + } + } + + return subscriptions; + } + + /** + * Maps a ResultSet row to a Subscription object using the constructor. + */ + private SubscriptionDto populateSubscription( ResultSet resultSet) throws Exception { + + long id = resultSet.getLong("id"); + return new SubscriptionDto.Builder() + .setId(id) + .setActivationDate(resultSet.getDate("activationDate")) + .setDeactivationReason(resultSet.getString("deactivationReason")) + .setEndDate(resultSet.getDate("endDate")) + .setFirstMessageDayOfWeek(resultSet.getString("firstMessageDayOfWeek")) + .setNeedsWelcomeMessageViaObd(resultSet.getBoolean("needsWelcomeMessageViaObd")) + .setOrigin(resultSet.getString("origin")) + .setSecondMessageDayOfWeek(resultSet.getString("secondMessageDayOfWeek")) + .setStartDate(resultSet.getDate("startDate")) + .setStatus(resultSet.getString("status")) + .setSubscriberIdOid(resultSet.getLong("subscriber_id_OID")) + .setSubscriptionId(resultSet.getString("subscriptionId")) + .setSubscriptionPackIdOid(resultSet.getLong("subscriptionPack_id_OID")) + .setCreationDate(resultSet.getDate("creationDate")) + .setCreator(resultSet.getString("creator")) + .setModificationDate(resultSet.getDate("modificationDate")) + .setModifiedBy(resultSet.getString("modifiedBy")) + .setOwner(resultSet.getString("owner")) + .setNeedsWelcomeOptInForWp(resultSet.getBoolean("needsWelcomeOptInForWP")) + .setCallingNumber(resultSet.getString("callingNumber")) + .setLanguageCode(resultSet.getString("languageCode")) + .setCircleName(resultSet.getString("circleName")) + .setStateId(resultSet.getLong("stateID")) + .build(); + + } + + @Override public List findActiveSubscriptionsForDayWP(final DayOfTheWeek dow, final long offset, final int rowCount, final Date date) { diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionTimeSlotServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionTimeSlotServiceImpl.java index 857900071..63361f79d 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionTimeSlotServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionTimeSlotServiceImpl.java @@ -48,7 +48,6 @@ public List findTimeSlotsForSubscriptionsById(final List> queryExecution = new SqlQueryExecution>() { @@ -77,14 +76,14 @@ public List execute(Query query) { List subscriptionTimeSlots = new ArrayList<>(); for (Object[] row : rawResults) { - SubscriptionTimeSlot timeSlot = new SubscriptionTimeSlot(); + SubscriptionTimeSlot timeSlot = new SubscriptionTimeSlot(); - timeSlot.setSubscription_id(((Number) row[0]).longValue()); - timeSlot.setSubscriptionId((String) row[1]); - timeSlot.setTimeStamp1((Integer) row[2]); - timeSlot.setTimeStamp2((Integer) row[3]); - timeSlot.setTimeStamp3((Integer) row[4]); - subscriptionTimeSlots.add(timeSlot); + timeSlot.setSubscription_id(((Number) row[0]).longValue()); + timeSlot.setSubscriptionId((String) row[1]); + timeSlot.setTimeStamp1((Integer) row[2]); + timeSlot.setTimeStamp2((Integer) row[3]); + timeSlot.setTimeStamp3((Integer) row[4]); + subscriptionTimeSlots.add(timeSlot); } if(subscriptionIds.isEmpty()) { LOGGER.warn("No subscriptionTimeSlots found for fresh calls subscriptionIds:"); @@ -93,4 +92,54 @@ public List execute(Query query) { return subscriptionTimeSlots; } + + + @Override + public List findAllTimeSlots() { + String query = "SELECT ts.subscription_id, ts.subscriptionId, ts.timeStamp1, ts.timeStamp2, ts.timeStamp3 " + + "FROM nms_subscriptions_time_slot ts " + + "LEFT JOIN nms_subscriptions s ON ts.subscription_id = s.id;"; + + LOGGER.debug("Executing SQL query: {}", query); + + SqlQueryExecution> queryExecution = new SqlQueryExecution>() { + + @Override + public String getSqlQuery() { + return query; + } + + @Override + public List execute(Query query) { + ForwardQueryResult fqr = (ForwardQueryResult) query.execute(); + List results = new ArrayList<>(); + + if (fqr != null && !fqr.isEmpty()) { + for (Object result : fqr) { + results.add((Object[]) result); + } + } + return results; + } + }; + + List rawResults = subscriptionTimeSlotDataService.executeSQLQuery(queryExecution); + List subscriptionTimeSlots = new ArrayList<>(); + + for (Object[] row : rawResults) { + SubscriptionTimeSlot timeSlot = new SubscriptionTimeSlot(); + timeSlot.setSubscription_id(((Number) row[0]).longValue()); + timeSlot.setSubscriptionId((String) row[1]); + timeSlot.setTimeStamp1((Integer) row[2]); + timeSlot.setTimeStamp2((Integer) row[3]); + timeSlot.setTimeStamp3((Integer) row[4]); + subscriptionTimeSlots.add(timeSlot); + } + + if (subscriptionTimeSlots.isEmpty()) { + LOGGER.warn("No time slots found."); + } + + return subscriptionTimeSlots; + } } diff --git a/kilkari/src/main/resources/kilkari.properties b/kilkari/src/main/resources/kilkari.properties index 4e81f6c75..b3f0dfa12 100644 --- a/kilkari/src/main/resources/kilkari.properties +++ b/kilkari/src/main/resources/kilkari.properties @@ -13,4 +13,8 @@ kilkari.accept_new_subscription_for_blocked_msisdn=true kilkari.chunk.size=1000 kilkari.thread.size=10000 -kilkari.highPriority.blockId=3257999, 3258000, 3258001, 3258002, 3258003, 3258004, 3258005, 3258006, 3258007, 3258008, 3258009, 3258010, 3258011, 3258012, 3258013, 3258014, 3258015, 3258016, 3258017, 3258018, 3258019, 3258020, 3258021, 3258022, 3258023, 3258024, 3258025, 3258026, 3258027, 3258028, 3258029, 3258030, 3258031, 3258032, 3258033, 3258034, 3258035, 3258036, 3258037, 3258038, 3258039, 3258040, 3258041, 3258042, 3258043, 3258044, 3258045, 3258046, 3258047, 3258048, 3258049, 3258050, 3258051, 3258052, 3258053, 3258054, 3258055, 3258056, 3258057, 3258058, 3258059, 3258060, 3258061, 3258062, 3258063, 3258064, 3258065, 3258066, 3258067, 3258068, 3258069, 3258070, 3258071, 3258072, 3258073, 3258074, 3258075, 3258076, 3258077, 3258078, 3258079, 3258080, 3258081, 3258082, 3258083, 3258084, 3258085, 3258086, 3258087, 3258088, 3258089, 3258090, 3258091, 3258092, 3258093, 3258094, 3258095, 3258096, 3258097, 3258098, 3258099, 3258100, 3258101, 3258102, 3258103, 3258104, 3258105, 3258106, 3258107, 3258108, 3258109, 3258110, 3258111, 3258112, 3258113, 3258114, 3258115, 3258116, 3258117, 3258118, 3258119, 3258120, 3258121, 3258122, 3258123, 3258124, 3258125, 3258126, 3258127, 3258128, 3258129, 3258130, 3258131, 3258132, 3258133, 3258134, 3258135, 3258136, 3258137, 3258138, 3258139, 3258140, 3258141, 3258142, 3258143, 3258144, 3258145, 3258146, 3258147, 3258148, 3258149, 3258150, 3258151, 3258152, 3258153, 3258154, 3258155, 3258156, 3258157, 3258158, 3258159, 3258160, 3258161, 3258162, 3258163, 3258164, 3258165, 3258166, 3258167, 3258168, 3258169, 3258170, 3258171, 3258172, 3258173, 3258174, 3258175, 3258176, 3258177, 3258178, 3258179, 3258180, 3257859, 3257860, 3257861, 3257862, 3257863, 3257864, 3257865, 3257866, 3257867, 3257868, 3257869, 3257870, 3257871, 3257872, 3257873, 3257874, 3257875, 3257876, 3122, 3139, 3144, 1669, 3027, 3240, 3251, 3252, 3266, 3322, 3331, 3340 \ No newline at end of file +kilkari.highPriority.blockId=3257999, 3258000, 3258001, 3258002, 3258003, 3258004, 3258005, 3258006, 3258007, 3258008, 3258009, 3258010, 3258011, 3258012, 3258013, 3258014, 3258015, 3258016, 3258017, 3258018, 3258019, 3258020, 3258021, 3258022, 3258023, 3258024, 3258025, 3258026, 3258027, 3258028, 3258029, 3258030, 3258031, 3258032, 3258033, 3258034, 3258035, 3258036, 3258037, 3258038, 3258039, 3258040, 3258041, 3258042, 3258043, 3258044, 3258045, 3258046, 3258047, 3258048, 3258049, 3258050, 3258051, 3258052, 3258053, 3258054, 3258055, 3258056, 3258057, 3258058, 3258059, 3258060, 3258061, 3258062, 3258063, 3258064, 3258065, 3258066, 3258067, 3258068, 3258069, 3258070, 3258071, 3258072, 3258073, 3258074, 3258075, 3258076, 3258077, 3258078, 3258079, 3258080, 3258081, 3258082, 3258083, 3258084, 3258085, 3258086, 3258087, 3258088, 3258089, 3258090, 3258091, 3258092, 3258093, 3258094, 3258095, 3258096, 3258097, 3258098, 3258099, 3258100, 3258101, 3258102, 3258103, 3258104, 3258105, 3258106, 3258107, 3258108, 3258109, 3258110, 3258111, 3258112, 3258113, 3258114, 3258115, 3258116, 3258117, 3258118, 3258119, 3258120, 3258121, 3258122, 3258123, 3258124, 3258125, 3258126, 3258127, 3258128, 3258129, 3258130, 3258131, 3258132, 3258133, 3258134, 3258135, 3258136, 3258137, 3258138, 3258139, 3258140, 3258141, 3258142, 3258143, 3258144, 3258145, 3258146, 3258147, 3258148, 3258149, 3258150, 3258151, 3258152, 3258153, 3258154, 3258155, 3258156, 3258157, 3258158, 3258159, 3258160, 3258161, 3258162, 3258163, 3258164, 3258165, 3258166, 3258167, 3258168, 3258169, 3258170, 3258171, 3258172, 3258173, 3258174, 3258175, 3258176, 3258177, 3258178, 3258179, 3258180, 3257859, 3257860, 3257861, 3257862, 3257863, 3257864, 3257865, 3257866, 3257867, 3257868, 3257869, 3257870, 3257871, 3257872, 3257873, 3257874, 3257875, 3257876, 3122, 3139, 3144, 1669, 3027, 3240, 3251, 3252, 3266, 3322, 3331, 3340 + +db.url= +db.user= +db.password= \ No newline at end of file diff --git a/testing/src/test/java/org/motechproject/nms/testing/it/kilkari/SubscriptionServiceBundleIT.java b/testing/src/test/java/org/motechproject/nms/testing/it/kilkari/SubscriptionServiceBundleIT.java index 653dac34f..c77b6f75f 100644 --- a/testing/src/test/java/org/motechproject/nms/testing/it/kilkari/SubscriptionServiceBundleIT.java +++ b/testing/src/test/java/org/motechproject/nms/testing/it/kilkari/SubscriptionServiceBundleIT.java @@ -12,6 +12,7 @@ import org.motechproject.event.MotechEvent; import org.motechproject.mds.ex.JdoListenerInvocationException; import org.motechproject.nms.kilkari.domain.*; +import org.motechproject.nms.kilkari.dto.SubscriptionDto; import org.motechproject.nms.kilkari.repository.CallRetryDataService; import org.motechproject.nms.kilkari.repository.InboxCallDataDataService; import org.motechproject.nms.kilkari.repository.InboxCallDetailRecordDataService; @@ -763,10 +764,10 @@ public void testActiveSubscriptionsForDay() { subscription.setStartDate(startDate); subscriptionDataService.create(subscription); - List subscriptions = subscriptionService.findActiveSubscriptionsForDay(startDay, 0, 10); + List subscriptions = subscriptionService.findActiveSubscriptionsForDay(startDay, 0, 10); assertTrue(subscriptions.size() > 0); - for (Subscription s : subscriptions) { - if (s.getSubscriber().getCallingNumber() == 1111111111L) { + for (SubscriptionDto s : subscriptions) { + if (Long.parseLong(s.getCallingNumber()) == 1111111111L) { return; } }