class ComplianceRepositoryImpl implements ComplianceRepository, Constants {

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> updateKernelVersion(final KernelVersion kernelVersion) {
        Map<String, Object> response = Maps.newHashMap();
        RhnSystemDetails systemDetails = null;
        Map<String, String> kernelCriteriaMapCurrent = buildCriteriaMap(getCurrentQuarterCriteriaKey());
        Set<String> kernelCriteriaMapkeysCurrent = kernelCriteriaMapCurrent.keySet();
        try {
            if ((kernelVersion.getInstanceId() != null && !"".equals(kernelVersion.getInstanceId()))
                    && (kernelVersion.getKernelVersionId() != null && !"".equals(kernelVersion.getKernelVersionId()))) {
                Boolean isKernelComplaint = isCompliant(kernelVersion.getKernelVersionId(),
                        kernelCriteriaMapkeysCurrent, kernelCriteriaMapCurrent);
                systemDetails = rhnSystemDetailsRepository.findRhnSystemDetailsByInstanceId(kernelVersion
                        .getInstanceId());
                if (systemDetails != null) {
                    systemDetails.setKernelVersion(kernelVersion.getKernelVersionId());
                    systemDetails.setIsKernelCompliant(isKernelComplaint);
                    systemDetails.setModifiedDate(new Date());
                    systemDetails.setUserName("webservice");
                    rhnSystemDetailsRepository.save(systemDetails);
                    response.put(MESSAGE_KEY, "Kernel Version Updated Successfully!!!");
                    response.put(STATUS, true);
                    return response;
                } else {
                    String instanceIdSearchQuery = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"latest\":{\"value\":\"true\"}}},{\"term\":{\"instanceid.keyword\":{\"value\":\""
                            + kernelVersion.getInstanceId() + "\"}}}]}}}";
                    StringBuilder urlToQueryinstanceId = new StringBuilder(esUrl).append("/").append("aws_ec2/ec2")
                            .append("/").append(UNDERSCORE_COUNT);
                    String instanceDetails = PacHttpUtils.doHttpPost(urlToQueryinstanceId.toString(),
                            instanceIdSearchQuery);
                    Gson serializer = new GsonBuilder().create();
                    Map<String, Object> instanceDetailsMap = (Map<String, Object>) serializer.fromJson(instanceDetails,
                            Object.class);
                    double count = (double) instanceDetailsMap.get(COUNT);
                    if (count >= 1) {
                        systemDetails = new RhnSystemDetails();
                        systemDetails.setGroupId(0L);
                        systemDetails.setCompanyId(0L);
                        systemDetails.setUserId(0L);
                        systemDetails.setUserName("webservice");
                        systemDetails.setCreateDate(new Date());
                        systemDetails.setModifiedDate(new Date());
                        systemDetails.setSystemId(0);
                        systemDetails.setKernelVersion(kernelVersion.getKernelVersionId());
                        systemDetails.setInstanceId(kernelVersion.getInstanceId());
                        systemDetails.setLastCheckedIn(null);
                        systemDetails.setOs("");
                        systemDetails.setIsKernelCompliant(isKernelComplaint);
                        rhnSystemDetailsRepository.save(systemDetails);
                        response.put(MESSAGE_KEY, "Successfully added new Kernel Version for the InstanceId!!!");
                        response.put(STATUS, true);
                        return response;
                    } else {
                        response.put(MESSAGE_KEY, "InstanceId not Found!!!");
                        response.put(STATUS, false);
                        return response;
                    }
                }
            } else {
                response.put(MESSAGE_KEY, "Kernel Version or InstanceId cannot be null!!!");
                response.put(STATUS, false);

                return response;
            }
        } catch (Exception exception) {
            logger.error(exception);
            response.put(MESSAGE_KEY, "Unexpected Error Occurred!!!");
            response.put(STATUS, false);
            return response;
        }
    }

}