We will now add the relational procedures between FolioModule and the new Project Type. This step is essential because the FolioModule has to know how to retrieve Folio Items of Project type.

Create a new class called Proc_FolioModule_Project, based on MSSQLRelationProceduresExtensionBase.

CopyC#
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Cartella.Classes;
using Cartella.Interfaces;

namespace Cartella.Extension.Sample
{
    public class Proc_FolioModule_Project : MSSQLRelationProceduresExtensionBase
    {
        public Proc_FolioModule_Project(MSSQLUserSpecificDataProceduresBase baseProc)
            : base(baseProc)
        {
        }

        public override Type OwnerType
        {
            get { return typeof(FolioModule); }
        }

        public override Type TargetType
        {
            get { return typeof(Project); }
        }

        public override bool SelectRelationsProcedure(string ownerID, out IFieldCollection[] targets)
        {
            return selectChildEntities(ownerID, out targets);
        }

        public override bool InsertRelationsProcedure(IField ownerIDField, List<IField> targetUniqueIDFields)
        {
            return insertChildEntities(ownerIDField, targetUniqueIDFields);
        }

        public override bool RemoveRelationsProcedure(IField ownerIDField, List<IField> targetUniqueIDFields)
        {
            return removeChildEntities(ownerIDField, targetUniqueIDFields);
        }

        #region Private Methods
        private bool removeChildEntities(IField ownerIDField, List<IField> targetUniqueIDFields)
        {
            return base.removeRelations<SqlParameter>(
                ownerIDField, targetUniqueIDFields,
                "@folioModuleID", "@folioItemID",
                "sp_RemoveFolioModuleToFolioItemRelation");
        }

        private bool insertChildEntities(IField ownerIDField, List<IField> targetUniqueIDFields)
        {
            return base.insertRelations<SqlParameter>(ownerIDField,
                targetUniqueIDFields,
                "@folioModuleID",
                "@folioItemID",
                "sp_AddModuleFolioItemRelation");
        }

        private bool selectChildEntities(string ownerID, out IFieldCollection[] targets)
        {
            string classPath = TargetType.FullName;

            DataProcedureParameters parms = new DataProcedureParameters();
            parms.AddParameter<SqlParameter, int>("@entityModuleID", ownerID);
            parms.AddParameter<SqlParameter, string>("@entityClassPath", classPath);

            var CurrentUserState = proceduresBase.CurrentUserState;

            if (CurrentUserState != null && CurrentUserState.CurrentUser != null)
                parms.AddParameter<SqlParameter, int>("@userID", CurrentUserState.CurrentUser.ID);
            else
                parms.AddParameter<SqlParameter, int>("@userID", 0);

            parms.AddParameter<SqlParameter, bool>("@showDeleted", false);
            parms.AddParameter<SqlParameter, bool>("@recursive", false);

            List<IFieldCollection> objs = proceduresBase.Select("sp_GetFolioItemsByModule", parms);
            targets = objs.ToArray();

            return true;
        } 
        #endregion
    }
}