Chapter 4 Managing Profiles


Understanding event handlers

You can associate an event handler with a metaclass or a stereotype; criteria do not support event handlers.

Each event handler has predefined semantic and parameters. When you associate an event handler with a stereotype or a metaclass, the event handler script is initialized with the function to implement. For example, the CanCreate event handler takes as parameter the parent in which the object will be created:

Function %CanCreate%(parent)
   ' Implement your creation validation rule on <parent> here
   ' and return True in case of success

   %CanCreate% = True
End Function

You use the Event Handler Script tab to define the event handler function and adapt it to your model and needs. The Global Script tab is used for sharing library functions and static attributes in the resource file.

You can also use the Global Script tab to declare global variables. But in this case, you must be aware that global variables are not reinitialized each time the event handler is called. They keep their value until you modify the resource file, or until the end of the PowerDesigner session. This may cause errors especially when variables reference objects that can be modified or even deleted. Make sure you reinitialize the global variable at the beginning of an event handler if you do not want to keep the value from a previous run.

For more information on defining a script and using the Global Script tab, see the "Defining the script of a custom check" and "Using the global script" sections.

WARNING! 

Caution
Do not use statements like msgbox, or input box to open a dialog box in the event handler function

Several event handlers are defined in PowerDesigner.

CanCreate

The CanCreate event handler is used to implement a creation validation rule. Some objects cannot be created anywhere in the model, the CanCreate event handler makes sure that this requirement is met. For example, in a BPM for ebXML, a process with Business Transactions stereotype must be created under a process with Binary Collaboration stereotype. The script of the CanCreate event handler associated with the Business Transaction process stereotype is the following:

Function %CanCreate%(parent)

   if parent is Nothing or parent.IsKindOf(PdBpm.Cls_Process) then
      %CanCreate% = False
   else
      %CanCreate% = True
   end if
   
End Function

If the CanCreate event handler is set on a stereotype and the function return value is True, then you can use the custom tool to create the stereotyped object. Otherwise the custom tool is not available, and the stereotype list does not display the corresponding stereotype.

If the CanCreate event handler is set on a metaclass and the function return value is True, then you can create the object either from the palette or from the Browser or in a list.

Note   Imported and reverse engineered models
When you import or reverse engineer a model, the CanCreate functions are ignored since they could modify the model and make it diverge from the origin.

Initialize

The Initialize event handler is used to instantiate objects with predefined templates. For example, in a BPM, a Business Transaction must be a composite process with a predefined sub-graph. The script of the Initialize event handler associated with the Business Transaction process stereotype contains all the functions needed to create the sub-graph. The following piece of script is a subset of the Initialize event handler for a Business Transaction.

...
' Search for an existing requesting activity symbol
   Dim ReqSym  
   Set ReqSym = Nothing  
   If Not ReqBizAct is Nothing Then
      If ReqBizAct.Symbols.Count > 0 Then
         Set ReqSym = ReqBizAct.Symbols.Item(0)
      End If
   End If
   
   ' Create a requesting activity if not found
   If ReqBizAct is Nothing Then
      Set ReqBizAct = BizTrans.Processes.CreateNew 
      ReqBizAct.Stereotype = "RequestingBusinessActivity"
      ReqBizAct.Name = "Request"
   End If
...

If the Initialize event handler is set on a stereotype and the function return value is True, the initialization script will be launched whenever the stereotype is assigned, either with a custom tool in the palette, or from the object property sheet.

If the Initialize event handler is set on a metaclass and the function return value is True, the initialization script will be launched when you create a new object from the tool palette, from the Browser, in a list or using the New tool in a property sheet.

If the Initialize event handler is set on the model metaclass and the function return value is True, the initialization script is launched when you assign a target (DBMS, object language, process language, schema language) to the model at creation time, when you change the target of the model or when you assign an extended model definition to the model.

Validate

The Validate event handler is used to validate changes performed on object properties and to implement cascade updates. It is triggered when the user:

The return value lets the user disallow changes.

You can define an error message that will appear when the condition is not satisfied. To do so, fill the message variable and set the %Validate% variable to False.

In the following example, the validate event handler verifies that a comment is added to the definition of an object when the user validates the property sheet. A message is displayed to explain the problem.

Function %Validate%(obj, ByRef message)
   if obj.comment = "" then 
      %Validate% = False
      message = "Comment cannot be empty"
   else
      %Validate% = True
   end if
End Function

CanLinkKind

The CanLinkKind event handler is used to restrict the kind and stereotype of the objects you want to link together. It is triggered when the user:

The CanLinkKind event handler has two input parameters: the source and destination extremity of the link. You can also use the sourceStereotype and destinationStereotype parameters. These are optional and used to perform additional checks on stereotypes.

In the following example, the source of the extended link must be a start object:

Function %CanLinkKind%(sourceKind, sourceStereotype, destinationKind, destinationStereotype)
   if sourceKind = cls_Start Then
   %CanLinkKind% = True
   end if
End Function

 


Copyright (C) 2005. Sybase Inc. All rights reserved.