Example #1
0
        public double CityConsumtion(string city)//metoda za readera
        {
            if (StateService.stateService != EStateServers.Primarni)
            {
                MyException ex = new MyException("Error! Not primary\n");
                throw new FaultException <MyException>(ex);
            }

            if (StateService.stateService != EStateServers.Primarni)
            {
                MyException ex = new MyException("Error! Not primary\n");
                throw new FaultException <MyException>(ex);
            }

            CustomPrincipal principal = Thread.CurrentPrincipal as CustomPrincipal;

            if (!principal.IsInRole(Permissions.ReadingCityAvgConsumption.ToString()))
            {
                SecurityException ex = new SecurityException("Error! IS NOT A Reader\n");
                throw new FaultException <SecurityException>(ex);
            }

            if (!File.Exists(DataBase.FileName))
            {
                MyException ex = new MyException("Error! file not find\n");
                throw new FaultException <MyException>(ex);
            }

            double suma  = 0;
            int    count = 0;

            lock (DataBase.lockObject)
            {
                foreach (var item in DataBase.consumers.Values)
                {
                    if (item.City == city)
                    {
                        suma += item.Consumation;
                        count++;
                    }
                }
            }

            Console.WriteLine("Srednja vrednost za grad " + city + " je {0}", Math.Round((suma / count), 2));

            return(suma / count);
        }
Example #2
0
        public bool Delete()
        {
            bool            end   = false;
            CustomPrincipal cp    = Thread.CurrentPrincipal as CustomPrincipal;
            Alarm           alarm = new Alarm();

            if (cp.IsInRole(Permissions.Delete.ToString()))
            {
                Console.WriteLine("Deleting alarm!\n");

                List <Alarm> a;
                a = alarm.Deserialization(file);
                string alarmName;
                string name = cp.Identity.Name.ToString();
                name = Formatter.ParseName(name.ToString());
                List <Alarm> newL = new List <Alarm>();

                foreach (Alarm al in a)
                {
                    alarmName = Formatter.ParseName(al.name.ToString());
                    while (alarmName.Equals(name))
                    {
                        newL.Add(al);
                        break;
                    }
                }

                foreach (Alarm user in newL)
                {
                    a.Remove(user);
                    alarms.Remove(user);
                }

                alarm.Serialization(a);

                Console.WriteLine("Deleting alarm is done!\n");
                end = true;
            }
            else
            {
                Console.WriteLine("Deleting alarm not successed because of wrong permission!\n");
            }

            return(end);
        }
Example #3
0
        public bool ModificationConsumer(Consumer consumer)//treba da se doda u fajl modifikovani potrosac
        {
            if (StateService.stateService != EStateServers.Primarni)
            {
                MyException ex = new MyException("Error! Not primary\n");
                throw new FaultException <MyException>(ex);
            }

            CustomPrincipal principal = Thread.CurrentPrincipal as CustomPrincipal;

            if (!principal.IsInRole(Permissions.Modify.ToString()))
            {
                SecurityException ex = new SecurityException("Error! IS NOT A Writer\n");
                throw new FaultException <SecurityException>(ex);
            }

            if (!DataBase.consumers.ContainsKey(consumer.ConsumerID))
            {
                return(false);
            }

            if (!File.Exists(DataBase.FileName))
            {
                MyException ex = new MyException("Error!Cant open file\n");
                throw new FaultException <MyException>(ex);
            }

            string TextAll = string.Empty;
            string TextNew = CreateConsumerString(consumer.ConsumerID, consumer.Region, consumer.City, consumer.Year, consumer.Consumation);
            string TextOld = CreateConsumerString(DataBase.consumers[consumer.ConsumerID].ConsumerID, DataBase.consumers[consumer.ConsumerID].Region,
                                                  DataBase.consumers[consumer.ConsumerID].City, DataBase.consumers[consumer.ConsumerID].Year, DataBase.consumers[consumer.ConsumerID].Consumation);

            lock (DataBase.lockObject)
            {
                TextAll = File.ReadAllText(DataBase.FileName);

                string NewTextAll = TextAll.Replace(TextOld, TextNew);

                File.WriteAllText(DataBase.FileName, NewTextAll);
                DataBase.consumers[consumer.ConsumerID]      = consumer;
                DataBase.consumersDelta[consumer.ConsumerID] = consumer;
            }

            return(true);
        }
Example #4
0
        public double MaxRegionConsumation(string region)
        {
            if (StateService.stateService != EStateServers.Primarni)
            {
                MyException ex = new MyException("Error! Not primary\n");
                throw new FaultException <MyException>(ex);
            }

            CustomPrincipal principal = Thread.CurrentPrincipal as CustomPrincipal;

            if (!principal.IsInRole(Permissions.ReadingMaxAvgConsumption.ToString()))
            {
                MyException ex = new MyException("Error! IS NOT A Reader\n");
                throw new FaultException <MyException>(ex);
            }


            if (!File.Exists(DataBase.FileName))
            {
                MyException ex = new MyException("Error! file not find\n");
                throw new FaultException <MyException>(ex);
            }

            double maxTemp = 0;
            double max     = 0;

            lock (DataBase.lockObject)
            {
                foreach (var item in DataBase.consumers.Values)
                {
                    if (item.Region == region)
                    {
                        if (max < maxTemp)
                        {
                            max = maxTemp;
                        }
                        max = item.Consumation;
                    }
                }
            }
            Console.WriteLine("Maksimalna vrednost za " + region + " region je {0}", max);

            return(max);
        }
Example #5
0
        public bool DodajKorisnika(string korisnickoIme, string sifra, string ime, string prezime, int brojRacuna, float novcanaSredstva)
        {
            CustomPrincipal principal    = Thread.CurrentPrincipal as CustomPrincipal;
            Korisnik        noviKorisnik = new Korisnik(korisnickoIme, sifra, ime, prezime, brojRacuna, novcanaSredstva);

            //Korisnik pronadjiKorisnika = FindUserName(korisnickoIme);
            //prvo provera da li ima prms da doda novog korisnikia
            // da li postouji korisnik sa takvim korisnickim imenom..
            if (principal.IsInRole(Permissions.DodajKorisnika.ToString()))
            {
                //if (pronadjiKorisnika == null)
                //{
                //    // da li postoji fajl pre nego sto kreiras...
                //    // ako ne postoji kreiraj novi
                //    //ako postoji onda pisi u tom..
                //    using (XmlWriter writer = XmlWriter.Create("Korisnici.xml"))
                //    {
                //        writer.WriteStartDocument();
                //        writer.WriteStartElement("Korisnik");

                //        writer.WriteAttributeString("Korisnicko ime", korisnickoIme);
                //        writer.WriteAttributeString("sifra", sifra);
                //        writer.WriteAttributeString("Ime", ime);
                //        writer.WriteAttributeString("Prezime", prezime);
                //        writer.WriteAttributeString("Broj racuna", brojRacuna.ToString());
                //        writer.WriteAttributeString("Novcana sredstva", novcanaSredstva.ToString());

                //        writer.WriteEndElement();
                //        writer.WriteEndDocument();
                //    }
                //}
            }
            else
            {
                Console.WriteLine("Nemate dozvolu da dodate korisnika {0}!", principal.Identity.Name);
                //neuspesna autorizacija -- beleziti u Event logu
            }

            return(false);
        }
Example #6
0
        public bool AddConsumer(Consumer consumer)
        {
            if (StateService.stateService != EStateServers.Primarni)
            {
                MyException ex = new MyException("Error! Not primary\n");
                throw new FaultException <MyException>(ex);
            }

            CustomPrincipal principal = Thread.CurrentPrincipal as CustomPrincipal;

            if (!principal.IsInRole(Permissions.Writting.ToString()))
            {
                SecurityException ex = new SecurityException("Error! IS NOT A Writer\n");
                throw new FaultException <SecurityException>(ex);
            }
            // CustomAuditBehavior.AuthenticationSuccess(principal.Identity.Name.ToString());
            if (!File.Exists(DataBase.FileName))
            {
                MyException ex = new MyException("Error! file not found\n");
                throw new FaultException <MyException>(ex);
            }

            if (DataBase.consumers.ContainsKey(consumer.ConsumerID))
            {
                return(false);
            }

            lock (DataBase.lockObject)
            {
                DataBase.consumers[consumer.ConsumerID]      = consumer;
                DataBase.consumersDelta[consumer.ConsumerID] = consumer;
                using (StreamWriter file = File.AppendText(DataBase.FileName))
                {
                    file.WriteLine(CreateConsumerString(consumer.ConsumerID, consumer.Region, consumer.City, consumer.Year, consumer.Consumation));
                }
            }

            return(true);
        }
Example #7
0
        public bool ArchiveConsumation()
        {
            if (StateService.stateService != EStateServers.Primarni)
            {
                MyException ex = new MyException("Error! Not primary\n");
                throw new FaultException <MyException>(ex);
            }
            CustomPrincipal principal = Thread.CurrentPrincipal as CustomPrincipal;


            if (!File.Exists(DataBase.FileName))
            {
                return(false);
                //MyException ex = new MyException("Error! File doesn't exist\n");
                //throw new FaultException<MyException>(ex);
            }
            if (!principal.IsInRole(Permissions.Arhiving.ToString()))
            {
                MyException ex = new MyException("Error! IS NOT A Reader\n");
                throw new FaultException <MyException>(ex);
            }

            string fileNameCopy = DataBase.FileName;

            do
            {
                fileNameCopy += "-copy";
            } while (File.Exists(fileNameCopy));
            File.Copy(DataBase.FileName, fileNameCopy);

            if (StateService.stateService == EStateServers.Primarni)
            {
                ChannelFactory <IReplicator> cfh2 = new ChannelFactory <IReplicator>("sekundarni");
                IReplicator proxy2 = cfh2.CreateChannel();
                proxy2.ArchiveConsumation();
            }
            return(true);
        }
Example #8
0
        public bool DeleteAll()
        {
            bool  end   = false;
            Alarm alarm = new Alarm();

            CustomPrincipal cp = Thread.CurrentPrincipal as CustomPrincipal;

            if (cp.IsInRole(Permissions.DeleteAll.ToString()))
            {
                Console.WriteLine("Deleting all alarms...\n");

                alarms.Clear();
                alarm.Serialization(alarms);

                Console.WriteLine("Deleting all alarms is done!\n");
                end = true;
            }
            else
            {
                Console.WriteLine("Deleting all not successed because of wrong permission!\n");
            }

            return(end);
        }