/// <summary>
        /// Convert a ServiceInstance (used for the database) in a WService (used for the web).
        /// </summary>
        /// <param name="db">Database connector data context.</param>
        /// <param name="user">User that requires the conversion.</param>
        /// <param name="serviceInstance">The ServiceInstance to convert.</param>
        /// <param name="calculateFeature">True if you need to have all the information about the User, false otherwise.</param>
        /// <returns>A WService.</returns>
        public static WService ServiceInstanceToWService(ConnectorDataContext db, User user, ServiceInstance serviceInstance, bool calculateFeature)
        {
            WService result = null;

            if (calculateFeature)
            {
                bool isRegistered = false;
                Stopwatch w = Stopwatch.StartNew();
                IEnumerable<ServiceInstance> myServices = db.Registrations.Where(r => r.user == user.id).Select(r => r.ServiceInstance);
                w.Stop();
                ILog log = LogManager.GetLogger("QueryLogger");
                log.Info(" Elapsed time: " + w.Elapsed + ", user id: " + user.id + ", select all service instances of an user");
                if (myServices.Contains(serviceInstance))
                    isRegistered = true;

                List<FeaturesType> privateFeatures = ServiceFactory.getService(serviceInstance.Service.name).GetPrivateFeatures();
                bool requireOAuth = false;
                int oauthVersion = 0;
                if (privateFeatures.Contains(FeaturesType.OAuth1))
                {
                    requireOAuth = true;
                    oauthVersion = 1;
                }
                else if (privateFeatures.Contains(FeaturesType.OAuth2))
                {
                    requireOAuth = true;
                    oauthVersion = 2;
                }

                bool requireTFSAuthentication = false;
                bool requireTFSDomain = false;
                if (privateFeatures.Contains(FeaturesType.TFSAuthenticationWithDomain))
                {
                    requireTFSAuthentication = true;
                    requireTFSDomain = true;
                }
                else if (privateFeatures.Contains(FeaturesType.TFSAuthenticationWithoutDomain))
                {
                    requireTFSAuthentication = true;
                    requireTFSDomain = false;
                }

                result = new WService()
                {
                    Id = serviceInstance.id,
                    Name = serviceInstance.name,
                    Host = serviceInstance.host,
                    BaseService = serviceInstance.Service.name,
                    Image = serviceInstance.Service.image,
                    Registered = isRegistered,
                    RequireOAuth = requireOAuth,
                    OAuthVersion = oauthVersion,
                    RequireTFSAuthentication = requireTFSAuthentication,
                    RequireTFSDomain = requireTFSDomain
                };
            }
            else
            {
                result = new WService()
                {
                    Id = serviceInstance.id,
                    Name = serviceInstance.name,
                    BaseService = serviceInstance.Service.name,
                    Image = serviceInstance.Service.image
                };
            }

            return result;
        }
        private bool RegisterUserOnAService(SocialTFSEntities db, User user, ServiceInstance serviceInstance, IUser iUser, String accessToken, String accessSecret)
        {
            try
            {
                db.Registration.AddObject(new Registration
                {
                    pk_fk_user = user.pk_id,
                    ServiceInstance = serviceInstance,
                    nameOnService = iUser.UserName,
                    idOnService = iUser.Id.ToString(),
                    accessToken = accessToken,
                    accessSecret = accessSecret
                });

                db.SaveChanges();
                return true;
            }
            catch (ChangeConflictException)
            {
                return false;
            }
        }
        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 ServiceInstance. Utilizzare il metodo .Add della proprietà associata ObjectSet&lt;T&gt;.
 /// </summary>
 public void AddToServiceInstance(ServiceInstance serviceInstance)
 {
     base.AddObject("ServiceInstance", serviceInstance);
 }
 /// <summary>
 /// Crea un nuovo oggetto ServiceInstance.
 /// </summary>
 /// <param name="pk_id">Valore iniziale della proprietà pk_id.</param>
 /// <param name="name">Valore iniziale della proprietà name.</param>
 /// <param name="host">Valore iniziale della proprietà host.</param>
 /// <param name="fk_service">Valore iniziale della proprietà fk_service.</param>
 public static ServiceInstance CreateServiceInstance(global::System.Int32 pk_id, global::System.String name, global::System.String host, global::System.Int32 fk_service)
 {
     ServiceInstance serviceInstance = new ServiceInstance();
     serviceInstance.pk_id = pk_id;
     serviceInstance.name = name;
     serviceInstance.host = host;
     serviceInstance.fk_service = fk_service;
     return serviceInstance;
 }
        /// <summary>
        /// Convert a ServiceInstance (used for the database) in a WService (used for the web).
        /// </summary>
        /// <param name="db">Database connector data context.</param>
        /// <param name="user">User that requires the conversion.</param>
        /// <param name="serviceInstance">The ServiceInstance to convert.</param>
        /// <param name="calculateFeature">True if you need to have all the information about the User, false otherwise.</param>
        /// <returns>A WService.</returns>
        public static WService ServiceInstanceToWService(SocialTFSEntities db, User user, ServiceInstance serviceInstance, bool calculateFeature)
        {
            WService result = null;

            if (calculateFeature)
            {
                bool isRegistered = false;
                IEnumerable<ServiceInstance> myServices = db.Registration.Where(r => r.pk_fk_user == user.pk_id).Select(r => r.ServiceInstance);
                if (myServices.Contains(serviceInstance))
                    isRegistered = true;

                List<FeaturesType> privateFeatures = ServiceFactory.getService(serviceInstance.Service.name).GetPrivateFeatures();
                bool requireOAuth = false;
                int oauthVersion = 0;
                if (privateFeatures.Contains(FeaturesType.OAuth1))
                {
                    requireOAuth = true;
                    oauthVersion = 1;
                }
                else if (privateFeatures.Contains(FeaturesType.OAuth2))
                {
                    requireOAuth = true;
                    oauthVersion = 2;
                }

                bool requireTFSAuthentication = false;
                bool requireTFSDomain = false;
                if (privateFeatures.Contains(FeaturesType.TFSAuthenticationWithDomain))
                {
                    requireTFSAuthentication = true;
                    requireTFSDomain = true;
                }
                else if (privateFeatures.Contains(FeaturesType.TFSAuthenticationWithoutDomain))
                {
                    requireTFSAuthentication = true;
                    requireTFSDomain = false;
                }

                result = new WService()
                {
                    Id = serviceInstance.pk_id,
                    Name = serviceInstance.name,
                    Host = serviceInstance.host,
                    BaseService = serviceInstance.Service.name,
                    Image = serviceInstance.Service.image,
                    Registered = isRegistered,
                    RequireOAuth = requireOAuth,
                    OAuthVersion = oauthVersion,
                    RequireTFSAuthentication = requireTFSAuthentication,
                    RequireTFSDomain = requireTFSDomain
                };
            }
            else
            {
                result = new WService()
                {
                    Id = serviceInstance.pk_id,
                    Name = serviceInstance.name,
                    BaseService = serviceInstance.Service.name,
                    Image = serviceInstance.Service.image
                };
            }

            return result;
        }