wrusman Absent Member.
Absent Member.
145 views

read csv file with ecma script

Hello,
I'm trying to read and parse a csv file for pseudonimising users in a dedicated IDV, and found the forum post where an ecma script was posted that should work for reading files.
https://forums.novell.com/showthread.php/485563-read-text-file-for-information-and-use-it-in-a-driver

It is however unclear to me where to put the csv, or maybe include it in the driver as a "text/+text" Resource ?
can anyone elaborate on this ?

My ecma scripting knowlegde until this morning was zero, so please forgive me if my question may seem somewhat stupid 😉
Labels (1)
0 Likes
6 Replies
Knowledge Partner
Knowledge Partner

Re: read csv file with ecma script

wrusman wrote:

> It is however unclear to me where to put the csv, or maybe include it in
> the driver as a "text/+text" Resource ?


If it's a static file why don't you load it into a mapping table and work with
token-map? Or read the mapping table into a nodeset var and xpath your way
around it?

--
http://www.is4it.de/en/solution/identity-access-management/

(If you find this post helpful, please click on the star below.)
0 Likes
wrusman Absent Member.
Absent Member.

Re: read csv file with ecma script

I'm not sure if that will work, because the mapping should not be static. I would like to take a random first and last name from csv's and combine them to a new pseudonym name for that user.
0 Likes
Knowledge Partner
Knowledge Partner

Re: read csv file with ecma script

wrusman wrote:

>
> I'm not sure if that will work, because the mapping should not be
> static. I would like to take a random first and last name from csv's and
> combine them to a new pseudonym name for that user.


Add an index column to the table, numbering the rows starting at 0. Say you
have 188 rows, generate a random number between 0 and 187. Then map the index
column to the first name column via the random number. Repeat for last name.

function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}

--
http://www.is4it.de/en/solution/identity-access-management/

(If you find this post helpful, please click on the star below.)
0 Likes
wrusman Absent Member.
Absent Member.

Re: read csv file with ecma script

ah, that should work indeed.
Thank you for the input !
0 Likes
wrusman Absent Member.
Absent Member.

Re: read csv file with ecma script

Since this is my first attempt in usinbg ecma script and mapping tables, i'm still a bit fresh on this subject.
I created an ecmascript item with the random code you posted above, thanks for that!
Is this code right for mapping a random number to a mapping table index and returning the value assigned to that index number ?

Thanks in advance


<do-set-src-attr-value name="Given Name">
<arg-value type="string">
<token-map dest="name" src="index" table="\[root]\drv\ds\asds-pseudo\firstNames">
<token-op-attr name="es:getRandomInt(4226)"/>
</token-map>
</arg-value>
</do-set-src-attr-value>


The Given Name attribute is filtered out back to the source IDV so that shouldn't be updated
0 Likes
wrusman Absent Member.
Absent Member.

Re: read csv file with ecma script

for future reference and other people trying to achieve the same, here is what i found out so far:

* Use designer 4.6.1 or higher, otherwise ecmascript in the simulation will NOT work and you will stare yourself blind on the error.
* In your driver (or store) create a ecmascript with the randomInt function from lhaeger :
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
*

In your driver (or store) create mapping tables with first names and surnames like so :

<?xml version="1.0" encoding="UTF-8"?>
<mapping-table>
<col-def name="index" type="numeric"/>
<row>
<col>1</col>
<col>Aachal</col>
</row>
<row>
<col>2</col>
<col>Aad</col>
</row>
...................
<row>
<col>4226</col>
<col>Żywisław</col>
</row>
<col-def name="name" type="nocase"/>
</mapping-table>


In your driver (or library) create a new DirXML script and in this create a function like this :
			
<actions>
<do-reformat-op-attr name="Given Name">
<arg-value type="string">
<token-map dest="name" src="index" table="\[root]\drv\ds\asds-pseudo\firstNames">
<token-xpath expression="es:getRandomInt(4226)"/>
</token-map>
</arg-value>
</do-reformat-op-attr>
</actions>


This will yield this result in the Simulation:

Anon-Driver :Applying policy: %+C%14Cpub-cp-transform-userdata%-C.
Anon-Driver : Applying to add #1.
Anon-Driver : Evaluating selection criteria for rule 'Random-firstname-lastname'.
Anon-Driver : (if-class-name equal "User") = TRUE.
Anon-Driver : Rule selected.
Anon-Driver : Applying rule 'Random-firstname-lastname'.
Anon-Driver : Action: do-reformat-op-attr("Given Name",token-map("\[root]\drv\ds\Anon-Driver\firstNames","index","name",token-xpath("es:getRandomInt(4226)"))).
Anon-Driver : arg-string(token-map("\[root]\drv\ds\Anon-Driver\firstNames","index","name",token-xpath("es:getRandomInt(4226)")))
Anon-Driver : token-map("\[root]\drv\ds\Anon-Driver\firstNames","index","name",token-xpath("es:getRandomInt(4226)"))
Anon-Driver : token-map("\[root]\drv\ds\Anon-Driver\firstNames","index","name",token-xpath("es:getRandomInt(4226)"))
Anon-Driver : token-xpath("es:getRandomInt(4226)")
Anon-Driver : Token Value: "1956".
Anon-Driver : Arg Value: "1956".
Anon-Driver : Token Value: "Jeremy".
Anon-Driver : Arg Value: "Jeremy".
Anon-Driver : Action: do-reformat-op-attr("Surname",token-map("\[root]\drv\ds\Anon-Driver\lastNames","index","name",token-xpath("es:getRandomInt(3630)"))).
Anon-Driver : arg-string(token-map("\[root]\drv\ds\Anon-Driver\lastNames","index","name",token-xpath("es:getRandomInt(3630)")))
Anon-Driver : token-map("\[root]\drv\ds\Anon-Driver\lastNames","index","name",token-xpath("es:getRandomInt(3630)"))
Anon-Driver : token-map("\[root]\drv\ds\Anon-Driver\lastNames","index","name",token-xpath("es:getRandomInt(3630)"))
Anon-Driver : token-xpath("es:getRandomInt(3630)")
Anon-Driver : Token Value: "3370".
Anon-Driver : Arg Value: "3370".
Anon-Driver : Token Value: "Urs".
Anon-Driver : Arg Value: "Urs".
Anon-Driver :Policy returned:
Anon-Driver :
<nds dtdversion="4.0" ndsversion="8.x">
<source>
<product version="4.6.1.0">DirXML</product>
<contact>NetIQ Corporation</contact>
</source>
<input>
<add class-name="User" src-dn="cn=ID2435,ou=users,o=id">
<add-attr attr-name="Given Name">
<value type="string">Jeremy</value>
</add-attr>
<add-attr attr-name="Surname">
<value type="string">Urs</value>
</add-attr>
</add>
</input>
</nds>
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.