예제 #1
0
        private void LinkToFile()
        {
            if (m_selectedItem == null)
            {
                MessageBox.Show("You must select an Item first");
                return;
            }

            try
            {
                VDF.Vault.Forms.Settings.SelectEntitySettings settings = new VDF.Vault.Forms.Settings.SelectEntitySettings();
                settings.MultipleSelect = false;
                settings.ActionableEntityClassIds.Add(VDF.Vault.Currency.Entities.EntityClassIds.Files);
                settings.ConfigureActionButtons("Select", null, null, false);
                VDF.Vault.Forms.Results.SelectEntityResults results = VDF.Vault.Forms.Library.SelectEntity(m_connection, settings);

                if (results == null || results.SelectedEntities == null || !results.SelectedEntities.Any())
                {
                    return;
                }

                VDF.Vault.Currency.Entities.FileIteration file = results.SelectedEntities.First() as VDF.Vault.Currency.Entities.FileIteration;
                if (file == null)
                {
                    MessageBox.Show("You must select a file");
                    return;
                }

                ItemService itemSvc = m_connection.WebServiceManager.ItemService;

                // first assign the file to a new item
                itemSvc.AddFilesToPromote(new long[] { file.EntityIterationId }, ItemAssignAll.Default, true);
                DateTime timestamp;
                GetPromoteOrderResults promoteOrderResults = itemSvc.GetPromoteComponentOrder(out timestamp);
                //long[] componentIds = promoteOrderResults.
                if (promoteOrderResults.PrimaryArray != null && promoteOrderResults.PrimaryArray.Any())
                {
                    itemSvc.PromoteComponents(timestamp, promoteOrderResults.PrimaryArray);
                }
                if (promoteOrderResults.NonPrimaryArray != null && promoteOrderResults.NonPrimaryArray.Any())
                {
                    itemSvc.PromoteComponentLinks(promoteOrderResults.NonPrimaryArray);
                }
                ItemsAndFiles promoteResult = itemSvc.GetPromoteComponentsResults(timestamp);

                // find out which item corresponds to the file
                long itemId = -1;
                foreach (ItemFileAssoc assoc in promoteResult.FileAssocArray)
                {
                    if (assoc.CldFileId == file.EntityIterationId)
                    {
                        itemId = assoc.ParItemId;
                    }
                }

                if (itemId < 0)
                {
                    MessageBox.Show("Promote error");
                }
                else
                {
                    // next reassign the file from the new item to the existing item
                    Item[] updatedItems = itemSvc.ReassignComponentsToDifferentItems(
                        new long[] { itemId },
                        new long[] { m_selectedItem.Id });

                    // commit the changes
                    itemSvc.UpdateAndCommitItems(updatedItems);
                }

                // clear out the items from the initial Promote
                long[] itemIds       = new long[promoteResult.ItemRevArray.Length];
                long[] itemMasterIds = new long[promoteResult.ItemRevArray.Length];

                for (int i = 0; i < promoteResult.ItemRevArray.Length; i++)
                {
                    itemIds[i]       = promoteResult.ItemRevArray[i].Id;
                    itemMasterIds[i] = promoteResult.ItemRevArray[i].MasterId;
                }

                itemSvc.DeleteUnusedItemNumbers(itemMasterIds);
                itemSvc.UndoEditItems(itemIds);
            }
            catch (Exception e)
            {
                ErrorHandler.HandleError(e);
            }

            RefreshItemList();
        }