#1 2014-08-25 22:36:31

marcosfilho
Member
Registered: 2014-08-04

Allocating user variables by script

Hello guys,

I would like to automatically allocate user variables consuming data from my other system. Is there anyway I can do it by using reportServer API/groovy script? Do you guys have any example of this?

Thanks.

Offline

#2 2014-08-26 09:44:17

Dennie
Member
Registered: 2014-08-22

Re: Allocating user variables by script

I haven't tried it but I think this could work: add a script in onlogin.d with a uservariablehook.

GLOBALS.services.callbackRegistry.attachHook("My_Variables", UserVariableProviderHook.class, new UserVariableProviderHook(){
	
	public Collection<? extends UserVariableDefinition> getVariables(){
              // Read it from external file and return the collection 
        }
	
});

Offline

#3 2014-08-26 10:29:32

Arno Mittelbach
datenwerke
Registered: 2012-02-14

Re: Allocating user variables by script

Hi,

good guess Dennie, but the above regrettably does not work. The UserVariableProviderHook would be used to create new types of user variables (that is, a new type besides the string and list types supported by ReportServer). Let me note that adding new entity types (i.e., types that are database backed) does usually not work via scripts.

Good news is .. later this day we should have the script guide available and I'll also patch up a small script to demonstrate the automatic generation of user variables.

Cheers
Arno

Offline

#4 2014-08-26 11:00:11

Arno Mittelbach
datenwerke
Registered: 2012-02-14

Re: Allocating user variables by script

Hi Marcos,

naturally, this is possible big_smile. So, here we go. First step, you add the user variable definitions. Technically, we could also do this by script, but here I assume we do this step by hand once. For this we go to the user manager root object and to the user var management and create a single user variable called "myUserVar" of type String.

The user variable that we just created is of type StringUserVariableDefinition. If we open the terminal and type

desc StringUserVariableDefinition "hql:from StringUserVariableDefinition"

we should see that we have a single object of that type. (If you want to use List variables the object is called ListUserVariableDefinition). Next step is to create the script for the automatic generation of user variables.

The actual user variables are called UserVariableInstance and the instance corresponding to the above StringUserVariableDefinition is called StringUserVariableInstance which is located in package

net.datenwerke.rs.uservariables.service.variabletypes.string.StringUserVariableInstance

To create a new instance and assign it to a folk (i.e., a user or organizational units) basically all we need to do is to create a new object instance of type StringUserVariableInstance and assign the corresponding definition, the corresponding folk and the value the user variable should take:

def instance = new StringUserVariableInstance()
instance.setDefinition(theDefinition)
instance.setFolk(theFolk)
instance.setValue(theValue)

All that would be left to do is to persist the new object using the UserVariableService located in

net.datenwerke.rs.uservariables.service.uservariables.UserVariableService

Thus, the basic outline of our script could be the following (here we assume that the variable definition has id 1):

import net.datenwerke.rs.uservariables.service.uservariables.UserVariableService
import net.datenwerke.rs.uservariables.service.variabletypes.string.StringUserVariableInstance
import net.datenwerke.rs.uservariables.service.variabletypes.string.StringUserVariableDefinition

import net.datenwerke.security.service.usermanager.entities.AbstractUserManagerNode

// load uservariable service
def userVarService = GLOBALS.getRsService(UserVariableService.class)

// load user variable definition
// note the l to make the id a long
def varDefinition = GLOBALS.findEntity(StringUserVariableDefinition.class, 1l)

// map folk id -> variable value
// that would be the place to load your mapping
// note the l to make the ids longs
def map = [ 5l : 'value for user with id 5', 53l : 'value for OU with id 53']

// loop over map
map.each {
	def id = it.key
	def value = it.value
  
  	// load folk
  	def folk = GLOBALS.findEntity(AbstractUserManagerNode.class, id)
	
    // remove any user variables for this folk
    userVarService.getDefinedInstancesFor(folk).each {
      userVarService.remove(it)
    }
  
    // create instance
	def instance = new StringUserVariableInstance()
	instance.setDefinition(varDefinition)
    instance.setFolk(folk)
	instance.setValue(value)
  
    // persist value
    userVarService.persist(instance)
}

Note that I've added an extra command to remove all user variables of the current object. For this the UserVariableService offers the method getDefinedInstancesFor. Also note that, since you are changing data with your script you need to run it in commit mode, that is run it as

exec -c SCRIPT

I hope this helps.

Cheers
Arno

Offline

#5 2014-08-26 22:55:48

marcosfilho
Member
Registered: 2014-08-04

Re: Allocating user variables by script

Arno,

thanks a lot. Im going to work on this and let you know.

Marcos

Offline

#6 2014-08-26 22:57:28

marcosfilho
Member
Registered: 2014-08-04

Re: Allocating user variables by script

Is there any way i can trigger this script when the user log in?

so the user logs in, i run this script that will consume a external webservice and will update the user variable?

Marcos

Offline

#7 2014-08-27 00:53:35

marcosfilho
Member
Registered: 2014-08-04

Re: Allocating user variables by script

Hi,

Dont worry about my last question.. I figured out that if I place a script into fileserver/bin/onlogin.d it will be ran when the user logs in. It should be very helpful for me. I'll test it and let you know.

Thanks,
Marcos

Offline

Board footer

Powered by FluxBB