public AssetsViewItemProxy GetAsset(int assetID) { AssetsViewItem asset = AssetsView.GetAssetsViewItem(TSAuthentication.GetLoginUser(), assetID); if (asset.OrganizationID != TSAuthentication.OrganizationID) { return(null); } return(asset.GetProxy()); }
public static string GetAssetsViewItem(RestCommand command, int assetID) { AssetsViewItem assetsViewItem = AssetsView.GetAssetsViewItem(command.LoginUser, assetID); if (assetsViewItem.OrganizationID != command.Organization.OrganizationID) { throw new RestException(HttpStatusCode.Unauthorized); } return(assetsViewItem.GetXml("Asset", true)); }
public static string CreateAsset(RestCommand command) { DateTime now = DateTime.UtcNow; Assets assets = new Assets(command.LoginUser); Asset asset = assets.AddNewAsset(); asset.OrganizationID = command.Organization.OrganizationID; asset.FullReadFromXml(command.Data, true); if (asset.ProductID == null) { throw new RestException(HttpStatusCode.BadRequest, "Product required"); } // For consistency all assets are created in the warehouse. asset.Location = "2"; // This is normally not necessary, but as the CreatorID is defined as a null field in this table it is needed. asset.CreatorID = command.LoginUser.UserID; asset.NeedsIndexing = true; asset.DateCreated = now; asset.DateModified = now; asset.Collection.Save(); asset.UpdateCustomFieldsFromXml(command.Data); string description = String.Format("Asset {0} created via API.", GetAssetReference(asset)); AssetHistory history = new AssetHistory(command.LoginUser); AssetHistoryItem historyItem = history.AddNewAssetHistoryItem(); historyItem.OrganizationID = command.Organization.OrganizationID; historyItem.Actor = command.LoginUser.UserID; historyItem.AssetID = asset.AssetID; historyItem.ActionTime = DateTime.UtcNow; historyItem.ActionDescription = "Asset created via API."; historyItem.ShippedFrom = 0; historyItem.ShippedTo = 0; historyItem.TrackingNumber = string.Empty; historyItem.ShippingMethod = string.Empty; historyItem.ReferenceNum = string.Empty; historyItem.Comments = string.Empty; historyItem.DateCreated = now; historyItem.DateModified = now; history.Save(); return(AssetsView.GetAssetsViewItem(command.LoginUser, asset.AssetID).GetXml("Asset", true)); }
override protected void GetNextRecord() { AssetsViewItem asset = AssetsView.GetAssetsViewItem(_loginUser, _itemIDList[_rowIndex]); _lastItemID = asset.AssetID; UpdatedItems.Add((int)_lastItemID); StringBuilder builder = new StringBuilder(); builder.AppendLine(asset.CreatorName + " " + asset.DisplayName + " " + asset.DateCreated + " " + asset.DateModified + " " + asset.ModifierName + " " + asset.Notes + " " + asset.ProductName + " " + asset.ProductVersionNumber + " " + asset.SerialNumber + " " + asset.WarrantyExpiration); AssetHistoryView assetHistoryView = new AssetHistoryView(_loginUser); assetHistoryView.LoadByAssetID(asset.AssetID); foreach (AssetHistoryViewItem assetHistoryViewItem in assetHistoryView) { builder.AppendLine(assetHistoryViewItem.ActionTime + " " + assetHistoryViewItem.ActionDescription + " " + assetHistoryViewItem.NameAssignedFrom + " " + assetHistoryViewItem.NameAssignedTo + " " + assetHistoryViewItem.TrackingNumber + " " + assetHistoryViewItem.ShippingMethod + " " + assetHistoryViewItem.ReferenceNum + " " + assetHistoryViewItem.Comments + " " + assetHistoryViewItem.ActorName + " " + assetHistoryViewItem.DateModified + " " + assetHistoryViewItem.ModifierName + " " + assetHistoryViewItem.DateCreated); } DocText = builder.ToString(); StringBuilder assetLocationString = new StringBuilder(); switch (asset.Location) { case "1": assetLocationString.Append("Assigned"); break; case "2": assetLocationString.Append("Warehouse"); break; case "3": assetLocationString.Append("Junkyard"); break; } _docFields.Clear(); AddDocField("AssetID", asset.AssetID); AddDocField("Location", assetLocationString.ToString()); AddDocField("SerialNumber", asset.SerialNumber); if (string.IsNullOrWhiteSpace(asset.Name)) { if (string.IsNullOrWhiteSpace(asset.SerialNumber)) { AddDocField("Name", asset.AssetID); DocDisplayName = asset.AssetID.ToString(); } else { AddDocField("Name", asset.SerialNumber); DocDisplayName = asset.SerialNumber; } } else { AddDocField("Name", asset.Name); DocDisplayName = asset.Name; } InventorySearchAsset assetItem = new InventorySearchAsset(asset); AddDocField("**JSON", JsonConvert.SerializeObject(assetItem)); CustomValues customValues = new CustomValues(_loginUser); customValues.LoadByReferenceType(_organizationID, ReferenceType.Assets, asset.AssetID); foreach (CustomValue value in customValues) { object o = value.Row["CustomValue"]; string s = o == null || o == DBNull.Value ? "" : o.ToString(); AddDocField(value.Row["Name"].ToString(), s); } DocFields = _docFields.ToString(); DocIsFile = false; DocName = asset.AssetID.ToString(); try { DocCreatedDate = (DateTime)asset.Row["DateCreated"]; DocModifiedDate = (DateTime)asset.Row["DateModified"]; } catch (Exception) { } }
public static string UpdateAsset(RestCommand command, int assetID) { Asset asset = Assets.GetAsset(command.LoginUser, assetID); if (asset == null || asset.OrganizationID != command.Organization.OrganizationID) { throw new RestException(HttpStatusCode.Unauthorized); } string originalLocation = asset.Location; asset.FullReadFromXml(command.Data, false); if (asset.ProductID == null) { throw new RestException(HttpStatusCode.BadRequest, "Product required"); } if ( String.IsNullOrEmpty(asset.Location.Trim()) || ( asset.Location != "1" && asset.Location != "2" && asset.Location != "3" ) ) { asset.Location = originalLocation; } //Location can not be changed via the WebApp. The only change we are allowing is to bring an asset back to the Warehouse from the Junkyard. else if (asset.Location == "1" && originalLocation != "1") { throw new RestException(HttpStatusCode.BadRequest, "Please use a POST against the /assets/{id}/assignments/ URL to assign an asset."); } else if (asset.Location == "2" && originalLocation == "1") { throw new RestException(HttpStatusCode.BadRequest, "Please use a DELETE against the /assets/{id}/assignments/ URL to return an asset."); } else if (asset.Location == "3" && originalLocation == "1") { throw new RestException(HttpStatusCode.BadRequest, "Please return an asset to the Warehouse before sending it to the Junkyard."); } else if (asset.Location == "3" && originalLocation == "2") { throw new RestException(HttpStatusCode.BadRequest, "Please use a DELETE against the /assets/{id}/ URL to assign an asset to the Junkyard."); } asset.Collection.Save(); asset.UpdateCustomFieldsFromXml(command.Data); if (asset.Location == "2" && originalLocation == "3") { DateTime now = DateTime.UtcNow; AssetHistory assetHistory = new AssetHistory(command.LoginUser); AssetHistoryItem assetHistoryItem = assetHistory.AddNewAssetHistoryItem(); assetHistoryItem.AssetID = assetID; assetHistoryItem.OrganizationID = command.LoginUser.OrganizationID; assetHistoryItem.ActionTime = DateTime.UtcNow; assetHistoryItem.ActionDescription = "Restore asset from Junkyard via API"; assetHistoryItem.ShippedFrom = -1; assetHistoryItem.ShippedFromRefType = -1; assetHistoryItem.ShippedTo = -1; assetHistoryItem.RefType = -1; assetHistoryItem.TrackingNumber = string.Empty; assetHistoryItem.ShippingMethod = string.Empty; assetHistoryItem.ReferenceNum = string.Empty; assetHistoryItem.DateCreated = now; assetHistoryItem.Actor = command.LoginUser.UserID; assetHistoryItem.DateModified = now; assetHistoryItem.ModifierID = command.LoginUser.UserID; assetHistory.Save(); } return(AssetsView.GetAssetsViewItem(command.LoginUser, assetID).GetXml("Asset", true)); }