Article

tschloesser's picture
article
Reads:

4721

Score:
3
3
1
 
Comments:

2

Ensuring String Length in Policy Builder, using ECMAScript

Author Info

16 January 2008 - 9:05am
Submitted by: tschloesser

(View Disclaimer)

Problem

During one of my last trainings, a student asked me whether it is possible in Policy Builder to ensure that a string has a certain length. Some think at first sight that the substring verb will extend a string to the length specified by the length parameter, which is not the case. Currently there is no verb offering this capability, so I thought about introducing this capability though ECMAScript.

Solution

The following ECMAScript defines a function named extString, which offers three parameters. The first one specifies the value to be extended, the second the length of the resulting string, and the last parameter defines the character to be used for the concatenation.

Please ensure to specify all three parameters, otherwise the result will not be as expected. Consider using a substring() verb in advance to limit the length of the string, since this function only handles the extension to a given length.

The examples below show a simple ECMAScript and a very simple placement policy calling this function.

Example

// Function to extend the length of a string to a specified size
// To use it all three parameters have to be specified.
// value := Value to extend
// size := length of the resulting string
// fill := character to use for the concatenation

function extString(value,size,fill){

var l = value.length;

while (l < size){
value = value + fill;
l= value.length;
}
return value;
} 

Here's a sample policy to demonstrate the use of the script:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE policy PUBLIC "policy-builder-dtd" "/home/tschloesser/designer3/designer/eclipse/plugins/com.novell.designer.idm.policybuilder_2.0.0.200711271409/DTD/dirxmlscript.dtd"><policy xmlns:es="http://www.novell.com/nxsl/ecmascript">
<rule>
<description>CreateUniqueCN of 6 characters</description>
  <conditions>
    <and/>
  </conditions>
  <actions>
    <do-set-local-variable name="temp" scope="policy">
      <arg-string>
        <token-substring length="6">
        <token-op-attr name="Surname"/>
        </token-substring>
      </arg-string>
    </do-set-local-variable>
    <do-set-local-variable name="charCN" scope="policy">
      <arg-string>
        <token-xpath expression='es:extString($temp,5,"0")'/>
      </arg-string>
    </do-set-local-variable>
    <do-set-op-dest-dn>
      <arg-dn>
        <token-text xml:space="preserve">users\active\</token-text>
        <token-local-variable name="temp"/>
      </arg-dn>
    </do-set-op-dest-dn>
  </actions>
</rule>
</policy>


Disclaimer: As with everything else at Cool Solutions, this content is definitely not supported by Novell (so don't even think of calling Support if you try something and it blows up).

It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.




User Comments

bstumpp's picture

More than one way to do the same thing...

Submitted by bstumpp on 16 January 2008 - 2:23pm.

Below are two actions one adds "0" to the end of the surname, and the other adds "0" to be beginning of workforceID. Both pad a variable to a specific length with no scripting needed.


<actions>
<do-reformat-op-attr name="surname">
<arg-value>
<token-substring length="6">
<token-op-attr name="Surname"/>
<token-text xml:space="preserve">000000</token-text>
</token-substring>
</arg-value>
</do-reformat-op-attr>
<do-reformat-op-attr name="workforceID">
<arg-value>
<token-substring start="-9">
<token-text xml:space="preserve">000000000</token-text>
<token-op-attr name="workforceID"/>
</token-substring>
</arg-value>
</do-reformat-op-attr>
</actions>

tschloesser's picture

Really a nice way to do

Submitted by tschloesser on 17 January 2008 - 10:03am.

Really a nice way to do this, thanX!

© 2013 Novell