501 lines
18 KiB
C#
501 lines
18 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Security.Cryptography;
|
|
using biz = GZTW.AyaNova.BLL;
|
|
|
|
|
|
|
|
namespace AyaNova.PlugIn.V8
|
|
{
|
|
class util
|
|
{
|
|
public static GZTW.AyaNova.BLL.LocalizedTextTable LocaleText = null;
|
|
const string TEST_ROUTE = "ServerInfo";
|
|
const string API_BASE_ROUTE = "api/v8/";
|
|
static HttpClient client = new HttpClient();
|
|
//url once known to be good
|
|
internal static string ApiBaseUrl { get; set; }
|
|
internal static string JWT { get; set; }
|
|
public static bool Initialized { get; set; }
|
|
|
|
|
|
|
|
public util()
|
|
{
|
|
Initialized = false;
|
|
JWT = string.Empty;
|
|
}
|
|
|
|
|
|
#region INIT / AUTH
|
|
private static void InitClient()
|
|
{
|
|
if (Initialized) return;
|
|
client.BaseAddress = new Uri(ApiBaseUrl);
|
|
client.DefaultRequestHeaders.Accept.Clear();
|
|
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
Initialized = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Only a return value of "OK" is ok
|
|
/// </summary>
|
|
/// <param name="serverUrl"></param>
|
|
/// <returns></returns>
|
|
public static async Task<string> TestUrlAsync(string serverUrl)
|
|
{
|
|
if (string.IsNullOrEmpty(serverUrl)) return "Server url required";
|
|
|
|
if (!serverUrl.Contains("/api/"))
|
|
{
|
|
if (!serverUrl.EndsWith("/")) serverUrl += "/";
|
|
serverUrl += API_BASE_ROUTE;
|
|
}
|
|
|
|
if (!serverUrl.StartsWith("http")) serverUrl = "http://" + serverUrl;
|
|
|
|
//try to connect, ping the server api
|
|
if (!Initialized)
|
|
{
|
|
client.BaseAddress = new Uri(serverUrl);
|
|
client.DefaultRequestHeaders.Accept.Clear();
|
|
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
Initialized = true;
|
|
}
|
|
|
|
try
|
|
{
|
|
HttpResponseMessage response = await client.GetAsync(serverUrl + TEST_ROUTE);
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var ret = await response.Content.ReadAsStringAsync();
|
|
if (ret.Contains("AyaNova"))
|
|
{
|
|
ApiBaseUrl = serverUrl;
|
|
return "OK";
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return "Failed: " + response.StatusCode.ToString();
|
|
}
|
|
}
|
|
catch { return "Failed"; }
|
|
|
|
|
|
return "failed";
|
|
}
|
|
|
|
|
|
|
|
|
|
public async static Task<bool> AuthenticateAsync(string login, string password = null)
|
|
{
|
|
InitClient();
|
|
|
|
if (password == null)
|
|
password = login;
|
|
|
|
dynamic creds = new JObject();
|
|
creds.login = login;
|
|
creds.password = password;
|
|
|
|
ApiResponse a = await PostAsync("Auth", creds.ToString());
|
|
|
|
if (a.HttpResponse.IsSuccessStatusCode)
|
|
{
|
|
JWT = a.ObjectResponse["data"]["token"].Value<string>();
|
|
return true;
|
|
}
|
|
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async static Task<ApiResponse> GetAsync(string route)
|
|
{
|
|
var requestMessage = new HttpRequestMessage(HttpMethod.Get, route);
|
|
if (!string.IsNullOrWhiteSpace(JWT))
|
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT);
|
|
|
|
HttpResponseMessage response = await client.SendAsync(requestMessage);
|
|
var responseAsString = await response.Content.ReadAsStringAsync();
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
throw new Exception("GET error, route: " + route + "\n" + responseAsString + "\n" + response.ReasonPhrase);
|
|
}
|
|
else
|
|
return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) };
|
|
}
|
|
|
|
//
|
|
public async static Task<ApiResponse> PostAsync(string route, string postJson = null)
|
|
{
|
|
|
|
var requestMessage = new HttpRequestMessage(HttpMethod.Post, route);
|
|
if (!string.IsNullOrWhiteSpace(JWT))
|
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT);
|
|
|
|
if (!string.IsNullOrWhiteSpace(postJson))
|
|
requestMessage.Content = new StringContent(postJson, System.Text.Encoding.UTF8, "application/json");
|
|
|
|
HttpResponseMessage response = await client.SendAsync(requestMessage);
|
|
var responseAsString = await response.Content.ReadAsStringAsync();
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
throw new Exception("POST error, route: " + route + "\n" + responseAsString + "\n" + response.ReasonPhrase);
|
|
}
|
|
else
|
|
return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) };
|
|
}
|
|
|
|
|
|
|
|
public async static Task<ApiResponse> PutAsync(string route, string putJson = null)
|
|
{
|
|
|
|
|
|
var requestMessage = new HttpRequestMessage(HttpMethod.Put, route);
|
|
if (!string.IsNullOrWhiteSpace(JWT))
|
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT);
|
|
|
|
if (!string.IsNullOrWhiteSpace(putJson))
|
|
requestMessage.Content = new StringContent(putJson, System.Text.Encoding.UTF8, "application/json");
|
|
|
|
HttpResponseMessage response = await client.SendAsync(requestMessage);
|
|
var responseAsString = await response.Content.ReadAsStringAsync();
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
throw new Exception("PUT error, route: " + route + "\n" + responseAsString + "\n" + response.ReasonPhrase);
|
|
}
|
|
else
|
|
return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) };
|
|
}
|
|
|
|
|
|
public async static Task<ApiResponse> PostFormDataAsync(string route, MultipartFormDataContent formContent)
|
|
{
|
|
var requestMessage = new HttpRequestMessage(HttpMethod.Post, route);
|
|
if (!string.IsNullOrWhiteSpace(JWT))
|
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT);
|
|
|
|
requestMessage.Content = formContent;
|
|
|
|
HttpResponseMessage response = await client.SendAsync(requestMessage);
|
|
var responseAsString = await response.Content.ReadAsStringAsync();
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
throw new Exception("POST FORMDATA error, route: " + route + "\n" + responseAsString + "\n" + response.ReasonPhrase);
|
|
}
|
|
else
|
|
return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) };
|
|
}
|
|
|
|
//eoc
|
|
public class ApiResponse
|
|
{
|
|
public HttpResponseMessage HttpResponse { get; set; }
|
|
public JObject ObjectResponse { get; set; }
|
|
public string CompactResponse
|
|
{
|
|
get
|
|
{
|
|
return ObjectResponse.ToString(Newtonsoft.Json.Formatting.None);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//make an event log entry to preserve the original v7 history such as it is
|
|
public async static Task EventLog(AyaType RavenType, long RavenId, long RavenCreatorId, long RavenModifierId, string sCreated, string sModified)
|
|
{
|
|
DateTime Created = DateTime.UtcNow;
|
|
if (!string.IsNullOrWhiteSpace(sCreated))
|
|
Created = DateTime.Parse(sCreated).ToUniversalTime();
|
|
|
|
DateTime Modified = DateTime.UtcNow;
|
|
if (!string.IsNullOrWhiteSpace(sCreated))
|
|
Modified = DateTime.Parse(sModified).ToUniversalTime();
|
|
|
|
dynamic d = new JObject();
|
|
d.ayType = (int)RavenType;
|
|
d.ayId = RavenId;
|
|
d.creator = RavenCreatorId;
|
|
d.modifier = RavenModifierId;
|
|
d.created = Created;
|
|
d.modified = Modified;
|
|
|
|
await PostAsync("EventLog/v7", d.ToString());
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="jsonString"></param>
|
|
/// <returns></returns>
|
|
public static JObject Parse(string jsonString)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(jsonString))
|
|
{
|
|
return null;
|
|
}
|
|
return JObject.Parse(jsonString);
|
|
}
|
|
|
|
|
|
public static long IdFromResponse(ApiResponse a)
|
|
{
|
|
return a.ObjectResponse["data"]["id"].Value<long>();
|
|
}
|
|
|
|
public static uint CTokenFromResponse(ApiResponse a)
|
|
{
|
|
return a.ObjectResponse["data"]["concurrencyToken"].Value<uint>();
|
|
}
|
|
#endregion
|
|
|
|
#region Misc utils
|
|
|
|
//used to set nonsense values from imported user login and password
|
|
//just in case they are set active after import but never have their passwords set
|
|
//so they can't be as easily hacked into
|
|
public static string RandomString()
|
|
{
|
|
var b = new byte[32];
|
|
var random = RandomNumberGenerator.Create();
|
|
random.GetNonZeroBytes(b);
|
|
return Convert.ToBase64String(b);
|
|
}
|
|
|
|
public static string NormalizeTag(string inObj)
|
|
{
|
|
//Must be lowercase per rules
|
|
//This may be naive when we get international customers but for now supporting utf-8 and it appears it's safe to do this with unicode
|
|
inObj = inObj.ToLowerInvariant();
|
|
//No spaces in tags, replace with dashes
|
|
inObj = inObj.Replace(" ", "-");
|
|
//Remove multiple dash sequences
|
|
inObj = System.Text.RegularExpressions.Regex.Replace(inObj, "-+", "-");
|
|
//Ensure doesn't start or end with a dash
|
|
inObj = inObj.Trim('-');
|
|
//No longer than 255 characters
|
|
inObj = MaxLength(inObj, 255);
|
|
return inObj;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Trim a string if necessary
|
|
/// </summary>
|
|
/// <param name="s"></param>
|
|
/// <param name="maxLength"></param>
|
|
/// <returns></returns>
|
|
public static string MaxLength(string s, int maxLength)
|
|
{
|
|
if (s.Length > maxLength)
|
|
s = s.Substring(0, maxLength);
|
|
return s;
|
|
}
|
|
|
|
public enum AyaUiFieldDataType : int
|
|
{
|
|
NoType = 0,
|
|
DateTime = 1,
|
|
Date = 2,
|
|
Time = 3,
|
|
Text = 4,
|
|
Integer = 5,
|
|
Bool = 6,
|
|
Decimal = 7,
|
|
Currency = 8,
|
|
Tags = 9,
|
|
Enum = 10,
|
|
EmailAddress = 11
|
|
|
|
}
|
|
|
|
|
|
public static AyaType RootObjectToAyaType(biz.RootObjectTypes rot, biz.WorkorderTypes specificWoType = biz.WorkorderTypes.Unknown)
|
|
{
|
|
|
|
switch (rot)
|
|
{
|
|
case biz.RootObjectTypes.User:
|
|
return AyaType.User;
|
|
case biz.RootObjectTypes.Client:
|
|
return AyaType.Customer;
|
|
case biz.RootObjectTypes.Contract:
|
|
return AyaType.Contract;
|
|
case biz.RootObjectTypes.HeadOffice:
|
|
return AyaType.HeadOffice;
|
|
case biz.RootObjectTypes.LoanItem:
|
|
return AyaType.LoanUnit;
|
|
case biz.RootObjectTypes.Part:
|
|
return AyaType.Part;
|
|
case biz.RootObjectTypes.WorkorderPreventiveMaintenance:
|
|
return AyaType.PM;
|
|
case biz.RootObjectTypes.WorkorderItem:
|
|
switch (specificWoType)
|
|
{
|
|
case biz.WorkorderTypes.PreventiveMaintenance:
|
|
return AyaType.PMItem;
|
|
case biz.WorkorderTypes.Quote:
|
|
return AyaType.QuoteItem;
|
|
case biz.WorkorderTypes.Service:
|
|
return AyaType.WorkOrderItem;
|
|
default:
|
|
throw new NotImplementedException("V8:util:RootObjectToAyaType -> type " + rot.ToString() + "," + specificWoType.ToString() + " is not coded yet");
|
|
}
|
|
|
|
|
|
case biz.RootObjectTypes.Project:
|
|
return AyaType.Project;
|
|
case biz.RootObjectTypes.PurchaseOrder:
|
|
return AyaType.PurchaseOrder;
|
|
case biz.RootObjectTypes.Unit:
|
|
return AyaType.Unit;
|
|
case biz.RootObjectTypes.UnitModel:
|
|
return AyaType.UnitModel;
|
|
case biz.RootObjectTypes.Vendor:
|
|
return AyaType.Vendor;
|
|
|
|
default:
|
|
throw new NotImplementedException("V8:util:RootObjectToAyaType -> type " + rot.ToString() + " is not coded yet");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// All AyaNova types and their attributes indicating what features that type will support (tagging, attachments etc)
|
|
/// </summary>
|
|
public enum AyaType : int
|
|
{
|
|
//COREBIZOBJECT attribute must be set on objects that are:
|
|
//Attachable objects can have attachments,
|
|
//wikiable objects can have a wiki
|
|
//reviewable objects can have a review which is basically the same as a Reminder but with an object attached (was follow up schedmarker in v7)
|
|
//PIckList-able (has picklist template)
|
|
//Pretty much everything that represents some kind of real world object is wikiable or attachable as long as it has an ID and a type
|
|
//exceptions would be utility type objects like datalistview, formcustom etc that are not
|
|
|
|
//NOTE: NEW CORE OBJECTS - All areas of server AND CLIENT code that require adding any new core objects have been tagged with the following comment:
|
|
//CoreBizObject add here
|
|
//Search for that IN SERVER AND CLIENT CODE and you will see all areas that need coding for the new object
|
|
|
|
//***IMPORTANT: Also need to add translations for any new biz objects added that don't match exactly the name here in the key
|
|
//because enumlist gets it that way, i.e. "Global" would be the expected key
|
|
|
|
NoType = 0,
|
|
Global = 1,
|
|
//Corebiz
|
|
Widget = 2,//attachable, wikiable, reviewable
|
|
//Corebiz
|
|
User = 3,//attachable, wikiable, reviewable
|
|
ServerState = 4,
|
|
License = 5,
|
|
LogFile = 6,
|
|
PickListTemplate = 7,
|
|
//Corebiz
|
|
Customer = 8,
|
|
ServerJob = 9,
|
|
//Corebiz
|
|
Contract = 10,
|
|
TrialSeeder = 11,
|
|
Metrics = 12,
|
|
Translation = 13,
|
|
UserOptions = 14,
|
|
//Corebiz
|
|
HeadOffice = 15,
|
|
//Corebiz
|
|
LoanUnit = 16,
|
|
FileAttachment = 17,
|
|
DataListView = 18,
|
|
FormCustom = 19,
|
|
//Corebiz
|
|
Part = 20,
|
|
//Corebiz
|
|
PM = 21,
|
|
//Corebiz
|
|
PMItem = 22,
|
|
//Corebiz
|
|
PMTemplate = 23,
|
|
//Corebiz
|
|
PMTemplateItem = 24,
|
|
//Corebiz
|
|
Project = 25,
|
|
//Corebiz
|
|
PurchaseOrder = 26,
|
|
//Corebiz
|
|
Quote = 27,
|
|
//Corebiz
|
|
QuoteItem = 28,
|
|
//Corebiz
|
|
QuoteTemplate = 29,
|
|
//Corebiz
|
|
QuoteTemplateItem = 30,
|
|
//Corebiz
|
|
Unit = 31,
|
|
//Corebiz
|
|
UnitModel = 32,
|
|
//Corebiz
|
|
Vendor = 33,
|
|
//Corebiz
|
|
WorkOrder = 34,
|
|
//Corebiz
|
|
WorkOrderItem = 35,
|
|
//Corebiz
|
|
WorkOrderTemplate = 36,
|
|
//Corebiz
|
|
WorkOrderTemplateItem = 37
|
|
|
|
|
|
//NOTE: New objects added here need to also be added to the following classes:
|
|
//AyaNova.Biz.BizObjectExistsInDatabase
|
|
//AyaNova.Biz.BizObjectFactory
|
|
//AyaNova.Biz.BizRoles
|
|
//AyaNova.Biz.BizObjectNameFetcherDIRECT
|
|
//and in the CLIENT in ayatype.js
|
|
|
|
//and need TRANSLATION KEYS because any type could show in the event log at teh client end
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int GetOrderIndependentHashCode<T>(IEnumerable<T> source)
|
|
{
|
|
int hash = 0;
|
|
foreach (T element in source)
|
|
{
|
|
hash = unchecked(hash +
|
|
EqualityComparer<T>.Default.GetHashCode(element));
|
|
}
|
|
return hash;
|
|
}
|
|
|
|
|
|
public class NameIdItem
|
|
{
|
|
public long Id { get; set; }
|
|
public string Name { get; set; }
|
|
}
|
|
#endregion
|
|
|
|
}//eoc
|
|
|
|
|
|
}//ens
|