'***Patrick Rutledge, Rose Support, 8/98 'This script replaces an old class name with the new class name where 'it is used as a return type, parameter type, or attribute type. A dialog 'gets the old and new names from the user, and progress is reported in log window. '***Bill Taylor, 2/2000 'Script updated to handle attributes and to perform the substitution where the 'old class name is part of the "type", eg "in out OldClassName" Dim changedCount As Integer Begin Dialog TypeNamesDialog 81,64,150,90,"Enter Old and New Names" OKButton 4,65,40,14 CancelButton 60,65,40,14 Text 4,8, 80, 12, "Enter Old Class Name" TextBox 4,20,140,12,.OldNameText, 0 Text 4,35, 80, 12, "Enter New Class Name" TextBox 4,47,140,12,.NewNameText,0 End Dialog Function PluralisedString (theCount As Integer, theString As String) As String 'This function chooses between singular and plural spellings according to 'the value of theCount. 'The syntax of theString allows [pstring|sstring] where |sstring is optional For i = 1 To Len(theString)-2 If mid$(theString,i,1) = "[" Then theMultiStart = i+1 theMultiEnd = Len(theString) theSingleStart = theMultiEnd+1 theSingleEnd = theSingleStart-1 For j = theMultiStart To theMultiEnd If mid$(theString,j,1) = "]" Then theMultiEnd = j-1 theSingleStart = theMultiEnd+1 theSingleEnd = theSingleStart-1 Exit For ElseIf mid$(theString,j,1) = "|" Then theMultiEnd = j-1 theSingleStart = j+1 For k = theSingleStart To theSingleEnd If mid$(theString,k,1) = "]" Then theSingleEnd = k-1 Exit For End If Next k Exit For End If Next j If theCount = 1 Then PluralisedString = left$(theString,i-1) &_ mid$(theString,theSingleStart,theSingleEnd-theSingleStart+1) &_ PluralisedString(theCount,right$(theString,Len(theString)-theSingleEnd-1)) Exit Function Else PluralisedString = left$(theString,i-1) &_ mid$(theString,theMultiStart,theMultiEnd-theMultiStart+1) &_ PluralisedString(theCount,right$(theString,Len(theString)-theSingleEnd-1)) Exit Function End If End If Next i PluralisedString = theString End Function Function IsLeading (theString As String, theIndex As Integer) As Boolean If theIndex = 1 Then IsLeading = True ElseIf mid$(theString,theIndex-1,1) = " " Then IsLeading = True Else IsLeading = False End If End Function Function IsTrailing (theString As String, theLastIndex As Integer) As Boolean If theLastIndex = Len(theString) Then IsTrailing = True ElseIf mid$(theString,theLastIndex+1,1) = " " Then IsTrailing = True Else IsTrailing = False End If End Function Sub ReplaceName (theString As String, inOldName As String, inNewName As String, outChangeMade As Boolean) For i = 1 To Len(theString)-Len(inOldName)+1 If mid$(theString,i,Len(inOldName)) = inOldName Then If IsLeading(theString,i+0) And IsTrailing(theString,i+Len(inOldName)-1) Then theString = left$(theString,i-1) & inNewName & right$(theString,Len(theString)-i-Len(inOldName)+1) outChangeMade = True Exit Sub End If End If Next i outChangeMade = False End Sub Sub UpdateClassName (inOldName As String, inNewName As String) Dim theClasses as ClassCollection Dim theAtts As AttributeCollection Dim theAtt As Attribute Dim theOps as OperationCollection Dim theOp as Operation Dim testParam As Parameter Dim theClass As Class Dim changed As Boolean readOnlyClasses% = 0 changedCount% = 0 RoseApp.WriteErrorLog "" RoseApp.WriteErrorLog "Updating attribute, parameter and return types with new class name" RoseApp.WriteErrorLog "Start: Replace " & inOldName$ & " with " & inNewName$ Set theClasses = RoseApp.CurrentModel.GetAllClasses() 'Loop through classes For classIndex = 1 To theClasses.Count Set theClass = theClasses.GetAt(classIndex) If Not theClass.ParentCategory.IsModifiable() Then 'can't update readOnlyClasses = readOnlyClasses + 1 GoTo SkipClass End If Set theAtts = theClass.Attributes 'Loop through attributes For attIndex = 1 To theAtts.Count Set theAtt = theAtts.GetAt(attIndex) theAttType$ = theAtt.Type ReplaceName theAttType$, inOldName$, inNewName$, changed If changed Then theAtt.Type = theAttType$ changedCount = changedCount + 1 RoseApp.WriteErrorLog "Updated " & theClass.name & "::" & theAtt.name &_ " attribute type to """ & theAtt.Type & """" End If Next attIndex Set theOps = theClass.Operations 'Loop through operations For opIndex = 1 To theOps.Count Set theOp = theOps.GetAt(opIndex) 'Loop through parameters For paramIndex = 1 To theOp.Parameters.Count Set testParam = theOp.Parameters.GetAt(paramIndex) testParamType$ = testParam.Type ReplaceName testParamType, inOldName$, inNewName$, changed If changed Then testParam.Type = testParamType$ changedCount = changedCount + 1 RoseApp.WriteErrorLog "Updated " & theClass.name & "::" & theOp.name _ & "(), parameter type" & paramIndex & " to """ & testParam.Type & """" End If Next paramIndex 'Check return type theReturnType$ = theOp.ReturnType ReplaceName theReturnType$, inOldName$, inNewName$, changed If changed Then theOp.ReturnType = theReturnType$ changedCount = changedCount + 1 RoseApp.WriteErrorLog "Updated " & theClass.name & "::" & theOp.name _ & "() return type" & " to """ & theOp.ReturnType & """" End If Next opIndex SkipClass: Next classIndex RoseApp.WriteErrorLog "Done: Changed " &_ PluralisedString(changedCount,_ changedCount & " occurrence[s] of " & inOldName$) If readOnlyClasses > 0 Then RoseApp.WriteErrorLog PluralisedString (readOnlyClasses,_ readOnlyClasses & " class[es] in [|a ]read-only unit[s].") End If MsgBox PluralisedString(changedCount,_ changedCount & " occurrence[s] changed: see log for details") End Sub Sub Main Dim nameDlog As TypeNamesDialog Dim bLegalReply As Boolean Dim bCancelled As Boolean bCancelled = False While Not bCancelled bLegalReply = False While Not bLegalReply r% = Dialog(nameDlog) If r% = -1 Then If nameDlog.NewNameText$ = "" Or nameDlog.OldNameText$ = "" Then MsgBox "You must supply an old name and a new name" ElseIf nameDlog.NewNameText$ = nameDlog.OldNameText$ Then MsgBox "You must supply two different names" Else bLegalReply = True UpdateClassName nameDlog.OldNameText$, nameDlog.NewNameText$ End If Else bLegalReply = True bCancelled = True 'cancel button hit End If Wend Wend End Sub