private ItemDescriptionIdentification DetermineIdentification(IAtmlObject atmlObject, DocumentDAO dao,
                                                                      string uuid)
        {
            ItemDescriptionIdentification identification = null;
            PropertyInfo piId = atmlObject.GetType().GetProperty("Identification");

            if (piId != null)
            {
                identification = piId.GetValue(atmlObject, null) as ItemDescriptionIdentification;
            }
            else
            {
                piId = atmlObject.GetType().GetProperty("Item");
                if (piId != null)
                {
                    var item = piId.GetValue(atmlObject, null) as ItemDescription;
                    if (item != null)
                    {
                        piId = item.GetType().GetProperty("Identification");
                        if (piId != null)
                        {
                            identification = piId.GetValue(item, null) as ItemDescriptionIdentification;
                        }
                    }
                }
            }
            return(identification);
        }
        public bool Save(IAtmlObject atmlObject)
        {
            bool results          = false;
            bool localTransaction = false;
            bool isDeleted        = atmlObject.IsDeleted();
            var  dao = new DocumentDAO();

            try
            {
                if (!dao.IsInTransaction)
                {
                    dao.StartTransaction();
                    localTransaction = true;
                }
                string uuid = atmlObject.GetAtmlId();
                if (!string.IsNullOrWhiteSpace(uuid))
                {
                    byte[] content  = Serialize(atmlObject);
                    var    document = new Document();
                    if (dao.hasDocument(uuid))
                    {
                        document           = new Document(dao.openDatabaseDocument(uuid));
                        document.DataState = atmlObject.IsDeleted()
                                                 ? BASEBean.eDataState.DS_DELETE
                                                 : BASEBean.eDataState.DS_EDIT;
                    }
                    else
                    {
                        document.DataState    = BASEBean.eDataState.DS_ADD;
                        document.ContentType  = atmlObject.GetAtmlFileContext();
                        document.DocumentType = atmlObject.GetAtmlDocumentType();
                        document.uuid         = uuid;
                    }
                    string originalFileName = document.name;
                    document.name            = atmlObject.GetAtmlFileName();
                    document.Description     = atmlObject.GetAtmlFileDescription();
                    document.DocumentContent = content;
                    Save(document);

                    //-----------------------------------------------------------------------------//
                    //--- Save Asset Numbers - these include all model numbers and part numbers ---//
                    //-----------------------------------------------------------------------------//
                    ItemDescriptionIdentification identification = DetermineIdentification(atmlObject, dao, uuid);
                    if (identification != null && !isDeleted)
                    //When Deleting - let the cascade delete handle the related records.
                    {
                        SaveAssets(atmlObject, identification, dao, uuid);
                    }

                    //------------------------------------//
                    //--- Save Capabilities Separately ---//
                    //------------------------------------//
                    PropertyInfo piCapabilities = atmlObject.GetType().GetProperty("Capabilities");
                    if (piCapabilities != null)
                    {
                        var capabilities = piCapabilities.GetValue(atmlObject, null) as Capabilities;
                        SaveCapabilities(atmlObject, capabilities);
                    }

                    if (localTransaction)
                    {
                        dao.CommitTransaction();
                    }
                    results = true;
                    if (!string.IsNullOrEmpty(originalFileName) &&
                        !originalFileName.Equals(atmlObject.GetAtmlFileName()))
                    {
                        OnAtmlObjectNameChanged(originalFileName, atmlObject.GetAtmlFileName(), uuid,
                                                atmlObject.GetAtmlFileType());
                    }

                    //-------------------------------------------------------------------------------------//
                    //--- We only want to save a file to the project directory if a project is open and ---//
                    //--- the file name contains the project name. This will only occur for the Test    ---//
                    //--- Configuration and the Test Description documents.                             ---//
                    //-------------------------------------------------------------------------------------//
                    if (ATMLContext.HasProjectOpen && document.name.Contains(ATMLContext.CurrentProjectName))
                    {
                        FileManager.WriteFile(Path.Combine(ATMLContext.ProjectAtmlPath, document.name),
                                              document.DocumentContent);
                    }
                }
            }
            catch (Exception e)
            {
                LogManager.Error(e);
                if (localTransaction)
                {
                    dao.RollbackTransaction();
                }
            }
            return(results);
        }