Exemplo n.º 1
0
        public IWorkflowContextController Init(CodeActivityContext codeActivityContext)
        {
            bool dictionaryCreate          = false;
            bool orgServiceCreate          = false;
            bool tracingServiceCreate      = false;
            bool currentUserCreate         = false;
            bool currentOrganizationCreate = false;
            bool orgServiceFactoryCreate   = false;

            IWorkflowContext            wfContext = codeActivityContext.GetExtension <IWorkflowContext>();
            IOrganizationServiceFactory sFactory  = codeActivityContext.GetExtension <IOrganizationServiceFactory>();
            IWorkflowContext            workContext;

            workContext = wfContext;

            while (workContext.ParentContext != null)
            {
                workContext = workContext.ParentContext;
            }


            //获取当前用户
            var currentUser = ContextContainer.GetValue <ICurrentUserInfoContext>(ContextTypes.CurrentUser);
            //获取当前组织编号
            var currentOrganization = ContextContainer.GetValue <Guid>(ContextTypes.CurrentOrganization);
            //获取dictorany
            var dict = ContextContainer.GetValue <Dictionary <string, object> >(ContextTypes.Dictionary);
            //获取orgService
            var orgService       = ContextContainer.GetValue <IOrganizationService>(ContextTypes.OrgService);
            var isAutoOrgService = ContextContainer.IsAuto <IOrganizationService>(ContextTypes.OrgService.ToString());
            //获取orgServiceFactory
            var orgServiceFactory = ContextContainer.GetValue <IOrganizationServiceFactory>(ContextTypes.OrgServiceFactory);
            //获取tracingService
            var tracingService = ContextContainer.GetValue <ITracingService>(ContextTypes.TracingService);



            //新建所有上下文
            if (currentUser == null || ContextContainer.IsAuto <ICurrentUserInfoContext>(ContextTypes.CurrentUser))
            {
                currentUserCreate = true;
                ContextContainer.SetValue <ICurrentUserInfoContext>(ContextTypes.CurrentUser, CurrentUserInfoContextFactory.Get().Create(workContext.InitiatingUserId));
            }


            if (currentOrganization == Guid.Empty || ContextContainer.IsAuto <Guid>(ContextTypes.CurrentOrganization))
            {
                currentOrganizationCreate = true;
                ContextContainer.SetValue <Guid>(ContextTypes.CurrentOrganization, workContext.OrganizationId);
            }

            if (tracingService == null || ContextContainer.IsAuto <ITracingService>(ContextTypes.TracingService))
            {
                tracingServiceCreate = true;
                ContextContainer.SetValue <ITracingService>(ContextTypes.TracingService, codeActivityContext.GetExtension <ITracingService>());
            }


            if (dict == null || ContextContainer.IsAuto <Dictionary <string, object> >(ContextTypes.Dictionary))
            {
                dictionaryCreate = true;
                if (workContext.SharedVariables.Contains("dictionary"))
                {
                    var dictionary = BinarySerializerHelper.Deserialize <Dictionary <string, object> >(workContext.SharedVariables["dictionary"].ToString());

                    ContextContainer.SetValue <Dictionary <string, object> >(ContextTypes.Dictionary, dictionary);

                    dict = dictionary;
                }
                else
                {
                    dict = new Dictionary <string, object>();
                    ContextContainer.SetValue <Dictionary <string, object> >(ContextTypes.Dictionary, dict);
                }
            }


            lock (dict)
            {
                dict[ContextDictionaryKeys.OrgName] = workContext.OrganizationName;
            }

            if (orgService == null || isAutoOrgService)
            {
                orgServiceCreate = true;
                //获取临时OrgService
                var tempOrgService          = sFactory.CreateOrganizationService(workContext.InitiatingUserId);
                var tempOrgServiceDiposable = tempOrgService as IDisposable;

                if (tempOrgServiceDiposable != null)
                {
                    using (tempOrgServiceDiposable)
                    {
                        ContextContainer.SetValue <IOrganizationService>(ContextTypes.OrgService, tempOrgService);
                        EntityReference administratorId = _configurationService.GetAdministratorID();
                        orgService = sFactory.CreateOrganizationService(administratorId.Id);

                        ContextContainer.SetValue <IOrganizationService>(ContextTypes.OrgService, orgService);
                    }
                }
                else
                {
                    ContextContainer.SetValue <IOrganizationService>(ContextTypes.OrgService, tempOrgService);
                    EntityReference administratorId = _configurationService.GetAdministratorID();
                    orgService = sFactory.CreateOrganizationService(administratorId.Id);

                    ContextContainer.SetValue <IOrganizationService>(ContextTypes.OrgService, orgService);
                }
            }



            if (orgServiceFactory == null || ContextContainer.IsAuto <IOrganizationServiceFactory>(ContextTypes.OrgServiceFactory))
            {
                orgServiceFactoryCreate = true;
                ContextContainer.SetValue <IOrganizationServiceFactory>(ContextTypes.OrgServiceFactory, sFactory);
            }


            return(new WorkflowContextController(workContext, dictionaryCreate, orgServiceCreate, tracingServiceCreate, currentUserCreate, currentOrganizationCreate, orgServiceFactoryCreate));
        }