Access Keys:
Skip to content (Access Key - 0)

Step 4: Implement the getIdentifier Method


At this point, we have added the addPortfolioSymbols operation and saved the service. When we saved the service, Introduce generated all the required code for that operation to act as a grid service method. However, we now need to add our actual logic for the getIdentifier method.

Introduce generates a stubbed implementation of the service for the user to edit. Because our operation was added to the StockPortfolioManager of our service, the code that we need to edit is the class org.cagrid.introduce.tutorial.stockmanager.portfolio.service.StockPortfolioManagerImpl. This is where we will see a getIdentifier method that is not yet implemented. In this section, we will implement this method.

Using Eclipse to edit the service

Introduce provides Eclipse project files for the generated service. You can simply Import the project into Eclipse (if you have not already done so) by using the File --> Import --> Import Existing Project options in Eclipse and browsing to the generated service directory. This will make finding and modifying the service much easier.

Edit the Service Implementation

  • In a text editor or Eclipse, open the following file for editing:
    • <StockManager>/src/org/cagrid/introduce/tutorial/stockmanager/portfolio/service/StockPortfolioManagerImpl.java
  • Add the following imports:
    import gov.nih.nci.cagrid.identifiers.client.IdentifiersNAServiceClient;
    import namingauthority.IdentifierData;
    import namingauthority.KeyData;
    import namingauthority.KeyNameData;
    import org.apache.axis.types.URI;
    import org.cagrid.introduce.tutorial.tools.beans.Portfolio;
    import org.cagrid.introduce.tutorial.tools.beans.Quote;
    import org.cagrid.introduce.tutorial.tools.beans.Quotes;
    import org.cagrid.introduce.tutorial.tools.beans.Symbols;
    import org.apache.axis.types.URI.MalformedURIException;
    
  • Locate the getIdentifier method.
  • Replace:
    //TODO: Implement this autogenerated method
    throw new RemoteException("Not yet implemented");
    
    
  • With:
    	
    		//get the portfolio data
    		  org.cagrid.introduce.tutorial.stockmanager.portfolio.service.globus.resource.StockPortfolioManagerResource resource = null;
    		try {
    		  //get the currently addressed resource
    		  resource = getResourceHome().getAddressedResource();
    		  } catch (Exception e1) {
    			  throw new RemoteException("Unable to locate resource: " + e1.getMessage());
    			    }
    
    		  //get that resource instance's portfolio
    		  Portfolio port = resource.getPortfolio();
    		  //get NA service URL
    		  String identifiersNAServiceURL = "https://identifiers-na.training.citih.osumc.edu:8443/wsrf/services/cagrid/IdentifiersNAService"; 
    		  
    try{
    		  Map<String,String> symbolQuoteMap = getSymbolQuoteMap(port);
    		   Set<String> keySet = symbolQuoteMap.keySet();
    
    		   String[] symbols = keySet.toArray(new String[keySet.size()]);
    		   String[] quotes = new String[ keySet.size() ];
    
    		   for (int i = 0; i<keySet.size(); i++) {
    		      quotes[i] = symbolQuoteMap.get(symbols[i]);
    		   }
    
    		   String[] keys = new String[] { "SYMBOLS", "QUOTES" };
    		   String[][] values = new String[][]{ symbols, quotes };
    
    		   KeyNameData[] kvs = new KeyNameData[ keys.length ];
    		   for( int i=0; i < keys.length; i++) {
    		        KeyData kd = new KeyData();
    		        kd.setValue(values[i]);
    		        kvs[i] = new KeyNameData(kd, keys[i]);
    		   }
    		     
    		   IdentifierData id = new IdentifierData(kvs);
    		   IdentifiersNAServiceClient client =
    		      new IdentifiersNAServiceClient(identifiersNAServiceURL);
    		   URI identifier = client.createIdentifier(id);
    		   String identifierString = identifier.toString();
    		   return identifierString;
    	  }
    		catch (MalformedURIException e) {
    			throw new RemoteException(e.getMessage());
    	  }
      }
    
    
    
    
    	private Map<String,String> getSymbolQuoteMap(Portfolio port) {
    		//return a generated portfolio instance
    			 Symbols symbols = port.getSymbols();
    			 String[] symbolsArray = symbols.getSymbol();
    			 
    			 org.cagrid.introduce.tutorial.tools.beans.PortfolioInstance portfolioInstance = org.cagrid.introduce.tutorial.tools.PortfolioManager.generatePortfolioInstance(port);
    			 
    	Quotes quotes = portfolioInstance.getQuotes();
    	Quote[] quoteArray = quotes.getQuote();
    			 
    	Map<String, String> symbolQuoteMap = new HashMap();
    			 
    	for (int i = 0; i < symbolsArray.length; i++) {
    				 symbolQuoteMap.put(symbolsArray[i],quoteArray[i].getQuote());
    			 }
    			 return symbolQuoteMap;
    	  }
    
      }
    
  • Save the file.
    This tutorial uses the Naming Authority and PURL services that are deployed on the training grid.

Validate the implementation

  • Open a terminal, change directory to the generated service, and run ant all.

% cd <generated service location>
% ant all

  • If ant does not return a BUILD SUCCESSFUL, make sure you do not have a typo in the implementation file and make sure that you have correctly placed the supplied jar file in the lib directory.
Last edited by
Sarah Honacki (414 days ago)
Adaptavist Theme Builder Powered by Atlassian Confluence