private void GetDefaultZone() { base.ServiceBroker.Service.ServiceObjects[0].Properties.InitResultTable(); DataTable results = base.ServiceBroker.ServicePackage.ResultTable; WorkflowManagementServer mngServer = this.ServiceBroker.K2Connection.GetConnection <WorkflowManagementServer>(); using (mngServer.Connection) { string defaultZone = mngServer.ZoneGetDefault(); DataRow dRow = results.NewRow(); dRow[Constants.SOProperties.WorkingHoursConfiguration.ZoneName] = defaultZone; results.Rows.Add(dRow); } }
private void GetDefaultZone() { base.ServiceBroker.Service.ServiceObjects[0].Properties.InitResultTable(); DataTable results = base.ServiceBroker.ServicePackage.ResultTable; WorkflowManagementServer mngServer = new WorkflowManagementServer(); using (mngServer.CreateConnection()) { mngServer.Open(BaseAPIConnectionString); string defaultZone = mngServer.ZoneGetDefault(); DataRow dRow = results.NewRow(); dRow[Constants.SOProperties.WorkingHoursConfiguration.ZoneName] = defaultZone; results.Rows.Add(dRow); } }
private static TimeSpan WorkingHoursDateDiff(string zoneName, DateTime startDate, DateTime endDate) { TimeSpan workedHours = new TimeSpan(0); WorkflowManagementServer mngServer = new WorkflowManagementServer("localhost", 5555); //safe to do this as inline function runs on K2 Server. mngServer.Open(); //let K2 catch errors here. AvailabilityZone zone = new AvailabilityZone(); if (string.IsNullOrEmpty(zoneName)) //use default working hours if not specified. { try { zoneName = mngServer.ZoneGetDefault(); } catch (Exception ex) { throw new Exception("Couldn't get default working hours because: " + ex.Message, ex); } } try { zone = mngServer.ZoneLoad(zoneName); } catch (Exception ex) { throw new Exception("Couldn't get working hours named: '" + zoneName + "' Because: " + ex.Message, ex); } DateTime currentZoneDate = startDate.Date; bool carryOn = true; while (carryOn) { List<AvailabilityHours> singleDayListOfHours = zone.AvailabilityHoursList.Where(x => x.WorkDay == currentZoneDate.DayOfWeek).ToList(); foreach (AvailabilityHours hours in singleDayListOfHours) { //quickly filter out exception dates :) if (zone.AvailabilityDateList.Count(x => x.IsNonWorkDate == true && x.WorkDate.Date == currentZoneDate.Date) > 0) continue; DateTime endOfSection = new DateTime(currentZoneDate.Year, currentZoneDate.Month, currentZoneDate.Day); DateTime startOfSection = new DateTime(currentZoneDate.Year, currentZoneDate.Month, currentZoneDate.Day); startOfSection = startOfSection.Add(hours.TimeOfDay); endOfSection = startOfSection.Add(hours.Duration); if ((startDate >= startOfSection && startDate <= endOfSection) && (endDate <= endOfSection && endDate >= startOfSection)) //starts inside, ends inside { workedHours += endDate - startDate; } if (startDate < startOfSection && endDate > endOfSection) // starts outside, ends outside { workedHours += endOfSection - startOfSection; } if (startDate < startOfSection && (endDate <= endOfSection && endDate >= startOfSection)) //starts outside, ends inside { workedHours += endDate - startOfSection; } if ((startDate >= startOfSection && startDate <= endOfSection) && endDate > endOfSection) //starts inside, ends outside { workedHours += endOfSection - startDate; } } //special dates require more work - basically duplicate the logic above but decrement worked hours for every special day foreach (AvailabilityDate specialDate in zone.AvailabilityDateList.Where(x => x.WorkDate.Date == currentZoneDate.Date && x.IsNonWorkDate == false)) { DateTime specialDateStart = specialDate.WorkDate; DateTime specialDateEnd = specialDate.WorkDate + specialDate.Duration; if ((startDate >= specialDateStart && startDate <= specialDateEnd) && (endDate <= specialDateEnd && endDate >= specialDateStart)) //starts inside, ends inside { workedHours -= endDate - startDate; } if (startDate < specialDateStart && endDate > specialDateEnd) // starts outside, ends outside { workedHours -= specialDateEnd - specialDateStart; } if (startDate < specialDateStart && (endDate <= specialDateEnd && endDate >= specialDateStart)) //starts outside, ends inside { workedHours -= endDate - specialDateStart; } if ((startDate >= specialDateStart && startDate <= specialDateEnd) && endDate > specialDateEnd) //starts inside, ends outside { workedHours -= specialDateEnd - startDate; } } if (currentZoneDate > endDate) carryOn = false; currentZoneDate = currentZoneDate.AddDays(1); } mngServer.Connection.Close(); mngServer.Connection.Dispose(); return workedHours; }
private static TimeSpan WorkingHoursDateDiff(string zoneName, DateTime startDate, DateTime endDate) { TimeSpan workedHours = new TimeSpan(0); WorkflowManagementServer mngServer = new WorkflowManagementServer("localhost", 5555); //safe to do this as inline function runs on K2 Server. mngServer.Open(); //let K2 catch errors here. AvailabilityZone zone = new AvailabilityZone(); if (string.IsNullOrEmpty(zoneName)) //use default working hours if not specified. { try { zoneName = mngServer.ZoneGetDefault(); } catch (Exception ex) { throw new Exception("Couldn't get default working hours because: " + ex.Message, ex); } } try { zone = mngServer.ZoneLoad(zoneName); } catch (Exception ex) { throw new Exception("Couldn't get working hours named: '" + zoneName + "' Because: " + ex.Message, ex); } DateTime currentZoneDate = startDate.Date; bool carryOn = true; while (carryOn) { List <AvailabilityHours> singleDayListOfHours = zone.AvailabilityHoursList.Where(x => x.WorkDay == currentZoneDate.DayOfWeek).ToList(); foreach (AvailabilityHours hours in singleDayListOfHours) { //quickly filter out exception dates :) if (zone.AvailabilityDateList.Count(x => x.IsNonWorkDate == true && x.WorkDate.Date == currentZoneDate.Date) > 0) { continue; } DateTime endOfSection = new DateTime(currentZoneDate.Year, currentZoneDate.Month, currentZoneDate.Day); DateTime startOfSection = new DateTime(currentZoneDate.Year, currentZoneDate.Month, currentZoneDate.Day); startOfSection = startOfSection.Add(hours.TimeOfDay); endOfSection = startOfSection.Add(hours.Duration); if ((startDate >= startOfSection && startDate <= endOfSection) && (endDate <= endOfSection && endDate >= startOfSection)) //starts inside, ends inside { workedHours += endDate - startDate; } if (startDate < startOfSection && endDate > endOfSection) // starts outside, ends outside { workedHours += endOfSection - startOfSection; } if (startDate < startOfSection && (endDate <= endOfSection && endDate >= startOfSection)) //starts outside, ends inside { workedHours += endDate - startOfSection; } if ((startDate >= startOfSection && startDate <= endOfSection) && endDate > endOfSection) //starts inside, ends outside { workedHours += endOfSection - startDate; } } //special dates require more work - basically duplicate the logic above but decrement worked hours for every special day foreach (AvailabilityDate specialDate in zone.AvailabilityDateList.Where(x => x.WorkDate.Date == currentZoneDate.Date && x.IsNonWorkDate == false)) { DateTime specialDateStart = specialDate.WorkDate; DateTime specialDateEnd = specialDate.WorkDate + specialDate.Duration; if ((startDate >= specialDateStart && startDate <= specialDateEnd) && (endDate <= specialDateEnd && endDate >= specialDateStart)) //starts inside, ends inside { workedHours -= endDate - startDate; } if (startDate < specialDateStart && endDate > specialDateEnd) // starts outside, ends outside { workedHours -= specialDateEnd - specialDateStart; } if (startDate < specialDateStart && (endDate <= specialDateEnd && endDate >= specialDateStart)) //starts outside, ends inside { workedHours -= endDate - specialDateStart; } if ((startDate >= specialDateStart && startDate <= specialDateEnd) && endDate > specialDateEnd) //starts inside, ends outside { workedHours -= specialDateEnd - startDate; } } if (currentZoneDate > endDate) { carryOn = false; } currentZoneDate = currentZoneDate.AddDays(1); } mngServer.Connection.Close(); mngServer.Connection.Dispose(); return(workedHours); }