Adding a new validation rule for a Web activity on the OpenLaszlo client side

In this step, you will add a new validation rule for a Web activity in the Management Center. The validation rule will display an error message if the web activity is not set to be active for 10 days, by comparing the start and end dates.

About this task

In the Management Center, validators are implemented as OpenLaszlo classes. All validator classes must extend the wcfValidator class. In this step, you will create a new validation class that compares the start date and the end date. If the time period between the two dates is less than 10 days, the validator will set an error message on the activity. The error message must be translated and should be stored in a properties file. You will also create a properties file to store the message and an OpenLaszlo ResourceBundle class to retrieve the translated message. After creating these classes, you will include them in the extensions library file so that the OpenLaszlo compiler can find them.

Procedure

  1. Create a new validator class
    1. Open WebSphere Commerce Developer and switch to the Enterprise Explorer view.
    2. In the Enterprise Explorer view, navigate to LOBTools > WebContent > WEB-INF > src > lzx.
    3. Right-click lzx and select New > Folder. In the Folder name field, type the name of your company. This will distinguish your files from the files provided by IBM. For example, type mycompany and click Finish.
    4. Right-click mycompany and select New > Folder. In the Folder name field, type marketing and click Finish.
    5. To create a new validation rule source file under the directory marketing, right-click marketing and select New > File. In the File name field, type mycompanyValidator.lzx and click Finish.
    6. Define your validator by entering the following code in the newly created mycompanyValidator.lzx:
      Note: You may replace MyCompany or mycompany with your actual company name that you chose while creating the folder. Note the case sensitivity when making changes, in order to be consistent with the naming conventions.
      WebSphere Commerce Version 7.0.0.0Feature Pack 1
      
      <library>
      	<!--- 
      		This validator is validator of object level. It ensures that the end date must be at least 10 days after the start date if both have values.  The start date
      		and end date are determined by the propertyname startdate and enddate . If a validation error is found, an error message with key being endDateMustAfterStartDateOver10days will be added to 
      		the object. 
      	-->
      	<class name="extMycompanyStartEndDateValidator" extends="wcfValidator">
      		<!---
      			Performs validation on startdate and enddate. Call addValidationError and clearValidationError methods to handle
      			the error info.
      		-->
      		<method name="validate" args="o, changedProperty">
      			<![CDATA[
      			if(o.getProperty("enddate").value && o.getProperty("startdate").value){
      				var startDate = this.parseDate(o.getProperty("startdate").value);
      				var endDate = this.parseDate(o.getProperty("enddate").value);
      				var days = Math.floor(endDate.getTime()/(1000*60*60*24)) - Math.floor(startDate.getTime()/(1000*60*60*24));
      				if (days < 10) {
      					o.getProperty("enddate").addValidationError(this, mycompanyResources.endDateMustAfterStartDateOver10days.string);
      					return false;
      				}
      			}
      			o.getProperty("enddate").clearValidationError(this);
      			]]>
      		</method>
      			
      		<!---
      			Parse the specified timestamp and return a Date object. The timestamp format is yyyy-MM-ddTHH:mm:00.001Z.
      			@param string timestamp: the timestamp string
      			@return Date the date object
      		-->
      		<method name="parseDate" args="timestamp">
      			<![CDATA[
      			var year = Number(timestamp.substr(0,4));
      			var month = Number(timestamp.substr(5,2));		
      			var day = Number(timestamp.substr(8,2));		
      			var hour = Number(timestamp.substr(11,2));		
      			var minutes = Number(timestamp.substr(14,2));		
      			var seconds= Number(timestamp.substr(17,2));	
      			var milliseconds = Number(timestamp.substr(20,3));	
      			return new Date(year, month, day, hour, minutes, seconds, milliseconds);
      			]]>
      		</method>
      		
      	</class>
      </library>
      
      Feature Pack 2Feature Pack 3
      
      <library>
      	<!--- 
      		This validator is validator of object level. It ensures that the end date must be at least 10 days after the start date if both have values.  The start date
      		and end date are determined by the propertyname startdate and enddate . If a validation error is found, an error message with key being endDateMustAfterStartDateOver10days will be added to 
      		the object. 
      	-->
      	<class name="extMycompanyStartEndDateValidator" extends="wcfValidator">
      		<!---
      			Performs validation on startdate and enddate. Call addValidationError and clearValidationError methods to handle
      			the error info.
      		-->
      		<method name="validate" args="o, changedProperty=null">
      			<![CDATA[
      			if(o.getProperty("enddate").value && o.getProperty("startdate").value){
      				var startDate = this.parseDate(o.getProperty("startdate").value);
      				var endDate = this.parseDate(o.getProperty("enddate").value);
      				var days = Math.floor(endDate.getTime()/(1000*60*60*24)) - Math.floor(startDate.getTime()/(1000*60*60*24));
      				if (days < 10) {
      					o.getProperty("enddate").addValidationError(this, mycompanyResources.endDateMustAfterStartDateOver10days.string);
      					return false;
      				}
      			}
      			o.getProperty("enddate").clearValidationError(this);
      			]]>
      		</method>
      			
      		<!---
      			Parse the specified timestamp and return a Date object. The timestamp format is yyyy-MM-ddTHH:mm:00.001Z.
      			@param string timestamp: the timestamp string
      			@return Date the date object
      		-->
      		<method name="parseDate" args="timestamp">
      			<![CDATA[
      			var year = Number(timestamp.substr(0,4));
      			var month = Number(timestamp.substr(5,2));		
      			var day = Number(timestamp.substr(8,2));		
      			var hour = Number(timestamp.substr(11,2));		
      			var minutes = Number(timestamp.substr(14,2));		
      			var seconds= Number(timestamp.substr(17,2));	
      			var milliseconds = Number(timestamp.substr(20,3));	
      			return new Date(year, month, day, hour, minutes, seconds, milliseconds);
      			]]>
      		</method>
      		
      	</class>
      </library>
      
      Note: The validation occurs at the object level. It ensures the end date is at least 10 days after the start date, when both have the same values. The start date and the end date are determined by the propertyname, startdate and enddate. If a validation error is found, an error message with endDateMustAfterStartDateOver10days key will be added to the object.
    7. Click Save to save the changes.
  2. Create properties files
    1. In the Enterprise Explorer view, navigate to LOBTools > Java Resources: scr. Right-click src and select New > Package. In the Name field, type com.mycompany.marketing.client.lobtools.properties and click Finish.
    2. Right-click com.mycompany.marketing.client.lobtools.properties and select New > Other > General > File. In the Name field, type MyMarketingLOB.properties. In the same way, create the MyMarketingLOB_en_US.properties file.
    3. Translate the message to the other locales that you want to support and place them in the .properties file for the specific locale that you created in step 2.
    4. Add the following information to the MyMarketingLOB.properties and MyMarketingLOB_en_US.properties files:
      endDateMustAfterStartDateOver10days=Client Warning: The end date must be at least 10 days after the start date.
    5. Click Save to save the changes.
  3. Create the ResourceBundle class
    1. Right-click LOBTools > WebContent > WEB-INF > src > lzx > mycompany > marketing and select New > File. In the File name field, type mycompanyResourceBundle.lzx.
    2. Add the following code for this ResourceBundle class:
      <library>
          <class name="extMycompanyResourceBundle" extends="wcfResourceBundle" baseName="com.mycompany.marketing.client.lobtools.properties.MyMarketingLOB">
              <wcfResourceBundleKey name="endDateMustAfterStartDateOver10days" />
          </class>
          <extMycompanyResourceBundle id="mycompanyResources"/>
      </library>
      
    3. Click Save to save the changes.
  4. Include the classes in the extension library:
    1. Navigate to LOBTools > WebContent > WEB-INF > src > lzx > commerce > marketing and open MarketingExtensionsLibrary.lzx. Insert the following code between the open and close library tags within the file and click Save.
      Note: You must include your newly created OpenLaszlo files in the application so that the OpenLaszlo compiler knows about them. Each tool within the Management Center provides an ExtensionsLibrary.lzx for this purpose.
      <include href="../../mycompany/marketing/mycompanyResourceBundle.lzx"/>
      <include href="../../mycompany/marketing/mycompanyValidator.lzx"/>
      

  5. Use the new validator WebSphere Commerce Version 7.0.0.0Feature Pack 1
    1. Navigate to LOBTools > WebContent > WEB-INF > src > lzx > commerce > marketing > objectDefinitions.
    2. Open the WebActivityPrimaryObjectDefinition.lzx file.
    3. Find the mktWebActivityPrimaryObjectDefinition class and insert <extMycompanyStartEndDateValidator/> before the </class> tag.
    4. Click Save to save the changes.
    Feature Pack 2Feature Pack 3
    1. Navigate to LOBTools > WebContent > config > commerce > marketing > objectDefinitions.
    2. Open the WebActivityPrimaryObjectDefinition.def file.
    3. Find the mktBaseWebActivityPrimaryObjectDefinition definition and insert <MycompanyStartEndDateValidator package="ext"/> before the </PrimaryObjectDefinition> tag.
    4. Click Save to save the changes.
  6. Select the LOBTools project; then click Build OpenLaszlo Project.