Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
377 lines (342 sloc) 14.6 KB
/*
* Copyright 2017 GIG Technology NV
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @@license_version:1.3@@
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Text;
using Newtonsoft.Json;
using Com.Mobicage.Rogerthat;
using Com.Mobicage.Rogerthat.Messaging;
/// <summary>
/// Summary description for MeetingRoomBooking
/// </summary>
public class MeetingRoomBooking
{
public const string GET_SCHEDULE = "load_meeting_room_info";
public const string CANCEL_BOOKING = "cancel_meeting_room";
public const string WARN_ME_WHEN_PEOPLE_ARRIVE = "warn_me_when_people_arrive";
public const string NEW_MEETING = "book-meeting-room";
public long Id;
public long Day;
public int From;
public int Till;
public string Users;
public long meetingRoom;
public List<string> NotifyUsers;
public bool UsersNotified;
public string UsersCheckedIn;
private MeetingRoomBooking(IDataReader reader)
{
this.Id = reader.GetInt32(0);
this.Day = reader.GetInt32(1);
this.From = reader.GetInt32(2);
this.Till = reader.GetInt32(3);
this.Users = reader.GetString(4);
this.meetingRoom = reader.GetInt32(5);
this.UsersNotified = reader.IsDBNull(6) ? false : reader.GetBoolean(6);
if (reader.IsDBNull(7))
this.NotifyUsers = new List<string>();
else
this.NotifyUsers = JsonConvert.DeserializeObject<List<string>>(reader.GetString(7));
this.UsersCheckedIn = reader.IsDBNull(8) ? null : reader.GetString(8);
}
public static List<MeetingRoomBooking> list(IDbConnection db, MeetingRoom mr)
{
using (IDbCommand command = db.CreateCommand())
{
command.CommandText = "select id, day, [from], till, _user, _meetingroom, users_notified, users_to_be_notified, users_checkedin from booking";
List<MeetingRoomBooking> MeetingRoomBooking = new List<MeetingRoomBooking>();
using (IDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
MeetingRoomBooking.Add(new MeetingRoomBooking(reader));
}
return MeetingRoomBooking;
}
}
}
public static MeetingRoomBooking get(IDbConnection db, long mrbid)
{
using (IDbCommand command = db.CreateCommand())
{
command.CommandText = "select id, day, [from], till, _user, _meetingroom, users_notified, users_to_be_notified, users_checkedin from booking where id = @Id";
IDbDataParameter p = command.CreateParameter();
p.ParameterName = "@Id";
p.Value = mrbid;
command.Parameters.Add(p);
using (IDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
return new MeetingRoomBooking(reader);
}
return null;
}
}
}
public static List<MeetingRoomBooking> list(IDbConnection db, MeetingRoom mr, long day)
{
using (IDbCommand command = db.CreateCommand())
{
command.CommandText = "select id, day, [from], till, _user, _meetingroom, users_notified, users_to_be_notified, users_checkedin from booking where day= @Day";
List<MeetingRoomBooking> MeetingRoomBooking = new List<MeetingRoomBooking>();
IDbDataParameter p = command.CreateParameter();
p.ParameterName = "@Day";
p.Value = day;
command.Parameters.Add(p);
using (IDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
MeetingRoomBooking.Add(new MeetingRoomBooking(reader));
}
return MeetingRoomBooking;
}
}
}
public static bool IsBooking(IDbConnection db, MeetingRoom mr, long day, long till, long from)
{
using (IDbCommand command = db.CreateCommand())
{
command.CommandText = "select count(*) from booking where day = @Day and _meetingroom = @Meetingroom and (till between @From and @Till or [from] between @From and @Till)";
IDbDataParameter p1 = command.CreateParameter();
p1.ParameterName = "@Day";
p1.Value = day;
command.Parameters.Add(p1);
IDbDataParameter p2 = command.CreateParameter();
p2.ParameterName = "@Meetingroom";
p2.Value = mr.Id;
command.Parameters.Add(p2);
IDbDataParameter p3 = command.CreateParameter();
p3.ParameterName = "@Till";
p3.Value = till;
command.Parameters.Add(p3);
IDbDataParameter p4 = command.CreateParameter();
p4.ParameterName = "@From";
p4.Value = from;
command.Parameters.Add(p4);
return (int)command.ExecuteScalar() > 0;
}
}
public static string GetSchedule(IDbConnection db)
{
TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
long today = (long)t.TotalSeconds;
StringBuilder sb = new StringBuilder();
LitJson.JsonWriter writer = new LitJson.JsonWriter(sb);
writer.WriteObjectStart();
writer.WritePropertyName("bookings");
writer.WriteArrayStart();
using (IDbCommand command = db.CreateCommand())
{
command.CommandText = "select id, day, [from], till, _user, _meetingroom, users_notified, users_to_be_notified, users_checkedin from booking where day>= @Day order by day, [from]";
IDbDataParameter p = command.CreateParameter();
p.ParameterName = "@Day";
p.Value = today;
command.Parameters.Add(p);
using (IDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
new MeetingRoomBooking(reader).Write(writer);
}
}
}
writer.WriteArrayEnd();
writer.WritePropertyName("rooms");
writer.WriteObjectStart();
List<MeetingRoom> MeetingRooms = new List<MeetingRoom>();
MeetingRooms = MeetingRoom.List(db);
foreach (MeetingRoom meetingRoom in MeetingRooms)
{
writer.WritePropertyName(meetingRoom.Id.ToString());
meetingRoom.Write(writer);
}
writer.WriteObjectEnd();
writer.WritePropertyName("users");
writer.WriteObjectStart();
List<User> Users = new List<User>();
Users = User.list(db);
foreach (User user in Users)
{
writer.WritePropertyName(user.Email);
user.Write(writer);
}
writer.WriteObjectEnd();
writer.WriteObjectEnd();
return sb.ToString();
}
public static void Cancel(IDbConnection db, long meetingRoomBookingId)
{
using (IDbCommand command = db.CreateCommand())
{
using (IDbCommand deletecommand = db.CreateCommand())
{
deletecommand.CommandText = "delete from booking where id = @Id";
IDbDataParameter p2 = deletecommand.CreateParameter();
p2.ParameterName = "@Id";
p2.Value = meetingRoomBookingId;
deletecommand.Parameters.Add(p2);
deletecommand.ExecuteNonQuery();
}
}
}
public static void WarnMeWhenPeopleArrive(IDbConnection db, long meetingRoomBookingId, string userEmail)
{
MeetingRoomBooking booking = MeetingRoomBooking.get(db, meetingRoomBookingId);
if (booking == null)
return;
if (!booking.NotifyUsers.Remove(userEmail))
booking.NotifyUsers.Add(userEmail);
string notifyUsers = JsonConvert.SerializeObject(booking.NotifyUsers);
using (IDbCommand command = db.CreateCommand())
{
command.CommandText = "update booking set users_to_be_notified = @NotifyUsers where id = @Id";
IDbDataParameter p = command.CreateParameter();
p.ParameterName = "@Id";
p.Value = meetingRoomBookingId;
command.Parameters.Add(p);
p = command.CreateParameter();
p.ParameterName = "@NotifyUsers";
p.Value = notifyUsers;
command.Parameters.Add(p);
command.ExecuteNonQuery();
}
}
public static Com.Mobicage.Rogerthat.Messaging.FlowMemberResultResponse BookMeetingRoom(IDbConnection db, Com.Mobicage.Rogerthat.Messaging.FlowMemberResultRequest request)
{
Com.Mobicage.Rogerthat.Messaging.FlowMemberResultResponse result = new Com.Mobicage.Rogerthat.Messaging.FlowMemberResultResponse();
if (!User.Exists(db, request.UserDetails.Email))
User.CreateUser(db, request.UserDetails);
Com.Mobicage.Rogerthat.FormFlowStep Step = (Com.Mobicage.Rogerthat.FormFlowStep)request.Steps[0];
long when = ((Com.Mobicage.Rogerthat.LongWidgetResult)Step.result.widget).Value;
Step = (Com.Mobicage.Rogerthat.FormFlowStep)request.Steps[1];
long From = (long)((Com.Mobicage.Rogerthat.FloatListWidgetResult)Step.result.widget).Values[0];
long Till = (long)((Com.Mobicage.Rogerthat.FloatListWidgetResult)Step.result.widget).Values[1];
Step = (Com.Mobicage.Rogerthat.FormFlowStep)request.Steps[2];
long capacity = (long)((Com.Mobicage.Rogerthat.FloatWidgetResult)Step.result.widget).Value;
List<MeetingRoom> MeetingRooms = MeetingRoom.ValidRoom(db, capacity);
bool isBooking;
foreach (MeetingRoom MR in MeetingRooms)
{
isBooking = MeetingRoomBooking.IsBooking(db, MR, when, Till, From);
if (!isBooking)
{
using (IDbCommand command = db.CreateCommand())
{
command.CommandText = "insert into booking (day, _user, _meetingroom, [from], till) values (@Day, @User, @Meetingroom, @From, @till)";
IDbDataParameter p = command.CreateParameter();
p.ParameterName = "@Day";
p.Value = when;
command.Parameters.Add(p);
IDbDataParameter p2 = command.CreateParameter();
p2.ParameterName = "@User";
p2.Value = request.UserDetails.Email;
command.Parameters.Add(p2);
IDbDataParameter p3 = command.CreateParameter();
p3.ParameterName = "@Meetingroom";
p3.Value = MR.Id;
command.Parameters.Add(p3);
IDbDataParameter p4 = command.CreateParameter();
p4.ParameterName = "@From";
p4.Value = From;
command.Parameters.Add(p4);
IDbDataParameter p5 = command.CreateParameter();
p5.ParameterName = "@Till";
p5.Value = Till;
command.Parameters.Add(p5);
command.ExecuteNonQuery();
}
Com.Mobicage.Rogerthat.MessageCallbackResult CR = new Com.Mobicage.Rogerthat.MessageCallbackResult();
CR.Flags = Com.Mobicage.Rogerthat.MessageFlag.ALLOW_DISMISS.Value;
CR.Text = "Meeting room " + MR.Name + " is successfully reserved for you.";
result.Result = CR;
return result;
}
}
Com.Mobicage.Rogerthat.MessageCallbackResult noRoomMessage = new Com.Mobicage.Rogerthat.MessageCallbackResult();
noRoomMessage.Flags = Com.Mobicage.Rogerthat.MessageFlag.ALLOW_DISMISS.Value;
noRoomMessage.Text = "No Room with a minimum capacity of " + capacity + " available";
result.Result = noRoomMessage;
return result;
}
public static void UserInReach(IDbConnection db, UserDetails userDetails, Beacon beacon, string Proximity)
{
MeetingRoom mr = MeetingRoom.GetByBeacon(db, beacon);
if (mr == null)
return;
TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
long today = (long)t.TotalSeconds;
string UserEmail = userDetails.Email;
if (!MeetingRoomCheckin.DidAlreadyCheckin(db, UserEmail, today))
{
SendRequest Request = new SendRequest();
Request.Members.Add(UserEmail);
Request.Flags = MessageFlag.ALLOW_DISMISS.Value;
Request.Text = "Welcome at TP Office. \nHave a nice working day!";
Api api = new Api();
api.ApiKey = "ak56ba7f5d4285bb685ba8ddf1ad25c8245245dccef6e127724d4743a6cebb1dcd";
try
{
api.Send(Request);
}
catch (Com.Mobicage.Rogerthat.ApiException e)
{
}
}
List<MeetingRoomBooking> bookings = MeetingRoomBooking.list(db, mr, today);
MeetingRoomBooking booking = null;
foreach (MeetingRoomBooking meetingRoomBooking in bookings)
{
long Start = today + (meetingRoomBooking.From * 3600);
long End = today + (meetingRoomBooking.Till * 3600);
if((Start - 5 * 60)<today && today<(End-5*60))
{
booking = meetingRoomBooking;
break;
}
}
}
public void Write(LitJson.JsonWriter writer)
{
writer.WriteObjectStart();
writer.WritePropertyName("id");
writer.Write(this.Id);
writer.WritePropertyName("day");
writer.Write(this.Day);
writer.WritePropertyName("from");
writer.Write(this.From);
writer.WritePropertyName("till");
writer.Write(this.Till);
writer.WritePropertyName("meetingRoom");
writer.Write(this.meetingRoom);
writer.WritePropertyName("user");
writer.Write(this.Users);
writer.WritePropertyName("notifyUsers");
writer.WriteArrayStart();
foreach (string user in this.NotifyUsers)
writer.Write(user);
writer.WriteArrayEnd();
writer.WritePropertyName("usersCheckedIn");
writer.Write(this.UsersCheckedIn);
writer.WriteObjectEnd();
}
}