Search In a MDM Repository

Following code snippet shows the step to search inside MDM repository. It searches the string inside field Vendors in the table Vendors under repository Vendor1 .

public class Connection {     private SimpleConnection simpleConnection;     private String serverName = " ";     private String repositoryName="Vendor1";     private String dbServerName = " ";     private DBMSType dbmsType = DBMSType.MS_SQL;     private RegionProperties dataRegion = new RegionProperties();     private String userSession;     private String repositorySession;     private String userName="Admin";     private String password="mdm";     private RepositorySchema schema;     private RepositoryIdentifier repositoryID;     public void getConnection() {         try {             if (simpleConnection == null) {                 simpleConnection =                     SimpleConnectionFactory.getInstance(serverName);             }         } catch (Exception e) {             //              }     }         public void closeConnection( )       {         //@@begin closeConnection()         try{             if(simpleConnection!=null){                 DestroySessionCommand destroySessionCommand=new DestroySessionCommand(simpleConnection);                 destroySessionCommand.setSession(userSession);                 destroySessionCommand.execute();                 userSession=null;                 simpleConnection.close();             }         }catch(Exception e){
//                    }         //@@end       }           public void getAuthenticatedUserSession( )       {         //@@begin getAuthenticatedUserSession()         /*             * Create and authenticate a new user session to an MDM repository             */            repositoryID=new RepositoryIdentifier(repositoryName,dbServerName,dbmsType);            CreateUserSessionCommand createUserSessionCommand=new CreateUserSessionCommand(simpleConnection);            createUserSessionCommand.setRepositoryIdentifier(repositoryID);            createUserSessionCommand.setDataRegion(dataRegion);            try {                createUserSessionCommand.execute();                    userSession=createUserSessionCommand.getUserSession();// Get the session identifier               /* Authenticate User Session */            TrustedUserSessionCommand trustedUserSessionCommand=new TrustedUserSessionCommand(simpleConnection);            trustedUserSessionCommand.setUserName(userName);                      trustedUserSessionCommand.setSession(userSession);            try {                trustedUserSessionCommand.execute();                userSession=trustedUserSessionCommand.getSession();            } catch (CommandException e1) {                /* Trusted Connection is not accepted */                AuthenticateUserSessionCommand authenticateUserSessionCommand=new AuthenticateUserSessionCommand(simpleConnection);                authenticateUserSessionCommand.setSession(userSession);                authenticateUserSessionCommand.setUserName(userName);                authenticateUserSessionCommand.setUserPassword(password);                authenticateUserSessionCommand.execute();                    }                   SetUnicodeNormalizationCommand unicodeNormalizationCommand=new SetUnicodeNormalizationCommand(simpleConnection);            unicodeNormalizationCommand.setSession(userSession);            unicodeNormalizationCommand.setNormalizationType(SetUnicodeNormalizationCommand.NORMALIZATION_COMPOSED);            unicodeNormalizationCommand.execute();            }catch (CommandException e2) {
//                         }       }           public void setRegionProperties( )       {         //@@begin setRegionProperties()         dataRegion.setRegionCode("engUSA");         // Set the locale on data region         dataRegion.setLocale(new Locale("en", "US"));         // Set the name of data region         dataRegion.setName("US");         //@@end       }     public void setRepositoryInfo( java.lang.String repositoryName, java.lang.String userID, java.lang.String password )      {        //@@begin setRepositoryInfo()        this.repositoryName=repositoryName;        this.userName=userID;        this.password=password;            //@@end      }           public void getAuthenticatedRepositorySession( )     {       //@@begin getAuthenticatedRepositorySession()           try{               CreateRepositorySessionCommand repositorySessionCommand=new CreateRepositorySessionCommand(simpleConnection);               repositorySessionCommand.setRepositoryIdentifier(repositoryID);               repositorySessionCommand.execute();               repositorySession=repositorySessionCommand.getRepositorySession();                       AuthenticateRepositorySessionCommand authenticatedRepositorySession=new AuthenticateRepositorySessionCommand(simpleConnection);               authenticatedRepositorySession.setSession(repositorySession);               authenticatedRepositorySession.setUserName(userName);               authenticatedRepositorySession.setUserPassword(password);               authenticatedRepositorySession.execute();                       GetRepositorySchemaCommand repositroySchemaCommand=new GetRepositorySchemaCommand(simpleConnection);               repositroySchemaCommand.setSession(repositorySession);               repositroySchemaCommand.execute();               schema=repositroySchemaCommand.getRepositorySchema();                           }catch(CommandException e){
//                        }catch(Exception e){
//                        }               //@@end     }         public com.sap.mdm.ids.FieldId getFieldID( java.lang.String tableName, java.lang.String fieldCode )       {         //@@begin getFieldID()             return schema.getField(tableName,fieldCode).getId();             //@@end       }           public com.sap.mdm.ids.TableId getTableID( java.lang.String tableName )       {         //@@begin getTableID()         return schema.getTable(tableName).getId();         //@@end       }           public void searchRepository() {             //@@begin searchRepository()             ResultDefinition resultDef =                 new ResultDefinition(schema.getTable("Vendors").getId());             resultDef.addSelectField(schema.getField("Vendors", "Name").getId());             //resultDef.addSelectField(new FieldId(15));             FieldSearchDimension searchDimension =                 new FieldSearchDimension(                     schema.getField("Vendors", "Name").getId());             TextSearchConstraint constraint =                 new TextSearchConstraint("ABC", TextSearchConstraint.CONTAINS);             Search search = new Search(schema.getTable("Vendors").getId());             //search.addSearchItem(searchDimension,constraint);             RetrieveLimitedRecordsCommand limitedRecordsCommand =                 new RetrieveLimitedRecordsCommand(simpleConnection);             limitedRecordsCommand.setSearch(search);             limitedRecordsCommand.setSession(userSession);             limitedRecordsCommand.setResultDefinition(resultDef);             try {                 limitedRecordsCommand.execute();             } catch (CommandException e) {                 //                          }             //return limitedRecordsCommand.getRecords();             RecordResultSet recordSet = limitedRecordsCommand.getRecords();             String recordAsString = null;             String fieldValue = null;             for (int i = 0; i < recordSet.getCount(); i++) {                 recordAsString = "";                 FieldId[] returnedFields = recordSet.getRecord(i).getFields();                 for (int j = 0; j < returnedFields.length; j++) {                     if (recordSet                         .getRecord(i)                         .getFieldValue(returnedFields[j])                         .isNull()) {                         fieldValue = " |";                     } else {                         fieldValue =                             recordSet.getRecord(i).getFieldValue(returnedFields[j])                                 + "|";                     }                                         recordAsString += fieldValue;                     System.out.println(recordAsString);                 }             }         }                 public static void main (String[] args)                 {             Connection test=new Connection();             test.getConnection();             test.setRegionProperties();             test.setRepositoryInfo("Vendor1","Admin","mdm");             test.getAuthenticatedUserSession();             test.getAuthenticatedRepositorySession();             test.searchRepository();             test.closeconnection();              }

}

SAP Developer Network Latest Updates