Пример #1
0
        public bool CommitTransaction(string comment)
        {
            bool result = false;

            // get the path
            string pgdbDirectory = "";
            string pgdbFile = "";
            string mxdFile = "";
            // get the see ID
            string seeID = "";

            try
            {
                Logger.Write("Testing valid tranasaction state...");

                if (BaseTransactionManager.Current() == null)
                {
                    throw new Exception("The current transaction manager has timed out. Please reload ArcMap. Transaction is no longer valid.");
                }

                if (BaseTransactionManager.Current().PGDB == null)
                {
                    throw new Exception("The current transaction manager has timed out. Please reload ArcMap. FGDB reference is no longer valid.");
                }

                bool usePgdbDirectory = false;
                if (BaseTransactionManager.Current().MXD == null)
                {
                    // check the PGDB directory
                    if (Directory.Exists(BaseTransactionManager.Current().PGDB.DirectoryName) && !File.Exists(BaseTransactionManager.Current().PGDB.DirectoryName + "\\" + BaseTransactionManager.Current().PGDB.Name.Replace(".gdb", ".mxd")))
                    {
                        usePgdbDirectory = true;
                        BaseTransactionManager.Current().MXD = new FileInfo(BaseTransactionManager.Current().PGDB.DirectoryName + "\\" + BaseTransactionManager.Current().PGDB.Name.Replace(".gdb", ".mxd"));
                    }
                    else throw new Exception("The current transaction manager has timed out. Please reload ArcMap. MXD reference is no longer valid.");
                }

                if (string.IsNullOrEmpty(BaseTransactionManager.Current().TxId))
                {
                    throw new Exception("The current transaction manager has timed out. Please reload ArcMap. SEE ID reference is no longer valid.");
                }

                BaseTransactionManager.startHourGlass("Creating Temproary Directory...");

                try
                {
                    pgdbDirectory = BaseTransactionManager.Current().PGDB.DirectoryName;
                    pgdbFile = BaseTransactionManager.Current().PGDB.Name;
                    mxdFile = usePgdbDirectory ? BaseTransactionManager.Current().PGDB.DirectoryName + "\\" + BaseTransactionManager.Current().PGDB.Name.Replace(".gdb", ".mxd") : BaseTransactionManager.Current().MXD.DirectoryName + "\\" + BaseTransactionManager.Current().MXD.Name;
                    // get the see ID
                    seeID = BaseTransactionManager.Current().TxId;
                    Logger.Write("Located transaction documents at " + pgdbDirectory);
                }
                catch (Exception parseEx)
                {
                    Logger.Write("Could not locate FGDB directory. " + parseEx.Message);
                    throw new Exception(parseEx.Message + ". An error occured reading the transaction directories from Metadata. Please ensure you're FGDB and MXD exist in the correct metadata location.");
                }

                if (!Directory.Exists(pgdbDirectory + "\\temp\\"))
                {
                    Logger.Write("Creating temporary directory...");
                    Directory.CreateDirectory(pgdbDirectory + "\\temp\\");
                }
                else
                {
                    Logger.Write("Deleting old temporary directory...");
                    Directory.Delete(pgdbDirectory + "\\temp\\", true);
                    Logger.Write("Creating temporary directory...");
                    Directory.CreateDirectory(pgdbDirectory + "\\temp\\");
                }

                BaseTransactionManager.Close();

                //Marshal.ReleaseComObject(BaseTransactionManager.Current().PGDBConnection);
                //BaseTransactionManager.Current().PGDB = null;

                // close the PGDB, maintain the file state
                SystemDefaults defaults = new SystemDefaults();

                BaseTransactionManager.app.SaveDocument(null);
                BaseTransactionManager.app.NewDocument(false, defaults.get_Default("tm.temp"));

                GC.Collect();

                BaseTransactionManager.startHourGlass("Creating transaction FGDB Backup...");
                Logger.Write("Creating transaction backup");

                // create temp file of PGDB
                IWorkspaceName sourceWS = (IWorkspaceName)new WorkspaceName();
                sourceWS.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory";
                sourceWS.PathName = pgdbDirectory + "\\" + pgdbFile;

                IWorkspaceFactory2 wf2 = new FileGDBWorkspaceFactoryClass();
                IWorkspaceName wsResult = null;
                wf2.Copy(sourceWS, pgdbDirectory + "\\temp\\", out wsResult);

                string tempFileLocation = "";

                if (wsResult != null) tempFileLocation = pgdbDirectory + "\\temp\\" + pgdbFile;
                else throw new Exception("Commit failed. Could not create temporary transaction file.");

                Marshal.ReleaseComObject(wf2);
                Marshal.ReleaseComObject(wsResult);
                wf2 = null;
                wsResult = null;

                Logger.Write("Renaming transaction backup to see ID: " + seeID.Replace("-", "").ToUpper());

                string newTempFolder = pgdbDirectory + "\\temp\\" + seeID.Replace("-", "").ToUpper() + ".gdb";

                Directory.Move(pgdbDirectory + "\\temp\\" + pgdbFile, newTempFolder);

                BaseTransactionManager.startHourGlass("Creating ZIP file...");
                Logger.Write("Creating Zip File...");

                // zip the temp folder.
                ZipFile zip = new ZipFile();
                zip.AddDirectory(pgdbDirectory + "\\temp\\");
                zip.Save(newTempFolder + ".zip");
                zip = null;

                // flag the transaction as checked in. Once complete, we'll set the flag back to checked out on an error, or complete on success
                //SetTransactionStatus("CHECKED_IN", "The Transaction has been checked in", seeID);

                BaseTransactionManager.startHourGlass("Waiting for SUITT FME Server. This may take a while...");
                Logger.Write("Subitting transaction...");

                RestRequest checkinRequest = ClientRequest("sees/" + seeID + "/features", Method.POST, true);
                checkinRequest.RequestFormat = DataFormat.Json;
                checkinRequest.AddFile("file", File.ReadAllBytes(newTempFolder + ".zip"), seeID.Replace("-", "").ToUpper() + ".gdb.zip");

                var checkinResponse = ApplicationClient.Execute(checkinRequest);
                if (checkinResponse.StatusCode == System.Net.HttpStatusCode.Accepted)
                {
                    Logger.Write("Submission successful, completeing transaction...");
                    result = CompleteCheckin(seeID, comment);
                }
                else
                {
                    string[] status = GetTransactionStatus(seeID);

                    MessageBox.Show("An error has occured commiting the transaction, and could not be completed at this time. Please verify your transaction is valid, with no spatial errors, and your transaction does not conflict with other transactions.");
                    SetTransactionStatus("CHECKED_OUT", "The Transaction has been checked out", seeID);
                }
            }
            catch (Exception e)
            {
                result = false;
                MessageBox.Show("An error has occured while committing your transaction: " + e.Message + Environment.NewLine + e.StackTrace);
                Logger.Write("#### COMMIT TRANSACTION ENDED: " + e + Environment.NewLine + e.StackTrace);
                SetTransactionStatus("CHECKED_OUT", "The Transaction has been checked out", seeID);
            }

            if (result)
            {
                MessageBox.Show("Transaction check-in completed successfully. No check-in errors were reported. Click 'OK' to clean up your ArcMap session and close the completed transaction.");

                BaseTransactionManager.Close();

                SystemDefaults defaults = new SystemDefaults();
                BaseTransactionManager.app.NewDocument(false, defaults.get_Default("tm.temp"));
            }
            else
            {
                if(!string.IsNullOrEmpty(mxdFile)) BaseTransactionManager.app.OpenDocument(mxdFile);
                MessageBox.Show("Transaction check-in did not complete successfully. Please review the log for errors");
            }

            return result;
        }