Esempio n. 1
0
        /// <summary>
        /// Persists all pending transactions in <see cref="Oms.PendingTransactions" /> to the backing store.
        /// </summary>
        /// <param name="oms">Oms.</param>
        /// <param name="path">Path.</param>
        public static void Save(this Oms oms, string path)
        {
            string filename        = System.IO.Path.Combine(new string[] { path, String.Format("{0}.mcx", DateTime.Now.ToString("s").Replace(':', '-')) });
            string parentDirectory = System.IO.Path.GetDirectoryName(filename);

            if (!System.IO.Directory.Exists(parentDirectory))
            {
                System.IO.Directory.CreateDirectory(parentDirectory);
            }

            MochaSnapshotObjectModel mcx = new MochaSnapshotObjectModel();

            foreach (Transaction transaction in oms.PendingTransactions)
            {
                string tenantName = oms.GetTenantName(transaction.Tenant);
                MochaSnapshotTransaction snapshotTransaction = new MochaSnapshotTransaction();
                snapshotTransaction.TenantName = tenantName;

                foreach (TransactionOperation op in transaction.Operations)
                {
                    if (op is AssignAttributeTransactionOperation aa)
                    {
                        MochaSnapshotAssignAttributeTransactionOperation ssaa = new MochaSnapshotAssignAttributeTransactionOperation();
                        ssaa.SourceInstanceID    = oms.GetInstanceID(aa.SourceInstance);
                        ssaa.AttributeInstanceID = oms.GetInstanceID(aa.AttributeInstance);
                        ssaa.EffectiveDate       = aa.EffectiveDate;
                        ssaa.Value = aa.Value;
                        snapshotTransaction.Operations.Add(ssaa);
                    }
                    else if (op is AssociateRelationshipTransactionOperation ar)
                    {
                        MochaSnapshotAssociateRelationshipTransactionOperation ssar = new MochaSnapshotAssociateRelationshipTransactionOperation();
                        ssar.SourceInstanceID       = oms.GetInstanceID(ar.SourceInstance);
                        ssar.RelationshipInstanceID = oms.GetInstanceID(ar.RelationshipInstance);
                        ssar.EffectiveDate          = ar.EffectiveDate;
                        for (int i = 0; i < ar.TargetInstances.Length; i++)
                        {
                            ssar.TargetInstanceIDs.Add(oms.GetInstanceID(ar.TargetInstances[i]));
                        }
                        snapshotTransaction.Operations.Add(ssar);
                    }
                    else if (op is CreateInstanceTransactionOperation ci)
                    {
                        MochaSnapshotCreateInstanceTransactionOperation ssci = new MochaSnapshotCreateInstanceTransactionOperation();
                        ssci.GlobalIdentifier      = ci.GlobalIdentifier;
                        ssci.ClassGlobalIdentifier = ci.ClassGlobalIdentifier;
                        snapshotTransaction.Operations.Add(ssci);
                    }
                }
                mcx.Transactions.Add(snapshotTransaction);
            }
            Document.Save(mcx, mcldf, new FileAccessor(filename, true, true));

            oms.PendingTransactions.Clear();
        }