#!/usr/bin/env python

import cherrypy
from dm.common.service.dmSessionController import DmSessionController
from dm.ds_web_service.service.impl.userInfoSessionControllerImpl import UserInfoSessionControllerImpl

class UserInfoSessionController(DmSessionController):

    def __init__(self):
        DmSessionController.__init__(self)
        self.userInfoSessionControllerImpl = UserInfoSessionControllerImpl()

    @cherrypy.expose
    @DmSessionController.require(DmSessionController.isAdministrator())
    @DmSessionController.execute
    def getUsers(self, **kwargs):
        return self.listToJson(self.userInfoSessionControllerImpl.getUsers())

    @cherrypy.expose
    @DmSessionController.require(DmSessionController.isAdministrator())
    @DmSessionController.execute
    def getUserById(self, id, **kwargs):
        if not id:
            raise InvalidRequest('Invalid id provided.')
        response = self.userInfoSessionControllerImpl.getUserById(id).getFullJsonRep()
        self.logger.debug('Returning user info for %s: %s' % (id,response))
        return response

    @cherrypy.expose
    @DmSessionController.require(DmSessionController.isAdministrator())
    @DmSessionController.execute
    def getUserByUsername(self, username, **kwargs):
        if not len(username):
            raise InvalidRequest('Invalid username provided.')
        response = self.userInfoSessionControllerImpl.getUserByUsername(username).getFullJsonRep()
        self.logger.debug('Returning user info for %s: %s' % (username,response))
        return response

    @cherrypy.expose
    @DmSessionController.require(DmSessionController.isAdministrator())
    @DmSessionController.execute
    def addUserExperimentRole(self, username, experimentName, roleName, **kwargs):
        if not username:
            raise InvalidRequest('Invalid username provided.')
        if not experimentName:
            raise InvalidRequest('Invalid experiment name provided.')
        if not roleName:
            raise InvalidRequest('Invalid role name provided.')
        response = self.userInfoSessionControllerImpl.addUserExperimentRole(username, experimentName, roleName).getFullJsonRep()
        return response

    @cherrypy.expose
    @DmSessionController.require(DmSessionController.isAdministrator())
    @DmSessionController.execute
    def deleteUserExperimentRole(self, username, experimentName, roleName, **kwargs):
        if not username:
            raise InvalidRequest('Invalid username provided.')
        if not experimentName:
            raise InvalidRequest('Invalid experiment name provided.')
        if not roleName:
            raise InvalidRequest('Invalid role name provided.')
        response = self.userInfoSessionControllerImpl.deleteUserExperimentRole(username, experimentName, roleName).getFullJsonRep()
        return response