Class DocumentIDRegistry

  • Direct Known Subclasses:
    PersistableDocumentIDRegistry

    public class DocumentIDRegistry
    extends java.lang.Object
    Represents a registry for document ID's. A document ID is a placeholder for PK's of not unique items which are currently not known. For example, assume the import of customers with additional specification of their delivery address. When importing a customer, the delivery address itself is not yet imported and because addresses do not have unique attributes you can not declare the reference to address. So you can use a placeholder (the document ID) to reference a address. When importing the address you also declare a special column for assigning the ID. While import, the mapping between ID's and PK's will be created and managed with usage of this registry class. It holds for each qualifier a map with the existing mappings. These mappings can be made persistent with usage of readers and writers. (see DocumentIDRegistry(CSVReader, CSVWriter)). For declaring a column holding ID's you have to use a qualifier started with an &. The ID is unique within the scope of this qualifier.

    Example script:
    INSERT_UPDATE Customer; uid[unique=true]; defaultPaymentAddress( &payAddress ); defaultShipmentAddress( &delAddress )
    ; andy ; payAddress0 ; delAddress1 ;
    ; rigge; payAddress1 ; delAddress0 ;
    INSERT Address; &payAddress; &delAddress ; owner( Customer.uid ) ; department
    ; payAddress0 ; delAddress0 ; andy ; a1
    ; payAddress1 ; delAddress1 ; andy ; a2

    This script is used for importing a customer with referenced addresses. When importing, first the customer will be created but marked as unresolved (here the unresolved of the ImpExImportReader is meant, because the used document IDs can not be found in registry. When importing the addresses, the specified IDs will be registered with the PKs of the addresses and in second import cycle the customers can be finished.
    When using the example script for export (without value lines), the customers will be exported first. Because there are no IDs used for the referenced addresses, new IDs will be generated, but stored in the storage with unresolved IDs. When exporting the addresses the registry recognizes the already used but unresolved IDs for the addresses and moves them to the resolved storage. So you can be sure, when you export an item A which references to an item B, that item B is also exported by simply calling hasUnresolvedIDs after the export process.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  DocumentIDRegistry.MODE
      Enumeration representing the two modes of storing an ID => resolved and unresolved.
    • Constructor Summary

      Constructors 
      Constructor Description
      DocumentIDRegistry()
      Creates a registry without import of mappings and without export of new mappings.
      DocumentIDRegistry​(CSVReader documentIDReader)
      Creates a registry with import of mappings and without export of new mappings.
      DocumentIDRegistry​(CSVReader documentIDReader, CSVWriter documentIDWriter)
      Creates a registry with import of mappings and with export of new mappings.
      DocumentIDRegistry​(CSVWriter documentIDWriter)
      Creates a registry without import of mappings and with export of new mappings.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addID​(java.lang.String qualifier, java.lang.String documentID, long pk, DocumentIDRegistry.MODE mode)
      Adds a new mapping ID<->PK to the registry.
      protected java.lang.String calculateNextID​(java.lang.String qualifier)
      Generates a new unused ID containing the given qualifier name.
      void closeStreams()
      Closes all used streams (for import and export).
      boolean containsID​(java.lang.String qualifier, java.lang.String documentID)
      Checks whether the registry contains an mapping with the given ID in scope of the qualifier.
      boolean containsPK​(java.lang.String qualifier, long pk)
      Checks whether the registry contains an mapping with the given PK in scope of the qualifier.
      protected void exportID​(java.lang.String qualifier, java.lang.String documentID, long pk)
      Exports an existing mapping to the local writer.
      protected java.lang.String getID​(java.lang.String qualifier, long pk, DocumentIDRegistry.MODE mode)
      Gets the ID to which the given PK is mapped.
      protected long getPK​(java.lang.String qualifier, java.lang.String documentID, DocumentIDRegistry.MODE mode)
      Gets the PK to which the given ID is mapped.
      protected java.util.Map<java.lang.String,​org.apache.commons.collections4.bidimap.DualHashBidiMap<java.lang.String,​java.lang.Long>> getQualifiersMap​(DocumentIDRegistry.MODE mode)
      Returns the storage related to the given mode.
      boolean hasUnresolvedIDs()
      Checks whether there are unresolved IDs in registry.
      void importIDs​(CSVReader documentIDReader)
      Imports a set of ID<->PK mappings from given reader.
      boolean isResolved​(java.lang.String qualifier, java.lang.String documentID)
      Checks a given ID if it is in resolved mode.
      boolean isUnresolved​(java.lang.String qualifier, java.lang.String documentID)
      Checks a given ID if it is in unresolved mode.
      long lookupID​(java.lang.String qualifier, java.lang.String documentID)
      Gets the PK to which the given ID is mapped.
      java.lang.String lookupPK​(java.lang.String qualifier, long pk)
      Checks if there is always an ID for the given PK in scope of the qualifier, if not, a new ID will be generated marked as unresolved.
      java.util.List<java.lang.String> printUnresolvedIDs​(java.lang.String separator)
      Gathers all unresolved IDs and prints each one in a list entry.
      java.lang.String registerID​(java.lang.String qualifier, java.lang.String documentID, long pk)
      Registers a new mapping ID<->PK to the registry.
      java.lang.String registerPK​(java.lang.String qualifier, long pk)
      Registers a new mapping ID<->PK to the registry.
      protected void resolveID​(java.lang.String qualifier, java.lang.String documentID, long pk)
      Resloves an unresolved ID.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DocumentIDRegistry

        public DocumentIDRegistry()
        Creates a registry without import of mappings and without export of new mappings. You can start an import manually using importIDs(CSVReader) later.
      • DocumentIDRegistry

        public DocumentIDRegistry​(CSVWriter documentIDWriter)
        Creates a registry without import of mappings and with export of new mappings. You have to call closeStreams() before usage of the exported stream data. You can start an import manually using importIDs(CSVReader) later.
        Parameters:
        documentIDWriter - writer to which new mappings will be exported.
      • DocumentIDRegistry

        public DocumentIDRegistry​(CSVReader documentIDReader)
        Creates a registry with import of mappings and without export of new mappings.
        Parameters:
        documentIDReader - the reader from which mappings will be read and imported when instantiating.
      • DocumentIDRegistry

        public DocumentIDRegistry​(CSVReader documentIDReader,
                                  CSVWriter documentIDWriter)
        Creates a registry with import of mappings and with export of new mappings. You have to call closeStreams() before usage of the exported stream data.
        Parameters:
        documentIDReader - the reader from which mappings will be read and imported when instantiating.
        documentIDWriter - writer to which new mappings will be exported.
    • Method Detail

      • importIDs

        public void importIDs​(CSVReader documentIDReader)
        Imports a set of ID<->PK mappings from given reader.
        Parameters:
        documentIDReader - the reader with which the mappings will be read
      • exportID

        protected void exportID​(java.lang.String qualifier,
                                java.lang.String documentID,
                                long pk)
        Exports an existing mapping to the local writer.
        Parameters:
        qualifier - the qualifier to which the ID is related
        documentID - the ID
        pk - the PK to which the ID is mapped
      • registerID

        public java.lang.String registerID​(java.lang.String qualifier,
                                           java.lang.String documentID,
                                           long pk)
                                    throws ImpExException
        Registers a new mapping ID<->PK to the registry. If the pair is already existent, it will be replaced. Used for import to register the ID of an item.
        Parameters:
        qualifier - the qualifier to which the ID is related
        documentID - the new ID
        pk - the PK to which the ID will be mapped
        Returns:
        the document ID mapped to the given PK (same ID as given one)
        Throws:
        ImpExException - the ID already exists and maps to another PK
      • registerPK

        public java.lang.String registerPK​(java.lang.String qualifier,
                                           long pk)
        Registers a new mapping ID<->PK to the registry. Checks if there is always an ID for the given PK in scope of the qualifier, if not, a new ID will be generated. Used for export to give each item a unique ID related to its PK.
        Parameters:
        qualifier - qualifier in whose scope the ID will be generated
        pk - the pk for which an ID mapping is needed
        Returns:
        the (new) ID mapped to the given PK
      • lookupPK

        public java.lang.String lookupPK​(java.lang.String qualifier,
                                         long pk)
        Checks if there is always an ID for the given PK in scope of the qualifier, if not, a new ID will be generated marked as unresolved. Used for export to use the correct ID for an item reference. Is the referenced item not exported the lookup will generate an unresolved ID and will be resolved when item is exported.
        Parameters:
        qualifier - qualifier in whose scope the ID will be generated
        pk - the pk for which an ID mapping is needed
        Returns:
        the (new) ID mapped to the given PK
      • lookupID

        public long lookupID​(java.lang.String qualifier,
                             java.lang.String documentID)
        Gets the PK to which the given ID is mapped. Used while import for resolving an ID to an PK.
        Parameters:
        qualifier - scope of the ID
        documentID - ID for which the PK will be returned
        Returns:
        the PK to the given ID or -1
      • containsID

        public boolean containsID​(java.lang.String qualifier,
                                  java.lang.String documentID)
        Checks whether the registry contains an mapping with the given ID in scope of the qualifier. Checks within resolved and unresolved mappings.
        Parameters:
        qualifier - the scope in which the ID is valid
        documentID - the ID which will be checked
        Returns:
        true if a mapping with the given ID to a PK is existent
      • containsPK

        public boolean containsPK​(java.lang.String qualifier,
                                  long pk)
        Checks whether the registry contains an mapping with the given PK in scope of the qualifier. Checks within resolved and unresolved mappings.
        Parameters:
        qualifier - the scope which will be searched
        pk - the PK which will be checked for an ID
        Returns:
        true if a mapping with the given PK to a ID is existent within the qualifier
      • hasUnresolvedIDs

        public boolean hasUnresolvedIDs()
        Checks whether there are unresolved IDs in registry. An ID is unresolved, if it is used while export and the item with mapped pk is not export until yet.
        Returns:
        true if there are unresolved IDs, otherwise false
      • isUnresolved

        public boolean isUnresolved​(java.lang.String qualifier,
                                    java.lang.String documentID)
        Checks a given ID if it is in unresolved mode. An ID is unresolved, if it is used while export and the item with mapped pk is not exported until yet.
        Parameters:
        qualifier - the scope which will be searched
        documentID - the ID which will be checked
        Returns:
        true if the given ID in given scope is in unresolved mode, otherwise false
      • isResolved

        public boolean isResolved​(java.lang.String qualifier,
                                  java.lang.String documentID)
        Checks a given ID if it is in resolved mode. An ID is resolved, if it is used while import, or export and the item with mapped pk is already exported
        Parameters:
        qualifier - the scope which will be searched
        documentID - the ID which will be checked
        Returns:
        true if the given ID in given scope is in resolved mode, otherwise false
      • printUnresolvedIDs

        public java.util.List<java.lang.String> printUnresolvedIDs​(java.lang.String separator)
        Gathers all unresolved IDs and prints each one in a list entry. So one entry of the resulting list represents one unresolved ID with format qualifier+separator+id+separator+pk.
        Parameters:
        separator - the separator symbol used between the attributes of a documentID in resulting list
        Returns:
        list of all unresolved ID with one ID per entry
      • closeStreams

        public void closeStreams()
        Closes all used streams (for import and export).
      • getID

        protected java.lang.String getID​(java.lang.String qualifier,
                                         long pk,
                                         DocumentIDRegistry.MODE mode)
        Gets the ID to which the given PK is mapped.
        Parameters:
        qualifier - scope in which will be searched
        pk - the PK for which the ID will be returned
        mode - defines the ID storage in which will be searched
        Returns:
        the ID to the given PK or null
      • getPK

        protected long getPK​(java.lang.String qualifier,
                             java.lang.String documentID,
                             DocumentIDRegistry.MODE mode)
        Gets the PK to which the given ID is mapped. Used while import for resolving an ID to an PK.
        Parameters:
        qualifier - scope of the ID
        documentID - ID for which the PK will be returned
        mode - defines the ID storage in which will be searched
        Returns:
        the PK to the given ID or -1
      • calculateNextID

        protected java.lang.String calculateNextID​(java.lang.String qualifier)
        Generates a new unused ID containing the given qualifier name. Generation uses the qualifier name concatenated with sum of sizes of the storages. Afterwards it increments the resulting number while the id is already used.
        Parameters:
        qualifier - text which will be part of the generated id
        Returns:
        a new id in format qualifier+number
      • addID

        protected void addID​(java.lang.String qualifier,
                             java.lang.String documentID,
                             long pk,
                             DocumentIDRegistry.MODE mode)
                      throws ImpExException
        Adds a new mapping ID<->PK to the registry. If the pair is already existent, it will be replaced. T
        Parameters:
        qualifier - the qualifier to which the ID is related
        documentID - the new ID
        pk - the PK to which the ID will be mapped
        mode - defines the ID storage where the ID will be added
        Throws:
        ImpExException - the ID already exists and maps to another OK
      • resolveID

        protected void resolveID​(java.lang.String qualifier,
                                 java.lang.String documentID,
                                 long pk)
        Resloves an unresolved ID. Tries to remove the ID from the unresolved ID storage and to add it to the resolved storage.
        Parameters:
        qualifier - the qualifier to which the ID is related
        documentID - the id which will be moved from unresolved to resolved
        pk - the mapped pk of the ID
      • getQualifiersMap

        protected java.util.Map<java.lang.String,​org.apache.commons.collections4.bidimap.DualHashBidiMap<java.lang.String,​java.lang.Long>> getQualifiersMap​(DocumentIDRegistry.MODE mode)
        Returns the storage related to the given mode.
        Parameters:
        mode - the mode to which the associated storage is needed
        Returns:
        storage associated to given mode