How to create a replica in a disconnected environment


Summary
This article demonstrates how to create a replica in a disconnected environment using ArcObjects. A disconnected environment is one in which the parent and child replica geodatabases are not connected by a local area network (LAN) or a wide area network (WAN).

In this topic


Exporting the replica workspace document from the parent replica geodatabase

  1. Connect to a geodatabase to host the parent replica. This requires initializing a GeoDataServer object. GeoDataServer objects allow you to access a geodatabase on a LAN or WAN through ArcGIS Server. To see how to initialize a GeoDataServer, refer to How to initialize a geodataserver object.
  2. Call the following procedure by pasting it into your application and passing in the following parameters:
parentGDS
The GeoDataServer that you initialized in Step 1.  This GeoDataServer will host your parent replica.
replicaName
The string name of the replica to be created.
accessType
This parameter specifies the type of replica to create. Three replica types are supported, including checkout replicas, two-way replicas, and one-way replicas. Two-way and one-way replicas require an ArcSDE geodatabase for the child replica. However, checkout replicas allow personal, file, or ArcSDE geodatabases for the child replica. The parent replica must always be hosted by an ArcSDE geodatabase.
featureDataset
This parameter is the name of the feature dataset to replicate. All datasets to be replicated must be registered as versioned on the parent replica's geodatabase. Two-way and one-way replicas also require the datasets to have a GlobalID column and be stored with a high-precision coordinate system.
geometry
This parameter specifies the replica geometry that is applied as a filter where only features intersecting the geometry are replicated. If the filter is set to NULL or nothing, all features are replicated.
outputDirectory
This parameter specifies the output location for the replica workspace document. This location should point to a directory on your local drive. If this folder exists, it will be replaced. If it doesn't exist, it will be created. The replica workspace document created by this function is written in a compressed ZIP file format. This ZIP file will be written to the sOutputDirectory passed to the function.
[Java]
static void createReplicaDisconnected(IGeoDataServer parentGDS, String replicaName,
    int accessType, String featureDataset, IGeometry geometry, String
    outputDirectory)throws Exception{
    // Set and expand the replica datasets.
    IGPReplicaDatasets replicaDatasets = new GPReplicaDatasets();
    IGPReplicaDataset replicaDataset = new GPReplicaDataset();
    replicaDataset.setDatasetType(esriDatasetType.esriDTFeatureDataset);
    replicaDataset.setName(featureDataset);
    replicaDatasets.add(replicaDataset);
    IGPReplicaDatasets replicaDatasetsExpand = parentGDS.expandReplicaDatasets
        (replicaDatasets);
    // Set the replica description.
    IGPReplicaDescription description = new GPReplicaDescription();
    description.setReplicaDatasetsByRef(replicaDatasetsExpand);
    description.setModelType(esriReplicaModelType.esriModelTypeFullGeodatabase);
    description.setSingleGeneration((accessType ==
        esriReplicaAccessType.esriReplicaAccessNone));
    description.setQueryGeometryByRef(geometry);
    // Set the replica options.
    IGPReplicaOptions options = new GPReplicaOptions();
    options.setAccessType(accessType);
    options.setChildReconcilePolicy
        (esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone);
    options.setParentReconcilePolicy
        (esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone);
    options.setIsChildFirstSender(true);
    // Set the export options.
    IGDSExportOptions exportOptions = new GDSExportOptions();
    exportOptions.setExportFormat(esriGDSExportFormat.esriGDSExportFormatXml);
    exportOptions.setCompressed(true);
    exportOptions.setBinaryGeometry(false);
    // Create the replica.
    IGDSData gdsData = parentGDS.createReplica("", replicaName, description, options,
        exportOptions, esriGDSTransportType.esriGDSTransportTypeUrl);
    /* Force deletion of folder and contents if they exist.
    Create the output folder again. */
    initializeDirectory(outputDirectory);
    // Get the compressed replica workspace document file from the URL to the local output directory.
    if (gdsData.getTransportType() == esriGDSTransportType.esriGDSTransportTypeUrl){
        String fileURL = gdsData.getURL();
        URLConnection connection = new URL(fileURL).openConnection();
        String fileName = fileURL.substring(fileURL.lastIndexOf('/') + 1);
        connection.connect();
        InputStream input = connection.getInputStream();
        FileOutputStream output = new FileOutputStream(new File(outputDirectory +
            File.separator + fileName));
        byte[] bytes = new byte[10000];
        int actuallyRead = input.read(bytes);
        while (actuallyRead !=  - 1){
            output.write(bytes, 0, actuallyRead);
            actuallyRead = input.read(bytes);
        }
        input.close();
        output.flush();
        output.close();
    }
    else{
        System.out.println("Server is not configured with a virtual directory");
        gdsData.getEmbeddedData(); 
            //The file has been embedded because there is no output directory set on ArcGIS Server.
    }
}

private static void initializeDirectory(String outputDirectory){
    File directory = new File(outputDirectory);
    if (!directory.exists()){
        directory.mkdir();
        return ;
    }
    else{
        deleteContents(directory);
        directory.delete();
        directory.mkdir();
    }
}

private static void deleteContents(File directory){
    File[] contents = directory.listFiles();
    for (File file: contents){
        if (file.isDirectory())
            deleteContents(file);
        file.delete();
    }
}

Importing the replica workspace document into the child replica geodatabase

  1. Connect to a geodatabase to host the child replica. This requires initializing a GeoDataServer object. GeoDataServer objects allow you to access a geodatabase on a LAN or WAN through ArcGIS Server. To see how to initialize a GeoDataServer, refer to How to initialize a geodataserver object.
  2. Call the following procedure by pasting it into your application and passing in the following parameters:
childGDS
This is the GeoDataServer that you initialized in Step 1. This GeoDataServer will host your child replica.
inputDirectory
This specifies the input location for the replica workspace document. This location should point to the directory on your local drive that holds the replica workspace document. The function expects that the replica workspace document is written in a compressed format (XML compressed as a ZIP file). See the following note:
The ZIP file must have the same name as the XML file contained within it. The function can easily be modified to import uncompressed XML by setting the gdsData.Compressed property to false. The function also assumes that there is only one file in this directory. If there are more than one file in the inputDirectory, only the first file will be read.
[Java]
// Completes the creation of a replica by importing a compressed replica workspace document to the child GeoDataServer.
static void importReplica(IGeoDataServer childGDS, String inputDirectory)throws
    Exception{
    /* Get the replica workspace document from the input directory.
    It is assumed that the first file in the directory is the replica workspace document. */
    File zipFile = new File(inputDirectory).listFiles()[0];
    long size = zipFile.length();
    if (size > Integer.MAX_VALUE){
        System.out.println("File is larger than anticipated. Aborting.");
        return ;
    }
    byte[] bytes = new byte[(int)size];
    FileInputStream inputStream = new FileInputStream(zipFile);
    inputStream.reset();
    int actuallyRead = inputStream.read(bytes);
    while (actuallyRead !=  - 1){
        actuallyRead += inputStream.read(bytes, actuallyRead, bytes.length -
            actuallyRead);
    }
    // Embed the GDSData object.
    IGDSData pGDSdata = new GDSData();
    pGDSdata.setTransportType(esriGDSTransportType.esriGDSTransportTypeEmbedded);
    pGDSdata.setCompressed(true);
    pGDSdata.setEmbeddedData(bytes);
    // Import the replica workspace document to create the replica.
    childGDS.importData(pGDSdata,
        esriGDSImportFormat.esriGDSImportFormatXmlWorkspace);
}






Development licensingDeployment licensing
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine: Geodatabase Update