public static string savePlayer(Player player) { string summary = ""; // commit changes to Scala playlist and update local data Player old = players.Find(Player.byID(player.id)); if (old == null) { Global.d("Scala.savePlayer got nonexistent player ID " + player.id); throw new ArgumentException("player ID " + player.id + " doesn't exist"); } // save changes to Scala List<int> oldIDs = new List<int>(); List<int> replaced = new List<int>(); foreach (Slide s in old.slides) oldIDs.Add(s.id); // create new messages foreach (Slide slide in player.slides) { if (!slide.changed) { oldIDs.Remove(slide.id); continue; } else { replaced.Add(slide.id); } // create new message Template template = templates.Find(Template.byID(slide.templateID)); messageTO message = new messageTO(); message.name = slide.name; message.templateId = slide.templateID; message.templateIdSpecified = true; List<messageDataFieldTO> fields = new List<messageDataFieldTO>(); for (int f=0; f < slide.fields.Count; f++) { Field field = slide.fields[f]; Field tfield = template.fields.Find(Field.byName(field.name)); if (tfield == null) continue; // no such field in template if (field.widget != null && field.widget is WidgetNone) continue; // no field value messageDataFieldTO fieldTO = new messageDataFieldTO(); fieldTO.id = tfield.templateFieldID; fieldTO.idSpecified = true; // use template field ID fieldTO.name = field.name; // this should live in the Widget class if (field.widget != null && !(field.widget is WidgetImage) && !(field.widget is WidgetVideo)) field.mediaID = field.widget.upload(); if (field.widget is WidgetTwitter && player.info.defaultTwitter == "") { // update default twitter handle WidgetTwitter wt = (WidgetTwitter)field.widget; player.info.defaultTwitter = wt.handles; Global.dataDirty = true; } fieldTO.value = field.mediaID.ToString(); fields.Add(fieldTO); } if (template.durationFieldID > 0) { messageDataFieldTO fieldTO = new messageDataFieldTO(); fieldTO.id = template.durationFieldID; fieldTO.name = "SlideTime"; fieldTO.value = slide.duration.ToString(); fields.Add(fieldTO); } // workaround braindead 1.x API issues in release 10 message.approvalStatus = ScalaWS.Message.approvalStatusEnum.DRAFT; message.approvalStatusSpecified = true; message = messageServ.create(message, fields.ToArray()); if (oldIDs.Contains(slide.id)) summary += " -- updated " + slide.id + "=>" + message.id + ": " + message.name; else summary += " -- added " + message.id + ": " + message.name; setApproval(message.id); messageServ.addCategory(message.id, true, managedCatID, true); // update IDs slide.id = message.id; slide.changed = false; } foreach (int id in oldIDs) { // deleted or replaced try { messageServ.delete(id, true); } catch (Exception e) { Global.d("Failed to delete slide " + id + ": " + e.Message); } thumbCache.Remove(id); if (!replaced.Contains(id)) summary += " -- deleted slide " + id; } // reorder messages in playlist ScalaWS.Playlist.playlistItemTO[] items = new ScalaWS.Playlist.playlistItemTO[player.slides.Count]; Dictionary<int, timeScheduleTO> schedules = new Dictionary<int, timeScheduleTO>(); for (int s=0; s < player.slides.Count; s++) { Slide slide = player.slides[s]; items[s] = new ScalaWS.Playlist.playlistItemTO(); items[s].mediaId = slide.id; items[s].mediaIdSpecified = true; if (slide.startDate != null || slide.stopDate != null) { items[s].useValidRange = true; items[s].useValidRangeSpecified = true; if (slide.startDate != null) { items[s].startValidDate = DateTime.Parse(slide.startDate); items[s].startValidDateSpecified = true; } if (slide.stopDate != null) { items[s].endValidDate = DateTime.Parse(slide.stopDate); items[s].endValidDateSpecified = true; } } /* // time range is disabled if (slide.startTime != null || slide.stopTime != null || (slide.days != null && slide.days.Count < 7)) { if (slide.startTime == null) slide.startTime = "00:00"; if (slide.stopTime == null) slide.stopTime = "24:00"; // no, it doesn't make sense timeScheduleTO sched = new timeScheduleTO(); sched.startTime = slide.startTime; sched.endTime = slide.stopTime; sched.days = slide.scalaDays(); schedules.Add(slide.id, sched); } */ } playlistServ.deleteAllPlaylistItems(player.id, true); foreach (ScalaWS.Playlist.playlistItemTO item in items) { try { ScalaWS.Playlist.playlistItemTO playlistItem = playlistServ.addPlaylistItem(player.id, true, item); if (schedules.ContainsKey(item.mediaId)) { playlistServ.addTimeSchedule(playlistItem.id, true, schedules[item.mediaId]); } } catch (Exception e) { Global.d("savePlayer: failed to add item " + item.id + " to playlist " + player.name + ": " + e); } } // save changes locally players.Remove(old); players.Add(player); if (old.info != player.info) { summary += " -- updated settings"; Global.dataDirty = true; } if (summary == "") summary = " -- reordered slides"; return summary; }
// get groups and player names from Scala public static void loadData() { Global.d("Scala.loadData"); players.Clear(); templates.Clear(); try { // find category ScalaWS.Playlist.searchCriteriaTO[] plcrit = new ScalaWS.Playlist.searchCriteriaTO[1]; plcrit[0] = new ScalaWS.Playlist.searchCriteriaTO(); plcrit[0].column = "name"; plcrit[0].value = Global.cfg("catManaged"); plcrit[0].restriction = ScalaWS.Playlist.restrictionEnum.EQUALS; plcrit[0].restrictionSpecified = true; ScalaWS.Playlist.categoryTO[] cats = playlistServ.listCategories(plcrit, null); if (cats.Length != 1) throw new Exception("Did not find managed category named '" + Global.cfg("catManaged") + "' (Web.config:catManaged)"); managedCatID = cats[0].id; // get templates in master category ScalaWS.Template.searchCriteriaTO[] tcrit = new ScalaWS.Template.searchCriteriaTO[1]; tcrit[0] = new ScalaWS.Template.searchCriteriaTO(); tcrit[0].column = "categoryId"; tcrit[0].value = managedCatID.ToString(); tcrit[0].restriction = ScalaWS.Template.restrictionEnum.EQUALS; tcrit[0].restrictionSpecified = true; templateTO[] temps = templateServ.list(tcrit, null); foreach (templateTO templateTO in temps) { try { Template template = new Template(templateTO.id, templateTO.name); templateDataFieldTO[] fields = templateServ.getFiles(template.id, true); foreach (templateDataFieldTO fieldTO in fields) { if (fieldTO.type != templateFieldTypeEnum.IMAGE) continue; Field field = new Field(fieldTO.name); field.templateFieldID = fieldTO.id; template.fields.Add(field); } fields = templateServ.getFields(template.id, true); foreach (templateDataFieldTO fieldTO in fields) { if (fieldTO.name == "SlideTime") template.durationFieldID = fieldTO.id; } template.sortFields(); templates.Add(template); } catch (Exception e) { Global.d("Failed to load template data: " + e); } } // find playlists plcrit[0].column = "categoryId"; plcrit[0].value = managedCatID.ToString(); ScalaWS.Playlist.playlistTO[] playlists = playlistServ.list(plcrit, null); foreach (playlistTO playlist in playlists) { Player player = new Player(playlist.id, playlist.name); players.Add(player); ScalaWS.Playlist.playlistItemTO[] items = playlistServ.getPlaylistItems(playlist.id, true); foreach (ScalaWS.Playlist.playlistItemTO item in items) { try { // if (item.playlistItemType != playlistItemTypeEnum.MESSAGE) continue; // not supported by ScalaWS, hopefully no non-message items show up in this playlist // item.mediaID is the message ID, item.id is the playlistItemID if (item.mediaId == 0) continue; // sub-playlist? messageTO message = messageServ.get(item.mediaId, true); Slide slide = new Slide(item.mediaId, message.name); player.slides.Add(slide); slide.templateID = message.templateId; if (item.useValidRange) { slide.startDate = item.startValidDate.ToShortDateString(); if (slide.startDate == "1/1/0001") slide.startDate = null; slide.stopDate = item.endValidDate.ToShortDateString(); if (slide.stopDate == "1/1/0001") slide.stopDate = null; } /* // time range is disabled timeScheduleTO[] times = playlistServ.getTimeSchedules(item.id, true); if (times.Length > 0) { slide.startTime = times[0].startTime; if (slide.startTime == "0:00" || slide.startTime == "00:00") slide.startTime = null; slide.stopTime = times[0].endTime; if (slide.stopTime == "24:00") slide.stopTime = null; slide.days = new List<string>(); foreach (weekdayEnum day in times[0].days) { slide.days.Add(day.ToString().Substring(0,3).ToLower()); } } */ messageDataFieldTO[] fields = messageServ.getFiles(item.mediaId, true); List<string> foundFields = new List<string>(); foreach (messageDataFieldTO fieldTO in fields) { Field field = new Field(fieldTO.name); field.mediaID = Convert.ToInt32(fieldTO.value); field.widget = Widget.renderList.Find(Widget.byMediaID(field.mediaID)); if (field.widget == null) { field.widget = new WidgetImage(); // the non-widget widget } slide.fields.Add(field); foundFields.Add(fieldTO.name); } fields = messageServ.getFields(item.mediaId, true); foreach (messageDataFieldTO fieldTO in fields) { if (fieldTO.name == "SlideTime") try { slide.duration = Convert.ToInt32(fieldTO.value); } catch {} } if (slide.duration == 0) slide.duration = player.info.defaultDuration; // missing fields = WidgetNone Template t = templates.Find(Template.byID(slide.templateID)); foreach (Field tf in t.fields) { if (!foundFields.Contains(tf.name)) { Field nf = new Field(tf.name); nf.widget = new WidgetNone(); slide.fields.Add(nf); } } } catch (Exception e) { Global.d("Failed to load playlist item in " + player.name + ": " + e); } } } // get crawl message text ScalaWS.Message.searchCriteriaTO[] mcrit = new ScalaWS.Message.searchCriteriaTO[1]; mcrit[0] = new ScalaWS.Message.searchCriteriaTO(); mcrit[0].column = "name"; mcrit[0].value = Global.cfg("crawlMessage"); mcrit[0].restriction = ScalaWS.Message.restrictionEnum.EQUALS; mcrit[0].restrictionSpecified = true; messageTO[] msgs = messageServ.list(mcrit, null); if (msgs.Length == 0) throw new Exception("Cannot find crawl message named '" + Global.cfg("crawlMessage") + "'"); messageTO msg = msgs[0]; messageDataFieldTO[] f = messageServ.getFields(msg.id, true); crawlTextField = f[0]; } catch (Exception e) { Global.d("Scala.loadData: " + e); } Global.d("end Scala.loadData: " + players.Count + " players and " + templates.Count + " templates loaded."); }