SCOUT-80 simplifying some more code, and potential NPE.

git-svn-id: https://svn.apache.org/repos/asf/webservices/scout/branches/v1.1/scout@797308 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/ws/scout/registry/BusinessQueryManagerImpl.java b/src/main/java/org/apache/ws/scout/registry/BusinessQueryManagerImpl.java
index 07de1f1..17b3868 100644
--- a/src/main/java/org/apache/ws/scout/registry/BusinessQueryManagerImpl.java
+++ b/src/main/java/org/apache/ws/scout/registry/BusinessQueryManagerImpl.java
@@ -138,28 +138,18 @@
             

             BusinessInfo[] bizInfoArr =null;

             BusinessInfos bizInfos = result.getBusinessInfos();

+            LinkedHashSet<Organization> orgs = new LinkedHashSet<Organization>();

             if(bizInfos != null)

             {

             	List<BusinessInfo> bizInfoList = bizInfos.getBusinessInfo();

+            	for (BusinessInfo businessInfo : bizInfoList) {

+                    //Now get the details on the individual biz

+                    BusinessDetail detail = registry.getBusinessDetail(businessInfo.getBusinessKey());

+                    orgs.add(registryService.getLifeCycleManagerImpl().createOrganization(detail));

+				}

             	bizInfoArr = new BusinessInfo[bizInfoList.size()];

             	bizInfoList.toArray(bizInfoArr);

             }

-            

-            LinkedHashSet<Organization> orgs = null;

-            int len = 0;

-            if (bizInfoArr != null)

-            {

-                len = bizInfoArr.length;

-                orgs = new LinkedHashSet<Organization>();

-            }

-            for (int i = 0; i < len; i++)

-            {

-                BusinessInfo info = bizInfoArr[i];

-                //Now get the details on the individual biz

-                BusinessDetail detail = registry.getBusinessDetail(info.getBusinessKey());

-

-                orgs.add(registryService.getLifeCycleManagerImpl().createOrganization(detail));

-            }

             return new BulkResponseImpl(orgs);

         } catch (RegistryException e)

         {

@@ -181,19 +171,8 @@
             PublisherAssertions result =

                     registry.getPublisherAssertions(auth.getAuthInfo());

             List<PublisherAssertion> publisherAssertionList = result.getPublisherAssertion();

-            PublisherAssertion[] publisherAssertionArr = new PublisherAssertion[publisherAssertionList.size()];

-            publisherAssertionList.toArray(publisherAssertionArr);

-            

-            LinkedHashSet<Association> col = null;

-            int len = 0;

-            if (publisherAssertionArr != null)

-            {

-                len = publisherAssertionArr.length;

-                col = new LinkedHashSet<Association>();

-            }

-            for (int i = 0; i < len; i++)

-            {

-                PublisherAssertion pas = publisherAssertionArr[i];

+            LinkedHashSet<Association> col = new LinkedHashSet<Association>();

+            for (PublisherAssertion pas : publisherAssertionList) {

                 String sourceKey = pas.getFromKey();

                 String targetKey = pas.getToKey();

                 Collection<Key> orgcol = new ArrayList<Key>();

@@ -239,7 +218,7 @@
             else

               if(!caller  && other  )

                         confirm = Constants.COMPLETION_STATUS_FROMKEY_INCOMPLETE;

-           else

+            else

                  if(caller  && !other   )

                         confirm = Constants.COMPLETION_STATUS_TOKEY_INCOMPLETE;

 

@@ -247,19 +226,8 @@
             

             

             List<AssertionStatusItem> assertionStatusItemList = report.getAssertionStatusItem();

-            AssertionStatusItem[] assertionStatusItemArr = new AssertionStatusItem[assertionStatusItemList.size()];

-            assertionStatusItemList.toArray(assertionStatusItemArr);

-            

-            LinkedHashSet<Association> col = null;

-            int len = 0;

-            if (assertionStatusItemArr != null)

-            {

-                len = assertionStatusItemArr.length;

-                col = new LinkedHashSet<Association>();

-            }

-            for (int i = 0; i < len; i++)

-            {

-                AssertionStatusItem asi = assertionStatusItemArr[i];

+            LinkedHashSet<Association> col = new LinkedHashSet<Association>();

+            for (AssertionStatusItem asi : assertionStatusItemList) {

                 String sourceKey = asi.getFromKey();

                 String targetKey = asi.getToKey();

                 Collection<Key> orgcol = new ArrayList<Key>();

diff --git a/src/main/java/org/apache/ws/scout/util/ScoutUddiJaxrHelper.java b/src/main/java/org/apache/ws/scout/util/ScoutUddiJaxrHelper.java
index 08d2e84..0d214e6 100644
--- a/src/main/java/org/apache/ws/scout/util/ScoutUddiJaxrHelper.java
+++ b/src/main/java/org/apache/ws/scout/util/ScoutUddiJaxrHelper.java
@@ -112,23 +112,16 @@
            throws JAXRException
    {
       List<Name> namesList = businessEntity.getName();
-
-      Name[] namearray = new Name[namesList.size()];
-      namesList.toArray(namearray);
-      
-      Name n = namearray != null && namearray.length > 0 ? namearray[0] : null;
-      String name = n != null ? n.getValue() : null;
-      
+      Name n = null;
+      if (namesList.size()>0) n = namesList.get(0);
       
       List<Description> descriptionList = businessEntity.getDescription();
-      Description[] descarray = new Description[descriptionList.size()];
-      descriptionList.toArray(descarray);
-      
-      Description desc = descarray != null && descarray.length > 0 ? descarray[0]: null;
+      Description desc =null;
+      if (descriptionList.size()>0) desc = descriptionList.get(0);
 
       Organization org = new OrganizationImpl(lifeCycleManager);
-      if(name != null ) {
-          org.setName(getIString(n.getLang(), name, lifeCycleManager));
+      if(n != null ) {
+          org.setName(getIString(n.getLang(), n.getValue(), lifeCycleManager));
       }
       if( desc != null) {
           org.setDescription(getIString(desc.getLang(), desc.getValue(), lifeCycleManager));
@@ -139,15 +132,10 @@
       BusinessServices services = businessEntity.getBusinessServices();
       if(services != null)
       {
-          List<BusinessService> bizServiceList = services.getBusinessService();
-          BusinessService[] sarr = new BusinessService[bizServiceList.size()];
-          bizServiceList.toArray(sarr);
-          
-          for (int i = 0; sarr != null && i < sarr.length; i++)
-          {
-             BusinessService s = (BusinessService)sarr[i];
-             org.addService(getService(s, lifeCycleManager));
-          } 
+    	  List<BusinessService> bizServiceList = services.getBusinessService();
+    	  for (BusinessService businessService : bizServiceList) {
+    		  org.addService(getService(businessService, lifeCycleManager));
+    	  }
       }
 
       /*
@@ -164,40 +152,32 @@
       if(contacts != null)
       {
     	  List<Contact> contactList = contacts.getContact();
-    	  Contact[] carr = new Contact[contactList.size()];
-    	  contactList.toArray(carr);
-    	  for (int i = 0; carr != null && i < carr.length; i++)
-          {
-             Contact contact = (Contact)carr[i];
-             User user = new UserImpl(null);
-             String pname = contact.getPersonName();
-             user.setPersonName(new PersonNameImpl(pname));
-             if (i == 0)
-             {
-                org.setPrimaryContact(user);
-             }
-             else
-             {
-                org.addUser(user);
-             }
-          }
+    	  if (contactList!=null) {
+    		  boolean isFirst=true;
+    		  for (Contact contact : contactList) {
+    			  User user = new UserImpl(null);
+    			  String pname = contact.getPersonName();
+    			  user.setPersonName(new PersonNameImpl(pname));
+    			  if (isFirst) {
+    				  isFirst=false;
+    				  org.setPrimaryContact(user);
+    			  } else {
+    				  org.addUser(user);
+    			  }
+			}
+    	  }
       }
        
       //External Links
       DiscoveryURLs durls = businessEntity.getDiscoveryURLs();
       if (durls != null)
       {
-    	 List<DiscoveryURL> discoveryURL_List = durls.getDiscoveryURL();
-         DiscoveryURL[] darr = new DiscoveryURL[discoveryURL_List.size()];
-         discoveryURL_List.toArray(darr);
-         
-         for (int j = 0; darr != null && j < darr.length; j++)
-         {
-            DiscoveryURL durl = (DiscoveryURL)darr[j];
-            ExternalLink link = new ExternalLinkImpl(lifeCycleManager);
-            link.setExternalURI(durl.getValue());
-            org.addExternalLink(link);
-         }
+    	  List<DiscoveryURL> discoveryURL_List = durls.getDiscoveryURL();
+    	  for (DiscoveryURL discoveryURL : discoveryURL_List) {
+    		  ExternalLink link = new ExternalLinkImpl(lifeCycleManager);
+    		  link.setExternalURI(discoveryURL.getValue());
+    		  org.addExternalLink(link);
+    	  }
       }
 
       org.addExternalIdentifiers(getExternalIdentifiers(businessEntity.getIdentifierBag(), lifeCycleManager));
@@ -216,22 +196,16 @@
       bizEntityList.toArray(bz);
 
       BusinessEntity entity = bz[0];
-      List<Name> nameList = entity.getName();
-      Name[] namearr = new Name[nameList.size()];
-      nameList.toArray(namearr);
-      
-      Name n = namearr != null && namearr.length > 0 ? namearr[0] : null;
-      String name = n != null ? n.getValue(): null;
+      Name n = null;
+      if (entity.getName().size()>0) n = entity.getName().get(0);
       
       List<Description> descriptionList = entity.getDescription();
-      Description[] descarr = new Description[descriptionList.size()];
-      descriptionList.toArray(descarr);
-      
-      Description desc = descarr != null && descarr.length > 0 ? descarr[0] : null;
+      Description desc =null;
+      if (descriptionList.size()>0) desc = descriptionList.get(0);
 
       Organization org = new OrganizationImpl(lifeCycleManager);
-      if( name != null ) {
-          org.setName(getIString(n.getLang(), name, lifeCycleManager));
+      if( n != null ) {
+          org.setName(getIString(n.getLang(), n.getValue(), lifeCycleManager));
       }
       if( desc != null ) {
           org.setDescription(getIString(desc.getLang(), desc.getValue(), lifeCycleManager));
@@ -240,17 +214,10 @@
 
       //Set Services also
       BusinessServices services = entity.getBusinessServices();
-      
       List<BusinessService> bizServiceList = services.getBusinessService();
-      BusinessService[] sarr = new BusinessService[bizServiceList.size()];
-      bizServiceList.toArray(sarr);
-      
-      for (int i = 0; sarr != null && i < sarr.length; i++)
-      {
-         BusinessService s = (BusinessService)sarr[i];
-         org.addService(getService(s, lifeCycleManager));
-         
-      }
+      for (BusinessService businessService : bizServiceList) {
+    	  org.addService(getService(businessService, lifeCycleManager));
+	  }
 
       /*
        *  Users
@@ -263,62 +230,46 @@
        */
       Contacts contacts = entity.getContacts();
       List<Contact> contactList = contacts.getContact();
-      Contact[] carr = new Contact[contactList.size()];
-      contactList.toArray(carr);
-      
-      for (int i = 0; carr != null && i < carr.length; i++)
-      {
-         Contact contact = carr[i];
+      for (Contact contact : contactList) {
          User user = new UserImpl(null);
          String pname = contact.getPersonName();
          user.setType(contact.getUseType());
          user.setPersonName(new PersonNameImpl(pname));
          
          List<Email> emailList = contact.getEmail();
-         Email[] emails = new Email[emailList.size()];
-         emailList.toArray(emails);
-         
          ArrayList<EmailAddress> tempEmails = new ArrayList<EmailAddress>();
-         for (int x = 0; x < emails.length; x++) {
-        	 tempEmails.add(new EmailAddressImpl(emails[x].getValue(), null));
-         }
+         for (Email email : emailList) {
+        	 tempEmails.add(new EmailAddressImpl(email.getValue(), null));
+		 }
          user.setEmailAddresses(tempEmails);
          
          List<Address> addressList = contact.getAddress();
-         Address[] addresses = new Address[addressList.size()];
-         addressList.toArray(addresses);
-         
          ArrayList<PostalAddress> tempAddresses = new ArrayList<PostalAddress>();
-         for (int x = 0; x < addresses.length; x++) {
-        	 ArrayList<AddressLine> addressLineList = new ArrayList<AddressLine>(addresses[x].getAddressLine());
+         for (Address address : addressList) {
+        	 ArrayList<AddressLine> addressLineList = new ArrayList<AddressLine>(address.getAddressLine());
         	 AddressLine[] alines = new AddressLine[addressLineList.size()];
         	 addressLineList.toArray(alines);
         	 
         	 PostalAddress pa = getPostalAddress(alines);
         	 tempAddresses.add(pa);
-         }
+		 }
          user.setPostalAddresses(tempAddresses);
          
          List<Phone> phoneList = contact.getPhone();
-         Phone[] phones = new Phone[phoneList.size()];
-         phoneList.toArray(phones);
-         
          ArrayList<TelephoneNumber> tempPhones = new ArrayList<TelephoneNumber>();
-         for (int x = 0; x < phones.length; x++) {
+         boolean isFirst=true;
+         for (Phone phone : phoneList) {
         	 TelephoneNumberImpl tni = new TelephoneNumberImpl();
-        	 tni.setType(phones[x].getUseType());
-        	 tni.setNumber(phones[x].getValue());
+        	 tni.setType(phone.getUseType());
+        	 tni.setNumber(phone.getValue());
         	 tempPhones.add(tni);
-         }
-         user.setTelephoneNumbers(tempPhones);
-         
-         if (i == 0)
-         {
-            org.setPrimaryContact(user);
-         }
-         else
-         {
-            org.addUser(user);
+	         user.setTelephoneNumbers(tempPhones);
+	         if (isFirst) {
+	        	 isFirst=false;
+	            org.setPrimaryContact(user);
+	         } else {
+	            org.addUser(user);
+	         }
          }
       }
 
@@ -327,14 +278,9 @@
       if (durls != null)
       {
     	 List<DiscoveryURL> discoveryURL_List = durls.getDiscoveryURL();
-         DiscoveryURL[] darr = new DiscoveryURL[discoveryURL_List.size()];
-         discoveryURL_List.toArray(darr);
-         
-         for (int j = 0; darr != null && j < darr.length; j++)
-         {
-            DiscoveryURL durl = darr[j];
+         for (DiscoveryURL discoveryURL : discoveryURL_List) {
             ExternalLink link = new ExternalLinkImpl(lifeCycleManager);
-            link.setExternalURI(durl.getValue());
+            link.setExternalURI(discoveryURL.getValue());
             org.addExternalLink(link);
          }
       }
@@ -382,7 +328,11 @@
    private static InternationalString getIString(String lang, String str, LifeCycleManager lifeCycleManager)
        throws JAXRException
    {
-       return lifeCycleManager.createInternationalString(getLocale(lang), str);
+	   if (str!=null) {
+		   return lifeCycleManager.createInternationalString(getLocale(lang), str);
+	   } else {
+		   return null;
+	   }
    }
    
    public static InternationalString getIString(String str, LifeCycleManager lifeCycleManager)
@@ -403,17 +353,16 @@
          serve.setKey(lifeCycleManager.createKey(keystr));
       }
 
-      Name[] namearr = getNameArray(businessService.getName());
-
-      Name n = namearr != null && namearr.length > 0 ? namearr[0] : null;
+      Name n = null;
+      if (businessService.getName().size()>0) n = businessService.getName().get(0);
 
       if (n != null) {
     	  String name = n.getValue();
     	  serve.setName(lifeCycleManager.createInternationalString(getLocale(n.getLang()), name));
       }
 
-      Description[] descarr = getDescriptionArray(businessService.getDescription());
-      Description desc = descarr != null && descarr.length > 0 ? descarr[0] : null;
+      Description desc =null;
+      if (businessService.getDescription().size()>0) desc = businessService.getDescription().get(0);
       if (desc != null ) {
           serve.setDescription(lifeCycleManager.createInternationalString(getLocale(desc.getLang()), desc.getValue()));
       }
@@ -421,17 +370,9 @@
       //Populate the ServiceBindings for this Service
       BindingTemplates bts = businessService.getBindingTemplates();
       List<BindingTemplate> bindingTemplateList = bts.getBindingTemplate();
-      
-      BindingTemplate[] btarr = bts != null ? new BindingTemplate[bindingTemplateList.size()] : null;
-      if(btarr != null)
-    	  bindingTemplateList.toArray(btarr);
-      
-      for (int i = 0; btarr != null && i < btarr.length; i++)
-      {
-    	  BindingTemplate bindingTemplate = (BindingTemplate)btarr[i];
-          serve.addServiceBinding(getServiceBinding(bindingTemplate, lifeCycleManager));
-      }
-      
+      for (BindingTemplate bindingTemplate : bindingTemplateList) {
+    	  serve.addServiceBinding(getServiceBinding(bindingTemplate, lifeCycleManager));
+	  }
       serve.addClassifications(getClassifications(businessService.getCategoryBag(), lifeCycleManager));
       
       return serve;
@@ -449,14 +390,12 @@
          service.setKey(lifeCycleManager.createKey(keystr));
       }
 
-      Name[] namearr = getNameArray(serviceInfo.getName());
-      Name n = namearr != null && namearr.length > 0 ? namearr[0] : null;
-
+      Name n = null;
+      if (serviceInfo.getName().size()>0) n = serviceInfo.getName().get(0);
       if (n != null) {
     	  String name = n.getValue();
     	  service.setName(lifeCycleManager.createInternationalString(getLocale(n.getLang()), name));
       }
-
       return service;
    }
 
@@ -480,11 +419,10 @@
       if (access != null) serviceBinding.setAccessURI(access.getValue());
 
       //Description
-      Description[] da = getDescriptionArray(businessTemplate.getDescription());
-      if (da != null && da.length > 0)
-      {
-         Description des = da[0];
-         serviceBinding.setDescription(new InternationalStringImpl(des.getValue()));
+      Description desc = null;
+      if (businessTemplate.getDescription().size()>0) desc = businessTemplate.getDescription().get(0);
+      if (desc!=null) {
+         serviceBinding.setDescription(new InternationalStringImpl(desc.getValue()));
       }
       /**Section D.10 of JAXR 1.0 Specification */
       
@@ -529,13 +467,7 @@
    {
       Concept concept = new ConceptImpl(lifeCycleManager);
       List<TModel> tmodelList = tModelDetail.getTModel();
-      
-      TModel[] tc = new TModel[tmodelList.size()];
-      tmodelList.toArray(tc);
-      
-      TModel tmodel = tc != null && tc.length > 0 ? tc[0] : null;
-      
-      if (tmodel != null) {
+      for (TModel tmodel : tmodelList) {
     	  concept.setKey(lifeCycleManager.createKey(tmodel.getTModelKey()));
     	  concept.setName(lifeCycleManager.createInternationalString(getLocale(tmodel.getName().getLang()),
     			  tmodel.getName().getValue()));
@@ -585,9 +517,9 @@
 
    private static Description getDescription( TModel tmodel )
    {
-      Description[] descarr = getDescriptionArray(tmodel.getDescription());
-      Description desc = descarr != null && descarr.length > 0 ? descarr[0] : null;
-      return desc;
+	   Description desc = null;
+	   if (tmodel.getDescription().size()>0) desc=tmodel.getDescription().get(0);
+       return desc;
    }
 
    /**
@@ -603,19 +535,12 @@
 	   Collection<Classification> classifications = null;
 	   if (categoryBag != null) {
 		    classifications = new ArrayList<Classification>();
-		    
 		    List<KeyedReference> keyedReferenceList = categoryBag.getKeyedReference();
-			KeyedReference[] keyrarr = new KeyedReference[keyedReferenceList.size()];
-			keyedReferenceList.toArray(keyrarr);
-			
-			for (int i = 0; keyrarr != null && i < keyrarr.length; i++)
-			{
-				KeyedReference keyr = (KeyedReference)keyrarr[i];
+		    for (KeyedReference keyedReference : keyedReferenceList) {
 				Classification classification = new ClassificationImpl(lifeCycleManager);
-				classification.setValue(keyr.getKeyValue());
-				classification.setName(new InternationalStringImpl(keyr.getKeyName()));
-				 
-				String tmodelKey = keyr.getTModelKey();
+				classification.setValue(keyedReference.getKeyValue());
+				classification.setName(new InternationalStringImpl(keyedReference.getKeyName()));
+				String tmodelKey = keyedReference.getTModelKey();
 				if (tmodelKey != null) {
 					ClassificationScheme scheme = new ClassificationSchemeImpl(lifeCycleManager);
 					scheme.setKey(new KeyImpl(tmodelKey));
@@ -633,12 +558,11 @@
        ArrayList<ExternalLink> alist = new ArrayList<ExternalLink>(1);
        if(overviewDoc != null)
        {
-           Description[] descVect = getDescriptionArray(overviewDoc.getDescription());
-           String desc = "";
-           if(descVect != null && descVect.length > 0) {
-             desc = ((Description)descVect[0]).getValue(); 
-           }
-           alist.add(lifeCycleManager.createExternalLink(overviewDoc.getOverviewURL(),desc));
+    	   String descStr = "";
+    	   Description desc = null;
+    	   if (overviewDoc.getDescription().size()>0) desc = overviewDoc.getDescription().get(0);
+           if (desc !=null) descStr = desc.getValue();
+           alist.add(lifeCycleManager.createExternalLink(overviewDoc.getOverviewURL(),descStr));
        }
        
        return alist;
@@ -659,17 +583,12 @@
     	  extidentifiers = new ArrayList<ExternalIdentifier>();
     	  
     	  List<KeyedReference> keyedReferenceList = identifierBag.getKeyedReference();
-          KeyedReference[] keyrarr = new KeyedReference[keyedReferenceList.size()];
-          keyedReferenceList.toArray(keyrarr);
-          
-          for (int i = 0; keyrarr != null && i < keyrarr.length; i++)
-          {
-             KeyedReference keyr = (KeyedReference)keyrarr[i];
+          for (KeyedReference keyedReference : keyedReferenceList) {
              ExternalIdentifier extId = new ExternalIdentifierImpl(lifeCycleManager);
-             extId.setValue(keyr.getKeyValue());
-             extId.setName(new InternationalStringImpl(keyr.getKeyName()));
+             extId.setValue(keyedReference.getKeyValue());
+             extId.setName(new InternationalStringImpl(keyedReference.getKeyName()));
              
-             String tmodelKey = keyr.getTModelKey();
+             String tmodelKey = keyedReference.getTModelKey();
              if (tmodelKey != null) {
             	 ClassificationScheme scheme = new ClassificationSchemeImpl(lifeCycleManager);
             	 scheme.setKey(new KeyImpl(tmodelKey));
@@ -691,17 +610,4 @@
        } 
    }
    
-   private static Name[] getNameArray(List<Name> nameList)
-   {
-	   Name[] namearr = new Name[nameList.size()];
-	   nameList.toArray(namearr);
-	   return namearr;
-   }
-   
-   private static Description[] getDescriptionArray(List<Description> descList)
-   {
-	   Description[] descarr = new Description[descList.size()];
-	   descList.toArray(descarr);
-	   return descarr;
-   }
 }
\ No newline at end of file
diff --git a/src/test/java/org/apache/ws/scout/Creator.java b/src/test/java/org/apache/ws/scout/Creator.java
index b471168..eacc208 100644
--- a/src/test/java/org/apache/ws/scout/Creator.java
+++ b/src/test/java/org/apache/ws/scout/Creator.java
@@ -63,10 +63,10 @@
         org.setPrimaryContact(user);
         PersonName personName = blm.createPersonName("John AXel Rose");
         TelephoneNumber telephoneNumber = blm.createTelephoneNumber();
-        telephoneNumber.setNumber("111-222-333");
+        telephoneNumber.setNumber("111-222-3333");
         telephoneNumber.setType(null);
         PostalAddress address = blm.createPostalAddress("1",
-            "UDDI Drive", "Apache Town","OS", "USA", "00000-1111", "");
+            "Uddi Drive", "Apache Town","CA", "USA", "00000-1111", "");
         
         Collection<PostalAddress> postalAddresses = new ArrayList<PostalAddress>();
         postalAddresses.add(address);