public static List<SalesforceFileProxy> DownloadAllFilesSynchronously(PackageEntity package, SalesforceContext ctx,  CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (!ctx.IsLoggedIn)
                throw new InvalidOperationException("Service should be logged into Salesforce");

            List<SalesforceFileProxy> listFileProxies = new List<SalesforceFileProxy>();
            RetrieveRequest[] retrieveRequestArray = ConvertPackageToRequests(package);

            List<AsyncResult> listResults = new List<AsyncResult>();

            foreach (var retrieveRequest in retrieveRequestArray)
            {
                RetrieveRequest request = retrieveRequest;
                cancellationToken.ThrowIfCancellationRequested();
                AsyncResult asyncResult = ctx.SessionExpirationWrapper(() => ctx.MetadataServiceAdapter.Retrieve(request));
                listResults.Add(asyncResult);
            }

            cancellationToken.ThrowIfCancellationRequested();
            ICollection<Task<RetrieveResult>> collection = new Collection<Task<RetrieveResult>>();
            foreach (var asyncResult in listResults)
            {
                cancellationToken.ThrowIfCancellationRequested();
                Task<RetrieveResult> task = Task<RetrieveResult>.Factory.StartNew(() => ctx.PollingResult.PollForResultWrapper(3, 180, 3, () =>
                        ctx.CheckRetrieveResult(asyncResult.id), res => res != null, cancellationToken), cancellationToken);
                collection.Add(task);
            }

            ctx.WaitAllTaskskWithHandlingAggregateException(collection.ToArray(), cancellationToken);
            foreach (Task<RetrieveResult> task in collection)
            {
                cancellationToken.ThrowIfCancellationRequested();
                RetrieveResult result = task.Result;

                string fileName = String.Format("{0}\\{1}.zip", ctx.OutputLocation, Guid.NewGuid().ToString());

                bool rawZip = Boolean.Parse(ConfigurationManager.AppSettings["salesforcemigrations:dumprawzip"]);

                if(rawZip)
                {
                    var rawZipFile = new SalesforceFileProxy
                    {
                        FullName = result.id,
                        FileName = String.Format("unpackaged/zips/{0}.zip", result.id),
                        Type = "zip",
                        BinaryBody = result.zipFile
                    };

                    listFileProxies.Add(rawZipFile);
                }
               
                List<SalesforceFileProxy> files = UnzipPackageFilesHelper.UnzipPackageFilesRecursive(result.zipFile);

                UpdateFileProperties(files, result.fileProperties);
                listFileProxies.AddRange(files);
            }
            return listFileProxies;
        }
        public static XmlDocument GetPackageXml(this List<SalesforceFileProxy> fileProxies)
        {
            // save a mater package xml with all types
            var grouped = fileProxies
                            .Where(w => !String.IsNullOrWhiteSpace(w.FullName))
                            .GroupBy(g => g.Type)
                            .Where(gw => gw.Key.ToLowerInvariant() != "package" && gw.Key.ToLowerInvariant() != "zip")
                            .Select(s => new PackageTypeEntity
                            {
                                Name = s.Key,
                                Members = s.Select(sm => sm.FullName).ToArray()
                            }).ToArray();

            var pe = new PackageEntity { Version = "29.0", Types = grouped };

            return pe.GetXml();
        }
    static public PackageEntity BuildQueryAllCommonTypes(this PackageEntity entity)
    {
        var pe = new PackageEntity()
            .SetVersion("29.0")
            .AddGlobal("ApexClass")
            .AddGlobal("ApexPage")
            .AddGlobal("ApexComponent")
            .AddGlobal("ApexTrigger")
            .AddGlobal("ApprovalProcess")
            .AddGlobal("CustomLabels")
            .AddGlobal("StaticResource")
            .AddGlobal("Layouts")
            .AddGlobal("CustomObject")
            .AddGlobal("Workflow");

        return pe;
    }
        public static XmlDocument GetPackageXml(this List <SalesforceFileProxy> fileProxies)
        {
            // save a mater package xml with all types
            var grouped = fileProxies
                          .Where(w => !String.IsNullOrWhiteSpace(w.FullName))
                          .GroupBy(g => g.Type)
                          .Where(gw => gw.Key.ToLowerInvariant() != "package" && gw.Key.ToLowerInvariant() != "zip")
                          .Select(s => new PackageTypeEntity
            {
                Name    = s.Key,
                Members = s.Select(sm => sm.FullName).ToArray()
            }).ToArray();

            var pe = new PackageEntity {
                Version = "29.0", Types = grouped
            };

            return(pe.GetXml());
        }
 public static RetrieveRequest[] ConvertPackageToRequests(PackageEntity package)
 {
     return(package.Types.Select(t =>
     {
         RetrieveRequest retrieveRequest = new RetrieveRequest
         {
             apiVersion = Double.Parse(package.Version, CultureInfo.InvariantCulture),
             unpackaged = new Package()
             {
                 types = new[]
                 {
                     new PackageTypeMembers()
                     {
                         name = t.Name,
                         members = t.Members
                     }
                 }
             }
         };
         return retrieveRequest;
     }).ToArray());
 }
 public RetrieveRequest[] ConvertPackageToRequests(PackageEntity package)
 {
     return package.Types.Select(t =>
     {
         RetrieveRequest retrieveRequest = new RetrieveRequest
         {
             apiVersion = double.Parse(package.Version, CultureInfo.InvariantCulture),
             unpackaged = new Package()
             {
                 types = new[]
                 {
                     new PackageTypeMembers()
                     {
                         name = t.Name,
                         members = t.Members
                     }
                 }
             }
         };
         return retrieveRequest;
     }).ToArray();
 }
        public static List <SalesforceFileProxy> DownloadAllFilesSynchronously(PackageEntity package, SalesforceContext ctx, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (!ctx.IsLoggedIn)
            {
                throw new InvalidOperationException("Service should be logged into Salesforce");
            }

            List <SalesforceFileProxy> listFileProxies = new List <SalesforceFileProxy>();

            RetrieveRequest[] retrieveRequestArray = ConvertPackageToRequests(package);

            List <AsyncResult> listResults = new List <AsyncResult>();

            foreach (var retrieveRequest in retrieveRequestArray)
            {
                RetrieveRequest request = retrieveRequest;
                cancellationToken.ThrowIfCancellationRequested();
                AsyncResult asyncResult = ctx.SessionExpirationWrapper(() => ctx.MetadataServiceAdapter.Retrieve(request));
                listResults.Add(asyncResult);
            }

            cancellationToken.ThrowIfCancellationRequested();
            ICollection <Task <RetrieveResult> > collection = new Collection <Task <RetrieveResult> >();

            foreach (var asyncResult in listResults)
            {
                cancellationToken.ThrowIfCancellationRequested();
                Task <RetrieveResult> task = Task <RetrieveResult> .Factory.StartNew(() => ctx.PollingResult.PollForResultWrapper(3, 180, 3, () =>
                                                                                                                                  ctx.CheckRetrieveResult(asyncResult.id), res => res != null, cancellationToken), cancellationToken);

                collection.Add(task);
            }

            ctx.WaitAllTaskskWithHandlingAggregateException(collection.ToArray(), cancellationToken);
            foreach (Task <RetrieveResult> task in collection)
            {
                cancellationToken.ThrowIfCancellationRequested();
                RetrieveResult result = task.Result;

                string fileName = String.Format("{0}\\{1}.zip", ctx.OutputLocation, Guid.NewGuid().ToString());

                bool rawZip = Boolean.Parse(ConfigurationManager.AppSettings["salesforcemigrations:dumprawzip"]);

                if (rawZip)
                {
                    var rawZipFile = new SalesforceFileProxy
                    {
                        FullName   = result.id,
                        FileName   = String.Format("unpackaged/zips/{0}.zip", result.id),
                        Type       = "zip",
                        BinaryBody = result.zipFile
                    };

                    listFileProxies.Add(rawZipFile);
                }

                List <SalesforceFileProxy> files = UnzipPackageFilesHelper.UnzipPackageFilesRecursive(result.zipFile);

                UpdateFileProperties(files, result.fileProperties);
                listFileProxies.AddRange(files);
            }
            return(listFileProxies);
        }
        public static List <SalesforceFileProxy> DownloadAllFilesSynchronously(CancellationToken cancellationToken, SalesforceContext ctx)
        {
            PackageEntity pe = new PackageEntity().BuildQueryAllCommonTypes();

            return(DownloadAllFilesSynchronously(pe, ctx, cancellationToken));
        }
예제 #9
0
 protected SalesforceFileNode(PackageEntity root)
 {
 }
예제 #10
0
 public StaticResourceContainerNode(PackageEntity root) : base(root)
 {
 }
 public StaticResourceContainerNode(PackageEntity root) : base(root)
 {
     
 }
 public static List<SalesforceFileProxy> DownloadAllFilesSynchronously(CancellationToken cancellationToken, SalesforceContext ctx)
 {
     PackageEntity pe = new PackageEntity().BuildQueryAllCommonTypes();
     return DownloadAllFilesSynchronously(pe, ctx, cancellationToken);
 }
 protected SalesforceFileNode(PackageEntity root)
 {
     
 }
        public string Deploy(string folder, DeployOptions options)
        {
            if (!Directory.Exists(folder))
            {
                throw new DirectoryNotFoundException();
            }

            var staticResourceFolder = Path.Combine(folder, "package\\staticresources");

            if (!Directory.Exists(staticResourceFolder))
            {
                throw new DirectoryNotFoundException("Couldn't find static resources subfolder under package folder.");
            }

            var directories = Directory.EnumerateDirectories(staticResourceFolder);

            IList <IDeployableItem> members = new List <IDeployableItem>();

            foreach (var directory in directories)
            {
                var      splits  = directory.Split('\\');
                var      zipName = splits[splits.Length - 1];
                string[] files   = Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories);

                var directoryPlusZipName = Path.Combine(staticResourceFolder, zipName);

                using (ZipFile zip = new ZipFile())
                {
                    foreach (var file in files)
                    {
                        var zipFileNamePlusDirectory = file.Replace(directoryPlusZipName, String.Empty);
                        var zipDirectory             = Path.GetDirectoryName(zipFileNamePlusDirectory);
                        zip.AddFile(file, zipDirectory);
                    }

                    MemoryStream memoryStream = new MemoryStream();
                    zip.Save(memoryStream);

                    members.Add(new StaticResourceDeployableItem
                    {
                        FileBody = memoryStream.ToArray(),
                        FileName = String.Format("{0}\\{1}.resource", directory, zipName),
                        FileNameWithoutExtension = String.Format("{0}", zipName)
                    });

                    XmlOutput xo = new XmlOutput()
                                   .XmlDeclaration()
                                   .Node("StaticResource").Attribute("xmlns", "http://soap.sforce.com/2006/04/metadata").Within()
                                   .Node("cacheControl").InnerText("Public")
                                   .Node("contentType").InnerText("application/zip").EndWithin();

                    members.Add(new StaticResourceDeployableItem
                    {
                        FileBody = System.Text.Encoding.Default.GetBytes(xo.GetOuterXml()),
                        FileName = String.Format("{0}\\{1}.resource-meta.xml", directory, zipName),
                        FileNameWithoutExtension = String.Format("{0}.resource-meta.xml", zipName),
                        AddToPackage             = false
                    });
                }
            }

            PackageEntity pe = new PackageEntity
            {
                Types = new[]
                {
                    new PackageTypeEntity
                    {
                        Members = members.Where(w => w.AddToPackage).Select(s => s.FileNameWithoutExtension).ToArray(),
                        Name    = "StaticResource"
                    }
                },
                Version = "29.0"
            };

            var zipFile = UnzipPackageFilesHelper.ZipObjectsForDeploy(members, pe.GetXml().OuterXml);

            SalesforceFileProcessing.SaveByteArray(String.Format("{0}\\package_{1}.zip", folder, Guid.NewGuid()), zipFile);

            var id = Context.MetadataServiceAdapter.Deploy(zipFile, options).id;

            return(id);
        }
 public IEnumerable <SalesforceFileProxy> DownloadFiles(PackageEntity pe, CancellationToken cancellationToken)
 {
     return(SalesforceRepositoryHelpers.DownloadAllFilesSynchronously(pe, GetContext, cancellationToken));
 }