//-----------------------------------------------------------------------------
// IDatasetExample.js file
//-----------------------------------------------------------------------------
// There is described the usage of the IDataset and IDataField objects in the example.
//-----------------------------------------------------------------------------
//@ Start Example
// Creating the database connection and data storage objects
var Connector = new ActiveXObject('TSDskObjectLibrary.DskConnector');
var ScriptUtils = new ActiveXObject('TSObjectLibrary.ScriptUtils');
var Script = new ActiveXObject('TSObjectLibrary.Script');
var ServiceInfo = new ActiveXObject('TSObjectLibrary.ServiceInfo');
// Flag to save or delete the database service without any confirmation
var sdoaSave = 1;
// Message displaying function
function ShowMessage(Str) {
for (var i = 1; i < arguments.length; i++ ) {
Str = Str.replace(new RegExp('%' + i), arguments[i]);
}
WScript.Echo(Str);
}
if (Connector.Login("Example of the IDataset and IDataField use")) {
var Services = Connector.Services;
ScriptUtils.Connector = Connector;
// Displaying the information about the service quantity in the configuration
ShowMessage("Service quantity: %1", Services.InformationsCount);
ServiceInfo = Services.InformationsByUSI('enm_CustomerEnum');
// Creating the service 'enm_CustomerEnum'
if (ServiceInfo == ScriptUtils.EmptyValue) {
ShowMessage("Adding the enumeration service");
// Creating the enumeration service and adding its items
Enum = Services.CreateItem('Enum');
Enum.USI = 'X15\\enm_CustomerEnum';
Enum.Caption = "User's enumeration";
for (i = 0; i < 5; i++) {
EnumItem = Enum.CreateItem();
EnumItem.ID = Connector.GenGUID();
EnumItem.Caption = "Item caption of the enumeration # " + i;
EnumItem.Code = 'Code' + i;
Enum.Add(EnumItem);
}
// Saving enumeration service to the database
if (Services.SaveItem(Enum, sdoaSave)) {
// Updating service information
Services.LoadItemsInfo();
ShowMessage("Service quantity: %1", Services.InformationsCount);
} else {
ShowMessage("Enumeration service is not saved");
}
} else {
// The required enumeration is already available in the database
// Creating the enumeration service instance
Enum = Services.GetNewItemByUSI('enm_CustomerEnum');
}
// Creating the country lookup record for filling the value in the contact card
// Displaying the lookup data filed displayed value
CountryDataset = Services.GetNewItemByUSI('ds_Country');
CountryDataset.Append();
var CountryID = Connector.GenGUID();
CountryDataset.Values('ID') = CountryID;
CountryDataset.Values('Name') = "Country";
CountryDataset.Post();
// Setting the default values for displaying in the contact card
var DefaultValues = new ActiveXObject('Scripting.Dictionary');
DefaultValues.Add('OwnerID', Connector.CurrentUser.ID);
DefaultValues.Add('CountryID', CountryID);
// Creating the window service by the method GetSingleItemByUSI for its opening in the modal mode
var EditWindow = Services.GetSingleItemByUSI('wnd_ContactEdit');
EditWindow.Attributes('DefaultValues') = DefaultValues;
EditWindow.Attributes('RecordID') = '';
// Enumeration substitution for the field 'Gender'
ShowMessage("Changing the enumeration for field 'Gender' ");
EditWindow.ComponentsByName('cbGender').DatasetLink = ScriptUtils.EmptyValue;
EditWindow.ComponentsByName('dlData').Dataset.Open();
EditWindow.ComponentsByName('dlData').Dataset.DataFields.ItemsByName('GenderID').Enum = Enum;
EditWindow.ComponentsByName('cbGender').DatasetLink = EditWindow.ComponentsByName('dlData');
EditWindow.ComponentsByName('cbGender').DataFieldName = 'GenderID';
EditWindow.ComponentsByName('edtAccountName').IsEnabled = false;
// Displaying the window
EditWindow.Prepare();
EditWindow.ShowModal();
// Removing the created enumeration
Services.DeleteItem('X15\\enm_CustomerEnum', sdoaSave);
// Loading the file to the data field
FilesDataset = Services.GetNewItemByUSI('ds_Files');
var FileName = ScriptUtils.CreateObject('TSObjectLibrary.Value');
if (ScriptUtils.PromptForFileName(FileName, '*.txt', '*.txt', "Selecting the file to load to the database", '?:\\', false)) {
// Opening the dataset in the record insert state
FilesDataset.Append();
// Setting the identifier for the created record
var FileID = Connector.GenGUID();
FilesDataset.Values('ID') = FileID;
// Loading the selected file
BlobDataField = FilesDataset.DataFields.ItemsByName('FileData');
BlobDataField.LoadFromFile(FileName.Value);
// Displaying the file content
ShowMessage(BlobDataField.Value);
FilesDataset.Post();
}
}
//@ End Example
|
Copyright (с) Terrasoft 2002-2007.
|