Joe said something today on #sipx that sounded familiar.
He was looking at deleting LocationSpecificServices and needed some changes
for the cascade to work properly. (It's really about deleting orphans but I
am a bit worried that if I say so Kathy will call me medieval).

Anyway - I had my share of problems with Location object: specifically with
how Hibernate called setter does some extra stuff converting between Map
and Collection.

This is how I wanted to simplify that code. What do you think?
D.


>From 83177894be9f226f620daddad89cb7098dcb521e Mon Sep 17 00:00:00 2001
From: dkrzemin <[EMAIL PROTECTED]>
Date: Fri, 5 Dec 2008 11:43:24 -0500
Subject: [PATCH] simplify setters used for location mapping

---
 .../sipxconfig/admin/commserver/Location.java      |   53 ++++++++++---------
 .../admin/commserver/SipxProcessTest.java          |   33 +++++++------
 2 files changed, 46 insertions(+), 40 deletions(-)

diff --git a/sipXconfig/neoconf/src/org/sipfoundry/sipxconfig/admin/commserver/Location.java b/sipXconfig/neoconf/src/org/sipfoundry/sipxconfig/admin/commserver/Location.java
index e63e26e..8d0718b 100644
--- a/sipXconfig/neoconf/src/org/sipfoundry/sipxconfig/admin/commserver/Location.java
+++ b/sipXconfig/neoconf/src/org/sipfoundry/sipxconfig/admin/commserver/Location.java
@@ -9,10 +9,9 @@
  */
 package org.sipfoundry.sipxconfig.admin.commserver;
 
+import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.Iterator;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -33,7 +32,7 @@ public class Location extends BeanWithId {
     private String m_fqdn;
     private String m_password;
 
-    private Map<String, LocationSpecificService> m_services;
+    private Collection<LocationSpecificService> m_services;
 
     private DaoEventPublisher m_daoEventPublisher;
 
@@ -95,10 +94,7 @@ public class Location extends BeanWithId {
      * @param services
      */
     public void setServices(Collection<LocationSpecificService> services) {
-        m_services = new HashMap<String, LocationSpecificService>();
-        for (LocationSpecificService locationSpecificService : services) {
-            addService(locationSpecificService);
-        }
+        m_services = services;
     }
 
     /**
@@ -108,7 +104,7 @@ public class Location extends BeanWithId {
      * @param services
      */
     public void setServiceDefinitions(Collection<SipxService> services) {
-        m_services = new HashMap<String, LocationSpecificService>();
+        m_services = new ArrayList<LocationSpecificService>();
         for (SipxService sipxService : services) {
             LocationSpecificService newService = new LocationSpecificService();
             newService.setSipxService(sipxService);
@@ -116,15 +112,8 @@ public class Location extends BeanWithId {
         }
     }
 
-    /**
-     * Returns an unmodifiable collection of sipx services for this location. To add or remove
-     * services on this location, use the Location's addService or removeService methods.
-     */
     public Collection<LocationSpecificService> getServices() {
-        if (m_services == null) {
-            return Collections.unmodifiableCollection(Collections.<LocationSpecificService> emptyList());
-        }
-        return Collections.unmodifiableCollection(m_services.values());
+        return m_services;
     }
 
     public void removeService(LocationSpecificService service) {
@@ -132,21 +121,35 @@ public class Location extends BeanWithId {
     }
 
     public void removeServiceByBeanId(String beanId) {
-        LocationSpecificService service = m_services.get(beanId);
-        if (service != null && service.getSipxService() instanceof SipxAcdService) {
-            m_daoEventPublisher.publishDelete(service);
+        if (m_services == null) {
+            return;
+        }
+        Iterator<LocationSpecificService> iterator = m_services.iterator();
+        while (iterator.hasNext()) {
+            LocationSpecificService next = iterator.next();
+            SipxService service = next.getSipxService();
+            if (beanId.equals(service.getBeanId())) {
+                if (service instanceof SipxAcdService) {
+                    m_daoEventPublisher.publishDelete(next);
+                }
+                iterator.remove();
+                break;
+            }
         }
-        m_services.remove(beanId);
     }
 
     public void addService(LocationSpecificService service) {
         if (m_services == null) {
-            m_services = new HashMap<String, LocationSpecificService>();
+            m_services = new ArrayList<LocationSpecificService>();
         }
-        if (!m_services.containsKey(service.getSipxService().getBeanId())) {
-            service.setLocation(this);
-            m_services.put(service.getSipxService().getBeanId(), service);
+        String serviceName = service.getSipxService().getBeanId();
+        for (LocationSpecificService lss : m_services) {
+            if (serviceName.equals(lss.getSipxService().getBeanId())) {
+                return;
+            }
         }
+        service.setLocation(this);
+        m_services.add(service);
     }
 
     public void addService(SipxService service) {
diff --git a/sipXconfig/neoconf/test/org/sipfoundry/sipxconfig/admin/commserver/SipxProcessTest.java b/sipXconfig/neoconf/test/org/sipfoundry/sipxconfig/admin/commserver/SipxProcessTest.java
index c7701d3..c7a5a87 100644
--- a/sipXconfig/neoconf/test/org/sipfoundry/sipxconfig/admin/commserver/SipxProcessTest.java
+++ b/sipXconfig/neoconf/test/org/sipfoundry/sipxconfig/admin/commserver/SipxProcessTest.java
@@ -1,10 +1,5 @@
 package org.sipfoundry.sipxconfig.admin.commserver;
 
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
-
 import java.util.Arrays;
 
 import junit.framework.TestCase;
@@ -12,28 +7,35 @@ import junit.framework.TestCase;
 import org.sipfoundry.sipxconfig.admin.commserver.SipxProcessModel.ProcessName;
 import org.sipfoundry.sipxconfig.service.SipxProxyService;
 import org.sipfoundry.sipxconfig.service.SipxRegistrarService;
-import org.sipfoundry.sipxconfig.service.SipxService;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
 
 public class SipxProcessTest extends TestCase {
 
     SipxProcessContext m_sipxProcessContext;
     LocationsManager m_locationsManager;
 
+    @Override
     protected void setUp() throws Exception {
         m_sipxProcessContext = createMock(SipxProcessContext.class);
         m_locationsManager = createMock(LocationsManager.class);
-        
+
         Location location = new Location();
-        location.setServiceDefinitions(Arrays.asList(new SipxService[] {
-                new SipxRegistrarService(), new SipxProxyService()
-        }));
-        
-        
+        SipxRegistrarService sipxRegistrarService = new SipxRegistrarService();
+        sipxRegistrarService.setBeanId(SipxRegistrarService.BEAN_ID);
+        SipxProxyService sipxProxyService = new SipxProxyService();
+        sipxProxyService.setBeanId(SipxProxyService.BEAN_ID);
+        location.setServiceDefinitions(Arrays.asList(sipxRegistrarService, sipxProxyService));
+
         Location[] locations = new Location[] {
-                location
+            location
         };
-        
-        ServiceStatus servStatus1 = new ServiceStatus(new Process(ProcessName.ACD_SERVER), ServiceStatus.Status.Running);
+
+        ServiceStatus servStatus1 = new ServiceStatus(new Process(ProcessName.ACD_SERVER),
+                ServiceStatus.Status.Running);
         ServiceStatus servStatus2 = new ServiceStatus(new Process(ProcessName.PRESENCE_SERVER),
                 ServiceStatus.Status.Disabled);
         ServiceStatus[] serviceStatusList = new ServiceStatus[] {
@@ -45,6 +47,7 @@ public class SipxProcessTest extends TestCase {
         replay(m_sipxProcessContext, m_locationsManager);
     }
 
+    @Override
     protected void tearDown() throws Exception {
         verify(m_sipxProcessContext, m_locationsManager);
     }
-- 
1.5.4.3

_______________________________________________
sipx-dev mailing list
[email protected]
List Archive: http://list.sipfoundry.org/archive/sipx-dev
Unsubscribe: http://list.sipfoundry.org/mailman/listinfo/sipx-dev

Reply via email to