public bool Post(String username, String password, String message)
        {
            Contract.Requires(!String.IsNullOrEmpty(username));
            Contract.Requires(!String.IsNullOrEmpty(password));
            Contract.Requires(!String.IsNullOrEmpty(message));

            SocialTFSEntities db = new SocialTFSEntities();

            User user = CheckCredentials(db, username, password);
            if (user == null)
                return false;

            int service = db.ServiceInstance.Where(si => si.Service.name == "SocialTFS").Single().pk_id;

            long chosenFeature = -1;

            try
            {
              String str = FeaturesType.Post.ToString();
              chosenFeature = db.ChosenFeature.Where(cf => cf.fk_user == user.pk_id && cf.fk_serviceInstance == service && cf.fk_feature == str).SingleOrDefault().pk_id;
            }
            catch (InvalidOperationException)
            {
                try
                {
                    db.Registration.Where(r => r.pk_fk_user == user.pk_id && r.pk_fk_serviceInstance == service).Single();
                }
                catch
                {
                    Registration registration = new Registration()
                    {
                        User = user,
                        pk_fk_serviceInstance = db.ServiceInstance.Where(si => si.Service.name == "SocialTFS").Single().pk_id,
                        nameOnService = username,
                        idOnService = username
                    };
                    db.Registration.AddObject(registration);
                    db.SaveChanges();
                }

                ChosenFeature newChoseFeature = new ChosenFeature()
                {
                    Registration = db.Registration.Where(r => r.pk_fk_user == user.pk_id && r.pk_fk_serviceInstance == service).Single(),
                    fk_feature = FeaturesType.Post.ToString(),
                    lastDownload = new DateTime(1900, 1, 1)
                };

                db.ChosenFeature.AddObject(newChoseFeature);
                db.SaveChanges();
                chosenFeature = newChoseFeature.pk_id;
            }

            db.Post.AddObject( new Post
            {
                fk_chosenFeature = chosenFeature,
                message = message,
                createAt = DateTime.UtcNow
            });

            db.SaveChanges();

            return true;
        }
        public int SubscribeUser(String email, String password, String username)
        {
            Contract.Requires(!String.IsNullOrEmpty(email));
            Contract.Requires(!String.IsNullOrEmpty(password));
            Contract.Requires(!String.IsNullOrEmpty(username));

            SocialTFSEntities db = new SocialTFSEntities();
            User user;
            try
            {
                user = db.User.Where(u => u.email == email).Single();
            }
            catch (InvalidOperationException)
            {
                return 1;
            }

            if (user.password != (password))
                return 2;

            if (!IsAvailable(username))
                return 3;

            user.username = username;
            user.active = true;

            List<ServiceInstance> tmplst = db.ServiceInstance.ToList<ServiceInstance>();
            ServiceInstance si = db.ServiceInstance.FirstOrDefault( _si => _si.Service.name == "SocialTFS");
            int pk_fk_serviceInstance = si.pk_id;

            Registration registration = new Registration()
            {
                User = user,
                pk_fk_serviceInstance = pk_fk_serviceInstance,
                nameOnService = username,
                idOnService = username
            };

            db.Registration.AddObject(registration);

            ChosenFeature cf = new ChosenFeature()
            {
                Registration = registration,
                fk_feature = FeaturesType.Post.ToString(),
                lastDownload = new DateTime(1900, 1, 1)
            };
            db.ChosenFeature.AddObject(cf);

            db.SaveChanges();

            return 0;
        }
        public int SubscribeUser(String email, String password, String username)
        {
            Contract.Requires(!String.IsNullOrEmpty(email));
            Contract.Requires(!String.IsNullOrEmpty(password));
            Contract.Requires(!String.IsNullOrEmpty(username));

            ConnectorDataContext db = new ConnectorDataContext();
            User user;
            try
            {
                Stopwatch w = Stopwatch.StartNew();
                user = db.Users.Where(u => u.email == email).Single();
                w.Stop();
                ILog log = LogManager.GetLogger("QueryLogger");
                log.Info(" Elapsed time: " + w.Elapsed + ", user email: " + email + ", select the user to subscribe him");
            }
            catch (InvalidOperationException)
            {
                return 1;
            }

            if (user.password != db.Encrypt(password))
                return 2;

            if (!IsAvailable(username))
                return 3;

            user.username = username;
            user.active = true;

            Stopwatch w1 = Stopwatch.StartNew();
            int sInstance = db.ServiceInstances.Where(si => si.Service.name == "SocialTFS").Single().id;
            w1.Stop();
            ILog log1 = LogManager.GetLogger("QueryLogger");
            log1.Info(" Elapsed time: " + w1.Elapsed + ", select the service instance with name 'SocialTFS'");

            Registration registration = new Registration()
            {
                User = user,
                serviceInstance = sInstance,
                nameOnService = username,
                idOnService = username
            };
            Stopwatch w2 = Stopwatch.StartNew();
            db.Registrations.InsertOnSubmit(registration);
            db.SubmitChanges();
            w2.Stop();
            ILog log2 = LogManager.GetLogger("QueryLogger");
            log2.Info(" Elapsed time: " + w2.Elapsed + ", service instance's id: " + sInstance + ", name and id on service: " + username + ", insert a new registration");

            Stopwatch w3 = Stopwatch.StartNew();
            db.ChosenFeatures.InsertOnSubmit(new ChosenFeature()
            {
                Registration = registration,
                feature = FeaturesType.Post.ToString(),
                lastDownload = new DateTime(1900, 1, 1)
            });
            db.SubmitChanges();
            w3.Stop();
            ILog log3 = LogManager.GetLogger("QueryLogger");
            log3.Info(" Elapsed time: " + w3.Elapsed + ", feature: " + FeaturesType.Post.ToString() + ", last download: " + new DateTime(1900, 1, 1) + ", insert a new Chosen feature");

            return 0;
        }
        public bool Post(String username, String password, String message)
        {
            Contract.Requires(!String.IsNullOrEmpty(username));
            Contract.Requires(!String.IsNullOrEmpty(password));
            Contract.Requires(!String.IsNullOrEmpty(message));

            ConnectorDataContext db = new ConnectorDataContext();

            User user = CheckCredentials(db, username, password);
            if (user == null)
                return false;

            ILog log = LogManager.GetLogger("PanelLogger");
            log.Info(user.id + ",P");

            Stopwatch w1 = Stopwatch.StartNew();
            int service = db.ServiceInstances.Where(si => si.Service.name == "SocialTFS").Single().id;
            w1.Stop();
            ILog log1 = LogManager.GetLogger("QueryLogger");
            log1.Info(" Elapsed time: " + w1.Elapsed + ", select service instance's id of the service 'SocialTFS'");

            long chosenFeature = -1;

            try
            {
                Stopwatch w2 = Stopwatch.StartNew();
                chosenFeature = db.ChosenFeatures.Where(cf => cf.user == user.id && cf.serviceInstance == service && cf.feature == FeaturesType.Post.ToString()).First().id;
                w2.Stop();
                ILog log2 = LogManager.GetLogger("QueryLogger");
                log2.Info(" Elapsed time: " + w2.Elapsed + ", user id: " + user.id + ", service instance: " + service + ", feature's name: " + FeaturesType.Post.ToString() + ", select chosen feature's id");
            }
            catch (InvalidOperationException)
            {
                try
                {
                    Stopwatch w3 = Stopwatch.StartNew();
                    db.Registrations.Where(r => r.user == user.id && r.serviceInstance == service).Single();
                    w3.Stop();
                    ILog log3 = LogManager.GetLogger("QueryLogger");
                    log3.Info(" Elapsed time: " + w3.Elapsed + ", user id: " + user.id + ", service instance: " + service + ", select registration of a service");
                }
                catch
                {
                    Registration registration = new Registration()
                    {
                        User = user,
                        serviceInstance = db.ServiceInstances.Where(si => si.Service.name == "SocialTFS").Single().id,  //considerata poco sopra per il log
                        nameOnService = username,
                        idOnService = username
                    };
                    Stopwatch w4 = Stopwatch.StartNew();
                    db.Registrations.InsertOnSubmit(registration);
                    db.SubmitChanges();
                    w4.Stop();
                    ILog log4 = LogManager.GetLogger("QueryLogger");
                    log4.Info(" Elapsed time: " + w4.Elapsed + ", insert a registration");
                }

                ChosenFeature newChoseFeature = new ChosenFeature()
                {
                    Registration = db.Registrations.Where(r => r.user == user.id && r.serviceInstance == service).Single(), //considerata poco sopra per il log
                    feature = FeaturesType.Post.ToString(),
                    lastDownload = new DateTime(1900, 1, 1)
                };
                Stopwatch w5 = Stopwatch.StartNew();
                db.ChosenFeatures.InsertOnSubmit(newChoseFeature);
                db.SubmitChanges();
                w5.Stop();
                ILog log5 = LogManager.GetLogger("QueryLogger");
                log5.Info(" Elapsed time: " + w5.Elapsed + ", feature's name: " + FeaturesType.Post.ToString() + ", last download: " + new DateTime(1900, 1, 1) + ", insert a new chosen feature");
                chosenFeature = newChoseFeature.id;
            }

            Stopwatch w6 = Stopwatch.StartNew();
            db.Posts.InsertOnSubmit(new Post
            {
                chosenFeature = chosenFeature,
                message = message,
                createAt = DateTime.UtcNow
            });

            db.SubmitChanges();
            w6.Stop();
            ILog log6 = LogManager.GetLogger("QueryLogger");
            log6.Info(" Elapsed time: " + w6.Elapsed + ", message: " + message + ", date time: " + DateTime.UtcNow + ", insert the post");

            return true;
        }
        private bool RegisterUserOnAService(ConnectorDataContext db, User user, ServiceInstance serviceInstance, IUser iUser, String accessToken, String accessSecret)
        {
            try
            {
                Registration reg = new Registration();

                reg.user = user.id;
                reg.ServiceInstance = serviceInstance;
                reg.nameOnService = iUser.UserName != null ? iUser.UserName : iUser.Id;
                reg.idOnService = iUser.Id.ToString();
                reg.accessToken = accessToken;
                reg.accessSecret = accessSecret;

                Stopwatch w = Stopwatch.StartNew();
                db.Registrations.InsertOnSubmit(reg);
                db.SubmitChanges();
                w.Stop();
                ILog log = LogManager.GetLogger("QueryLogger");
                log.Info(" Elapsed time: " + w.Elapsed + ", user id: " + user.id + ", id on service: " + iUser.Id.ToString() + ", access token: " + accessToken + ", access secret: " + accessSecret + ", register user on a service");
                return true;
            }
            catch (ChangeConflictException)
            {
                return false;
            }
        }
 /// <summary>
 /// Metodo deprecato per l'aggiunta di un nuovo oggetto all'elemento EntitySet Registration. Utilizzare il metodo .Add della proprietà associata ObjectSet&lt;T&gt;.
 /// </summary>
 public void AddToRegistration(Registration registration)
 {
     base.AddObject("Registration", registration);
 }
 /// <summary>
 /// Crea un nuovo oggetto Registration.
 /// </summary>
 /// <param name="pk_fk_user">Valore iniziale della proprietà pk_fk_user.</param>
 /// <param name="pk_fk_serviceInstance">Valore iniziale della proprietà pk_fk_serviceInstance.</param>
 /// <param name="nameOnService">Valore iniziale della proprietà nameOnService.</param>
 /// <param name="idOnService">Valore iniziale della proprietà idOnService.</param>
 public static Registration CreateRegistration(global::System.Int32 pk_fk_user, global::System.Int32 pk_fk_serviceInstance, global::System.String nameOnService, global::System.String idOnService)
 {
     Registration registration = new Registration();
     registration.pk_fk_user = pk_fk_user;
     registration.pk_fk_serviceInstance = pk_fk_serviceInstance;
     registration.nameOnService = nameOnService;
     registration.idOnService = idOnService;
     return registration;
 }