internal void SendEmails(UserTDO user, EventTDO ev, PhotoAnnotation photoAnnotation, ICollection <string> permalinks) { this.Error = ""; foreach (GuestTDO guest in (IEnumerable <GuestTDO>)photoAnnotation.Guests) { this.SendEmailTo(user, ev, guest, permalinks); } }
internal static bool ReannotatePhoto(Photo photo, PhotoAnnotation photoAnnotation, FotoShoutDbContext db) { Event ev = photo.Event; ICollection <GuestTDO> tdos = photoAnnotation.Guests; if (tdos == null || tdos.Count == 0) { if (photo.GuestPhotos.Any()) { while (photo.GuestPhotos.Any()) { db.GuestPhotos.Remove(photo.GuestPhotos.FirstOrDefault()); } ClearAllGuests(photo); } photo.Rating = photoAnnotation.Rating; photo.Status = (byte)PhotoStatus.Unselected; photo.Submitted = null; photo.SubmittedBy = 0; photo.Error = ""; } else { ICollection <GuestPhoto> gps = photo.GuestPhotos; HashSet <GuestPhoto> removedGuests = new HashSet <GuestPhoto>(); foreach (GuestPhoto gp in gps) { GuestTDO tdo = null; if (string.IsNullOrEmpty(gp.Guest.Email)) { tdo = tdos.Where(t => t.GuestId == gp.Guest.GuestId).SingleOrDefault(); } else { tdo = tdos.Where(t => t.GuestId == gp.Guest.GuestId || (!string.IsNullOrEmpty(t.Email) && t.Email.Equals(gp.Guest.Email, StringComparison.InvariantCultureIgnoreCase))).SingleOrDefault(); } if (tdo == null) { removedGuests.Add(gp); } } foreach (GuestPhoto removedGuest in removedGuests) { gps.Remove(removedGuest); db.GuestPhotos.Remove(removedGuest); } if (!AnnotatePhoto(photo, photoAnnotation, db)) { return(false); } } return(true); }
// id to Guid // PUT api/PhotoAnnotation/1 public HttpResponseMessage PutPhotoAnnotation(Guid id, PhotoAnnotation photoAnnotation) { if (!ModelState.IsValid) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.Values.SelectMany(v => v.Errors))); } try { // Check that all constrains are met PhotoAnnotationService.CheckConstrains(photoAnnotation, true); Photo photo = db.Photos.Where(p => p.PhotoId == id).Include(p => p.Event.EventOption).SingleOrDefault(); bool successed = false; if (photo != null) { if (photo.Status == (byte)PhotoStatus.Submitted) { successed = PhotoAnnotationService.ReannotatePhoto(photo, photoAnnotation, db); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, string.Format("The photo status is {0}, so it is invalid for this request.", Enum.GetName(typeof(PhotoStatus), photo.Status)))); } } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "Photo not found.")); } if (successed) { db.SaveChanges(); // Restore the photo back to the event's folder if its status is back to Unselected if (photo.Status == (byte)PhotoStatus.Unselected) { PhotoAnnotationService.RestorePhoto(photo.Folder, Constants.STR_PROCESSED, photo.Filename); } } } catch (DbUpdateConcurrencyException ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, ex.ToString())); } catch (DbEntityValidationException ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.EntityValidationErrors)); } catch (ArgumentNullException ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NoContent, ex.Message)); } catch (Exception ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.ToString())); } return(Request.CreateResponse(HttpStatusCode.OK)); }
internal void SendEmails(UserTDO user, EventTDO ev, PhotoAnnotation photoAnnotation, string permalink) { Error = ""; ICollection <GuestTDO> guests = photoAnnotation.Guests; foreach (GuestTDO guest in guests) { SendEmailTo(user, ev, guest, permalink); } }
internal static void CheckConstrains(PhotoAnnotation photoAnnotation, bool reAnnotate) { if (photoAnnotation == null) { throw new ArgumentNullException("Data is empty."); } if (!reAnnotate && (photoAnnotation.Guests == null || photoAnnotation.Guests.Count == 0)) { throw new PhotoAnnotationException("A photo must have at least one guest prior to submit."); } }
private void SendEmails(UserTDO user, EventTDO ev, PhotoTDO photo, PhotoAnnotation photoAnnotation, string photoUrl) { if (ev.WebsiteId != null) { Website website = _fsWebService.Get <Website>("Websites/" + ev.WebsiteId, true); photoUrl = GeneratePhotoWebsiteUrl(photoUrl, website); // string.Format("{0}PhotoWebsite/{1}?website={2}", Regex.Replace(AppSettings.FsApiBaseAddress, @"api/", ""), photo.PhotoId, ev.WebsiteId); } ICollection <GuestTDO> guests = photoAnnotation.Guests; IEnumerable <PhotoEmail> list = this._fsWebService.GetList <PhotoEmail>("PhotoEmails/" + (object)photo.PhotoId, true); foreach (GuestTDO guestTdo in (IEnumerable <GuestTDO>)guests) { GuestTDO guest = guestTdo; bool flag = true; PhotoEmail postObject = list.Where(ee => ee.PhotoId == photo.PhotoId && ee.GuestId == guest.GuestId).FirstOrDefault(); if (postObject == null) { postObject = new PhotoEmail { EventId = ev.EventId, PhotoId = photo.PhotoId, GuestId = guest.GuestId }; flag = false; } else if (postObject.Status == (byte)1) { continue; } this._emailService.Error = ""; this._emailService.SendEmailTo(user, ev, guest, photoUrl); if (string.IsNullOrEmpty(this._emailService.Error)) { postObject.Status = (byte)1; postObject.Error = null; } else { postObject.Status = byte.MaxValue; postObject.Error = this._emailService.Error; } if (flag) { this._fsWebService.UploadString("PhotoEmails?photoEmailId=" + (object)postObject.PhotoEmailId, this.GeneratePostContent <PhotoEmail>(postObject), "PUT"); } else { this._fsWebService.UploadString("PhotoEmails", this.GeneratePostContent <PhotoEmail>(postObject), null); } } }
internal static bool AnnotatePhoto(Photo photo, PhotoAnnotation photoAnnotation, FotoShoutDbContext db) { // Updating photo rating photo.Rating = photoAnnotation.Rating; // Adding the list of guests to the photo Event ev = photo.Event; ICollection <GuestPhoto> gps = photo.GuestPhotos; ICollection <GuestTDO> tdos = photoAnnotation.Guests; foreach (GuestTDO tdo in tdos) { Guest guest = !string.IsNullOrEmpty(tdo.Email) ? GetGuestByEmail(ev.EventId, tdo.Email, db) : GetGuestById(ev.EventId, tdo.GuestId, db); if (guest == null) { guest = PhotoAnnotationService.GenerateGuest(tdo, photo.Event.EventOption); ev.Guests.Add(guest); } else { PhotoAnnotationService.UpdateGuest(guest, tdo, photo.Event.EventOption); } GuestPhoto guestPhoto = gps.Where(gp => gp.Guest.GuestId == guest.GuestId).SingleOrDefault(); if (guestPhoto == null) { guestPhoto = new GuestPhoto { Id = Guid.NewGuid(), Event_EventId = photo.Event.EventId, Photo = photo, Guest = guest, AuthorizePublish = tdo.AuthorizePublish }; gps.Add(guestPhoto); } else { guestPhoto.AuthorizePublish = tdo.AuthorizePublish; } } photo.Error = ""; photo.Status = (byte)PhotoStatus.Submitted; return(true); }
private void PublishPhotoToChannel(EventTDO ev, PhotoTDO photo, ChannelGroup channelGroup) { if (channelGroup.Fields == null || !channelGroup.Fields.Any()) { FotoShoutUtils.Log.LogManager.Info(_logger, string.Format("There is no broadcast fields in the {0} template.", channelGroup.Name)); return; } PhotoAnnotation annotation = _fsWebService.Get <PhotoAnnotation>("PhotoAnnotation/" + photo.PhotoId, true); FotoShoutUtils.Log.LogManager.Info(_logger, string.Format("Publishing the {0} photo to the {1} channel group...", photo.Filename, string.IsNullOrEmpty(channelGroup.Name) ? channelGroup.ID.ToString() : channelGroup.Name)); string filename = photo.Folder.EndsWith(Path.DirectorySeparatorChar.ToString()) ? photo.Folder : (photo.Folder + Path.DirectorySeparatorChar) + Constants.STR_PROCESSED + Path.DirectorySeparatorChar + photo.Filename; PostBroadcast bc = new PostBroadcast { TemplateID = channelGroup.ID, Status = "Pending Publish", Description = "FotoShout Broadcast", Name = Path.GetFileNameWithoutExtension(photo.Filename), ScheduledTime = DateTime.Now.ToShortDateString(), BroadcastFields = new List <BroadcastFieldValue>() }; foreach (BroadcastField bf in channelGroup.Fields) { if (bf.Name.Equals("Album Title", StringComparison.InvariantCultureIgnoreCase)) { BroadcastFieldValue bfv = new BroadcastFieldValue { IdToken = bf.IdToken, Value = string.IsNullOrEmpty(ev.PublishAlbum) ? ev.EventName : ev.PublishAlbum }; bc.BroadcastFields.Add(bfv); } else if (bf.Name.Equals("Album Description", StringComparison.InvariantCultureIgnoreCase)) { BroadcastFieldValue bfv = new BroadcastFieldValue { IdToken = bf.IdToken, Value = string.IsNullOrEmpty(ev.PublishAlbum) ? ev.EventName : ev.PublishAlbum }; bc.BroadcastFields.Add(bfv); } else if (bf.Name.Equals("Upload Photos", StringComparison.InvariantCultureIgnoreCase)) { string serverUri = _c9WebService.UploadFile("Media/Upload", filename); if (string.IsNullOrEmpty(serverUri)) { throw new PublishingException(string.Format("Unexpected error: Can not upload the {0} photo.", photo.Filename)); } BroadcastFieldValue bfv = new BroadcastFieldValue { IdToken = bf.IdToken, Value = serverUri }; bc.BroadcastFields.Add(bfv); } else if (bf.Name.Equals("Title", StringComparison.InvariantCultureIgnoreCase)) { BroadcastFieldValue bfv = new BroadcastFieldValue { IdToken = bf.IdToken, Value = bc.Name }; bc.BroadcastFields.Add(bfv); } else if (bf.Name.Equals("Image File", StringComparison.InvariantCultureIgnoreCase)) { string serverUri = _c9WebService.UploadFile("Media/Upload", filename); if (string.IsNullOrEmpty(serverUri)) { throw new PublishingException(string.Format("Unexpected error: Can not upload the {0} photo.", photo.Filename)); } BroadcastFieldValue bfv = new BroadcastFieldValue { IdToken = bf.IdToken, Value = serverUri }; bc.BroadcastFields.Add(bfv); } else if (bf.Name.Equals("File", StringComparison.InvariantCultureIgnoreCase)) { string serverUri = _c9WebService.UploadFile("Media/Upload", filename); if (string.IsNullOrEmpty(serverUri)) { throw new PublishingException(string.Format("Unexpected error: Can not upload the {0} photo.", photo.Filename)); } BroadcastFieldValue bfv = new BroadcastFieldValue { IdToken = bf.IdToken, Value = serverUri }; bc.BroadcastFields.Add(bfv); } } string ret = _c9WebService.UploadString("Broadcast", GeneratePostContent <PostBroadcast>(bc)); if (!string.IsNullOrEmpty(ret)) { EventBroadcast eventBroadcast = new EventBroadcast { BroadcastId = int.Parse(ret.Trim(new char[] { '\"' })), EventId = ev.EventId, PhotoId = photo.PhotoId, Status = 0 }; ret = _fsWebService.UploadString("EventBroadcasts", GeneratePostContent <EventBroadcast>(eventBroadcast)); } else { throw new PublishingException(string.Format("Unsuccessully publishing the \"{0}\" photo using the \"{1}\" channel group.", photo.Filename, channelGroup.Name)); } FotoShoutUtils.Log.LogManager.Info(_logger, string.Format("Published the {0} photo.", photo.Filename)); }
private void PostProcessEvent(EventTDO ev) { try { _emailService.EmailTemplate = null; if (_emailService.IsValid()) { if (ev.EmailTemplateId <= 0) { FotoShoutUtils.Log.LogManager.Error(_logger, "There is no email template assigned to the event, so there will be no email sent out to guests for this event."); return; } else { _emailService.EmailTemplate = _fsWebService.GetEmailTemplate((int)ev.EmailTemplateId); } } IEnumerable <PhotoTDO> unauthorizedPhotos = this._fsWebService.GetUnauthorizedPhotos(ev.EventId); foreach (PhotoTDO photo in unauthorizedPhotos) { PhotoAnnotation photoAnnotation = this._fsWebService.Get <PhotoAnnotation>("PhotoAnnotation/" + photo.PhotoId, true); if (photoAnnotation != null && photoAnnotation.Guests.Any <GuestTDO>() && this._emailService.EmailTemplate != null) { this.SendEmails(this.User, ev, photo, photoAnnotation, photo.Image); } } FotoShoutUtils.Log.LogManager.Info(_logger, string.Format("Getting broadcasts of the {0} event...", ev.EventName)); IEnumerable <EventBroadcast> broadcasts = _fsWebService.GetList <EventBroadcast>("EventBroadcasts/" + ev.EventId, true); if (!broadcasts.Any()) { FotoShoutUtils.Log.LogManager.Info(_logger, "There is no broadcast published recently."); return; } foreach (EventBroadcast bc in broadcasts) { PhotoAnnotation photoAnnotation = _fsWebService.Get <PhotoAnnotation>("PhotoAnnotation/" + bc.PhotoId, true); if (photoAnnotation != null && photoAnnotation.Guests.Any()) { FotoShoutUtils.Log.LogManager.Info(_logger, string.Format("Getting the permalinks for the broadcast {0}.", bc.BroadcastId)); try { IDictionary <string, string> permalinksDict = _c9WebService.GetPermaLinks(bc.BroadcastId); if (!permalinksDict.Any()) { FotoShoutUtils.Log.LogManager.Debug(_logger, string.Format("The broadcast with the id of {0} does not provide any perma-link. It might not be published yet.", bc.BroadcastId)); } else { bool updated = false; ICollection <string> newPermaLinks = new List <string>(); updated = UpdatePermaLinks(bc, permalinksDict, newPermaLinks); if (_emailService.EmailTemplate != null && updated) { _emailService.SendEmails(User, ev, photoAnnotation, newPermaLinks); bc.Error = _emailService.Error; if (string.IsNullOrEmpty(_emailService.Error)) { bc.Status = permalinksDict.ContainsKey("pending") ? (byte)EventBroadcastStatus.PublishPending : (byte)EventBroadcastStatus.Processed; } else { bc.Error = _emailService.Error; } updated = true; } else if (updated) { bc.Status = permalinksDict.ContainsKey("pending") ? (byte)EventBroadcastStatus.PublishPending : (byte)EventBroadcastStatus.Processed; } if (updated) { _fsWebService.UploadString("EventBroadcasts?broadcastId=" + bc.EventBroadcastId, GeneratePostContent <EventBroadcast>(bc), "PUT"); } } } catch (Exception ex) { FotoShoutUtils.Log.LogManager.Error(_logger, ex.ToString()); } } } } catch (Exception ex) { FotoShoutUtils.Log.LogManager.Error(_logger, ex.ToString()); } }
// id to Guid // POST api/PhotoAnnotation/1 public HttpResponseMessage PostPhotoAnnotation(Guid id, PhotoAnnotation photoAnnotation) { if (!ModelState.IsValid) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.Values.SelectMany(v => v.Errors))); } try { // Check that all constrains are met PhotoAnnotationService.CheckConstrains(photoAnnotation, false); Photo photo = db.Photos.Where(p => p.PhotoId == id).Include(p => p.Event.EventOption).SingleOrDefault(); bool succeeded = false; if (photo != null) { if (photo.Status == (byte)PhotoStatus.Selected) { LogManager.Info(PhotoAnnotationController._logger, string.Format("Annotating the photo {0} with the option {1} ...", id, photo.Event.EventOption.EventOptionId)); succeeded = PhotoAnnotationService.AnnotatePhoto(photo, photoAnnotation, db); LogManager.Info(PhotoAnnotationController._logger, string.Format("Annotated the photo {0} ...", id)); } else if (photo.Status == (byte)PhotoStatus.Unselected) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, "The photo need to be selected prior to be assigned to guests.")); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, string.Format("The photo status is {0}, so it can not be annotated.", Enum.GetName(typeof(PhotoStatus), photo.Status)))); } } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "Photo not found.")); } if (succeeded) { photo.Submitted = DateTime.Now; photo.SubmittedBy = CurrentUser.Id; //LogManager.Info(PhotoAnnotationController._logger, string.Format("Saving the photo {0} ...", id)); db.SaveChanges(); //LogManager.Info(PhotoAnnotationController._logger, string.Format("Saved the photo {0} ...", id)); // Move the photo to the Processed sub-folder if (photo.Status == (byte)PhotoStatus.Submitted) { //LogManager.Info(PhotoAnnotationController._logger, string.Format("Moving the photo {0} ...", id)); PhotoAnnotationService.MovePhoto(photo.Folder, Constants.STR_PROCESSED, photo.Filename); //LogManager.Info(PhotoAnnotationController._logger, string.Format("Moved the photo {0} ...", id)); } } } catch (DbUpdateConcurrencyException ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, ex.ToString())); } catch (DbEntityValidationException ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.EntityValidationErrors)); } catch (ArgumentNullException ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NoContent, ex.Message)); } catch (Exception ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.ToString())); } return(Request.CreateResponse(HttpStatusCode.OK)); }