Ejemplo n.º 1
0
        private void Import()
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "CSV Files|*.csv";
            DialogResult result = dialog.ShowDialog();

            if (result != DialogResult.OK)
            {
                return;
            }

            string filename = dialog.FileName;

            PackageService packageSvc = m_connection.WebServiceManager.PackageService;

            // Create a mapping between the CVS columns and the item properties.
            // NOTE: It is assumed the we are importing a file created by the
            // export command in this sample.  Otherwise we can't assume the
            // columns being used.
            Map info = new Map();

            //map the first column to level
            MapPair levelPair = new MapPair();

            levelPair.FromName = "Level";
            levelPair.ToName   = "BOMIndicator-41FF056B-8EEF-47E2-8F9E-490BC0C52C71";

            //map the second column to number
            MapPair numberPair = new MapPair();

            numberPair.FromName = "Number";
            numberPair.ToName   = "Number";

            //map the third column to type
            MapPair typePair = new MapPair();

            typePair.FromName = "Type";
            typePair.ToName   = "ItemClass";

            info.PairArray = new MapPair[] { levelPair, numberPair, typePair };

            FileNameAndURL fileNameandURL = null;

            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                long   partSize = m_connection.PartSizeInBytes;
                byte[] contents = new byte[partSize];
                int    bytesRead;
                while ((bytesRead = fs.Read(contents, 0, (int)partSize)) > 0)
                {
                    string existingPackageName = null;
                    if (fileNameandURL != null)
                    {
                        existingPackageName = fileNameandURL.Name;
                    }
                    fileNameandURL = packageSvc.UploadPackagePart(existingPackageName, ".csv", bytesRead == partSize ? contents : contents.Take(bytesRead).ToArray());
                }
            }

            PkgItemsAndBOM createResult = packageSvc.ImportFromPackage(FileFormat.CSV_LEVEL, info, fileNameandURL.Name);

            foreach (PkgItem item in createResult.PkgItemArray)
            {
                // to keep things simple, this sample only handles cases where we are
                // importing new items.  It does not handle complex cases like when an item needs updating.
                if (item.Resolution != null && item.Resolution.ResolutionMethod != ResolutionMethod.Create)
                {
                    MessageBox.Show("There are conflicts in the import.  This function can only be used for creating new Items.");
                    return;
                }
            }

            packageSvc.CommitImportedData(createResult);

            MessageBox.Show("Import completed");
            RefreshItemList();
        }