<!--- AZ-3178 Create calendar admin page. Adding LastSync and Enabled fields. These fields are needed for the list of Calendar syncs --->
<cftransaction>
<cfquery datasource="#application.config.DSN#" name="addLastSync">
    IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CalendarSyncTargets' AND COLUMN_NAME = 'LastSync')
    BEGIN
        ALTER TABLE CalendarSyncTargets
            ADD LastSync datetime;
    END
</cfquery>
</cftransaction>

<cftransaction>
<cfquery datasource="#application.config.DSN#" name="addLastSync">
    IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CalendarSyncTargets' AND COLUMN_NAME = 'Enabled')
    BEGIN
        ALTER TABLE CalendarSyncTargets
            ADD 
            Enabled tinyint DEFAULT 1;
    END
</cfquery>
</cftransaction>


<!--- Add SyncTargetID to EventSyncTracker table --->
<cftransaction>
<cfquery datasource="#application.config.DSN#" name="addLastSync">
    IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'EventSyncTracker' AND COLUMN_NAME = 'SyncTargetID')
    BEGIN
        ALTER TABLE EventSyncTracker
            ADD 
            SyncTargetID int;
    END
</cfquery>
</cftransaction>


<!--- AZ-3178 App Setting TIMEZONE --->
<cftransaction>
<cfquery datasource="#application.config.DSN#" name="TIMEZONE">
    IF NOT EXISTS(SELECT * FROM AppSetting WHERE Name = 'TIMEZONE')
    BEGIN
        INSERT INTO [dbo].[AppSetting]
            ([Name]
            ,[Value])
        VALUES
            ('TIMEZONE'
            ,'Pacific Standard Time')
    END
</cfquery>
</cftransaction>

<!---  AZ-3178 App Setting CalendarSyncRecur  --->
<cftransaction>
<cfquery datasource="#application.config.DSN#" name="CalendarSyncRecur">
    IF NOT EXISTS(SELECT * FROM AppSetting WHERE Name = 'CalendarSyncRecur')
    BEGIN
        INSERT INTO [dbo].[AppSetting]
            ([Name]
            ,[Value])
        VALUES
            ('CALENDARSYNCRECUR'
            ,'Hour')
    END
</cfquery>
</cftransaction>

<!---  AZ-3178 App Setting CalendarSyncInterval  --->
<cftransaction>
<cfquery datasource="#application.config.DSN#" name="CalendarSyncInterval">
    IF NOT EXISTS(SELECT * FROM AppSetting WHERE Name = 'CalendarSyncInterval')
    BEGIN
        INSERT INTO [dbo].[AppSetting]
            ([Name]
            ,[Value])
        VALUES
            ('CALENDARSYNCINTERVAL'
            ,'1')
    END
</cfquery>
</cftransaction>


<!--- Case AZ-2480 - Override Lucee blocked extension --->
<cftransaction>
<cfquery datasource="#application.config.DSN#" name="blockedExtensions">
IF NOT EXISTS(SELECT * FROM AppSetting WHERE Name = 'BlockedExtForFileUpload')
BEGIN
      INSERT INTO [dbo].[AppSetting]
          ([Name]
          ,[Value])
    VALUES
          ('BLOCKEDEXTFORFILEUPLOAD'
          ,'asp,aspx,cfc,cfm,cfml,do,htm,html,jsp,jspx,php')
END
</cfquery>
</cftransaction>

<!--- AZ-3542 Add the Calendar Sync Lucee Scheduler Item.  --->
<cfscript>
    //check it exists firsts by looping over the taskList
    var taskList = "";
	schedule action="list" returnvariable="taskList";
    var bFoundScheduledItem = false;
    for (task in taskList) {
        if(StructKeyExists(task, 'name')){
            if (ListLast(task.name,"_") == "CALENDARSYNC") {
                bFoundScheduledItem = true;
            }
        }
	}

    

    //if it doesn't exist we create it
    if (bFoundScheduledItem == false) {
        var startTime = "12:34 AM";
        var sTaskName = application.config.appSettingsRepository.readSetting('COOKIENAME') & "_CALENDARSYNC";
        var sIntranetRoot = application.config.appSettingsRepository.readSetting('IntranetRoot');
        var sTaskUrl = sIntranetRoot & "/Scheduled/calendar-sync.cfm";
        var interval = 300;
        var port = cgi.server_port;
        schedule operation="HTTPRequest"
                action="update"
                task=sTaskname
                URL=sTaskUrl
                startDate=DateFormat(DateAdd("d",-1,now()),"M/d/YY")
                startTime=startTime
                interval=interval
                port=port;
    }
    
</cfscript>