8 Merge, Unmerge, Link and Unlink Person Data in HDR

The code samples below help you to:

Most of the basic operations described in this guide are based on the RIM Services APIs. Usage of RIM Service APIs is described in "RIM Service".

Example 8-1 Classes Used by EMPI Examples

The following code sample implements classes used by the following examples:

import oracle.hsgbu.hdr.configuration.InternalOID;
import oracle.hsgbu.hdr.configuration.OIDService;
import oracle.hsgbu.hdr.empi.Domain;
import oracle.hsgbu.hdr.fwk.base.common.ETSException;
import oracle.hsgbu.hdr.fwk.serviceLocator.common.ServiceLocator;
import oracle.hsgbu.hdr.hl7.RimService;
import oracle.hsgbu.hdr.hl7.domain.ActMood;
import oracle.hsgbu.hdr.hl7.domain.ActRelationshipType;
import oracle.hsgbu.hdr.hl7.domain.EntityClass;
import oracle.hsgbu.hdr.hl7.domain.NullFlavor;
import oracle.hsgbu.hdr.hl7.domain.ParticipationType;
import oracle.hsgbu.hdr.hl7.domain.RoleClass;
import oracle.hsgbu.hdr.hl7.factories.ActFactory;
import oracle.hsgbu.hdr.hl7.factories.DataTypeFactory;
import oracle.hsgbu.hdr.hl7.factories.EntityFactory;
import oracle.hsgbu.hdr.hl7.factories.QueryComponentFactory;
import oracle.hsgbu.hdr.hl7.query.ActFetch;
import oracle.hsgbu.hdr.hl7.query.ActRelationshipFetch;
import oracle.hsgbu.hdr.hl7.query.CoreFetch;
import oracle.hsgbu.hdr.hl7.query.ENAttributeCriteria;
import oracle.hsgbu.hdr.hl7.query.ENXPAttributeCriteria;
import oracle.hsgbu.hdr.hl7.query.EntityAttributeCriteria;
import oracle.hsgbu.hdr.hl7.query.EntityFetch;
import oracle.hsgbu.hdr.hl7.query.ParticipationFetch;
import oracle.hsgbu.hdr.hl7.query.RoleAttributeCriteria;
import oracle.hsgbu.hdr.hl7.query.RoleFetch;
import oracle.hsgbu.hdr.hl7.query.SearchOperator;
import oracle.hsgbu.hdr.hl7.query.SetSearchOperator;
import oracle.hsgbu.hdr.hl7.rim.Act;
import oracle.hsgbu.hdr.hl7.rim.ActRelationship;
import oracle.hsgbu.hdr.hl7.rim.ControlAct;
import oracle.hsgbu.hdr.hl7.rim.Entity;
import oracle.hsgbu.hdr.hl7.rim.InfrastructureRoot;
import oracle.hsgbu.hdr.hl7.rim.Organization;
import oracle.hsgbu.hdr.hl7.rim.Participation;
import oracle.hsgbu.hdr.hl7.rim.Person;
import oracle.hsgbu.hdr.hl7.rim.QueryAct;
import oracle.hsgbu.hdr.hl7.rim.Role;
import oracle.hsgbu.hdr.hl7.types.AD;
import oracle.hsgbu.hdr.hl7.types.ADXP;
import oracle.hsgbu.hdr.hl7.types.BAG<AD>;
import oracle.hsgbu.hdr.hl7.types.BAG<EN>;
import oracle.hsgbu.hdr.hl7.types.CE;
import oracle.hsgbu.hdr.hl7.types.CS;
import oracle.hsgbu.hdr.hl7.types.EN;
import oracle.hsgbu.hdr.hl7.types.ENXP;
import oracle.hsgbu.hdr.hl7.types.II;
import oracle.hsgbu.hdr.hl7.types.SET_II;
import oracle.hsgbu.hdr.hl7.types.ST;
import oracle.hsgbu.hdr.hl7.types.common.CSImpl;
import oracle.hsgbu.hdr.hl7.types.common.NullFlavorImpl;
import oracle.hsgbu.hdr.empi.Context;
import oracle.hsgbu.hdr.empi.DedupContext;
import oracle.hsgbu.hdr.empi.ContextDomain;
import oracle.hsgbu.hdr.empi.LinkingContextDomain;
import oracle.hsgbu.hdr.empi.LinkingContext;
import oracle.hsgbu.hdr.empi.LinkingContextAttributeCriteria;
import oracle.hsgbu.hdr.empi.LinkingContextFetch;
import oracle.hsgbu.hdr.empi.common.LinkingContextFetchImpl;
import oracle.hsgbu.hdr.empi.common.LinkingContextDomainImpl;
import oracle.hsgbu.hdr.empi.common.LinkingContextDomainFetchImpl;
import oracle.hsgbu.hdr.empi.EmpiService;
import oracle.hsgbu.hdr.empi.EmpiConfigurationService;
import oracle.hsgbu.hdr.hl7.rim.Person;
import oracle.hsgbu.hdr.empi.EmpiFactory;
import oracle.hsgbu.hdr.empi.PersonFetch;
import oracle.hsgbu.hdr.empi.DomainAttributeCriteria;
import oracle.hsgbu.hdr.empi.common.DomainAttributeCriteriaImpl;
import oracle.hsgbu.hdr.empi.common.DomainFetchImpl;
import oracle.hsgbu.hdr.empi.common.DomainImpl;
import oracle.hsgbu.hdr.empi.PersonAttributeCriteria;

The following variables are used throughout the examples that follow (Examples 8-2 through 8-13):

Variables

Variable Name Class
mSL ServiceLocator
mOIDService OIDService
mRimService RimService
mAF ActFactory
mDTF DataTypeFactory
mEF EntityFactory
mQCF QueryComponentFactory
mEMF EmpiFactory
mConfigService EmpiConfigurationService

Example 8-2 Create a Person Entity

This code sample illustrates creation of a Person entity using RIM Services. Person attributes such as name, address, gender, and marital status have been set on the Person. The process involves creation of the Person entity using the RIM Service EntityFactory class and populating the attributes required on this object. All data types must be created using the DataTypeFactory class.

The process for creation and persistence of a person includes the following steps:

  • An Organization is created; the organization is the scoper of the Identification Role.

  • The EntityFactory returns a new Person and assigns a unique identifier to it. The status of the Person is set to Active.

  • Various attributes (such as name, address, gender,...) are set on the Person.

  • A Domain is attached to the person; the Domain is associated with the person in the form of a Type 2 role played by the Person. In the case of a Domain Enabled Environment, every person created must belong to a Domain.

  • A unique identification for the Person (such as Social Security Number) is also attached in the form of a Type 2 role played by the Person.

  • The Person is persisted using the standard RIM Services persistence mechanism.

Figure 8-1 Person Registry RMIM

Surrounding text describes Figure 8-1 .

Figure 8-1 illustrates the Person Registry RMIM, including related classes and the interactions between them.

To create a person in a domain-enabled environment, a type II role (the Domain Role) must be attached to the person; in a domain-disabled environment a type II role is not required. The domain role is a played role, which is identified by concept code 001896, coding scheme HDR Supplemental, and root OIDService.EMPI_DOMAIN. The domain id (extension associated with the domain role) is used to identify the person's domain. Note that the person must be associated with a single valid domain role.

When a person is created, a played type II role (the Master Person Role) is attached to the person by the EMPI service. This type II role is identified by concept code 001897, coding scheme HDR Supplemental, and root OIDService.EMPI_MASTER_ID. The master person id (extension associated with the master person role) is used as the logical identifier to represent persons belonging to the same linked set.

Create a person in a domain-enabled mode by attaching a valid domain role, using the following code sample:

Person Creation in Domain Enabled Scenario

public void testCreatePersonsDomainEnabled() throws ETSException
{
   //Assign the II for Sally Andrews
   String rootForPerson = "9.989898.5.1";
   String extension = "PSN0001";
   String domainId = "2147483640";
    
   person = persistPerson(rootForPerson , extension , domainId);
 
   personII = getPersonII(person);
   System.out.println("Person created with II " +personII);
   System.out.println("Person 1 created and persisted successfully");
    
   rootForPerson = "9.989898.5.1";
   extension = "PSN0002";
   person = persistPerson(rootForPerson , extension , domainId);
    
   System.out.println("Person created with II " +personII);
   System.out.println("Person 2 created and persisted successfully");
}                       
    
    
public Person persistPerson(String rootForPerson , String extension ,String domainId) throws ETSException
{
   //Create a Person object with the provided root and extension
   person = getPerson(rootForPerson , extension );
    
   if(domainId != null)
    
{
   //Create a domain role and attach it to the person
   addDomainRole(person , domainId );
}
    
   //Add a type II ident role
   addIdentToPerson(person, "PASSPORT", "1234567890");
    
   String rootForIdent = "9.515151.5";
   String extIdent = "535-77-3737";
    
   //Create the ident role with the person as the player
   Role identRole = getIdentRole(person, rootForIdent,extIdent );
    
   CS statusCode = mDataTypeFactory.newCS("active");
    
   //Create the Registry Act and set the status code to active
   Act registryAct = mActFactory.newRegistryAct(ActMood.EVN, null , null);
   registryAct.setStatusCode(statusCode);
   registryAct.setTitle(mDataTypeFactory.newST("Person Persistence Registry Act"));
    
   //Add the Participation to the ident role
   registryAct.addParticipation(ParticipationType.SBJ, identRole);
    
   //Get the Control Act
   ControlAct controlAct = mActFactory.newControlActEvent(null,null);
   controlAct.setStatusCode(statusCode);
    
   //Create an Act Relationship between Control Act and Registry Act
   ActRelationship ar = controlAct.addOBActRelationship(ActRelationshipType.SUBJ, registryAct);
    
   // Submit the Control Act and commit the data
   ControlAct returnedControlAct = mRimService.submit(controlAct);
    
   return person;
}
    
    
public Person getPerson(String rootForPerson , String extension ) throws ETSException
{
   //Create a SET_II object using the Persons roor and extension
   SET_II setII = mDataTypeFactory.newSET_II(mDataTypeFactory.newII(rootForPerson , extension, true));
    
   // Create a CS object for the determiner code 'INSTANCE'
   CS entityDeterminer = mDataTypeFactory.newCS("INSTANCE");
    
   //Create the Person object
   person = mEntityFactory.newPerson(null,entityDeterminer,setII);
    
   // Create a CS object for the status code 'active'
   CS statusCode = mDataTypeFactory.newCS("active");
    
   //Set the person's status to active
   person.setStatusCode(statusCode);
    
   //Create a new person with name as "Sally Andrews"
   String givenName = "Sally";
   String famName = "Andrews";
    
   //Populate the First Name
   CS namePart1 = mDataTypeFactory.newCS("GIV");
   ENXP namePartObj1 = mDataTypeFactory.newENXP(givenName,namePart1,mDataTypeFactory.nullSET<CS> 
    (CSImpl.nullCS(NullFlavorImpl.NI)));
    
   //Populate the Last Name
   CS namePart2 = mDataTypeFactory.newCS("FAM");
   ENXP namePartObj2 = mDataTypeFactory.newENXP(famName,namePart2,mDataTypeFactory.nullSET<CS> 
    (CSImpl.nullCS(NullFlavorImpl.NI)));
    
   ENXP[] namePartList = new ENXP[] {namePartObj1, namePartObj2 };
    
   //Set the Legal Name
   EN name = mDataTypeFactory.newEN(namePartList , mDataTypeFactory.newSET<CS>(mDataTypeFactory.newCS("L")), 
    mDataTypeFactory.nullIVL<TS>(CSImpl.nullCS(NullFlavorImpl.NI))); 
   BAG<EN> nameBag = mDataTypeFactory.newBAG<EN>(new EN[] {name});
    
   person.setName(nameBag);
    
   //Create the address parts
   ADXP addrPart1 = mDataTypeFactory.newADXP("WA", mDataTypeFactory.newCS("STA")) ;  
    //Name of the State
   ADXP addrPart2 = mDataTypeFactory.newADXP("Seattle", mDataTypeFactory.newCS("CTY"));  
    //Name of the City
   ADXP addrPart3 = mDataTypeFactory.newADXP("98105", mDataTypeFactory.newCS("ZIP"));  
    //Zip Code
   ADXP addrPart4 = mDataTypeFactory.newADXP("3545 18th Avenue", mDataTypeFactory.newCS("SAL"));
    //Street Address Line
   ADXP addrPart5 = mDataTypeFactory.newADXP("US", mDataTypeFactory.newCS("CNT"));  
    //Name of the Country
    
   ADXP[] addrParts = new ADXP[]{addrPart1, addrPart2, addrPart3, addrPart4, addrPart5};
    
   //Set the address as Home Primary Address
   AD addr = mDataTypeFactory.newAD(addrParts, mDataTypeFactory.newSET_CS(mDataTypeFactory.newCS("HP")), 
    mDataTypeFactory.nullGTS (CSImpl.nullCS(NullFlavorImpl.NI)));
    
   //Create an address bag
   BAG<AD> addrBag = mDataTypeFactory.newBAG<AD>(addr);
    
   //Set the address bag on the person
   person.setAddr(addrBag);
    
   //Add the Gender
   CE genderCode =  mDataTypeFactory.newCE(mDataTypeFactory.newST("000202"),
                                        mDataTypeFactory.newUID(mCodingSchemeOid),
                                        mDataTypeFactory.newST("HDR Supplemental"),
                                        mDataTypeFactory.nullST(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI),
                                        mDataTypeFactory.nullED(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI));
     
    person.setAdministrativeGenderCode (genderCode);
     
    //Add the Marital Status
    CE maritalCode =    mDataTypeFactory.newCE(mDataTypeFactory.newST("000282"),
                                                mDataTypeFactory.newUID(mCodingSchemeOid),
                                                mDataTypeFactory.newST("HDR Supplemental"),
                                                mDataTypeFactory.nullST(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI),
                                                mDataTypeFactory.nullED(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI));
     
    person.setMaritalStatusCode(maritalCode);
     
    //Add the Birth Time
    person.setBirthTime(mDataTypeFactory.newTS(new java.util.Date()));
     
    //Setting various other attributes for the person
    person.setLivingArrangementCode(mDataTypeFactory.newCE(mDataTypeFactory.newST("H"),
                                                mDataTypeFactory.nullUID(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI),
                                                mDataTypeFactory.newST("LivingArrangement"),
                                                mDataTypeFactory.nullST(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI),
                                                mDataTypeFactory.nullED(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI)));
     
    person.setReligiousAffiliationCode(mDataTypeFactory.newCE(mDataTypeFactory.newST("1041"),
                                                mDataTypeFactory.nullUID(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI),
                                                mDataTypeFactory.newST("ReligiousAffiliation"),
                                                mDataTypeFactory.nullST(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI),
                                                mDataTypeFactory.nullED(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI)));
    person.setDeceasedInd(mDataTypeFactory.newBL(false));
    person.setMultipleBirthInd(mDataTypeFactory.newBL(false));
    person.setOrganDonorInd(mDataTypeFactory.newBL(false));
    person.setTelecom(mDataTypeFactory.newBAG<TEL>(mDataTypeFactory.newTEL(mDataTypeFactory.newCS("H"),
    //Set as Home Number
                                                mDataTypeFactory.newST("+91-80-5108 0001"),
                                                mDataTypeFactory.nullGTS(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI),
                                                mDataTypeFactory.nullSET<CS>(oracle.hsgbu.hdr.hl7.domain.
                                                 NullFlavor.NI))));
    return person;
}
     
     
public Role getIdentRole(Person player, String rootForIdent , String extIdent ) throws ETSException
{
    //Adding the identification role to the Person, assuming the identification exists
    CE identRoleCode =  mDataTypeFactory.newCE(mDataTypeFactory.newST("398093005"),null,
                                                mDataTypeFactory.newST("SNOMED-CT"),
                                                mDataTypeFactory.nullST(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI),
                                                mDataTypeFactory.nullED(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI));
     
    CS statusCode = mDataTypeFactory.newCS("active");
     
    II[] identRoleII = new II[] {mDataTypeFactory.newII(rootForIdent,extIdent, true)};
    SET_II identRoleSetII = mDataTypeFactory.newSET_II(identRoleII);
     
    //Set the identification role on the person
    Role identRole = mRoleFactory.newIdentifiedEntity(null, player , null , null);
     
    //Role identRole = person.addPlayedRole(RoleClass.IDENT, identRoleCode, org,identRoleSetII);
    identRole.setStatusCode(statusCode); 
     
    return identRole;
}
     
     
public Role addDomainRole(Person person , String domainId) throws ETSException
{
    //For the domain enabled scenario, adding the domain information to the person assuming
    //the domain already exists
    String domCode = "001896"; //Domain Code
    String domCodeSystemName = "HDR Supplemental";
    CE domainRoleCode = mDataTypeFactory.newCE(mDataTypeFactory.newST(domCode),
                                        mDataTypeFactory.newUID(mCodingSchemeOid),
                                        mDataTypeFactory.newST(domCodeSystemName),
                                        mDataTypeFactory.nullST(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI),
                                        mDataTypeFactory.nullED(oracle.hsgbu.hdr.hl7.domain.NullFlavor.NI));
     
    OIDService oidService = mServiceLocator.getOIDService();
     
    //Getting the domain root from the OIDService
    String rootForDomain = oidService.getOID(OIDService.EMPI_DOMAIN).getRootId().stringValue();
     
    //Create the Domain II from the root and extension
    II[] domainRoleII = new II[] 
                {mDataTypeFactory.newII(mDataTypeFactory.newUID(rootForDomain), 
                 mDataTypeFactory.newST(domainId), mDataTypeFactory.newBL(true))};
    SET_II domainRoleSetII = mDataTypeFactory.newSET_II(domainRoleII);
     
    //Add the person to a Domain and set status to active
    Role domainRole = person.addPlayedRole(RoleClass.IDENT, domainRoleCode, null, domainRoleSetII);
     
    CS statusCode = mDataTypeFactory.newCS("active");
    domainRole.setStatusCode(statusCode); 
     
    return domainRole;
}
     
     
        //Method returns the II of the person
        public static II getPersonII(Person person1)
{
    II ii1 = null;
    try
    {
                SET_II setII = person1.getId();
                Set set = setII.toSet();
                Iterator iter = set.iterator();
                 
                while(iter.hasNext())
                 
                {
                        ii1 = (II) iter.next();
                        System.out.println("The root is " +ii1.root().stringValue());
                        System.out.println("The corresponding extension is " +ii1.extension().stringValue());
                        if(ii1.root().stringValue().equals(mRimService.getInternalOID().stringValue()))
                        {
                 break;
                        }
                }
    }
        catch(Exception e)
        {
        e.printStackTrace();
        }
        return ii1
}
 
public static Role addIdentToPerson(Person person, String conceptCode, String personIdent) throws ETSException
{
        String codingSchemeName = getCodingSchemeNameFromConceptListName("LOOKUPS","CTB_ID_TYPES",conceptCode);
        CE identRoleCode = mDataTypeFactory.newCE(mDataTypeFactory.newST(conceptCode),
                                           mDataTypeFactory.nullUID(NullFlavor.NI),
                                           mDataTypeFactory.newST(codingSchemeName),
                                           mDataTypeFactory.nullST(NullFlavor.NI),
                                           mDataTypeFactory.nullED(NullFlavor.NI));
         
        II identRoleII = getII(Constants.IDENT_ROLE_ROOT,personIdent,null); 
        SET_II identRoleSetII = mDataTypeFactory.newSET_II(identRoleII);
         
        //Add Identification to Person
        Role identRole = person.addPlayedRole(RoleClass.IDENT,identRoleCode,null,identRoleSetII);
        identRole.setStatusCode( mDataTypeFactory.newCS("active"));
         
        return identRole;
}
 
public static String getCodingSchemeNameFromConceptListName(String groupName,String conceptListName,String conceptCode) throws ETSException 
{
        String codingSchemeName = null;
        try
        {
        ConceptList conceptList = mEtsService .getConceptList(groupName,conceptListName);
        Concept[] concept = conceptList.getConceptsByConceptCode(conceptCode);
        if(concept != null && concept.length > 0)
        {
        for(int count=0;count<concept.length;count++)
                        {
                        String returnedConceptCode = concept[count].getConceptCode();
                        CodingSchemeVersion codingScheVersion =concept[0].getCodingSchemeVersion();
                        CodingScheme codingScheme = codingScheVersion.getCodingScheme();
                        codingSchemeName = codingScheme.getName();
                        }
                    }
                                }
                                catch(ETSException e)
                                {
                    throw new ETSException("ETS_EXCEPTION");
                }
                                return codingSchemeName;
}
 
public static II getII(String root,String extension,BL displayable)throws ETSException
{
                II ii=null;
                UID uid=mDataTypeFactory.newUID(root);
                ST st=mDataTypeFactory.newST(extension);
                ii=mDataTypeFactory.newII(uid,st,displayable);
                return ii;
}

The following code sample creates and persists the person in domain-disabled mode; the person persisted is associated with one linking and one dedup context:

Person Creation in Domain Disabled Scenario

public void testCreatePersonsDomainDisabled() throws ETSException
{
    //Assign the II for Sally Andrews
    String rootForPerson = "9.989898.5.1";
    String extension = "PSN0003";
     
    person = persistPerson(rootForPerson , extension , null);
     
    personII = getPersonII(person);
    System.out.println("Person created with II " +personII);
    System.out.println("Person 1 created and persisted successfully");
     
    rootForPerson = "9.989898.5.1";
    extension = "PSN0004";
    person = persistPerson(rootForPerson , extension , null);
     
    System.out.println("Person created with II " +personII);
    System.out.println("Person 2 created and persisted successfully");
}

Example 8-3 Revise a Person Entity

This code sample illustrates the revision of an existing Person entity:

  • The Person entity is fetched by using the Internal Identifier for the person; the fetch mechanism used is a standard RIM Services fetch (Section 7.5, RIM Query, Fetches).

  • The address and the domain are updated.

  • The Person is persisted back.

In this scenario the address of the person is updated with a new value, and a new version of the fetched person is created by using the createNewVersion method. The new address is set on this new version of the person. The persistence mechanism for the updated person uses the standard RIM persistence mechanism illustrated in this code sample.

In the domain-enabled environment a revised person must have an associated domain role to get persisted. Accordingly, if the existing domain role is removed from a person being revised, a new domain role must be attached to the person. If no changes are made to the domain role, the existing domain role is copied and attached to the person; the revised person retains the old domain id. Note that the revised person must be associated with a single valid domain role. In the domain-disabled environment no domain role is required on the revised person.

While revising a person, a new master person role can be attached to the person by removing the existing master person role. If no master person role is attached, the existing master person role is copied and attached to the revised person. The person thus retains the old master person id. Note that the revised person must be associated with a single master person role.

The following code sample illustrates a domain-enabled scenario, where the person is revised with a new domain ID:

//Test method to revise an existing Person. Retrieving all type 2 roles during update
//Person Sally Andrews with Internal Identifier as 9.989898.5.1-PSN0001 is revised to reflect change in address
public void testRevisePerson() throws ETSException
{
 //Providing the II to retrieve the person
  
 String rootForPerson = "9.989898.5.1";
 String extension = "PSN0001";
  
 II ii = mDataTypeFactory.newII(rootForPerson,extension, true);
  
 //Creating an entity criteria object
 EntityAttributeCriteria entityCriteria = mQueryComponentFactory.newEntityAttributeCriteria();
  
 //Retrieve the person assuming the person already exists in the database
  
 entityCriteria.setClassCode(SearchOperator.EQUALS , EntityClass.PSN); //Set the criteria as entity class is 
        Person (PSN)
 entityCriteria.setId(SetSearchOperator.ANY,new II[] {ii});
 entityCriteria.setCurrentVersion(true);
  
 //Creating the entity fetch based on the criteria
 EntityFetch entityFetch = mQueryComponentFactory.newEntityFetch(entityCriteria);
  
 //Specifiying the attributes of the person to be retrieved
 entityFetch.retrieveId(true);
 entityFetch.retrieveAddr(true);
 entityFetch.retrieveName(true);
  
 //Creating the role fetch to retrieve roles owned by the person
 RoleFetch roleFetch = mQueryComponentFactory.newRoleFetch();
 roleFetch.retrieveId(true);
 roleFetch.retrieveClassCode(true);
  
 //Adding the roleFetch to the entityFetch to retrieve roles owned by the entity
 entityFetch.addOwnedPlayedRoleFetch(roleFetch);
  
 //Execute the query
 InfrastructureRoot fetchedEntities[] = executeQuery(entityFetch);
  
 //Throw ETSException if person cannot be fetched
 if(fetchedEntities == null || fetchedEntities.length == 0)
 {
        throw new ETSException("NULL_PERSON_RETURNED");
 }
        else
        {
                //Fetching the person to be updated
                Person personA = null; 
                {
                        for (int k = 0; k < fetchedEntities.length; k++)
                        {
                                personA = (Person) fetchedEntities[k];
                                if (personA != null)
                                { 
                                        //Revising Person
                                        //Updating person Address attribute 
                                        personA = (Person) personA.createNewVersion();
                                         
                                        //Create the address parts
                                         
                                        ADXP addrPart1 = mDataTypeFactory.newADXP("WA", 
                                         mDataTypeFactory.newCS("STA")); //Name of the State
                                        ADXP addrPart2 = mDataTypeFactory.newADXP("Seattle", 
                                         mDataTypeFactory.newCS("CTY")); //Name of the City
                                        ADXP addrPart3 = mDataTypeFactory.newADXP("98105", 
                                         mDataTypeFactory.newCS("ZIP"));  //Zip Code
                                        ADXP addrPart4 = mDataTypeFactory.newADXP("35 Elm Avenue", 
                                         mDataTypeFactory.newCS("SAL")); //Street Address Line
                                        ADXP addrPart5 = mDataTypeFactory.newADXP("US", 
                                         mDataTypeFactory.newCS("CNT")); //Name of the Country
                                         
                                        ADXP[] addrParts = new ADXP[]{addrPart1, addrPart2, addrPart3, 
                                         addrPart4, addrPart5};
                                         
                                        //Set the address as Home Primary Address
                                        AD addr = mDataTypeFactory.newAD(addrParts, 
                                         mDataTypeFactory.newSET<CS>(mDataTypeFactory.newCS("HP")), 
                                         mDataTypeFactory.nullGTS (CSImpl.nullCS(NullFlavorImpl.NI)));
                                         
                                        //Create an address bag 
                                        BAG<AD> addrBag = mDataTypeFactory.newBAG<AD> (addr);
                                         
                                        //Set the address bag on the person
                                        personA.setAddr(addrBag); 
                                         
                                        //Retrieving Type 2 roles and printing the role code
                                        Iterator iter = personA.getOwnedPlayedRoles();
                                        if (iter != null)
                                        {
                                                while (iter.hasNext())
                                                {
                                                        Role role = (Role) iter.next();
                                                        if (role != null)
                                                        {
                                                                CE roleCode = role.getCode()
                                                                if (roleCode != null)
                                                                {
                                                                        ST code = roleCode.code();
                                                                        if(code!=null)
                                                                        {
                                                                                String codeValue = 
                                                                                 code.stringValue();
                                                                                 
                                                                                // Remove the current domain and 
                                                                                associate the person to a new domain
                                                                                if("001896".equals(codeValue))
                                                                                {
                                                                                        iter.remove();
                                                                                        addDomainRole
                                                                                         (personA,"2147483646");
                                                                                        System.out.println("
                                                                                         The domain id is updated");
                                                                                }
                                                                                System.out.println("
                                                                                 Role Code is " +codeValue);
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                }
                                                //Persisting the updated person
                                                //Set the status code to active
                                                CS statusCode = mDataTypeFactory.newCS("active");
                                                 
                                                //Create an ident role
                                                Role identRole = personA.addPlayedRole(RoleClass.IDENT, 
                                                 null ,null,null);
                                                identRole.setStatusCode(statusCode);
                                                 
                                                //Create the control act
                                                ControlAct controlAct = 
                                                 mActFactory.newControlActEvent(null,null);
                                                controlAct.setStatusCode(statusCode);
                                                 
                                                //Creating the registry act
                                                Act act = mActFactory.newRegistryAct(ActMood.EVN, 
                                                 null , null);
                                                act.setStatusCode(statusCode);
                                                act.setTitle(mDataTypeFactory.newST
                                                 ("Person Persistence Registry Act"));
                                                 
                                                //Add the participation to the act
                                                act.addParticipation(ParticipationType.SBJ, identRole);
                                                 
                                                //create an act relationship between the control act and 
                                                 registry act
                                                ActRelationship ar = controlAct.addOBActRelationship
                                                 (ActRelationshipType.SUBJ,act);
                                                 
                                                //Submit the control act, which persists the person
                                                ControlAct returnedControlAct =
                                                 mRimService.submit(controlAct);
                                        }
                        }
        }
}
 
//Method to execute the query 
 
public static InfrastructureRoot[] executeQuery(CoreFetch coreFetch) throws ETSException
{
//Wrapping the fetch in a control act
ControlAct queryAct = wrapFetchInControlAct(coreFetch);
 
//Submitting the control act
ControlAct queryResponseCACT = mRimService.submit(queryAct);
 
return getFetchedResultsFromQueryResult(queryResponseCACT,coreFetch);
 
}
 
//Method to wrap the coreFetch in a control act
 
public static ControlAct wrapFetchInControlAct(CoreFetch coreFetch) throws ETSException
{
                QueryAct queryAct = mActFactory.newQueryAct(coreFetch);
                ControlAct queryCACT = mActFactory.newControlActEvent(null,null);
                 
                //Create an out-bound act relationship between the query act and the control act
                queryCACT.addOBActRelationship(ActRelationshipType.SUBJ,queryAct);
                return queryCACT;
}
 
//Method to fetch the results from QueryResult
 
public static InfrastructureRoot[] getFetchedResultsFromQueryResult(ControlAct controlAct, CoreFetch fetch) throws ETSException
{
        List queryResults = new ArrayList();
        try
        {
                // Navigate from the returned control act to the QueryAct
                // ControlAct -> OB act relationship -> QueryAct -- all are pseudo objects so are linked via empty 
                        fetches
                Iterator obControlActRelationships = 
                 controlAct.getOBActRelationships(mQueryComponentFactory.newActRelationshipFetch());
                QueryAct queryResultAct = null;
                while(obControlActRelationships.hasNext() && queryResultAct == null)
                {
                        ActRelationship currentActRelationship = (ActRelationship) 
                         obControlActRelationships.next();
                        Act targetAct = currentActRelationship.getTarget(mQueryComponentFactory.newActFetch());
                        if(targetAct instanceof QueryAct)
                        {
                                queryResultAct = (QueryAct) targetAct;
                        }
        }
 
// Navigate down each OB act relationship from the returned QueryAct to fetched objects. There is one OB relationship per fetched result
    
   Iterator obQueryActRelationships = 
        queryResultAct.getOBActRelationships(mQueryComponentFactory.newActRelationshipFetch());
   While(obQueryActRelationships != null && obQueryActRelationships.hasNext())
        {
                ActRelationship currentActRelationship = (ActRelationship) obQueryActRelationships.next();
                if(currentActRelationship != null)
                {
                        if(fetch instanceof ActFetch)
                        {
                                queryResults.add(getAct(currentActRelationship, (ActFetch)fetch));\
                        }
                                else if(fetch instanceof ActRelationshipFetch)
                        {
                                queryResults.add(getActRelationship(currentActRelationship, 
                                 (ActRelationshipFetch)fetch));
                        }
                        else if(fetch instanceof ParticipationFetch)
                        {
                                queryResults.add(getParticipation(currentActRelationship, 
                                 (ParticipationFetch)fetch));
                        }
                        else if(fetch instanceof RoleFetch)
                        {
                                queryResults.add(getRole(currentActRelationship, (RoleFetch)fetch));
                        }
                        else if(fetch instanceof EntityFetch)
                        {
                                queryResults.add(getEntity(currentActRelationship, (EntityFetch)fetch));
                        }
                }
        }
}
catch(Exception e)
{
        System.out.println("Error");
        e.printStackTrace();
}
return (InfrastructureRoot[]) queryResults.toArray(new InfrastructureRoot[queryResults.size()]);
}
 
private static Act getAct(ActRelationship actRelationship, ActFetch fetch) throws ETSException
{
        return actRelationship.getTarget(fetch);
}
 
private static ActRelationship getActRelationship(ActRelationship actRelationship, ActRelationshipFetch fetch) throws ETSException
 
        Act pseudoAct = getAct(actRelationship, mQueryComponentFactory.newActFetch());
        Iterator obActRelationships = pseudoAct.getOBActRelationships(fetch);
        return (ActRelationship) obActRelationships.next()
}
 
private static Participation getParticipation(ActRelationship actRelationship, ParticipationFetch fetch) throws ETSException
{
        Act pseudoAct = getAct(actRelationship, mQueryComponentFactory.newActFetch());
        Iterator participations = pseudoAct.getParticipations(fetch);
        return (Participation)participations.next();
}
 
private static Role getRole(ActRelationship actRelationship, RoleFetch fetch) throws ETSException
{
        Participation pseudoParticipation = getParticipation(actRelationship, 
         mQueryComponentFactory.newParticipationFetch());
        return pseudoParticipation.getRole(fetch);
}
 
private static Entity getEntity(ActRelationship actRelationship, EntityFetch fetch) throws ETSException
{
        Role pseudoRole = getRole(actRelationship, mQueryComponentFactory.newRoleFetch());
        return pseudoRole.getPlayerEntity(fetch);

Example 8-4 Persist Domains

The EmpiConfigurationService provides functionality to persist and query the Domain object and assigns a system-generated domain identifier.

Domains enable a logical partitioning of Person objects for easier data management. In a domain-enabled implementation, every Person must belong to a single domain, and every domain must belong to a single Linking Context and a single Deduplication Context. The Domain Name must be unique across all Domains.

private static void persistDomain() throws ETSException {
            String name1 = "EMPI_PUG_DOMAIN_1";
            String name2 = "EMPI_PUG_DOMAIN_2";
              
            domain1 = mEMF.newDomain(name1);
            domain2 = mEMF.newDomain(name2);
            //Persisting the 2 domains by calling the Empi configuration service 
            Domain[] createDomainArr = new Domain[] { domain1, domain2 };
            mConfigService.persistDomains(createDomainArr);
            //Querying the persisted domains to assign the domain ids
            DomainAttributeCriteriaImpl domainAttribCriteria = new DomainAttributeCriteriaImpl();
            domainAttribCriteria.setName(SearchOperator.LIKE, name1);
            DomainFetchImpl domainFetch = new DomainFetchImpl();
            domainFetch.retrieveDescription(true);
            domainFetch.retrieveName(true);
            domainFetch.setTopLevelDomainCriteria(domainAttribCriteria);
            Domain[] createdDomains = mConfigService.queryDomains(domainFetch);
            System.out.println("the no of domains being created are "
                  + createdDomains.length);
            domainId1 = createdDomains[0].getId();
            domainAttribCriteria = new DomainAttributeCriteriaImpl();
            domainAttribCriteria.setName(SearchOperator.LIKE, name2);
            domainFetch = new DomainFetchImpl();
            domainFetch.retrieveDescription(true);
            domainFetch.retrieveName(true);
            domainFetch.setTopLevelDomainCriteria(domainAttribCriteria);
            createdDomains = mConfigService.queryDomains(domainFetch);
            System.out.println("the no of domains being created are "
                  + createdDomains.length);
            domainId2 = createdDomains[0].getId();
            }

Example 8-5 Persist a Deduplication Context

The EmpiConfigurationService includes context setup for the deduplication context type.

A Deduplication Context must contain a single domain, and the context name must be unique across all contexts.

private static void persistDedupContext(String domainId, String contextName)
            throws ETSException {
          
            //create Dedup Context
            DedupContext dedupContext = mEMF.newDedupContext();
            if (domainId != null) {
                  //create Context Domain
                  ContextDomain conDomain = mEMF.newContextDomain(domainId);
 
                  //set Context Domain in DedupContext
                  dedupContext.setContextDomain(conDomain);
            }
       
            mConfigService.persistContexts(new Context[] { dedupContext });

Example 8-6 Persist a Linking Context

The EmpiConfigurationService includes context setup for linking context type.

Multiple domains are permitted for each context, but the same domain cannot be included in more than one context. Domain precedence is required to be set and must be unique if more than one domain is defined in a context. The context name must be unique across all contexts.

private static void persistLinkingContext(String[] domainIds)
            throws ETSException {
 
            // Linking context with match conflict code,upper threshold,lower
            // threshold,match conflict code,dominant person conflict code and
            // default operation flag
            LinkingContext linkContext = mEMF.newLinkingContext;
 
            //adding the linking context doamins to linking context domain array
            LinkingContextDomain[] linkingContextDomainArr = new LinkingContextDomain[domainIds.length];
            //Linking context domain with domain id and domain precedence       
            LinkingContextDomain linkingContextDomain = null;
            String domainPrecedence = null;
            for (int i = 0; i < domainIds.length; i++) {
                  domainPrecedence = new Integer(i).toString();
                  linkingContextDomain = mEMF.newLinkingContextDomain(domainIds[i],
                        domainPrecedence);
                  linkingContextDomainArr[i] = linkingContextDomain;
            }
 
            //set LinkingContext Domain in LinkingContext
            linkContext.setLinkingContextDomain(linkingContextDomainArr);
 
      
                  mConfigService.persistContexts(new Context[] { linkContext });
                  //Querying the context to retrieve the context id
                  Context[] createdContext = null;
 
                  //create Linking Context Attribute Criteria
                  LinkingContextAttributeCriteria linkSearchCriteria = mEMF
                        .newLinkingContextAttributeCriteria();
                  linkSearchCriteria.setName(SearchOperator.EQUALS, contextName);
                  
 
                  LinkingContextFetch contextFetchImpl = mEMF
                        .newLinkingContextFetch(linkSearchCriteria);
                  contextFetchImpl.retrieveName(true);
                  
                  LinkingContextDomainFetchImpl linkingContextDomainFetch = (LinkingContextDomainFetchImpl) 
                                        mEMF.newLinkingContextDomainFetch();
                  linkingContextDomainFetch.retrieveDomainId(true);
                  linkingContextDomainFetch.retrieveDomainPrecedence(true);
                  ((LinkingContextFetchImpl) contextFetchImpl)
                                        .addLinkingContextDomainFetch(linkingContextDomainFetch);
 
                  createdContext = mConfigService.queryContexts(contextFetchImpl);
 
                  //checking the values of created contexts
 
                  LinkingContext createdLinkContext = (LinkingContext) createdContext[0];
                  linkingContextId = createdLinkContext.getId();

Example 8-7 Use Reference Modifiers

This code sample illustrates how to use reference modifiers to update the domain id:

CREATE_OR_OVERLAY and OVERLAY

Using reference modifiers CREATE_OR_OVERLAY and OVERLAY in the domain-enabled environment, a person can be attached to a new domain id. If no domain role is attached the existing domain role is copied and attached to the person; the overlaid person retains the old domain id.

The master person id cannot be updated with these reference modifiers. Accordingly, the existing master person role is copied and attached to the overlaid person, with the overlaid person retaining the old master person id. The EMPI service thus copies the two type II roles (domain and master person roles) as appropriate in the domain-enabled environment, and only one type II role (master person role) in the domain-disabled environment.

public void testOverlayPerson() throws ETSException
        {
                        String rootForPerson = "9.989898.5.1";
                        String extension = "PSN0001";
                        SET_II setII = mDataTypeFactory.newSET_II(mDataTypeFactory.newII(rootForPerson , 
                         extension, true));
                         
                        // Make a reference using OVERLAY reference modifier and resubmit the same
                        Person person1 = (Person) mEntityFactory.makeReference
(                        oracle.hsgbu.hdr.hl7.factories.ReferenceModifier.OVERLAY , 
                         EntityClass.PSN, null, mDataTypeFactory.newCS("INSTANCE"), setII , 0);
                        person1.setStatusCode(mDataTypeFactory.newCS("active"));
                         
                        String domainId = "2147483644";
                        //Create a new domain role and attach it to the person
                        addDomainRole(person1 , domainId );
                         
                        String rootForIdent = "9.515151.5";
                        String extIdent = "111-222-333";
                         
                        // Create the ident role with the person as the player
                        Role identRole = getIdentRole(person1, rootForIdent,extIdent );
                         
                        CS statusCode = mDataTypeFactory.newCS("active");
                         
                        //Create the Registry Act and set the status code to active
                        Act registryAct = mActFactory.newRegistryAct(ActMood.EVN, null , null);
                        registryAct.setStatusCode(statusCode);
                        registryAct.setTitle(mDataTypeFactory.newST("Person Persistence Registry Act"));
                         
                        //Add the Participation to the ident role
                        registryAct.addParticipation(ParticipationType.SBJ, identRole);
                         
                        //Get the Control Act
                        ControlAct controlAct = mActFactory.newControlActEvent(null,null);
                        controlAct.setStatusCode(statusCode);
                         
                        //Create an Act Relationship between Control Act and Registry Act
                        ActRelationship ar = controlAct.addOBActRelationship(ActRelationshipType.SUBJ, 
                         registryAct);
                         
                        // Submit the Control Act and commit the data
                        ControlAct returnedControlAct = mRimService.submit(controlAct);
                         
                        System.out.println("Person overlaid successfully"); 
        }

MUST_EXIST

With MUST_EXIST as the reference modifier, neither the domain role nor the master person role can be altered. The person in the repository is undisturbed and the object graph remains unchanged.

CREATE_IF

With the reference modifier CREATE_IF the person object is created if it not persisted earlier. The master person id is automatically generated internally by the EMPI service—the master person role must not be supplied externally.

In the domain enabled environment the domain id must be provided externally, but in the domain-disabled environment it is not necessary.

The following code sample of illustrates using the CREATE_IF reference modifier in the domain enabled mode:

public void testCreateIfPerson() throws ETSException
        {
                        String rootForPerson = "9.989898.5.1";
                        String extension = "PSN0005";
                         
                        SET_II setII = mDataTypeFactory.newSET_II(mDataTypeFactory.newII(rootForPerson ,
                         extension, true));
                         
                        // Make a reference using OVERLAY reference modifier and resubmit the same
                        Person person1 = (Person) mEntityFactory.makeReference
                         (oracle.hsgbu.hdr.hl7.factories.ReferenceModifier.CREATE_IF, EntityClass.PSN, null,
                         mDataTypeFactory.newCS("INSTANCE"), setII , 0);
                        person1.setStatusCode(mDataTypeFactory.newCS("active"));
                         
                                String domainId = "2147483642";
                         
                        //Create a new domain role and attach it to the person
                        addDomainRole(person1 , domainId );
                         
                        String rootForIdent = "9.515151.5";
                        String extIdent = "111-222-333";
                         
                        //Create the ident role with the person as the player
                        Role identRole = getIdentRole(person1, rootForIdent,extIdent );
                         
                        CS statusCode = mDataTypeFactory.newCS("active");
                         
                        //Create the Registry Act and set the status code to active
                        Act registryAct = mActFactory.newRegistryAct(ActMood.EVN, null , null);
                        registryAct.setStatusCode(statusCode);
                        registryAct.setTitle(mDataTypeFactory.newST("Person Persistence Registry Act"));
                         
                        //Add the Participation to the ident role
                        registryAct.addParticipation(ParticipationType.SBJ, identRole);
                         
                        //Get the Control Act
                        ControlAct controlAct = mActFactory.newControlActEvent(null,null);
                        controlAct.setStatusCode(statusCode);
                         
                        //Create an Act Relationship between Control Act and Registry Act
                        ActRelationship ar = controlAct.addOBActRelationship(ActRelationshipType.SUBJ, registryAct);
                         
                        // Submit the Control Act and commit the data
                        ControlAct returnedControlAct = mRimService.submit(controlAct);
        }