11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
4- using System . Text ;
5- using Objects ;
3+ using System . Xml . Linq ;
64using DataAccessLayer ;
5+ using Objects ;
76
87namespace BusinessLogic
98{
109 public class NotificationsBL
1110 {
1211 public NotificationsDAL dal
1312 {
14- get
15- {
16- return new NotificationsDAL ( ) ;
17- }
13+ get { return new NotificationsDAL ( ) ; }
1814 }
1915
2016 public void InsertNotification ( Notification notification )
@@ -27,27 +23,128 @@ public void SolveNotification(string notificationId, string username)
2723 dal . SolveNotification ( notificationId , username ) ;
2824 }
2925
30- public List < UserNotification > GetAllUserNotifications ( string username )
26+ private List < UserNotification > GetAllStudentNotifications ( string username )
3127 {
32- UsersBL usersBL = new UsersBL ( ) ;
33- User user = usersBL . GetUser ( username ) ;
28+ var usersBL = new UsersBL ( ) ;
29+ Student student = usersBL . GetStudent ( username ) ;
3430 List < UserNotification > list = null ;
35- if ( user != null && user . UserType == UserTypes . Teacher )
31+
32+ if ( student != null )
3633 {
37- var teacher = user as Teacher ;
38- if ( teacher != null ) list = teacher . Notifications ;
34+ list = student . Notifications ;
3935 }
40- if ( user != null && user . UserType == UserTypes . Student )
36+ return list ;
37+ }
38+
39+ private List < UserNotification > GetAllTeacherNotifications ( string username )
40+ {
41+ var usersBL = new UsersBL ( ) ;
42+ Teacher teacher = usersBL . GetTeacher ( username ) ;
43+ List < UserNotification > list = null ;
44+
45+ if ( teacher != null )
4146 {
42- var student = user as Student ;
43- if ( student != null ) list = student . Notifications ;
47+ list = teacher . Notifications ;
4448 }
4549 return list ;
4650 }
4751
48- public Notification GetNotification ( string notificationId )
52+ public Notification GetNotification ( string notificationId , NotificationTypes notificationType )
53+ {
54+ return dal . GetNotification ( notificationId , notificationType ) ;
55+ }
56+
57+ public string GetUserNotificationsAsXml ( string username , int count , UserTypes userType )
4958 {
50- return dal . GetNotification ( notificationId ) ;
59+ List < UserNotification > list = null ;
60+ if ( userType == UserTypes . Student )
61+ list = GetAllStudentNotifications ( username ) ;
62+ else if ( userType == UserTypes . Teacher )
63+ list = GetAllTeacherNotifications ( username ) ;
64+
65+ if ( list != null )
66+ {
67+ if ( count < list . Count )
68+ list = list . GetRange ( list . Count - count , count ) ;
69+ return GetXmlFromNotifications ( list ) ;
70+ }
71+ return String . Empty ;
72+ }
73+
74+ private string GetXmlFromNotifications ( List < UserNotification > list )
75+ {
76+ var document = new XDocument ( new XDeclaration ( "1.0" , "utf-8" , "yes" ) ) ;
77+ var notifications = new XElement ( "notifications" ) ;
78+ notifications . Add ( new XAttribute ( "count" , list . Count ) ) ;
79+ var notificationsBL = new NotificationsBL ( ) ;
80+ foreach ( UserNotification userNotification in list )
81+ {
82+ var notificationElement = new XElement ( "notification" ) ;
83+ notificationElement . Add ( new XElement ( "solved" , userNotification . UserSolved ) ) ;
84+
85+ var notificationDetails = new XElement ( "details" ) ;
86+ notificationDetails . Add ( new XAttribute ( "type" , userNotification . NotificationType ) ) ;
87+ Notification notification = null ;
88+ switch ( userNotification . NotificationType )
89+ {
90+ case NotificationTypes . FeedNotification :
91+
92+ var feedNotification =
93+ ( FeedNotification )
94+ notificationsBL . GetNotification ( userNotification . NotificationId , NotificationTypes . FeedNotification ) ;
95+ if ( feedNotification != null )
96+ {
97+ var feedbl = new FeedsBL ( ) ;
98+ Feed feed = feedbl . GetFeed ( feedNotification . FeedId ) ;
99+ if ( feed != null )
100+ {
101+ notificationDetails . Add ( new XElement ( "message" , feed . Message ) ) ;
102+ notificationDetails . Add ( new XElement ( "sender" , feed . Sender ) ) ;
103+ }
104+ notification = feedNotification ;
105+ }
106+ break ;
107+ case NotificationTypes . TimetableNotification :
108+ var timetableNotification =
109+ ( TimetableNotification )
110+ notificationsBL . GetNotification ( userNotification . NotificationId , NotificationTypes . TimetableNotification ) ;
111+ if ( timetableNotification != null )
112+ {
113+ notificationDetails . Add ( new XElement ( "timetableNotificationType" ,
114+ timetableNotification . TimetableNotificationType ) ) ;
115+ notificationDetails . Add ( new XElement ( "dayOfWeek" , timetableNotification . Day ) ) ;
116+ notificationDetails . Add ( new XElement ( "typeOfClass" ,
117+ timetableNotification . ModifiedItem . TypeOfClass ) ) ;
118+ notificationDetails . Add ( new XElement ( "className" ,
119+ timetableNotification . ModifiedItem . ClassName ) ) ;
120+ notificationDetails . Add ( new XElement ( "startTime" ,
121+ timetableNotification . ModifiedItem . StartTime ) ) ;
122+ notificationDetails . Add ( new XElement ( "endTime" ,
123+ timetableNotification . ModifiedItem . EndTime ) ) ;
124+ notification = timetableNotification ;
125+ }
126+ break ;
127+ case NotificationTypes . MonitoredWebsitesNotification :
128+ var websiteNotification =
129+ ( MonitoredWebsiteNotification ) notificationsBL . GetNotification ( userNotification . NotificationId , NotificationTypes . MonitoredWebsitesNotification ) ;
130+ if ( websiteNotification != null )
131+ {
132+ notificationDetails . Add ( new XElement ( "websiteLink" , websiteNotification . WebsiteId ) ) ;
133+ notification = websiteNotification ;
134+ }
135+ break ;
136+ }
137+ if ( notification != null )
138+ {
139+ notificationElement . Add ( new XElement ( "dateSent" , notification . SentDate . ToString ( ) ) ) ;
140+ notificationElement . Add ( new XElement ( "title" , notification . Title ) ) ;
141+ }
142+ notificationElement . Add ( notificationDetails ) ;
143+
144+ notifications . Add ( notificationElement ) ;
145+ }
146+ document . Add ( notifications ) ;
147+ return document . ToString ( ) ;
51148 }
52149 }
53- }
150+ }
0 commit comments