/// <summary>
        /// 
        /// </summary>
        /// <param name="qualifierReferenceID">the ID of the resource represented by the qualifier (null if not necessary);</param>
        /// <param name="type">one of the statically configured qualifierTypes registered with the Service Broker; the qualifier type is used to check type compatibility between function and qualifier in grants</param>
        /// <param name="name">A semantically meaningful string describing the purpose of the qualifier</param>
        /// <param name="parentQualifierID">the ID of a previously created Qualifier to serve as the parent of the Qualifier being added; the parentID locates the new Qualifier in the hierarchy; additional parents may be added by the AddQualifier-Parent() method</param>
        /// <returns>the ID of the new qualifier if it is successfully created, null otherwise</returns>
        public static int AddQualifier( int qualifierReferenceID, int type, string name, int parentQualifierID )
        {
            Qualifier q = new Qualifier();
            q.qualifierReferenceID = qualifierReferenceID;
            q.qualifierType = type;
            q.qualifierName = name;
            bool qualifierReferenceIDValid = false;

            int qualifierID = -1;
            ProcessAgentDB db = new ProcessAgentDB();
            ProcessAgent agent = null;
            //need to check whether the qualifierReferenceID for a particular qualifierType
            //actually exists before it is added to the table
            switch (type)
            {
                    // LabServerfo
                    // Replace with case for all processAgent types an use exits for the combination of ID & type
                case Qualifier.labServerQualifierTypeID:
                {
                    agent = db.GetProcessAgent(qualifierReferenceID);
                    if(agent != null && ((agent.type.Equals(ProcessAgentType.BATCH_LAB_SERVER))
                        ||(agent.type.Equals(ProcessAgentType.LAB_SERVER)))){
                            qualifierReferenceIDValid = true;
                    }
                    break;
                }

                    //LabClient

            case Qualifier.serviceBrokerQualifierTypeID:
                {
                    agent = db.GetProcessAgent(qualifierReferenceID);
                    if (agent != null && ((agent.type.Equals(ProcessAgentType.SERVICE_BROKER))
                          ||(agent.type.Equals(ProcessAgentType.BATCH_SERVICE_BROKER))
                          || (agent.type.Equals(ProcessAgentType.REMOTE_SERVICE_BROKER))))
                    {
                        qualifierReferenceIDValid = true;
                    }
                    break;
                }

            case Qualifier.labSchedulingQualifierTypeID:
                {
                    agent = db.GetProcessAgent(qualifierReferenceID);
                    if (agent != null && agent.type.Equals(ProcessAgentType.LAB_SCHEDULING_SERVER))
                    {
                        qualifierReferenceIDValid = true;
                    }
                    break;
                }
            case Qualifier.userSchedulingQualifierTypeID:
                {
                    agent = db.GetProcessAgent(qualifierReferenceID);
                    if (agent != null && agent.type.Equals(ProcessAgentType.SCHEDULING_SERVER))
                    {
                        qualifierReferenceIDValid = true;
                    }
                    break;
                }
            case Qualifier.storageServerQualifierTypeID:
                {
                    agent = db.GetProcessAgent(qualifierReferenceID);
                    if (agent != null && agent.type.Equals(ProcessAgentType.EXPERIMENT_STORAGE_SERVER))
                    {
                        qualifierReferenceIDValid = true;
                    }
                    break;
                }
            case Qualifier.labClientQualifierTypeID:
                {
                    int[] labClientIDs = InternalAdminDB.SelectLabClientIDs();
                    foreach (int labClientID in labClientIDs)
                    {
                        if (labClientID == qualifierReferenceID)
                        {
                            qualifierReferenceIDValid = true;
                            break;
                        }
                    }
                    break;
                }
                    //Group
                case Qualifier.groupQualifierTypeID:
                {
                    int[] groupIDs = InternalAdminDB.SelectGroupIDs();
                    foreach(int groupID in groupIDs)
                    {
                        if(groupID == qualifierReferenceID)
                        {
                            qualifierReferenceIDValid = true;
                            break;
                        }
                    }
                    break;
                }

                    //Experiment Collection
                case Qualifier.experimentCollectionQualifierTypeID:
                {
                    int[] groupIDs = InternalAdminDB.SelectGroupIDs();
                    foreach(int groupID in groupIDs)
                    {
                        if(groupID == qualifierReferenceID)
                        {
                            qualifierReferenceIDValid = true;
                            break;
                        }
                    }
                    break;
                }

                //Experiment
                case Qualifier.experimentQualifierTypeID:
                {
                    Criterion c = new Criterion("experiment_id","=",qualifierReferenceID.ToString());
                    if (InternalDataDB.SelectExperimentIDs(new Criterion[] { c }).Length > 0)
                        qualifierReferenceIDValid = true;
                    break;
                }

                // Resource Mapping
                case Qualifier.resourceMappingQualifierTypeID:
                {
                    BrokerDB brokerDb = new BrokerDB();
                    ResourceMapping mapping = brokerDb.GetResourceMapping(qualifierReferenceID);
                    if (mapping != null)

                            qualifierReferenceIDValid = true;
                            break;
                }
            }

            if(qualifierReferenceIDValid)
            {
                try
                {
                    qualifierID = InternalAuthorizationDB.InsertQualifier(q);

                    if(qualifierID != -1)
                    {
                        InternalAuthorizationDB.InsertQualifierHierarchy(qualifierID,parentQualifierID);
                    }
                }
                catch(Exception ex)
                {
                    throw;
                }
            }

            return qualifierID;
        }