Exemplo n.º 1
0
        /// <summary>
        /// This method turns a line of a CSV-File into a new FireAlarmSystem.
        /// </summary>
        /// <param name="csv">A line of a CSV-File you want to convert.</param>
        /// <returns>Returns a new FireAlarmSystem or null if an error occures.</returns>
        public static FireAlarmSystem GetFromCsv(string csv)
        {
            string[] values;

            if (csv != null)
            {
                try
                {
                    values = csv.Split(';');
                    int    id          = Convert.ToInt32(values[0]);
                    string company     = values[1];
                    string description = values[2];
                    string country     = values[3];
                    string city        = values[4];
                    string postalCode  = values[5];
                    string address     = values[6];

                    HashSet <int> fireBrigades = new HashSet <int>();
                    foreach (string s in values[7].Split(','))
                    {
                        fireBrigades.Add(Convert.ToInt32(s));
                    }

                    HashSet <int> serviceGroups = new HashSet <int>();
                    foreach (string s in values[8].Split(','))
                    {
                        serviceGroups.Add(Convert.ToInt32(s));
                    }

                    FireAlarmSystem fas = new FireAlarmSystem(
                        id,
                        company,
                        description,
                        country,
                        city,
                        postalCode,
                        address);

                    fas.FireBrigades  = fireBrigades;
                    fas.ServiceGroups = serviceGroups;

                    return(fas);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
 public static bool Equals(this FireAlarmSystem fas, FireAlarmSystem other)
 {
     if (fas.Id == other.Id)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 3
0
        public User[] GetUsers(int id, string type)
        {
            List <User> results = new List <User>();

            try
            {
                User user;
                Authentication.Token.CheckAccess(Request.Headers, out user);
                if (user != null)
                {
                    FireAlarmSystem fas = DatabaseOperations.FireAlarmSystems.GetById(id);

                    if (type == "fb")
                    {
                        results.AddRange(DatabaseOperations.FireAlarmSystems.GetUsers(fas, UserTypes.fireFighter));
                    }
                    else
                    {
                        if (type == "sg")
                        {
                            results.AddRange(DatabaseOperations.FireAlarmSystems.GetUsers(fas, UserTypes.servicemember));
                        }
                        else
                        {
                            if (type == "fas")
                            {
                                results.AddRange(DatabaseOperations.FireAlarmSystems.GetUsers(fas, UserTypes.fireSafetyEngineer));
                            }
                            else
                            {
                                results.AddRange(DatabaseOperations.FireAlarmSystems.GetUsers(fas));
                            }
                        }
                    }

                    results = Filter.UsersFilter.UserFilter(results, user).ToList();
                    results.Distinct();

                    return(results.ToArray());
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception)
            {
                return(new User[0]);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Upserts a FireAlarmSystem into the database.
        /// </summary>
        /// <param name="fas">The FireAlarmSystem you want to upsert.</param>
        /// <returns>Returns true if the FireAlarmSystem was upserted.</returns>
        public static bool UpsertFireAlarmSystem(FireAlarmSystem fas)
        {
            try
            {
                // Insert into local database.
                //LocalDatabase.UpsertFireAlarmSystem(fas);

                // Insert into remote database.
                return(DatabaseOperations.LiteDB.LiteDbUpserts.UpsertFireAlarmSystem(fas));
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Upserts a FireAlarmSystem into the LiteDB.
 /// </summary>
 /// <param name="fas">The FireAlarmSystem you want to upsert.</param>
 /// <returns>Returns true if the FireAlarmSystem was upserted.</returns>
 public static bool UpsertFireAlarmSystem(FireAlarmSystem fas)
 {
     if (fas != null)
     {
         using (var db = AppData.FireAlarmSystemDB())
         {
             var table = db.FireAlarmSystemTable();
             table.Upsert(fas);
             return(true);
         }
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Inserts a FireAlarmSystem into the database or updates it if it already exists.
        /// </summary>
        /// <param name="fas">The FireAlarmSystem you want to upsert.</param>
        /// <returns>Returns true if the FireAlarmSystem was inserted.</returns>
        public static bool Upsert(FireAlarmSystem fas, User user)
        {
            if (fas != null && fas.Id != 0)
            {
                bool ok = DatabaseOperations.DbUpserts.UpsertFireAlarmSystem(fas);
                if (ok)
                {
                    Logging.Logger.Log("upsert", user.GetUserDescription(), fas);
                }

                return(ok);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Checks if the id of a ServiceGroup is in the list
        /// of ServiceGroups of this FireAlarmSystem.
        /// </summary>
        /// <param name="id">The id of the ServiceGroup you want to check.</param>
        /// <returns>Returns true if the list of ServiceGroups contains the id.</returns>
        public static bool CheckServiceGroup(this FireAlarmSystem fas, int id)
        {
            bool found = false;

            if (fas.ServiceGroups != null)
            {
                foreach (int sg in fas.ServiceGroups)
                {
                    if (sg == id)
                    {
                        found = true;
                        break;
                    }
                }
            }
            return(found);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Checks if the id of a FireBrigade is in the list
        /// of FireBrigades of this FireAlarmSystem.
        /// </summary>
        /// <param name="id">The id of the FireBrigade you want to check.</param>
        /// <returns>Returns true if the list of FireBrigades contains the id.</returns>
        public static bool CheckFireBrigade(this FireAlarmSystem fas, int id)
        {
            bool found = false;

            if (fas.FireBrigades != null)
            {
                foreach (int fb in fas.FireBrigades)
                {
                    if (fb == id)
                    {
                        found = true;
                        break;
                    }
                }
            }
            return(found);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Returns a list of FireBrigades or ServiceGroups of this FireAlarmSystem.
        /// </summary>
        /// <param name="fas">The FireAlarmSystem with the Ids of the FireBrigades and ServiceGroups.</param>
        /// <param name="type">The type of member you want (FireBrigde or ServiceGroup).</param>
        /// <returns>Returns a list of FireBrigades or ServiceGroups of this FireAlarmSystem.</returns>
        public static IEnumerable <object> GetMembers(FireAlarmSystem fas, Type type)
        {
            List <object> results = new List <object>();

            if (type == typeof(FireBrigade))
            {
                results.AddRange(DatabaseOperations.FireBrigades.GetByFireAlarmSystem(fas));
            }
            else
            {
                if (type == typeof(ServiceGroup))
                {
                    results.AddRange(DatabaseOperations.ServiceGroups.GetByFireAlarmSystem(fas));
                }
            }

            return(results);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Returns all FireBrigades associated with this FireAlarmSystem.
        /// </summary>
        /// <param name="fas">The FireAlarmsSystem with the Ids of the FireBrigades.</param>
        /// <returns>Returns all FireBrigades associated with this FireAlarmSystem.</returns>
        public static IEnumerable <FireBrigade> GetByFireAlarmSystem(FireAlarmSystem fas)
        {
            List <FireBrigade> results = new List <FireBrigade>();

            try
            {
                foreach (int id in fas.FireBrigades)
                {
                    results.Add(GetById(id));
                }

                return(results);
            }
            catch (Exception)
            {
                return(new List <FireBrigade>());
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Returns all Users that are associated with this FireAlarmSystem.
        /// </summary>
        /// <param name="fas">The FireAlarmSystem you want to get the Users of.</param>
        /// <returns>Returns all Users that are associated with this FireAlarmSystem.</returns>
        public static IEnumerable <User> GetUsers(FireAlarmSystem fas)
        {
            List <User> results = new List <User>();

            // Get all Users from the FireBrigades of the FireAlarmSystem.
            foreach (int firebrigade in fas.FireBrigades)
            {
                results.AddRange(DatabaseOperations.Users.GetByAuthorizedObject(firebrigade, UserTypes.fireFighter));
            }

            // Get all Users from the ServiceGroups of the FireAlarmSystem.
            foreach (int servicegroup in fas.ServiceGroups)
            {
                results.AddRange(DatabaseOperations.Users.GetByAuthorizedObject(servicegroup, UserTypes.servicemember));
            }

            // Get all Users from the FireAlarmSystem itself.
            results.AddRange(DatabaseOperations.Users.GetByAuthorizedObject(fas.Id, UserTypes.fireSafetyEngineer));

            return(results);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Turns this FireAlarmSystem into a CSV line.
        /// </summary>
        /// <returns>Returns a CSV line with the values of the FireAlarmSystem.</returns>
        public static string ToCsv(this FireAlarmSystem fas)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(fas.Id);
            sb.Append(';');
            sb.Append(fas.Company);
            sb.Append(';');
            sb.Append(fas.Description);
            sb.Append(';');
            sb.Append(fas.Country);
            sb.Append(';');
            sb.Append(fas.City);
            sb.Append(';');
            sb.Append(fas.PostalCode);
            sb.Append(';');
            sb.Append(fas.Address);
            sb.Append(';');
            sb.Append(String.Join(",", fas.FireBrigades));
            sb.Append(';');
            sb.Append(String.Join(",", fas.ServiceGroups));

            return(sb.ToString());
        }
Exemplo n.º 13
0
        /// <summary>
        /// Deletes a FireAlarmSystem from the cache.
        /// </summary>
        /// <param name="id">Id of the FireAlarmSystem you want to delete.</param>
        public static void DeleteFireAlarmSystem(int id)
        {
            List <FireAlarmSystem> allFireAlarmSystems = GetAllFireAlarmSystems().ToList();
            FireAlarmSystem        old = null;

            foreach (FireAlarmSystem fas in allFireAlarmSystems)
            {
                if (fas.Id == id)
                {
                    old = fas;
                    break;
                }
            }

            if (old != null)
            {
                allFireAlarmSystems.Remove(old);
                GlobalCachingProvider.Instance.RemoveItem(fireAlarmSystemsString);
                if (allFireAlarmSystems != null)
                {
                    GlobalCachingProvider.Instance.AddItem(fireAlarmSystemsString, allFireAlarmSystems);
                }
            }
        }
Exemplo n.º 14
0
 public static int GetHashCode(this FireAlarmSystem fas)
 {
     return(fas.Id.GetHashCode());
 }
 public BuildingManagementSystem()
 {
     FireAlarmSystem = new FireAlarmSystem();
 }
Exemplo n.º 16
0
        public Int32 ChangeMember(int fireAlarmSystem, string type, int member, string operation)
        {
            try
            {
                User user;
                Authentication.Token.CheckAccess(Request.Headers, out user);
                if (user != null)
                {
                    if (user.UserType == UserTypes.admin)
                    {
                        if (type == null || operation == null)
                        {
                            throw new ArgumentNullException();
                        }
                        else
                        {
                            // Get the FireAlarmSystem by the id.
                            // The method throws an Exception if the FireAlarmSystem does not exist.
                            FireAlarmSystem fas = DatabaseOperations.FireAlarmSystems.GetById(fireAlarmSystem);

                            if (type == "fb")
                            {
                                // Get the FireBrigade by the id.
                                // The method throws an Exception if the FireBrigade does not exist.
                                DatabaseOperations.FireBrigades.GetById(member);

                                if (operation == "add")
                                {
                                    fas.FireBrigades.Add(member);
                                    DatabaseOperations.FireAlarmSystems.Upsert(fas, user);
                                    return(1);
                                }
                                else
                                {
                                    if (operation == "delete")
                                    {
                                        fas.FireBrigades.Remove(member);
                                        DatabaseOperations.FireAlarmSystems.Upsert(fas, user);
                                        return(1);
                                    }
                                    else
                                    {
                                        throw new ArgumentOutOfRangeException();
                                    }
                                }
                            }
                            else
                            {
                                if (type == "sg")
                                {
                                    // Get the ServiceGroup by the id.
                                    // The method throws an Exception if the ServiceGroup does not exist.
                                    DatabaseOperations.ServiceGroups.GetById(member);

                                    if (operation == "add")
                                    {
                                        fas.ServiceGroups.Add(member);
                                        DatabaseOperations.FireAlarmSystems.Upsert(fas, user);
                                        return(1);
                                    }
                                    else
                                    {
                                        if (operation == "delete")
                                        {
                                            fas.ServiceGroups.Remove(member);
                                            DatabaseOperations.FireAlarmSystems.Upsert(fas, user);
                                            return(1);
                                        }
                                        else
                                        {
                                            throw new ArgumentOutOfRangeException();
                                        }
                                    }
                                }
                                else
                                {
                                    throw new ArgumentOutOfRangeException();
                                }
                            }
                        }
                    }
                    else
                    {
                        // User is not an admin.
                        return(-1);
                    }
                }
                else
                {
                    // Notify user that the login was not successful.
                    return(0);
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                return(-2);
            }
            catch (Exception)
            {
                return(-3);
            }
        }
Exemplo n.º 17
0
        public object[] GetMembers(int id, string type)
        {
            List <object> results = new List <object>();

            try
            {
                User user;
                Authentication.Token.CheckAccess(Request.Headers, out user);
                if (user != null)
                {
                    if (user.UserType == UserTypes.admin ||
                        (user.UserType == UserTypes.servicemember && user.AuthorizedObjectIds.Contains(id)))
                    {
                        FireAlarmSystem fas = DatabaseOperations.FireAlarmSystems.GetById(id);
                        if (type == "fb")
                        {
                            // Add all FireBrigades of the FireAlarmSystem to result.
                            results.AddRange(DatabaseOperations.FireAlarmSystems.GetMembers(fas, typeof(FireBrigade)));
                        }
                        else
                        {
                            if (type == "sg")
                            {
                                // Add all ServiceGroups of the FireAlarmSystem to result.
                                results.AddRange(DatabaseOperations.FireAlarmSystems.GetMembers(fas, typeof(ServiceGroup)));
                            }
                            else
                            {
                                // Add all ServiceGroups and FireBrigades of the FireAlarmSystem to result.
                                results.AddRange(DatabaseOperations.FireAlarmSystems.GetMembers(fas));
                            }
                        }

                        // Get all FireBrigades of the FireAlarmSystem.
                        List <FireBrigade>  fb = new List <FireBrigade>();
                        List <ServiceGroup> sg = new List <ServiceGroup>();
                        foreach (object o in results)
                        {
                            if (o is FireBrigade)
                            {
                                fb.Add(o as FireBrigade);
                            }
                            else
                            {
                                if (o is ServiceGroup)
                                {
                                    sg.Add(o as ServiceGroup);
                                }
                            }
                        }

                        fb = Filter.FireBrigadesFilter.UserFilter(fb, user).ToList();
                        sg = Filter.ServiceGroupsFilter.UserFilter(sg, user).ToList();

                        results.Clear();
                        results.AddRange(fb);
                        results.AddRange(sg);
                        results.Distinct();

                        return(results.ToArray());
                    }
                    else
                    {
                        // The User is not an admin.
                        throw new Exception();
                    }
                }
                else
                {
                    // Notify user that the login was not successful.
                    return(null);
                }
            }
            catch (Exception)
            {
                return(new object[0]);
            }
        }