Using the clone feature
This topic provides an mrScriptBasic example of using the MDM's clone feature.
The example uses the short_drinks.mdd file that comes with the UNICOM Intelligence Developer Documentation Library. If the sample files are not in the default location, you must edit the example before you run it. Note that short_drinks.mdd example file contains two languages, ENU, English (United States), and ESN, Spanish (International Sort).
The example:
▪creates a CloneOptions object (see
ICloneOptions in the MDM Object Model Reference), and sets up a mapping between the ENU language in the source Document and ENG, English (United Kingdom), in the destination Document. It also defines that ESN is to be excluded.
▪creates a copy of the “frequenz” grid in a new Document using the CloneOptions object. This means that texts that are in ENU in the source Document are written as ENG in the destination Document and none of the Spanish texts are copied.
Dim MyDocument, MyNewDocument
Dim MyCloneObject, MyCloneOptions, MyCloneGrid
Set MyDocument = CreateObject("MDM.Document")
MyDocument.Open("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Data\Mdd\short_drinks.mdd", , _
MDMLib.openConstants.oREAD)
' Set the Clone object to the object we want to clone.
' The GetInterface function returns the IMDMClone interface,
' which is an alternative interface for any object
' in the Fields collection.
Set MyCloneObject = MyDocument.Fields["Frequenz"].GetInterface( _
"{F2780F44-AC4C-4afa-A429-30E817C05D37}")
' Create the CloneOptions object
Set MyCloneOptions = MyCloneObject.CreateOptions()
' Map all English (United States) texts to English (United Kingdom)
MyCloneOptions.Languages.Item["ENU"].CloneName = "ENG"
' Exclude Spanish texts
MyCloneOptions.Languages.Item["ESN"].Exclude = True
' Create the destination Document
Set MyNewDocument = CreateObject("MDM.Document")
' Set up languages in the destination Document
MyNewDocument.Languages.Add("ENG")
MyNewDocument.Languages.Add("ESN")
MyNewDocument.Languages.Base = "ENG"
' Set up contexts in the destination Document
MyNewDocument.Contexts.Add("QUESTION")
MyNewDocument.Contexts.Add("ANALYSIS")
MyNewDocument.Contexts.Base = "QUESTION"
' Clone the grid
Set MyCloneGrid = MyCloneObject.CloneEx[MyNewDocument][MyCloneOptions]
' Add the cloned grid to the Fields collection
MyNewDocument.Fields.Add(MyCloneGrid)
MyNewDocument.Save("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\MyClone.mdd")
MyNewDocument.Close()
A similar example in VB.NET is:
Private Sub Clone_A_Grid()
Dim MyDocument As MDMLib.Document
Dim MyNewDocument As New MDMLib.Document
Dim MyCloneObject As MDMLib.IMDMClone
Dim MyCloneOptions As MDMLib.CloneOptions
Dim MyCloneGrid As MDMLib.Grid
MyDocument = New MDMLib.Document
MyDocument.Open(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Data\Mdd\short_drinks.mdd", , _
MDMLib.openConstants.oREAD)
' Set the Clone object to the object we want to clone
MyCloneObject = MyDocument.Fields("Frequenz")
' Create the CloneOptions object
MyCloneOptions = MyCloneObject.CreateOptions
' Map all English (United States) texts to English (United Kingdom)
MyCloneOptions.Languages.Item("ENU").CloneName = "ENG"
' Exclude Spanish texts
MyCloneOptions.Languages.Item("ESN").Exclude = True
' Set up languages in the destination Document
MyNewDocument.Languages.Add("ENG")
MyNewDocument.Languages.Add("ESN")
MyNewDocument.Languages.Base = "ENG"
' Set up contexts in the destination Document
MyNewDocument.Contexts.Add("QUESTION")
MyNewDocument.Contexts.Add("ANALYSIS")
MyNewDocument.Contexts.Base = "QUESTION"
' Clone the grid
MyCloneGrid = MyCloneObject.CloneEx(MyNewDocument, MyCloneOptions)
' Add the cloned grid to the Fields collection
MyNewDocument.Fields.Add(MyCloneGrid)
MyNewDocument.Save(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\MyClone.mdd")
MyNewDocument.Close()
End Sub
The mrScriptBasic example uses the GetInterface function to return the IMDMClone interface of the object in the Fields collection. The IMDMClone interface is an alternative interface for all of the different types of objects that can exist in a Fields collection. The IMDMClone interface includes the CreateOptions method used in the statement:
Set MyCloneOptions = MyCloneObject.CreateOptions()
It is not necessary to specify the interface in the Visual Basic example because MyCloneObject is declared as a variable of type MDMLib.IMDMClone and therefore IMDMClone is already the default interface for MyCloneObject.
After you run the example, open MyClone.mdd in Questionnaire Viewer, and compare it with short_drinks.mdd.
As expected the MyClone.mdd has the languages, EN-GB and ES whereas short_drinks.mdd has the languages EN-US and ES. (Questionnaire Viewer uses longer, more explanatory language codes.) When you select EN-GB as the language for MyClone.mdd, you can see that the texts that are shown are the same as those in EN-US in the short_drinks.mdd. However, when you select Spanish in MyClone.mdd, the frequency grid has no texts. This is because the Exclude property was set for the CloneLanguage object that corresponds to ESN in the source Document.
Cloning shared lists, pages, and blocks
The following example demonstrates how to clone a shared list. The example can be modified to clone pages and blocks.
Dim mdmdoc
set mdmdoc = createobject("mdm.document")
mdmdoc.Script = "!
Metadata(en-us, question, label)
list1 define {a,b,c,d,e};
q1 "q1" categorical {use list1};
end metadata
!"
mdmdoc.Save("temp.mdd")
mdmdoc.Close()
set mdmdoc = createobject("mdm.document")
mdmdoc.Open("temp.mdd")
'create new document to store the new shared list
Dim newDoc
Set newDoc = createobject("mdm.document")
newDoc.Contexts.Base = "Question"
newDoc.Contexts.Current = "Question"
newDoc.IncludeSystemVariables = true
dim list, listClone, newClonedList
set list = mdmdoc.Types["list1"]
set listClone = list.GetInterface("{F2780F44-AC4C-4afa-A429-30E817C05D37}")
set newClonedList = listClone.CloneEx[newDoc][null]
newDoc.Types.Add(newClonedList)
Dim newVar
Set newVar = newDoc.CreateVariable("newQ1", "new label for q1")
newVar.DataType = 3 ' mtCategorical
Dim newElement
Set newElement = newDoc.CreateElements("lst","")
Set newElement.Reference = newdoc.Types["list1"]
newElement.Namespace = false
newVar.Elements.Add(newElement)
newDoc.Fields.Add(newVar)
newDoc.Save("newDoc.mdd")
debug.Log(newDoc.Script)
See also