예제 #1
0
        public void Save()
        {
            string content        = Serialize();
            var    document       = new dbDocument();
            var    dao            = new DocumentDAO();
            bool   documentExists = dao.hasDocument(uuid);

            //if( !documentExists )
            //    LogManager.Trace("Creating new Test Adapter Description with uuid of {0}", uuid);
            //else
            //    LogManager.Trace("Saving Test Adapter Description with uuid of {0}", uuid);

            document.contentType         = "text/xml";
            document.documentDescription = "Test Adapter";
            document.documentName        = Identification.ModelName;
            document.documentVersion     = version;
            document.documentSize        = content.Length;
            document.documentTypeId      = (int)dbDocument.DocumentType.TEST_ADAPTER_DESCRIPTION;
            document.documentContent     = Encoding.UTF8.GetBytes(content);
            document.UUID      = Guid.Parse(uuid);
            document.DataState = documentExists ? BASEBean.eDataState.DS_EDIT : BASEBean.eDataState.DS_ADD;
            document.save();

            foreach (IdentificationNumber idNumber in Identification.IdentificationNumbers)
            {
                string type   = Enum.GetName(typeof(IdentificationNumberType), idNumber.type);
                string number = idNumber.number;
                var    asset  = new AssetIdentificationBean();
                asset.assetNumber = number;
                asset.assetType   = type;
                asset.uuid        = Guid.Parse(uuid);
                asset.DetermineDataState();
                asset.save();
            }
        }
        protected void SaveAssets(IAtmlObject atmlObject,
                                  ItemDescriptionIdentification identification,
                                  DocumentDAO dao,
                                  string uuid)
        {
            if (identification != null)
            {
                //----------------------------------------------------------------------------------//
                //--- Get existing assets for this entity and remove those assets already listed ---//
                //--- whatever is left over in the list can be deleted. These would be those     ---//
                //--- assets that may have been deleted or renamed.                              ---//
                //----------------------------------------------------------------------------------//
                Dictionary <object, AssetIdentificationBean> existingAssets = dao.GetAssetsByUuid(uuid);
                var    asset   = new AssetIdentificationBean();
                string modelNo = atmlObject.GetAtmlName();
                if (!string.IsNullOrEmpty(modelNo))
                {
                    modelNo = modelNo.Trim();
                }
                if (modelNo != null && existingAssets.ContainsKey(modelNo))
                {
                    asset.ID = existingAssets[atmlObject.GetAtmlName()].ID;
                    existingAssets.Remove(modelNo);
                }
                asset.assetNumber = modelNo;
                asset.assetType   = "Model";
                asset.uuid        = Guid.Parse(uuid);
                if (atmlObject.IsDeleted())
                {
                    asset.DataState = BASEBean.eDataState.DS_DELETE;
                }
                else
                {
                    asset.DetermineDataState();
                }
                asset.save();
                if (identification.IdentificationNumbers != null)
                {
                    foreach (IdentificationNumber idNumber in identification.IdentificationNumbers)
                    {
                        string type   = Enum.GetName(typeof(IdentificationNumberType), idNumber.type);
                        string number = idNumber.number;
                        if (!string.IsNullOrEmpty(number))
                        {
                            number = number.Trim();
                        }

                        //-------------------------------------------------------------------------------------------------------------//
                        //--- There is no need to save the asset if the model Number is duplicated as another identification number ---//
                        //-------------------------------------------------------------------------------------------------------------//
                        if (!number.Equals(modelNo))
                        {
                            asset = new AssetIdentificationBean();
                            if (existingAssets.ContainsKey(number))
                            {
                                asset.ID = existingAssets[number].ID;
                                existingAssets.Remove(number);
                            }
                            asset.assetNumber = number;
                            asset.assetType   = type;
                            asset.uuid        = Guid.Parse(uuid);
                            if (atmlObject.IsDeleted())
                            {
                                asset.DataState = BASEBean.eDataState.DS_DELETE;
                            }
                            else
                            {
                                asset.DetermineDataState();
                            }
                            try
                            {
                                asset.save();
                            }
                            catch (Exception exception)
                            {
                                string msg = exception.Message;
                                throw new Exception(
                                          string.Format("Failed adding the identifier \"{0}\" as a new asset. Error: {1}",
                                                        idNumber,
                                                        msg.Contains("duplicate")
                                                       ? "The Identification Number is already assigned to another piece of equipment."
                                                       : msg));
                            }
                        }
                    }
                }

                //----------------------------------------//
                //--- Remove Assets no longer attached ---//
                //----------------------------------------//
                foreach (AssetIdentificationBean assetBean in existingAssets.Values)
                {
                    assetBean.DataState = BASEBean.eDataState.DS_DELETE;
                    assetBean.save();
                }
            }
        }