// check if schema version in Suggestion database and WorkflowConstants match public bool CheckSchemaVersion() { var match = Versions.Any(v => v.VersionType == DatabaseVersion.Schema && v.VersionString == WorkflowConstants.SchemaVersion); if (match == false) { TraceLog.TraceError(String.Format("Suggestions database schema version {0} not found", WorkflowConstants.SchemaVersion)); } return(match); }
// add an operation to the Operations table public Operation CreateOperation(User user, string opType, int?code, object body, object oldBody, string session = null) { Operation operation = null; try { // add the operation to the Operations table string name; Type bodyType = body.GetType(); Guid id = (Guid)bodyType.GetProperty("ID").GetValue(body, null); if (body is Suggestion) { // Suggestion does not have a Name property, use GroupDisplayName property name = (string)bodyType.GetProperty("GroupDisplayName").GetValue(body, null); } else { name = (string)bodyType.GetProperty("Name").GetValue(body, null); } operation = new Operation() { ID = Guid.NewGuid(), UserID = user.ID, Username = user.Name, EntityID = id, EntityName = name, EntityType = bodyType.Name, OperationType = opType, StatusCode = (int?)code, Body = JsonSerializer.Serialize(body), OldBody = JsonSerializer.Serialize(oldBody), Session = session, Timestamp = DateTime.Now }; Operations.Add(operation); if (SaveChanges() < 1) { // log failure to record operation TraceLog.TraceError("Failed to record operation: " + opType); } } catch (Exception ex) { // log failure to record operation TraceLog.TraceException("Failed to record operation", ex); } return(operation); }
IAuthorizationState GetAccessToken(WebServerClient client) { IAuthorizationState state = new AuthorizationState(GoogleClient.Scopes); UserCredential googleConsent = user.GetCredential(UserCredential.GoogleConsent); if (googleConsent != null) { TimeSpan difference = googleConsent.AccessTokenExpiration.Value - DateTime.UtcNow; if (difference.TotalMinutes < 5) { // token is expired or will expire within 5 minutes, refresh token googleConsent = RenewAccessToken(googleConsent); } state.AccessToken = googleConsent.AccessToken; } else { TraceLog.TraceError("Google access token is not available"); } return(state); }
public static List <Intent> DefaultIntents() { try { if (!File.Exists(IntentsFileName)) { TraceLog.TraceError("Intents file not found"); return(null); } // load intents from file var intents = new List <Intent>(); using (var file = File.Open(IntentsFileName, FileMode.Open, FileAccess.Read)) using (var reader = new StreamReader(file)) { string intentDef = reader.ReadLine(); while (!String.IsNullOrEmpty(intentDef)) { string[] parts = intentDef.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 3) { continue; } intents.Add(new Intent() { Verb = parts[0], Noun = parts[1], WorkflowType = parts[2] }); intentDef = reader.ReadLine(); } } return(intents); } catch (Exception ex) { TraceLog.TraceException("Reading intents failed", ex); return(null); } }
public static bool DeleteMessage(object message) { var msg = message as CloudQueueMessage; if (msg == null) { TraceLog.TraceError("Wrong message type: " + message.GetType().Name); return(false); } try { TraceLog.TraceInfo("Deleting message ID " + msg.Id); Queue.DeleteMessage(msg); return(true); } catch (Exception ex) { TraceLog.TraceException("DeleteMessage failed", ex); return(false); } }
public override bool ProcessUpdate(Item oldItem, Item newItem) { // base handles ItemType changing if (base.ProcessUpdate(oldItem, newItem)) { return(true); } if (newItem.Name != oldItem.Name) { // name changed, process like new item ProcessCreate(newItem); return(true); } // if the facebook ID is set or changed, retrieve FB info var fbfv = newItem.GetFieldValue(FieldNames.FacebookID); if (fbfv != null && fbfv.Value != null) { // the old category must not exist or the value must have changed var oldfbfv = oldItem.GetFieldValue(FieldNames.FacebookID); if (oldfbfv == null || oldfbfv.Value != fbfv.Value) { FBGraphAPI fbApi = new FBGraphAPI(); User userWithCreds = storage.GetUser(user.ID, true); UserCredential cred = userWithCreds.GetCredential(UserCredential.FacebookConsent); if (cred != null && cred.AccessToken != null) { fbApi.AccessToken = cred.AccessToken; } else { TraceLog.TraceError(FacebookHelper.TRACE_NO_FB_TOKEN); return(false); } // get or create an EntityRef in the UserFolder EntityRefs list var entityRefItem = storage.UserFolder.GetEntityRef(user, newItem); if (entityRefItem == null) { TraceLog.TraceError(FacebookHelper.TRACE_NO_CONTACT_ENTITYREF); return(false); } // get the contact's profile information from facebook try { // this is written as a foreach because the Query API returns an IEnumerable, but there is only one result foreach (var contact in fbApi.Query(fbfv.Value, FBQueries.BasicInformation)) { newItem.GetFieldValue(FieldNames.Picture, true).Value = String.Format("https://graph.facebook.com/{0}/picture", fbfv.Value); var birthday = (string)contact[FBQueryResult.Birthday]; if (birthday != null) { newItem.GetFieldValue(FieldNames.Birthday, true).Value = birthday; } var gender = (string)contact[FBQueryResult.Gender]; if (gender != null) { entityRefItem.GetFieldValue(FieldNames.Gender, true).Value = gender; } } //userContext.SaveChanges(); } catch (Exception ex) { TraceLog.TraceException(FacebookHelper.TRACE_NO_SAVE_FBCONTACTINFO, ex); } return(true); } } return(false); }
// update constants in Suggestion database to current version defined in WorkflowConstants public bool VersionConstants(string me) { try { bool updateDB = false; if (Versions.Any(v => v.VersionType == DatabaseVersion.Constants && v.VersionString == WorkflowConstants.ConstantsVersion) == false) { // no database - create and lock the new version entry TraceLog.TraceInfo(String.Format("Suggestions database version {0} not found", WorkflowConstants.ConstantsVersion)); // remove any existing database version (there should never be more than one) foreach (var existingVersion in Versions.Where(v => v.VersionType == DatabaseVersion.Constants).ToList()) { Versions.Remove(existingVersion); } SaveChanges(); // create the new version entry DatabaseVersion ver = new DatabaseVersion() { VersionType = DatabaseVersion.Constants, VersionString = WorkflowConstants.ConstantsVersion, Status = me }; Versions.Add(ver); SaveChanges(); updateDB = true; } else { var dbVersion = Versions.Single(v => v.VersionType == DatabaseVersion.Constants && v.VersionString == WorkflowConstants.ConstantsVersion); if (dbVersion.Status == DatabaseVersion.Corrupted) { // try to update the database again - take a lock TraceLog.TraceInfo("Suggestions database corrupted"); dbVersion.Status = me; SaveChanges(); updateDB = true; } } if (updateDB == false) { TraceLog.TraceInfo(String.Format("Suggestions database version {0} is up to date", WorkflowConstants.ConstantsVersion)); return(true); } } catch (Exception ex) { TraceLog.TraceException("Could not find database version", ex); return(false); } // update the default database values DatabaseVersion version = null; SuggestionsStorageContext versionContext = Storage.NewSuggestionsContext; try { // verify that this unit of execution owns the update lock for the database version version = versionContext.Versions.Single(v => v.VersionType == DatabaseVersion.Constants && v.VersionString == WorkflowConstants.ConstantsVersion); if (version.Status != me) { return(true); } TraceLog.TraceInfo(String.Format("{0} updating Suggestions database to version {1}", me, WorkflowConstants.ConstantsVersion)); // remove existing intents foreach (var entity in Intents.ToList()) { Intents.Remove(entity); } var intents = WorkflowConstants.DefaultIntents(); if (intents == null) { TraceLog.TraceError("Could not find or load intents"); version.Status = DatabaseVersion.Corrupted; versionContext.SaveChanges(); return(false); } // add current intents foreach (var entity in intents) { Intents.Add(entity); } SaveChanges(); TraceLog.TraceInfo("Replaced intents"); // remove existing workflow types foreach (var entity in WorkflowTypes.ToList()) { WorkflowTypes.Remove(entity); } var workflowTypes = WorkflowConstants.DefaultWorkflowTypes(); if (workflowTypes == null) { TraceLog.TraceError("Could not find or load workflow definitions"); version.Status = DatabaseVersion.Corrupted; versionContext.SaveChanges(); return(false); } // add current workflow types foreach (var entity in workflowTypes) { WorkflowTypes.Add(entity); } SaveChanges(); TraceLog.TraceInfo("Replaced workflow types"); // save the new version number version.Status = DatabaseVersion.OK; versionContext.SaveChanges(); return(true); } catch (Exception ex) { TraceLog.TraceException("VersionConstants failed", ex); // mark the version as corrupted version.Status = DatabaseVersion.Corrupted; versionContext.SaveChanges(); return(false); } }