/// <summary> /// Creates new work event in database. /// </summary> /// <param name="eventDetails">Work event details.</param> /// <returns>ID of new database record.</returns> public abstract int CreateEvent( WorkEventDetails eventDetails );
/// <summary> /// Updates work event information in database. /// </summary> /// <param name="eventDetails">Work event details.</param> /// <returns>True if information was updated; false, otherwise.</returns> public abstract bool UpdateEvent( WorkEventDetails eventDetails );
/// <summary> /// Creates work event from details. /// </summary> /// <param name="details">Work event details.</param> /// <returns>Work event from details.</returns> private static WorkEvent GetEventFromDetails( WorkEventDetails details ) { if( details == null ) throw new ArgumentNullException( "details" ); WorkEvent workEvent = new WorkEvent(); workEvent.ID = details.ID; workEvent.Name = details.Name; workEvent.BeginTime = ( details.BeginTime <= details.EndTime ) ? details.BeginTime : details.EndTime; workEvent.EndTime = ( details.EndTime >= details.BeginTime ) ? details.EndTime : details.BeginTime; workEvent.UserID = details.UserID; workEvent.ProjectID = details.ProjectID; workEvent.WorkCategoryID = details.WorkCategoryID; workEvent.EventTypeID = details.UptimeEventTypeID; return workEvent; }
private static Exception checkLunchEvent(WorkEventDetails eventDetails, bool hasAbsenceEvent, TimeSpan lunchDuration) { if (IsLunchEvent(eventDetails.UptimeEventTypeID)) { // ПРАВИЛО: Обед не может быть в выходные. if (CalendarItem.GetHoliday(eventDetails.BeginTime)) return new Exception("Lunch can't be on holidays."); // ПРАВИЛО: Обед не может быть в выходные. if (hasAbsenceEvent) return new Exception("Lunch can't be on absence days."); // ПРАВИЛО: Обед не может превышать определенной длительности. if (lunchDuration > Globals.Settings.WorkTime.MaxLunchTime) return new Exception(string.Format("Lunch can't be longer than {0}.", Globals.Settings.WorkTime.MaxLunchTime)); // ПРАВИЛО: Обед не может превышать определенной длительности. if (lunchDuration + (eventDetails.EndTime - eventDetails.BeginTime) > Globals.Settings.WorkTime.MaxLunchTime) return new Exception(string.Format("Lunch can't be longer than {0}.", Globals.Settings.WorkTime.MaxLunchTime)); } return null; }
/// <summary> /// Checks if work event can be updated. /// </summary> /// <param name="details">Work event details.</param> /// <returns>Exception, if event can't be updated; null, otherwise.</returns> private static Exception CanUpdateEvent(WorkEventDetails details) { WorkEvent checkEvent = GetEventFromDetails(details); WorkEvent oldEvent = GetEventByID(details.ID); if (oldEvent == null) return new Exception("There is no event for update."); //if( checkEvent.EventTypeID != oldEvent.EventTypeID ) //{ return new Exception( "Event type can't be changed." ); } WorkEvent[] dateEvents = GetEventsOfDate(details.UserID, details.BeginTime); if (IsAbsenceEvent(details.UptimeEventTypeID)) { // ПРАВИЛО: Только одно событие такого рода может быть в сутки. foreach (WorkEvent dateEvent in dateEvents) { //if( dateEvent.ID != checkEvent.ID ) // continue; if (dateEvent.ID != checkEvent.ID) if (IsAbsenceEvent(dateEvent.EventTypeID)) return new Exception("Only one absence event could be a day."); } // ПРАВИЛО: Длительность событий такого типа равна суточной норме работы. //details.BeginTime = new DateTime( details.BeginTime.Year, details.BeginTime.Month, details.BeginTime.Day, 12, 0, 0 ); //details.EndTime = details.BeginTime + // Globals.Settings.WorkTime.DefaultWorkTime; // ПРАВИЛО: События такого типа не могут быть открытыми. details.Duration = (int)(details.EndTime - details.BeginTime).TotalSeconds; } else if (IsMainWorkEvent(details.UptimeEventTypeID)) { // ПРАВИЛО: MW событие должно включать в себя все события W и B. foreach (WorkEvent dateEvent in dateEvents) { if (dateEvent.ID == checkEvent.ID) continue; if (IsMainWorkEvent(dateEvent.EventTypeID) || IsBreakEvent(dateEvent.EventTypeID)) { if (!checkEvent.Contains(dateEvent)) return new Exception("All work and break events must be inside main work event."); } } } else if (IsWorkEvent(details.UptimeEventTypeID)) { bool inMainWorkEvent = false; foreach (WorkEvent dateEvent in dateEvents) { if (dateEvent.ID == checkEvent.ID) continue; if (IsMainWorkEvent(dateEvent.EventTypeID)) { // ПРАВИЛО: MW событие должно включать в себя все события W и B. if (dateEvent.Contains(checkEvent)) inMainWorkEvent = true; } else if (IsWorkEvent(dateEvent.EventTypeID) || IsBreakEvent(dateEvent.EventTypeID)) { Exception exception = isIntersectsOrOpen(dateEvent, checkEvent); if (exception != null) return exception; } } if (!inMainWorkEvent) return new Exception("Work event must be inside main work event."); } else if (IsBreakEvent(details.UptimeEventTypeID)) { bool inMainWorkEvent = false; TimeSpan lunchDuration = TimeSpan.Zero; foreach (WorkEvent dateEvent in dateEvents) { if (dateEvent.ID == checkEvent.ID) continue; if (IsMainWorkEvent(dateEvent.EventTypeID)) { // ПРАВИЛО: События данного типа должны находиться внутри рабочих событий. if (dateEvent.Contains(checkEvent)) inMainWorkEvent = true; } else if (IsBreakEvent(dateEvent.EventTypeID) || IsWorkEvent(dateEvent.EventTypeID)) { Exception exception = isIntersectsOrOpen(dateEvent, checkEvent); if (exception != null) return exception; // ПРАВИЛО: Обед не может превышать определенной длительности. if (IsLunchEvent(dateEvent.EventTypeID)) lunchDuration += dateEvent.Duration; } } if (!inMainWorkEvent) return new Exception("Break event must be inside work event."); if (IsLunchEvent(details.UptimeEventTypeID)) { // ПРАВИЛО: Обед не может превышать определенной длительности. lunchDuration += (details.EndTime - details.BeginTime); if (lunchDuration > Globals.Settings.WorkTime.MaxLunchTime) return new Exception(string.Format("Lunch can't be longer than {0}.", Globals.Settings.WorkTime.MaxLunchTime)); } } else if (details.UptimeEventTypeID != (int)WorkEventType.NoData) return new Exception("Unknown type of event."); return null; }
/// <summary> /// Checks if work event can be created. /// </summary> /// <param name="details">Work event details.</param> /// <returns>Exception, if event can't be created; null, otherwise.</returns> private static Exception CanCreateEvent(WorkEventDetails details) { WorkEvent checkEvent = GetEventFromDetails( details ); WorkEvent[] dateEvents = GetEventsOfDate( details.UserID, details.BeginTime ); if(IsAbsenceEvent(details.UptimeEventTypeID)) { // ПРАВИЛО: Только одно событие такого рода может быть в сутки. foreach (WorkEvent dateEvent in dateEvents) { if (IsAbsenceEvent(dateEvent.EventTypeID)) //|| IsMainWorkEvent(dateEvent.EventTypeID)) { DeleteEvent(dateEvent.ID); //return new Exception("Only one absence event could be a day."); } } // ПРАВИЛО: Длительность событий такого типа равна суточной норме работы. //details.BeginTime = new DateTime( details.BeginTime.Year, details.BeginTime.Month, details.BeginTime.Day, 12, 0, 0 ); //details.EndTime = details.BeginTime + // Globals.Settings.WorkTime.DefaultWorkTime; // ПРАВИЛО: События такого типа не могут быть открытыми. details.Duration = (int)( details.EndTime - details.BeginTime ).TotalSeconds; } else if(IsMainWorkEvent(details.UptimeEventTypeID)) { foreach( WorkEvent dateEvent in dateEvents ) { // ПРАВИЛО: В сутках не может быть 2-х событий MW. if( IsMainWorkEvent( dateEvent.EventTypeID ) ) return new Exception( "There can't be two main work events." ); } } else if( IsWorkEvent( details.UptimeEventTypeID ) ) { bool inMainWorkEvent = false; foreach(WorkEvent dateEvent in dateEvents) { if( IsMainWorkEvent( dateEvent.EventTypeID ) ) { // ПРАВИЛО: События данного типа должны находиться внутри MW. if( dateEvent.Contains( checkEvent ) ) inMainWorkEvent = true; } else if (IsWorkEvent(dateEvent.EventTypeID) || IsBreakEvent(dateEvent.EventTypeID)) { Exception ex = isIntersectsOrOpen(dateEvent, checkEvent); if (ex != null) return ex; } } // ПРАВИЛО: События данного типа должны находиться внутри MW. if( !inMainWorkEvent ) return new Exception( "Break event must be inside main work event." ); } else if (IsBreakEvent(details.UptimeEventTypeID)) { bool inMainWorkEvent = false; bool hasAbsenceEvent = false; TimeSpan lunchDuration = TimeSpan.Zero; foreach (WorkEvent dateEvent in dateEvents) { if (IsAbsenceEvent(dateEvent.EventTypeID)) hasAbsenceEvent = true; if (IsMainWorkEvent(dateEvent.EventTypeID)) { // ПРАВИЛО: События данного типа должны находиться внутри MW. if (dateEvent.Contains(checkEvent)) inMainWorkEvent = true; } else if (IsBreakEvent(dateEvent.EventTypeID) || IsWorkEvent(dateEvent.EventTypeID)) { Exception ex = isIntersectsOrOpen(dateEvent, checkEvent); if (ex != null) return ex; if (IsLunchEvent(dateEvent.EventTypeID)) lunchDuration += dateEvent.Duration; } } // ПРАВИЛО: События данного типа должны находиться внутри MW. if (!inMainWorkEvent) return new Exception("Break event must be inside main work event."); Exception exception = checkLunchEvent(details, hasAbsenceEvent, lunchDuration); if (exception != null) return exception; } else if (details.UptimeEventTypeID != (int)WorkEventType.NoData) return new Exception("Unknown type of event."); return null; }
/// <summary> /// Updates work event in database. /// </summary> /// <param name="id">ID of work event.</param> /// <param name="name">Name of event.</param> /// <param name="begin">Time of event beginning.</param> /// <param name="end">Time of event ending.</param> /// <param name="userID">User ID.</param> /// <param name="projectID">Project ID.</param> /// <param name="workCategoryID">Work category ID.</param> /// <param name="eventTypeID">Event type ID.</param> /// <returns>True if record was updated; false, otherwise.</returns> public static bool UpdateEvent( int id, String name, DateTime begin, DateTime end, int userID, int projectID, int workCategoryID, int eventTypeID ) { isDateIsCorrect(begin, end); WorkEventDetails details = new WorkEventDetails(id, name, begin, end, userID, projectID, workCategoryID, eventTypeID); Exception ex = CanUpdateEvent( details ); if( ex != null ) throw ex; return SiteProvider.WorkEvents.UpdateEvent( details ); }