陈清红
2025-04-14 880f3c0257eeb8c37761d484258fdd102a369a19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Quintiq file version 2.0
#parent: #root
StaticMethod CopyDataset (LibOpt_Task task, String componentpositionname, Boolean hastocreaterobustcopy) as LibOpt_SnapshotReplannableCopyDataset
{
  Description:
  [*
    Create a copy of the current dataset.
    If `hastocreaterobustcopy` is `true`, then a memory only copy of the current dataset is created by using the `MDSObject.Copy` method. 
    If an error or rollback can occur in the same transaction as the current transaction, then it is required to use `hastocreaterobustcopy = true`, otherwise the dataset copy will fail.
    If `hastocreaterobustcopy` is `false`, then a standalone storage copy is created by using the `MDSObject::Create` method.
  *]
  TextBody:
  [*
    // evr3 Feb-27-2020 (created)
    snapshot := null( LibOpt_SnapshotReplannableCopyDataset );
    component := task.Component();
     
    mdsid := component.MDSID();
    datasetinfo := MDSEditor::Editor().LoadedObjectInfo( mdsid );
    
    namenewdataset := LibOpt_DatasetCopyConditional::GetNameCopyDataset( component, 
                                                                         datasetinfo.Name(),
                                                                         componentpositionname
                                                                         );
    // namenewdataset cannot contain most special characters, because this breaks mdsobject.Copy(). We therefore remove the special characters.
    namenewdataset := LibOpt_Utility::RemoveSpecialCharacters( namenewdataset );                 
    // Check if the new dataset name already exists.
    // If another dataset was created very recently, then MDSEditor::Editor().ObjectInfos() might not yet know about this dataset.
    // However, we did create a SnapshotReplannableCopyDataset snapshot for this dataset. 
    // We use this snapshot to check if our new dataset name is not already being used by that dataset.  
    objectinfos := MDSEditor::Editor().ObjectInfos();
    suffix := 1;    
    namewithoutsuffix := namenewdataset;
    while( exists( component.Run(), Snapshot.Children.astype( LibOpt_SnapshotReplannableCopyDataset ), replannablesnapshot, replannablesnapshot.DatasetName() = namenewdataset )
           or exists( objectinfos, Elements, objectinfo, objectinfo.Name() = namenewdataset ) )
    {    
      namenewdataset := namewithoutsuffix + "_" + [String] suffix;
      suffix++;
    }
    
    if( hastocreaterobustcopy )
    {
      snapshot := LibOpt_DatasetCopyConditional::CopyDatasetRobust( task,
                                                                    namenewdataset,
                                                                    componentpositionname,
                                                                    mdsid );
    }
    else
    {
      snapshot := LibOpt_DatasetCopyConditional::CopyDatasetQuick( task,
                                                                   datasetinfo.Kind(),
                                                                   namenewdataset,
                                                                   componentpositionname,
                                                                   mdsid );
    }
    
    return snapshot;
  *]
}