Example #1
0
        public static string ImportSitePackage(Context context, SiteSettings ss)
        {
            var includeData             = context.Forms.Bool("IncludeData");
            var includeSitePermission   = context.Forms.Bool("IncludeSitePermission");
            var includeRecordPermission = context.Forms.Bool("IncludeRecordPermission");
            var includeColumnPermission = context.Forms.Bool("IncludeColumnPermission");

            if (ss.SiteId == 0)
            {
                ss.PermissionType = context.SiteTopPermission();
            }
            if (!Parameters.SitePackage.Import ||
                !context.CanManageSite(ss: ss))
            {
                return(Messages.ResponseInvalidRequest(context: context).ToJson());
            }
            if (includeData && context.ContractSettings.Import == false)
            {
                return(Messages.ResponseInvalidRequest(context: context).ToJson());
            }
            var serializer = new JsonSerializer();

            using (var ms = new System.IO.MemoryStream(
                       buffer: context.PostedFiles.FirstOrDefault().Byte(),
                       writable: false))
                using (var sr = new System.IO.StreamReader(ms, Encoding.UTF8))
                    using (var reader = new JsonTextReader(sr))
                    {
                        var sitePackage = serializer.Deserialize <SitePackage>(reader);
                        if (context.ContractSettings.SitesLimit(
                                context: context,
                                number: sitePackage.Sites.Count()))
                        {
                            return(Messages.ResponseSitesLimit(context: context).ToJson());
                        }
                        if (includeData)
                        {
                            if (Parameters.General.ImportMax > 0 &&
                                Parameters.General.ImportMax < sitePackage.Data
                                .SelectMany(o => o.Body)
                                .Count())
                            {
                                return(Messages.ResponseImportMax(
                                           context: context,
                                           data: Parameters.General.ImportMax.ToString())
                                       .ToJson());
                            }
                            if (sitePackage.Data.Any(o => o.Header.Any(p =>
                                                                       context.ContractSettings.ItemsLimit(
                                                                           context: context,
                                                                           siteId: p.SiteId,
                                                                           number: o.Body.Count()))))
                            {
                                return(Messages.ResponseItemsLimit(
                                           context: context,
                                           data: Parameters.General.ImportMax.ToString())
                                       .ToJson());
                            }
                        }
                        sitePackage.HeaderInfo.SavedBaseSiteId        = context.SiteId;
                        sitePackage.HeaderInfo.SavedInheritPermission = ss.InheritPermission;
                        foreach (var conv in sitePackage.HeaderInfo.Convertors)
                        {
                            var response = Rds.ExecuteScalar_response(
                                context: context,
                                transactional: true,
                                selectIdentity: true,
                                statements: new SqlStatement[]
                            {
                                Rds.InsertItems(
                                    selectIdentity: true,
                                    param: Rds.ItemsParam()
                                    .ReferenceType("Sites")
                                    .Title(conv.SiteTitle)),
                                Rds.UpdateItems(
                                    where : Rds.ItemsWhere().ReferenceId(raw: Def.Sql.Identity),
                                    param: Rds.ItemsParam().SiteId(raw: Def.Sql.Identity)),
                            });
                            conv.SavedSiteId = response.Id;
                        }
                        foreach (var conv in sitePackage.HeaderInfo.Convertors)
                        {
                            var packageSiteModel = sitePackage.Sites
                                                   .Where(e => e.SiteId == conv.SiteId)
                                                   .FirstOrDefault();
                            packageSiteModel.SetSavedIds(
                                context: context,
                                ss: ss,
                                sitePackage: sitePackage,
                                savedSiteId: conv.SavedSiteId.ToLong(),
                                includeSitePermission: includeSitePermission &&
                                sitePackage.HeaderInfo.IncludeSitePermission);
                            packageSiteModel.SiteSettings = packageSiteModel.GetSavedSiteSettings(
                                context: context,
                                header: sitePackage.HeaderInfo,
                                includeColumnPermission: includeColumnPermission,
                                permissionIdList: sitePackage.PermissionIdList);
                            Rds.ExecuteScalar_response(
                                context: context,
                                transactional: true,
                                statements: new SqlStatement[]
                            {
                                Rds.InsertSites(param: Rds.SitesParam()
                                                .SiteId(packageSiteModel.SavedSiteId)
                                                .TenantId(packageSiteModel.SavedTenantId)
                                                .Title(packageSiteModel.Title)
                                                .Body(packageSiteModel.Body)
                                                .GridGuide(packageSiteModel.GridGuide)
                                                .EditorGuide(packageSiteModel.EditorGuide)
                                                .ReferenceType(packageSiteModel.ReferenceType.MaxLength(32))
                                                .ParentId(packageSiteModel.SavedParentId)
                                                .InheritPermission(packageSiteModel.SavedInheritPermission)
                                                .SiteSettings(packageSiteModel.SiteSettings.ToJson())
                                                .Publish(packageSiteModel.Publish)
                                                .Comments(packageSiteModel.Comments.ToJson())),
                                Rds.PhysicalDeleteLinks(
                                    where : Rds.LinksWhere().SourceId(packageSiteModel.SavedSiteId)),
                                LinkUtilities.Insert(link: packageSiteModel.SiteSettings.Links?
                                                     .Select(o => o.SiteId)
                                                     .Distinct()
                                                     .ToDictionary(o => o, o => packageSiteModel.SavedSiteId)
                                                     ?? new Dictionary <long, long>()),
                                Rds.InsertPermissions(
                                    param: Rds.PermissionsParam()
                                    .ReferenceId(packageSiteModel.SavedSiteId)
                                    .DeptId(0)
                                    .UserId(context.UserId)
                                    .PermissionType(Permissions.Manager()),
                                    _using: packageSiteModel.SavedInheritPermission == packageSiteModel.SavedSiteId),
                            });
                            var siteModel = new SiteModel(
                                context: context,
                                siteId: packageSiteModel.SavedSiteId);
                            var fullText = siteModel.FullText(
                                context: context,
                                ss: siteModel.SiteSettings);
                            Rds.ExecuteNonQuery(
                                context: context,
                                transactional: true,
                                statements: Rds.UpdateItems(
                                    where : Rds.ItemsWhere()
                                    .ReferenceId(packageSiteModel.SavedSiteId),
                                    param: Rds.ItemsParam()
                                    .FullText(fullText, _using: fullText != null)
                                    .SearchIndexCreatedTime(DateTime.Now, _using: fullText != null)));
                        }
                        var idHash = sitePackage.GetIdHashFromConverters();
                        foreach (long savedSiteId in sitePackage.HeaderInfo.Convertors.Select(e => e.SavedSiteId))
                        {
                            var siteModel = new SiteModel(
                                context: context,
                                siteId: savedSiteId);
                            switch (siteModel.ReferenceType)
                            {
                            case "Wikis":
                                var wikiModel = new WikiModel(
                                    context: context,
                                    ss: siteModel.SiteSettings)
                                {
                                    SiteId   = siteModel.SiteId,
                                    Title    = siteModel.Title,
                                    Body     = siteModel.Body,
                                    Comments = siteModel.Comments
                                };
                                wikiModel.Create(
                                    context: context,
                                    ss: siteModel.SiteSettings);
                                var wikiId = Rds.ExecuteScalar_long(
                                    context: context,
                                    statements: Rds.SelectWikis(
                                        top: 1,
                                        column: Rds.WikisColumn().WikiId(),
                                        where : Rds.WikisWhere().SiteId(siteModel.SiteId)));
                                idHash.Add(savedSiteId, wikiId);
                                break;

                            default:
                                Search.Indexes.Create(
                                    context: context,
                                    ss: siteModel.SiteSettings,
                                    id: siteModel.SiteId);
                                break;
                            }
                        }
                        int dataCount = 0;
                        if (includeData)
                        {
                            if (sitePackage.Data.Any())
                            {
                                ImportItems(
                                    context: context,
                                    sitePackage: sitePackage,
                                    idHash: idHash);
                                sitePackage.ConvertDataId(
                                    context: context,
                                    idHash: idHash);
                                dataCount = ImportData(
                                    context: context,
                                    sitePackage: sitePackage,
                                    idHash: idHash);
                            }
                        }
                        if (sitePackage.HeaderInfo.Convertors.Any())
                        {
                            foreach (var conv in sitePackage.HeaderInfo.Convertors)
                            {
                                if (conv.ReferenceType == "Sites")
                                {
                                    if ((!conv.Order.IsNullOrEmpty() && conv.Order.Equals("[]") == false))
                                    {
                                        var newOrders = new List <long>();
                                        var orders    = conv.Order.Deserialize <List <long> >()?.ToList()
                                                        ?? new List <long>();
                                        orders.ForEach(e => newOrders.Add(idHash.Get(e)));
                                        if (newOrders.Any())
                                        {
                                            new OrderModel()
                                            {
                                                ReferenceId   = conv.SavedSiteId.ToLong(),
                                                ReferenceType = "Sites",
                                                OwnerId       = 0,
                                                Data          = newOrders
                                            }.UpdateOrCreate(context: context);
                                        }
                                    }
                                }
                            }
                            var response = Rds.ExecuteScalar_response(
                                context: context,
                                transactional: true,
                                statements: StatusUtilities.UpdateStatus(
                                    tenantId: context.TenantId,
                                    type: StatusUtilities.Types.SitesUpdated));
                        }
                        if (sitePackage.Permissions.Any())
                        {
                            foreach (var conv in sitePackage.HeaderInfo.Convertors)
                            {
                                var packageSiteModel = sitePackage.Sites
                                                       .Where(e => e.SiteId == conv.SiteId)
                                                       .FirstOrDefault();
                                var packagePermissionModel = sitePackage.Permissions
                                                             .Where(e => e.SiteId == conv.SiteId)
                                                             .FirstOrDefault();
                                foreach (var permissionShortModel in packagePermissionModel.Permissions)
                                {
                                    if (includeSitePermission == false)
                                    {
                                        if (permissionShortModel.ReferenceId == packagePermissionModel.SiteId)
                                        {
                                            continue;
                                        }
                                    }
                                    if ((includeRecordPermission == false) || (dataCount == 0))
                                    {
                                        if (permissionShortModel.ReferenceId != packagePermissionModel.SiteId)
                                        {
                                            continue;
                                        }
                                    }
                                    var idConverter = new IdConverter(
                                        context: context,
                                        siteId: packageSiteModel.SavedSiteId,
                                        permissionShortModel: permissionShortModel,
                                        permissionIdList: sitePackage.PermissionIdList,
                                        convertSiteId: idHash[permissionShortModel.ReferenceId]);
                                    if (idConverter.Convert == true)
                                    {
                                        Rds.ExecuteNonQuery(
                                            context: context,
                                            transactional: true,
                                            statements: Rds.InsertPermissions(
                                                param: Rds.PermissionsParam()
                                                .ReferenceId(idHash[permissionShortModel.ReferenceId])
                                                .DeptId(idConverter.ConvertDeptId)
                                                .GroupId(idConverter.ConvertGroupId)
                                                .UserId(idConverter.ConvertUserId)
                                                .PermissionType(permissionShortModel.PermissionType)));
                                    }
                                }
                            }
                        }
                        Rds.ExecuteNonQuery(
                            context: context,
                            statements: StatusUtilities.UpdateStatus(
                                tenantId: context.TenantId,
                                type: StatusUtilities.Types.UsersUpdated));
                        SessionUtilities.Set(
                            context: context,
                            message: Messages.SitePackageImported(
                                context: context,
                                data: new string[]
                        {
                            sitePackage.HeaderInfo.Convertors.Count().ToString(),
                            dataCount.ToString()
                        }));
                        return(new ResponseCollection()
                               .Href(url: Locations.ItemIndex(
                                         context: context,
                                         id: ss.SiteId))
                               .ToJson());
                    }
        }
Example #2
0
        public static void Create(Context context, SiteSettings ss, long id, bool force = false)
        {
            if (force)
            {
                var itemModel = new ItemModel(
                    context: context,
                    referenceId: id);
                switch (itemModel.ReferenceType)
                {
                case "Sites":
                    var siteModel = new SiteModel(context: context, siteId: id);
                    CreateFullText(
                        context: context,
                        id: id,
                        fullText: siteModel.FullText(
                            context: context,
                            ss: ss,
                            backgroundTask: true));
                    break;

                case "Issues":
                    var issueModel = new IssueModel(
                        context: context,
                        ss: ss,
                        issueId: id);
                    ss.Links
                    ?.Where(o => o.SiteId > 0)
                    .Select(o => ss.GetColumn(
                                context: context,
                                columnName: o.ColumnName))
                    .Where(column => column?.UseSearch == true)
                    .ForEach(column =>
                             ss.SetChoiceHash(
                                 context: context,
                                 columnName: column.ColumnName,
                                 selectedValues: new List <string>
                    {
                        issueModel.PropertyValue(
                            context: context,
                            column: column)
                    }));
                    CreateFullText(
                        context: context,
                        id: id,
                        fullText: issueModel.FullText(
                            context: context,
                            ss: ss,
                            backgroundTask: true));
                    break;

                case "Results":
                    var resultModel = new ResultModel(
                        context: context,
                        ss: ss,
                        resultId: id);
                    ss.Links
                    ?.Where(o => o.SiteId > 0)
                    .Select(o => ss.GetColumn(
                                context: context,
                                columnName: o.ColumnName))
                    .Where(column => column?.UseSearch == true)
                    .ForEach(column =>
                             ss.SetChoiceHash(
                                 context: context,
                                 columnName: column.ColumnName,
                                 selectedValues: new List <string>
                    {
                        resultModel.PropertyValue(
                            context: context,
                            column: column)
                    }));
                    CreateFullText(
                        context: context,
                        id: id,
                        fullText: resultModel.FullText(
                            context: context,
                            ss: ss,
                            backgroundTask: true));
                    break;

                case "Wikis":
                    var wikiModel = new WikiModel(
                        context: context,
                        ss: ss,
                        wikiId: id);
                    CreateFullText(
                        context: context,
                        id: id,
                        fullText: wikiModel.FullText(
                            context: context,
                            ss: ss,
                            backgroundTask: true));
                    break;
                }
            }
        }