Example #1
0
        internal string[] CopyResourcesToFolder(RepositoryHandle[] data, string targetConnectionName, string folderId)
        {
            string rootSourceParent = GetCommonParent(data);

            //There is an implicit assumption here that all items dropped come from the same connection
            var sourceConn = data.First().Connection;
            var targetConn = _connManager.GetConnection(targetConnectionName);
            var migrator = new ResourceMigrator(sourceConn, targetConn);

            //Collect all source ids
            var sourceIds = new List<string>();
            foreach (var resId in data.Select(x => x.ResourceId.ToString()))
            {
                if (ResourceIdentifier.IsFolderResource(resId))
                    sourceIds.AddRange(GetFullResourceList(sourceConn, resId));
                else
                    sourceIds.Add(resId);

            }

            var targets = new List<string>();
            foreach (var resId in sourceIds)
            {
                var dstId = resId.Replace(rootSourceParent, folderId);
                System.Diagnostics.Trace.TraceInformation("{0} => {1}", resId, dstId); //NOXLATE
                targets.Add(dstId);
            }

            bool overwrite = true;
            var existing = new List<string>();
            foreach (var resId in targets)
            {
                if (targetConn.ResourceService.ResourceExists(resId))
                {
                    existing.Add(resId);
                }
            }
            if (existing.Count > 0)
                overwrite = MessageService.AskQuestion(string.Format(Strings.PromptOverwriteOnTargetConnection, existing.Count));

            var wb = Workbench.Instance;
            var dlg = new ProgressDialog();
            var worker = new ProgressDialog.DoBackgroundWork((w, evt, args) =>
            {
                LengthyOperationProgressCallBack cb = (s, cbe) =>
                {
                    w.ReportProgress(cbe.Progress, cbe.StatusMessage);
                };

                return migrator.CopyResources(sourceIds.ToArray(), targets.ToArray(), overwrite, new RebaseOptions(rootSourceParent, folderId), cb);
            });

            var result = (string[])dlg.RunOperationAsync(wb, worker);
            RefreshModel(targetConn.DisplayName, folderId);
            ExpandNode(targetConn.DisplayName, folderId);
            return result;
        }
Example #2
0
        internal static string GetCommonParent(RepositoryHandle[] data)
        {
            if (data.Length > 0)
            {
                if (data.Length == 1)
                {
                    if (data[0].ResourceId.IsFolder)
                        return data[0].ResourceId.ToString();
                    else
                        return data[0].ResourceId.ParentFolder;
                }
                else
                {
                    int matches = 0;
                    string[] parts = data.First().ResourceId.ToString()
                                         .Substring(StringConstants.RootIdentifier.Length)
                                         .Split('/'); //NOXLATE
                    string test = StringConstants.RootIdentifier;
                    string parent = test;
                    int partIndex = 0;
                    //Use first one as a sample to see how far we can go. Keep going until we have
                    //a parent that doesn't match all of them. The one we recorded before then will
                    //be the common parent
                    while (true)
                    {
                        partIndex++;
                        if (partIndex > parts.Length) //Shouldn't happen, but just in case
                            break;

                        parent = test;

                        test = test + parts[partIndex - 1] + "/";
                        matches = data.Where(x => x.ResourceId.ResourceId.StartsWith(test)).Count();

                        if (matches > 0 && matches != data.Length)
                            break;
                    }
                    return parent;
                }
            }
            else
            {
                return StringConstants.RootIdentifier;
            }
        }