class InfraActiveRequestsRepositoryImpl implements InfraActiveRequestsRepositoryCustom {

    @Override
    public InfraActiveRequests checkInstanceNameDuplicate(final Map<String, String> instanceIdMap,
            final String instanceName, final String requestScope) {

        final List<Predicate> predicates = new LinkedList<>();
        final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        final CriteriaQuery<InfraActiveRequests> crit = cb.createQuery(InfraActiveRequests.class);
        final Root<InfraActiveRequests> tableRoot = crit.from(InfraActiveRequests.class);
        InfraActiveRequests infraActiveRequests = null;

        if (instanceName != null && !instanceName.equals("")) {

            if ("service".equals(requestScope)) {
                predicates.add(cb.equal(tableRoot.get(SERVICE_INSTANCE_NAME), instanceName));
            } else if ("vnf".equals(requestScope)) {
                predicates.add(cb.equal(tableRoot.get(VNF_INSTANCE_NAME), instanceName));
            } else if ("volumeGroup".equals(requestScope)) {
                predicates.add(cb.equal(tableRoot.get(VOLUME_GROUP_INSTANCE_NAME), instanceName));
            } else if ("vfModule".equals(requestScope)) {
                predicates.add(cb.equal(tableRoot.get(VFMODULE_INSTANCE_NAME), instanceName));
            } else if ("network".equals(requestScope)) {
                predicates.add(cb.equal(tableRoot.get(NETWORK_INSTANCE_NAME), instanceName));
            } else if (requestScope.equals("configuration")) {
                predicates.add(cb.equal(tableRoot.get(CONFIGURATION_INSTANCE_NAME), instanceName));
            } else if (requestScope.equals(OPENV)) {
                predicates.add(cb.equal(tableRoot.get(OPERATIONAL_ENV_NAME), instanceName));
            }

        } else {
            if (instanceIdMap != null) {
                if ("service".equals(requestScope) && instanceIdMap.get(SERVICE_INSTANCE_ID) != null) {
                    predicates
                            .add(cb.equal(tableRoot.get(SERVICE_INSTANCE_ID), instanceIdMap.get("serviceInstanceId")));
                }

                if ("vnf".equals(requestScope) && instanceIdMap.get("vnfInstanceId") != null) {
                    predicates.add(cb.equal(tableRoot.get(VNF_INSTANCE_ID), instanceIdMap.get("vnfInstanceId")));
                }

                if ("vfModule".equals(requestScope) && instanceIdMap.get("vfModuleInstanceId") != null) {
                    predicates.add(
                            cb.equal(tableRoot.get(VFMODULE_INSTANCE_ID), instanceIdMap.get("vfModuleInstanceId")));
                }

                if ("volumeGroup".equals(requestScope) && instanceIdMap.get("volumeGroupInstanceId") != null) {
                    predicates.add(cb.equal(tableRoot.get(VOLUME_GROUP_INSTANCE_ID),
                            instanceIdMap.get("volumeGroupInstanceId")));
                }

                if ("network".equals(requestScope) && instanceIdMap.get("networkInstanceId") != null) {
                    predicates
                            .add(cb.equal(tableRoot.get(NETWORK_INSTANCE_ID), instanceIdMap.get("networkInstanceId")));
                }

                if (requestScope.equals("configuration") && instanceIdMap.get("configurationInstanceId") != null) {
                    predicates.add(cb.equal(tableRoot.get(CONFIGURATION_INSTANCE_ID),
                            instanceIdMap.get("configurationInstanceId")));
                }

                if (requestScope.equals(OPENV) && instanceIdMap.get("operationalEnvironmentId") != null) {
                    predicates.add(
                            cb.equal(tableRoot.get(OPERATIONAL_ENV_ID), instanceIdMap.get("operationalEnvironmentId")));
                }
            }
        }
        if (!predicates.isEmpty()) {
            predicates.add(tableRoot.get(REQUEST_STATUS)
                    .in(Arrays.asList("PENDING", "IN_PROGRESS", "TIMEOUT", "PENDING_MANUAL_TASK")));

            final Order order = cb.desc(tableRoot.get(START_TIME));

            final List<InfraActiveRequests> dupList = executeInfraQuery(crit, predicates, order);

            if (dupList != null && !dupList.isEmpty()) {
                infraActiveRequests = dupList.get(0);
            }
        }

        return infraActiveRequests;
    }

}