/* Auto-generated ProgramImpl Code */
import java.util.*; /* java Predefined*/
import javax.baja.nre.util.*; /* nre Predefined*/
import javax.baja.sys.*; /* baja Predefined*/
import javax.baja.status.*; /* baja Predefined*/
import javax.baja.util.*; /* baja Predefined*/
import com.tridium.program.*; /* program-rt Predefined*/
import javax.baja.schedule.*; /* schedule-rt User Defined*/
import javax.baja.naming.*; /* baja User Defined*/
import javax.baja.collection.*; /* baja User Defined*/
public class ProgramImpl
extends com.tridium.program.ProgramBase
{
////////////////////////////////////////////////////////////////
// Getters
////////////////////////////////////////////////////////////////
public BOrd getCalendarOrd() { return (BOrd)get("calendarOrd"); }
public BOrd getEntriesOrd() { return (BOrd)get("entriesOrd"); }
////////////////////////////////////////////////////////////////
// Setters
////////////////////////////////////////////////////////////////
public void setCalendarOrd(javax.baja.naming.BOrd v) { set("calendarOrd", v); }
public void setEntriesOrd(javax.baja.naming.BOrd v) { set("entriesOrd", v); }
////////////////////////////////////////////////////////////////
// Program Source
////////////////////////////////////////////////////////////////
public void onStart() throws Exception
{
// start up code here
System.out.println("Calendar Schedule Program starting...");
}
public void onExecute() throws Exception
{
try {
// Get the calendar schedule object first
BCalendarSchedule calendar = null;
try {
BObject calendarObj = getCalendarOrd().resolve().get();
if (calendarObj instanceof BCalendarSchedule) {
calendar = (BCalendarSchedule)calendarObj;
System.out.println("Calendar found: " + calendar.getDisplayName(null));
} else {
System.out.println("Calendar object is not a BCalendarSchedule: " +
(calendarObj != null ? calendarObj.getType() : "null"));
return;
}
} catch (Exception e) {
System.out.println("Error getting calendar: " + e.getMessage());
return;
}
// Query for objects that might contain schedule strings
BObject stringObjects = null;
BITable result = (BITable)getEntriesOrd().resolve().get();
TableCursor<BObject> cursor = result.cursor();
while (cursor.next()) {
stringObjects = cursor.get();
// Check if object is not null before accessing slots
if (stringObjects != null) {
try {
String objectName = "UnknownObject";
try {
if (stringObjects instanceof BComponent) {
objectName = ((BComponent)stringObjects).getDisplayName(null);
} else {
objectName = stringObjects.toString();
}
} catch (Exception e) {
objectName = stringObjects.getType().getTypeName();
}
System.out.println("Processing object: " + objectName);
// Look for "out" slot specifically
if (stringObjects instanceof BComponent) {
try {
BObject outSlot = ((BComponent)stringObjects).get("out");
if (outSlot != null) {
System.out.println(" - out slot found: " + outSlot.getType().getTypeName());
// Check if out slot contains a string value
String stringValue = extractStringValue(outSlot);
if (stringValue != null && !stringValue.trim().isEmpty()) {
System.out.println(" - String value found: '" + stringValue + "'");
processScheduleString(stringValue, calendar, objectName);
} else {
System.out.println(" - out slot does not contain a valid string value");
}
} else {
System.out.println(" - out slot value is NULL");
}
} catch (Exception slotException) {
System.out.println(" - Error accessing out slot (slot may not exist): " + slotException.getMessage());
}
} else {
System.out.println(" - object is not a BComponent");
}
// Also check for other common string properties
checkForStringProperties(stringObjects, calendar, objectName);
} catch (Exception slotException) {
System.out.println("Error accessing slots for object: " + slotException.getMessage());
}
} else {
System.out.println("Object is NULL");
}
}
cursor.close();
} catch (Exception e) {
System.out.println("An error has occurred during execution: " + e.getMessage());
e.printStackTrace();
}
}
public void onStop() throws Exception
{
// shutdown code here
System.out.println("Calendar Schedule Program stopping...");
}
////////////////////////////////////////////////////////////////
// Helper Methods
////////////////////////////////////////////////////////////////
/**
* Extract string value from various object types
*/
private String extractStringValue(BObject obj)
{
if (obj == null) return null;
try {
if (obj instanceof BString) {
// BString likely uses toString() or a different method
return obj.toString();
} else if (obj instanceof BStatusValue) {
BStatusValue statusValue = (BStatusValue)obj;
try {
Object value = statusValue.getStatusValue();
if (value != null) {
return value.toString();
}
} catch (Exception e) {
// If getValue() doesn't exist, try toString()
return statusValue.toString();
}
} else {
// Try to get string representation
return obj.toString();
}
} catch (Exception e) {
System.out.println(" Error extracting string value: " + e.getMessage());
}
return null;
}
/**
* Check for common string properties in the object
*/
private void checkForStringProperties(BObject obj, BCalendarSchedule calendar, String objectName)
{
if (!(obj instanceof BComponent)) return;
BComponent comp = (BComponent)obj;
// Check common property names that might contain schedule strings
String[] propertyNames = {"value", "displayName", "description", "text", "schedule", "time"};
for (String propName : propertyNames) {
try {
BObject propValue = comp.get(propName);
String stringValue = extractStringValue(propValue);
if (stringValue != null && !stringValue.trim().isEmpty()) {
System.out.println(" - Found string in '" + propName + "' property: '" + stringValue + "'");
processScheduleString(stringValue, calendar, objectName + "_" + propName);
}
} catch (Exception e) {
// Continue if property doesn't exist or can't be accessed
// This is normal - not all objects will have all properties
}
}
}
/**
* Process a string value to determine if it represents a date or week/day schedule
*/
private void processScheduleString(String stringValue, BCalendarSchedule calendar, String sourceName)
{
if (stringValue == null || stringValue.trim().isEmpty()) return;
String value = stringValue.trim();
System.out.println(" Analyzing string for schedule patterns: '" + value + "'");
// Check for week/day pattern FIRST (before date pattern)
// Look for "week" keyword to identify week/day schedules
if (value.toLowerCase().contains("week")) {
BWeekAndDaySchedule weekDaySchedule = parseAsWeekDaySchedule(value);
if (weekDaySchedule != null) {
addWeekDayScheduleToCalendar(weekDaySchedule, calendar, sourceName, value);
return;
}
}
// Try to parse as date schedule format (only if no "week" keyword found)
BDateSchedule dateSchedule = parseAsDateSchedule(value);
if (dateSchedule != null) {
addDateScheduleToCalendar(dateSchedule, calendar, sourceName, value);
return;
}
System.out.println(" No recognizable schedule pattern found in: '" + value + "'");
}
/**
* Parse string as date schedule (e.g., "15 Mar 2024", "Monday 15 March", "25 Dec", "21,August,2025")
*/
private BDateSchedule parseAsDateSchedule(String value)
{
try {
BDateSchedule dateSchedule = new BDateSchedule();
boolean foundComponent = false;
System.out.println(" Trying to parse as date schedule...");
// Clean up the string - remove status info and extra characters
String cleanValue = value.toLowerCase();
// Remove everything after { or @ symbols (status information)
if (cleanValue.contains("{")) {
cleanValue = cleanValue.substring(0, cleanValue.indexOf("{")).trim();
}
if (cleanValue.contains("@")) {
cleanValue = cleanValue.substring(0, cleanValue.indexOf("@")).trim();
}
System.out.println(" Cleaned value: '" + cleanValue + "'");
// Split by both spaces and commas to handle different formats
String[] parts = cleanValue.split("[\\s,]+");
for (String part : parts) {
part = part.trim();
if (part.isEmpty()) continue;
// Try to parse as day number (1-31)
try {
int day = Integer.parseInt(part);
if (day >= 1 && day <= 31) {
dateSchedule.setDay(day);
foundComponent = true;
System.out.println(" Found day: " + day);
continue;
}
} catch (NumberFormatException e) {
// Not a number, continue
}
// Try to parse as month
BMonth month = parseMonth(part);
if (month != null) {
dateSchedule.setMonth(month);
foundComponent = true;
System.out.println(" Found month: " + month.getTag());
continue;
}
// Try to parse as year (handle -1 as "any year")
try {
int year = Integer.parseInt(part);
if (year == -1) {
// -1 means "any year" - don't set a specific year
System.out.println(" Found year: any (-1)");
} else if (year >= 1900 && year <= 3000) {
dateSchedule.setYear(year);
foundComponent = true;
System.out.println(" Found year: " + year);
}
continue;
} catch (NumberFormatException e) {
// Not a year, continue
}
// Try to parse as weekday
BWeekday weekday = parseWeekday(part);
if (weekday != null) {
dateSchedule.setWeekday(weekday.getOrdinal());
foundComponent = true;
System.out.println(" Found weekday: " + weekday.getTag());
}
}
if (foundComponent) {
System.out.println(" Successfully parsed as date schedule");
return dateSchedule;
}
} catch (Exception e) {
System.out.println(" Failed to parse as date schedule: " + e.getMessage());
}
return null;
}
/**
* Parse string as week and day schedule (e.g., "Monday Week 2", "Tue Week 3 Jan", "Monday, Week 3, September")
*/
private BWeekAndDaySchedule parseAsWeekDaySchedule(String value)
{
try {
BWeekAndDaySchedule schedule = new BWeekAndDaySchedule();
boolean foundComponent = false;
System.out.println(" Trying to parse as week/day schedule...");
// Clean up the string - remove status info and extra characters
String cleanValue = value.toLowerCase();
// Remove everything after { or @ symbols (status information)
if (cleanValue.contains("{")) {
cleanValue = cleanValue.substring(0, cleanValue.indexOf("{")).trim();
}
if (cleanValue.contains("@")) {
cleanValue = cleanValue.substring(0, cleanValue.indexOf("@")).trim();
}
System.out.println(" Cleaned value: '" + cleanValue + "'");
// Split by both spaces and commas to handle different formats
String[] parts = cleanValue.split("[\\s,]+");
for (int i = 0; i < parts.length; i++) {
String part = parts[i].trim();
if (part.isEmpty()) continue;
// Try to parse as weekday
BWeekday weekday = parseWeekday(part);
if (weekday != null) {
schedule.setWeekday(weekday.getOrdinal());
foundComponent = true;
System.out.println(" Found weekday: " + weekday.getTag());
continue;
}
// Try to parse as month
BMonth month = parseMonth(part);
if (month != null) {
schedule.setMonth(month.getOrdinal());
foundComponent = true;
System.out.println(" Found month: " + month.getTag());
continue;
}
// Look for week patterns like "week 2" or "week2"
if (part.equals("week") && i + 1 < parts.length) {
// Next part should be the week number
String weekPart = parts[i + 1].trim();
i++; // Skip next part as we consumed it
try {
int week = Integer.parseInt(weekPart);
if (week >= 1 && week <= 6) {
schedule.setWeek(week);
foundComponent = true;
System.out.println(" Found week: " + week);
}
} catch (NumberFormatException e) {
// Not a valid week number
System.out.println(" Invalid week number: " + weekPart);
}
} else if (part.startsWith("week")) {
// Handle "week3" format
String weekPart = part.substring(4);
try {
int week = Integer.parseInt(weekPart);
if (week >= 1 && week <= 6) {
schedule.setWeek(week);
foundComponent = true;
System.out.println(" Found week: " + week);
}
} catch (NumberFormatException e) {
// Not a valid week number
}
}
}
if (foundComponent) {
System.out.println(" Successfully parsed as week/day schedule");
return schedule;
}
} catch (Exception e) {
System.out.println(" Failed to parse as week/day schedule: " + e.getMessage());
}
return null;
}
/**
* Parse month name from string
*/
private BMonth parseMonth(String monthStr)
{
monthStr = monthStr.toLowerCase();
if (monthStr.equals("january") || monthStr.equals("jan")) return BMonth.january;
if (monthStr.equals("february") || monthStr.equals("feb")) return BMonth.february;
if (monthStr.equals("march") || monthStr.equals("mar")) return BMonth.march;
if (monthStr.equals("april") || monthStr.equals("apr")) return BMonth.april;
if (monthStr.equals("may")) return BMonth.may;
if (monthStr.equals("june") || monthStr.equals("jun")) return BMonth.june;
if (monthStr.equals("july") || monthStr.equals("jul")) return BMonth.july;
if (monthStr.equals("august") || monthStr.equals("aug")) return BMonth.august;
if (monthStr.equals("september") || monthStr.equals("sep")) return BMonth.september;
if (monthStr.equals("october") || monthStr.equals("oct")) return BMonth.october;
if (monthStr.equals("november") || monthStr.equals("nov")) return BMonth.november;
if (monthStr.equals("december") || monthStr.equals("dec")) return BMonth.december;
return null;
}
/**
* Parse weekday name from string
*/
private BWeekday parseWeekday(String dayStr)
{
dayStr = dayStr.toLowerCase();
if (dayStr.equals("sunday") || dayStr.equals("sun")) return BWeekday.sunday;
if (dayStr.equals("monday") || dayStr.equals("mon")) return BWeekday.monday;
if (dayStr.equals("tuesday") || dayStr.equals("tue") || dayStr.equals("tues")) return BWeekday.tuesday;
if (dayStr.equals("wednesday") || dayStr.equals("wed")) return BWeekday.wednesday;
if (dayStr.equals("thursday") || dayStr.equals("thu") || dayStr.equals("thurs")) return BWeekday.thursday;
if (dayStr.equals("friday") || dayStr.equals("fri")) return BWeekday.friday;
if (dayStr.equals("saturday") || dayStr.equals("sat")) return BWeekday.saturday;
return null;
}
/**
* Add a date schedule to the calendar
*/
private void addDateScheduleToCalendar(BDateSchedule dateSchedule, BCalendarSchedule calendar, String sourceName, String originalValue)
{
try {
// Create a unique name for this schedule entry
String scheduleName = "DateSched_" + sourceName.replaceAll("[^a-zA-Z0-9]", "_");
// Try to add the schedule to the calendar
if (calendar instanceof BComponent) {
try {
((BComponent)calendar).add(scheduleName, dateSchedule);
System.out.println(" SUCCESS: Added date schedule '" + scheduleName + "' to calendar");
} catch (Exception addException) {
System.out.println(" ERROR: Failed to add date schedule to calendar: " + addException.getMessage());
return;
}
} else {
System.out.println(" ERROR: Calendar is not a BComponent, cannot add schedule");
return;
}
System.out.println(" Original value: '" + originalValue + "'");
System.out.println(" Parsed as - Day: " + (dateSchedule.getDay() > 0 ? dateSchedule.getDay() : "any") +
", Month: " + (dateSchedule.getMonth() >= 0 ? dateSchedule.getMonth() : "any") +
", Year: " + (dateSchedule.getYear() >= 0 ? dateSchedule.getYear() : "any") +
", Weekday: " + (dateSchedule.getWeekday() >= 0 ? dateSchedule.getWeekday() : "any"));
} catch (Exception e) {
System.out.println(" ERROR: Failed to add date schedule to calendar: " + e.getMessage());
}
}
/**
* Add a week and day schedule to the calendar
*/
private void addWeekDayScheduleToCalendar(BWeekAndDaySchedule weekDaySchedule, BCalendarSchedule calendar, String sourceName, String originalValue)
{
try {
// Create a unique name for this schedule entry
String scheduleName = "WeekDaySched_" + sourceName.replaceAll("[^a-zA-Z0-9]", "_");
// Try to add the schedule to the calendar
if (calendar instanceof BComponent) {
try {
((BComponent)calendar).add(scheduleName, weekDaySchedule);
System.out.println(" SUCCESS: Added week/day schedule '" + scheduleName + "' to calendar");
} catch (Exception addException) {
System.out.println(" ERROR: Failed to add week/day schedule to calendar: " + addException.getMessage());
return;
}
} else {
System.out.println(" ERROR: Calendar is not a BComponent, cannot add schedule");
return;
}
System.out.println(" Original value: '" + originalValue + "'");
System.out.println(" Parsed as - Weekday: " + (weekDaySchedule.getWeekday() >= 0 ? weekDaySchedule.getWeekday() : "any") +
", Week: " + (weekDaySchedule.getWeek() >= 0 ? weekDaySchedule.getWeek() : "any") +
", Month: " + (weekDaySchedule.getMonth() >= 0 ? weekDaySchedule.getMonth() : "any"));
} catch (Exception e) {
System.out.println(" ERROR: Failed to add week/day schedule to calendar: " + e.getMessage());
}
}
}
Here is a program object that can query String Writables (assuming that your json info goes to strings. And add right now, date, and week and day. I haven't done a date range yet or custom. Which might be more difficult.
This might be something you might be looking for.