コード例 #1
0
        /// <summary>
        /// Create organization type into specified application.
        /// </summary>
        /// <param name="applicationId"></param>
        /// <param name="orgType"></param>
        protected static void CreateOrganizationType(Guid applicationId, OrganizationTypeObject orgType)
        {
            Kit.NotNull(orgType, "orgType");

            using (MembershipDataContext ctx = DataContextFactory.Create<MembershipDataContext>())
            {
                OrganizationType organizationType = new OrganizationType
                {
                    ApplicationId = applicationId,
                    Domain = orgType.Domain,
                    Name = orgType.Name,
                    Description = orgType.Description,
                    Predefined = true,
                    LastUpdatedDate = DateTime.UtcNow
                };

                ctx.OrganizationTypes.InsertOnSubmit(organizationType);
                ctx.SubmitChanges();

                orgType.OrganizationTypeId = organizationType.OrganizationTypeId;
            }
        }
 partial void DeleteOrganizationType(OrganizationType instance);
 partial void InsertOrganizationType(OrganizationType instance);
 partial void UpdateOrganizationType(OrganizationType instance);
        /// <summary>
        /// Save organization type object.
        /// </summary>
        /// <param name="organizationTypeObject"></param>
        /// <exception cref="ValidationException">etc organization type name does exist.</exception>
        public void Save(OrganizationTypeObject organizationTypeObject)
        {
            Kit.NotNull(organizationTypeObject, "organizationTypeObject");
            Kit.NotNull(organizationTypeObject.Name, "organizationTypeObject.Name");
            if (!this.platformConfiguration.Domains.Select(d => d.Value).Contains(organizationTypeObject.Domain))
                throw new ArgumentException(string.Format(Resources.InvalidOrganizationTypeDomain, organizationTypeObject.Domain), "organizationTypeObject.Domain");

            Kit.NotNull(organizationTypeObject, "organizationTypeObject");
            Kit.NotNull(organizationTypeObject.Name, "organizationTypeObject.Name");
            if (!this.platformConfiguration.Domains.Select(d => d.Value).Contains(organizationTypeObject.Domain))
                throw new ArgumentException(string.Format(Resources.InvalidOrganizationTypeDomain, organizationTypeObject.Domain), "organizationTypeObject.Domain");

            try
            {
                using (TransactionScope ts = new TransactionScope())
                using (MembershipDataContext ctx = DataContextFactory.Create<MembershipDataContext>())
                {
                    OrganizationType organizationType = null;

                    DeleteStatus originalDeleteStatus = DeleteStatus.Deleted;
                    using (ValidationScope validationScope = new ValidationScope(true))
                    {
                        if (ctx.OrganizationTypes.Where(x => x.Name == organizationTypeObject.Name
                            && x.ApplicationId == this.authenticationContext.ApplicationId
                            && x.OrganizationTypeId != organizationTypeObject.OrganizationTypeId).Count() > 0)
                        {
                            validationScope.Error(Resources.ExistedOrganizationTypeName, organizationTypeObject.Name);
                        }

                        if (organizationTypeObject.OrganizationTypeId == Guid.Empty)
                        {
                            organizationType = new OrganizationType { ApplicationId = this.authenticationContext.ApplicationId };
                            ctx.OrganizationTypes.InsertOnSubmit(organizationType);
                        }
                        else
                        {
                            organizationType = ctx.OrganizationTypes.FirstOrDefault(orgType => orgType.OrganizationTypeId == organizationTypeObject.OrganizationTypeId);
                            if (organizationType == null)
                                validationScope.Error(Resources.InvalidOrganizationTypeID);

                            originalDeleteStatus = organizationType.DeleteStatus;
                        }
                    }

                    // set organization type properties.
                    organizationType.Domain = organizationTypeObject.Domain;
                    organizationType.Name = organizationTypeObject.Name;
                    organizationType.Description = organizationTypeObject.Description;
                    organizationType.LastUpdatedDate = DateTime.UtcNow;
                    organizationType.Predefined = organizationTypeObject.Predefined;
                    organizationType.DeleteStatus = organizationTypeObject.DeleteStatus;

                    // if disable an existed organization type
                    if (organizationTypeObject.OrganizationTypeId != Guid.Empty
                        && organizationTypeObject.DeleteStatus == DeleteStatus.Deleted
                        && organizationTypeObject.DeleteStatus != originalDeleteStatus)
                    {
                        // remove the cache copy for disabled organizations.
                        IEnumerable<Guid> disabledOrganizationIds = (from org in ctx.Organizations
                                                                     where org.ApplicationId == this.authenticationContext.ApplicationId
                                                                         && org.OrganizationTypeId == organizationTypeObject.OrganizationTypeId
                                                                         && org.Status != OrganizationStatus.Disabled
                                                                     select org.OrganizationId).ToList();

                        foreach (Guid disabledOrganizationId in disabledOrganizationIds)
                            base.RemoveCache(disabledOrganizationId);

                        // batch disable the organizations by sql command
                        string command = string.Format(CultureInfo.InvariantCulture, "UPDATE {0} SET Status={1} WHERE ApplicationId='{2}' AND OrganizationTypeId='{3}' AND Status<>{1}",
                            ctx.Mapping.GetTable(typeof(Organization)).TableName,
                            (int)OrganizationStatus.Disabled,
                            this.authenticationContext.ApplicationId,
                            organizationTypeObject.OrganizationTypeId);

                        ctx.ExecuteCommand(command);
                    }

                    ctx.SubmitChanges();
                    ts.Complete();

                    organizationTypeObject.OrganizationTypeId = organizationType.OrganizationTypeId;
                    organizationTypeObject.LastUpdatedDate = organizationType.LastUpdatedDate;

                    // remove cache.
                    base.RemoveCache(CacheKey4AllOrganizationTypes);
                }
            }
            catch (ValidationException)
            {
                throw;
            }
            catch (Exception exp)
            {
                Logger.Instance(this).Error(exp);
                throw;
            }
        }