Merge "Refactor RequestsDatabase method"
diff --git a/mso-api-handlers/mso-requests-db/src/main/java/org/openecomp/mso/requestsdb/RequestsDatabase.java b/mso-api-handlers/mso-requests-db/src/main/java/org/openecomp/mso/requestsdb/RequestsDatabase.java
index e32c456..3824df0 100644
--- a/mso-api-handlers/mso-requests-db/src/main/java/org/openecomp/mso/requestsdb/RequestsDatabase.java
+++ b/mso-api-handlers/mso-requests-db/src/main/java/org/openecomp/mso/requestsdb/RequestsDatabase.java
@@ -22,23 +22,20 @@
 
 import java.sql.Timestamp;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.http.HttpStatus;
 import org.hibernate.Criteria;
 import org.hibernate.Query;
 import org.hibernate.Session;
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Restrictions;
-import org.hibernate.persister.entity.AbstractEntityPersister;
 import org.openecomp.mso.db.HibernateUtils;
-import org.openecomp.mso.requestsdb.HibernateUtilsRequestsDb;
 import org.openecomp.mso.logger.MsoLogger;
-import org.openecomp.mso.logger.MessageEnum;
 
 public class RequestsDatabase {
 
@@ -67,6 +64,26 @@
     protected static final String         REQUEST_ID                 = "requestId";
     protected static MockRequestsDatabase mockDB                     = null;
 
+    protected static enum Scope {
+        SERVICE("service", SERVICE_INSTANCE_NAME, SERVICE_INSTANCE_ID, "serviceInstanceId"),
+        VNF_INSTANCE("vnf", VNF_INSTANCE_NAME, VNF_INSTANCE_ID, "vnfInstanceId"),
+        VOLUME_GROUP("volumeGroup", VOLUME_GROUP_INSTANCE_NAME, VOLUME_GROUP_INSTANCE_ID, "volumeGroupInstanceId"),
+        VFMODULE("vfModule", VFMODULE_INSTANCE_NAME, VFMODULE_INSTANCE_ID, "vfModuleInstanceId"),
+        NETWORK("network", NETWORK_INSTANCE_NAME, NETWORK_INSTANCE_ID, "networkInstanceId");
+
+        public final String type;
+        public final String nameColumn;
+        public final String idColumn;
+        public final String idMapKey;
+
+        private Scope(String type, String nameColumn, String idColumn, String idMapKey) {
+            this.type = type;
+            this.nameColumn = nameColumn;
+            this.idColumn = idColumn;
+            this.idMapKey = idMapKey;
+        }
+    }
+
     /**
      * Avoids creating an instance of this utility class.
      */
@@ -217,48 +234,24 @@
         return ar;
     }
     
-    public static InfraActiveRequests checkInstanceNameDuplicate (HashMap<String,String> instanceIdMap, String instanceName, String requestScope) {
+    public static InfraActiveRequests checkInstanceNameDuplicate (Map<String,String> instanceIdMap, String instanceName, String requestScope) {
 
         List <Criterion> criteria = new LinkedList <> ();
        
-        if(instanceName != null && !instanceName.equals("")) {
-        	
-        	if(requestScope.equals("service")){
-        		criteria.add (Restrictions.eq (SERVICE_INSTANCE_NAME, instanceName));
-        	} else if(requestScope.equals("vnf")){
-        		criteria.add (Restrictions.eq (VNF_INSTANCE_NAME, instanceName));
-        	} else if(requestScope.equals("volumeGroup")){
-        		criteria.add (Restrictions.eq (VOLUME_GROUP_INSTANCE_NAME, instanceName));
-        	} else if(requestScope.equals("vfModule")){
-        		criteria.add (Restrictions.eq (VFMODULE_INSTANCE_NAME, instanceName));
-        	} else if(requestScope.equals("network")){
-        		criteria.add (Restrictions.eq (NETWORK_INSTANCE_NAME, instanceName));
-        	}
-        
-        } else {
-            if(instanceIdMap != null){
-            	if(requestScope.equals("service") && instanceIdMap.get("serviceInstanceId") != null){
-            		criteria.add (Restrictions.eq (SERVICE_INSTANCE_ID, instanceIdMap.get("serviceInstanceId")));
-             	}
-            
-            	if(requestScope.equals("vnf") && instanceIdMap.get("vnfInstanceId") != null){
-            		criteria.add (Restrictions.eq (VNF_INSTANCE_ID, instanceIdMap.get("vnfInstanceId")));
-             	}
-            
-            	if(requestScope.equals("vfModule") && instanceIdMap.get("vfModuleInstanceId") != null){
-            		criteria.add (Restrictions.eq (VFMODULE_INSTANCE_ID, instanceIdMap.get("vfModuleInstanceId")));
-             	}
-            
-            	if(requestScope.equals("volumeGroup") && instanceIdMap.get("volumeGroupInstanceId") != null){
-            		criteria.add (Restrictions.eq (VOLUME_GROUP_INSTANCE_ID, instanceIdMap.get("volumeGroupInstanceId")));
-             	}
-            
-            	if(requestScope.equals("network") && instanceIdMap.get("networkInstanceId") != null){
-            		criteria.add (Restrictions.eq (NETWORK_INSTANCE_ID, instanceIdMap.get("networkInstanceId")));
-            	}
-            }
+        if (instanceName != null && !instanceName.equals("")) {
+
+            Arrays.stream(Scope.values()) //
+                    .filter(scope -> scope.type.equals(requestScope)) //
+                    .forEach(scope -> criteria.add(Restrictions.eq(scope.nameColumn, instanceName)));
+
+        } else if (instanceIdMap != null) {
+
+            Arrays.stream(Scope.values()) //
+                    .filter(scope -> scope.type.equals(requestScope) && instanceIdMap.get(scope.idMapKey) != null) //
+                    .forEach(scope -> criteria.add(Restrictions.eq(scope.idColumn, instanceIdMap.get(scope.idMapKey))));
+
         }
-        
+
         criteria.add (Restrictions.in ("requestStatus", new String[] { "PENDING", "IN_PROGRESS", "TIMEOUT" }));
         
         Order order = Order.desc (START_TIME);