package org.rabix.engine.service.impl;

import com.google.inject.Inject;
import org.rabix.engine.service.JobStatsRecordService;
import org.rabix.engine.store.lru.stats.JobStatsRecordCache;
import org.rabix.engine.store.model.JobStatsRecord;
import org.rabix.engine.store.repository.JobStatsRecordRepository;

import java.util.UUID;

public class JobStatsRecordServiceImpl implements JobStatsRecordService {

  private JobStatsRecordRepository jobStatsRecordRepository;

  private JobStatsRecordCache jobStatsRecordCache;

  @Inject
  public JobStatsRecordServiceImpl(JobStatsRecordRepository jobStatsRecordRepository,
                                   JobStatsRecordCache jobStatsRecordCache) {
    this.jobStatsRecordRepository = jobStatsRecordRepository;
    this.jobStatsRecordCache = jobStatsRecordCache;
  }

  public void create(JobStatsRecord jobStatsRecord) {
    jobStatsRecordRepository.insert(jobStatsRecord);
    jobStatsRecordCache.put(jobStatsRecord.getRootId(), jobStatsRecord);
  }

  public void update(JobStatsRecord jobStatsRecord) {
    jobStatsRecordRepository.update(jobStatsRecord);
    jobStatsRecordCache.put(jobStatsRecord.getRootId(), jobStatsRecord);
  }

  public JobStatsRecord find(UUID rootId) {
    JobStatsRecord jobStatsRecord = jobStatsRecordCache.get(rootId);
    if (jobStatsRecord == null) {
      jobStatsRecordRepository.get(rootId);
    }
    return jobStatsRecord;
  }

  @Override public JobStatsRecord findOrCreate(UUID rootId) {
    JobStatsRecord jobStatsRecord = find(rootId);
    if (jobStatsRecord == null) {
      jobStatsRecord = new JobStatsRecord(rootId, 0, 0, 0);
      create(jobStatsRecord);
    }
    return jobStatsRecord;
  }

  public void delete(UUID rootId) {
    jobStatsRecordRepository.delete(rootId);
    jobStatsRecordCache.remove(rootId);
  }

}

@@ -857,32 +857,47 @@
 atsRecord) %7B%0A   
+ int inserted =
  jobStatsRecordR
@@ -922,32 +922,58 @@
 obStatsRecord);%0A
+    if (inserted %3E 0) %7B%0A  
     jobStatsReco
@@ -1021,32 +1021,38 @@
 obStatsRecord);%0A
+    %7D%0A
   %7D%0A%0A  public vo
@@ -1089,32 +1089,46 @@
 atsRecord) %7B%0A   
+ int updated =
  jobStatsRecordR
@@ -1157,24 +1157,49 @@
 atsRecord);%0A
+    if (updated %3E 0) %7B%0A  
     jobStats
@@ -1255,24 +1255,30 @@
 atsRecord);%0A
+    %7D%0A
   %7D%0A%0A  publi

check for insterted/updated number of rows befor puting to cache