/*==================================================================================================================================
        * Public Methods
        *=================================================================================================================================*/

        #region LIST
        //----------------------------------------------------------------------------------------------------------------------------------
        // LIST
        //----------------------------------------------------------------------------------------------------------------------------------
        public questStatus GetParamerListInfo(ParameterListViewModel viewModel, out ParameterListViewModel parameterListViewModel)
        {
            // Initialize
            questStatus status = null;

            parameterListViewModel = null;
            DatabaseId databaseId = new DatabaseId(viewModel.DatabaseId);


            // Read the database
            DatabaseBaseViewModel databaseBaseViewModel = null;
            DatabaseBaseModeler   databaseBaseModeler   = new DatabaseBaseModeler(this.HttpRequestBase, this.UserSession);

            status = databaseBaseModeler.GetDatabase(databaseId, out databaseBaseViewModel);
            if (!questStatusDef.IsSuccess(status))
            {
                return(status);
            }

            // Read the stored procedure
            StoredProceduresListViewModel storedProceduresListViewModel = null;
            StoredProceduresListModeler   storedProceduresListModeler   = new StoredProceduresListModeler(this.HttpRequestBase, this.UserSession);

            status = storedProceduresListModeler.List(databaseId, out storedProceduresListViewModel);
            if (!questStatusDef.IsSuccess(status))
            {
                return(status);
            }


            // Get stored procedure info
            StoredProcedureId storedProcedureId = new StoredProcedureId(viewModel.Id);


            // Read the stored procedure info
            StoredProcedure     storedProcedure     = null;
            StoredProceduresMgr storedProceduresMgr = new StoredProceduresMgr(this.UserSession);

            status = storedProceduresMgr.Read(storedProcedureId, out storedProcedure);
            if (!questStatusDef.IsSuccess(status))
            {
                return(status);
            }

            // Get stored procedure parameters
            viewModel.StoredProcedureId = viewModel.Id;
            ParameterListViewModel _parameterListViewModel = null;

            status = List(viewModel, out _parameterListViewModel);
            if (!questStatusDef.IsSuccess(status))
            {
                return(status);
            }


            // Sort by display order
            // NOTE: ORDER IN DATABASE -SHOULD- BE ORDER IN SPROC.


            // Transfer model.
            parameterListViewModel            = new ParameterListViewModel(this.UserSession, viewModel);
            parameterListViewModel.DatabaseId = databaseId.Id;
            parameterListViewModel.Database   = databaseBaseViewModel;

            foreach (StoredProcedureLineItemViewModel storedProcedureLineItemViewModel in storedProceduresListViewModel.Items)
            {
                OptionValuePair opv = new OptionValuePair();
                opv.Id    = storedProcedureLineItemViewModel.Id.ToString();
                opv.Label = storedProcedureLineItemViewModel.Name;
                parameterListViewModel.StoredProcedureOptions.Add(opv);
            }

            parameterListViewModel.ParentEntityType  = viewModel.ParentEntityType;
            parameterListViewModel.StoredProcedureId = viewModel.StoredProcedureId;

            StoredProcedureViewModel storedProcedureViewModel = new StoredProcedureViewModel();

            BufferMgr.TransferBuffer(storedProcedure, storedProcedureViewModel);
            parameterListViewModel.StoredProcedure = storedProcedureViewModel;
            parameterListViewModel.Items           = _parameterListViewModel.Items;

            return(new questStatus(Severity.Success));
        }
        /*==================================================================================================================================
        * Public Methods
        *=================================================================================================================================*/

        #region LIST
        //----------------------------------------------------------------------------------------------------------------------------------
        // LIST
        //----------------------------------------------------------------------------------------------------------------------------------
        public questStatus List(ColumnListViewModel viewModel, out ColumnListViewModel columnListViewModel)
        {
            // Initialize
            questStatus status = null;

            columnListViewModel = null;


            // Read the database
            DatabaseId            databaseId            = new DatabaseId(viewModel.DatabaseId);
            DatabaseBaseViewModel databaseBaseViewModel = null;
            DatabaseBaseModeler   databaseBaseModeler   = new DatabaseBaseModeler(this.HttpRequestBase, this.UserSession);

            status = databaseBaseModeler.GetDatabase(databaseId, out databaseBaseViewModel);
            if (!questStatusDef.IsSuccess(status))
            {
                return(status);
            }

            // Get columns according to parent entity type
            ColumnsMgr    columnsMgr = new ColumnsMgr(this.UserSession);
            List <Column> columnList = null;
            Table         table      = null;
            View          view       = null;

            if (String.Equals(viewModel.ParentEntityType, "table", StringComparison.InvariantCultureIgnoreCase))
            {
                // Get table columns
                TableId tableId = new TableId(viewModel.Id);
                status = columnsMgr.Read(tableId, out columnList);
                if (!questStatusDef.IsSuccess(status))
                {
                    return(status);
                }

                // Read the table info
                TablesMgr tablesMgr = new TablesMgr(this.UserSession);
                status = tablesMgr.Read(tableId, out table);
                if (!questStatusDef.IsSuccess(status))
                {
                    return(status);
                }
            }
            else if (String.Equals(viewModel.ParentEntityType, "view", StringComparison.InvariantCultureIgnoreCase))
            {
                // Get view columns
                ViewId viewId = new ViewId(viewModel.Id);
                status = columnsMgr.Read(viewId, out columnList);
                if (!questStatusDef.IsSuccess(status))
                {
                    return(status);
                }

                // Read the view info
                ViewsMgr viewsMgr = new ViewsMgr(this.UserSession);
                status = viewsMgr.Read(viewId, out view);
                if (!questStatusDef.IsSuccess(status))
                {
                    return(status);
                }
            }
            else
            {
                return(new questStatus(Severity.Error, String.Format("Invalid ParentEntityType: {0}", viewModel.ParentEntityType)));
            }


            // Sort by display order
            columnList.Sort(delegate(Column i1, Column i2) { return(i1.DisplayOrder.CompareTo(i2.DisplayOrder)); });


            // Transfer model.
            columnListViewModel                  = new ColumnListViewModel(this.UserSession, viewModel);
            columnListViewModel.DatabaseId       = databaseId.Id;
            columnListViewModel.Database         = databaseBaseViewModel;
            columnListViewModel.Id               = viewModel.Id;
            columnListViewModel.ParentEntityType = viewModel.ParentEntityType;
            if (table != null)
            {
                TableViewModel tableViewModel = new TableViewModel();
                BufferMgr.TransferBuffer(table, tableViewModel);
                columnListViewModel.Table = tableViewModel;
            }
            if (view != null)
            {
                ViewViewModel viewViewModel = new ViewViewModel();
                BufferMgr.TransferBuffer(view, viewViewModel);
                columnListViewModel.View = viewViewModel;
            }
            foreach (Column column in columnList)
            {
                ColumnLineItemViewModel columnLineItemViewModel = new ColumnLineItemViewModel();
                BufferMgr.TransferBuffer(column, columnLineItemViewModel);
                columnListViewModel.Items.Add(columnLineItemViewModel);
            }
            return(new questStatus(Severity.Success));
        }