//update cluster in db
func (p *ClusterService) UpdateCluster(cluster entity.Cluster, x_auth_token string) (created bool,
	errorCode string, err error) {
	query := bson.M{}
	query["_id"] = cluster.ObjectId
	created, err = dao.HandleUpdateOne(cluster, dao.QueryStruct{p.collectionName, query, 0, 0, ""})
	if err != nil {
		errorCode = CLUSTER_ERROR_UPDATE
		return
	}
	return
}
func (p *UserService) UserChangePassword(token string, id string, password string, newpassword string, confirm_newpassword string) (created bool, errorCode string, err error) {
	code, err := GetTokenService().TokenValidate(token)
	if err != nil {
		return false, code, err
	}

	if authorized := GetAuthService().Authorize("change_password", token, id, p.userCollectionName); !authorized {
		logrus.Errorln("required opertion is not allowed!")
		return false, COMMON_ERROR_UNAUTHORIZED, errors.New("Required opertion is not authorized!")
	}

	user, err := p.GetUserByUserId(id)
	if err != nil {
		logrus.Errorln("user does exist %v", err)
		return false, COMMON_ERROR_INTERNAL, errors.New("User does not exist!")
	}

	pwdEncry := HashString(password)
	if !strings.EqualFold(pwdEncry, user.Password) {
		logrus.Errorln("incorrect password!")
		return false, USER_ERROR_WRONGPW, errors.New("Incorrect password!")
	}

	if !strings.EqualFold(newpassword, confirm_newpassword) {
		logrus.Errorln("inconsistence new password!")
		return false, USER_ERROR_WRONGPW, errors.New("Inconsistent new password!")
	}

	newpasswordEncry := HashString(newpassword)
	user.Password = newpasswordEncry

	user.TimeUpdate = dao.GetCurrentTime()

	// userDoc := ConvertToBson(*user)
	selector := bson.M{}
	selector["_id"] = bson.ObjectIdHex(id)

	queryStruct := dao.QueryStruct{
		CollectionName: p.userCollectionName,
		Selector:       selector,
		Skip:           0,
		Limit:          0,
		Sort:           ""}

	created, err = dao.HandleUpdateOne(user, queryStruct)
	if err != nil {
		logrus.Error("update user password error! %v", err)
		return created, USER_ERROR_UPDATE, err
	}

	return created, "", nil
}
func (p *UserService) UserUpdate(token string, newuser entity.User, userId string) (created bool, id string, errorCode string, err error) {
	code, err := GetTokenService().TokenValidate(token)
	if err != nil {
		return false, userId, code, err
	}

	if authorized := GetAuthService().Authorize("update_user", token, userId, p.userCollectionName); !authorized {
		logrus.Errorln("required opertion is not allowed!")
		return false, userId, COMMON_ERROR_UNAUTHORIZED, errors.New("Required opertion is not authorized!")
	}

	if !bson.IsObjectIdHex(userId) {
		logrus.Errorf("invalid user id format for user update %v", userId)
		return false, "", COMMON_ERROR_INVALIDATE, errors.New("Invalid object Id for user update")
	}

	selector := bson.M{}
	selector["_id"] = bson.ObjectIdHex(userId)

	queryStruct := dao.QueryStruct{
		CollectionName: p.userCollectionName,
		Selector:       selector,
		Skip:           0,
		Limit:          0,
		Sort:           ""}

	user := new(entity.User)
	err = dao.HandleQueryOne(user, queryStruct)
	if err != nil {
		logrus.Errorf("get user by id error %v", err)
		return false, "", USER_ERROR_UPDATE, err
	}

	if len(newuser.Company) > 0 {
		user.Company = newuser.Company
	}
	if len(newuser.Email) > 0 {
		user.Email = newuser.Email
	}

	user.TimeUpdate = dao.GetCurrentTime()

	created, err = dao.HandleUpdateOne(user, queryStruct)
	return created, userId, "", nil
}