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 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;
        }
 /// <summary>
 /// Crea un nuovo oggetto ChosenFeature.
 /// </summary>
 /// <param name="pk_id">Valore iniziale della proprietà pk_id.</param>
 /// <param name="fk_user">Valore iniziale della proprietà fk_user.</param>
 /// <param name="fk_serviceInstance">Valore iniziale della proprietà fk_serviceInstance.</param>
 /// <param name="fk_feature">Valore iniziale della proprietà fk_feature.</param>
 /// <param name="lastDownload">Valore iniziale della proprietà lastDownload.</param>
 public static ChosenFeature CreateChosenFeature(global::System.Int64 pk_id, global::System.Int32 fk_user, global::System.Int32 fk_serviceInstance, global::System.String fk_feature, global::System.DateTime lastDownload)
 {
     ChosenFeature chosenFeature = new ChosenFeature();
     chosenFeature.pk_id = pk_id;
     chosenFeature.fk_user = fk_user;
     chosenFeature.fk_serviceInstance = fk_serviceInstance;
     chosenFeature.fk_feature = fk_feature;
     chosenFeature.lastDownload = lastDownload;
     return chosenFeature;
 }
        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;
        }
 /// <summary>
 /// Metodo deprecato per l'aggiunta di un nuovo oggetto all'elemento EntitySet ChosenFeature. Utilizzare il metodo .Add della proprietà associata ObjectSet&lt;T&gt;.
 /// </summary>
 public void AddToChosenFeature(ChosenFeature chosenFeature)
 {
     base.AddObject("ChosenFeature", chosenFeature);
 }