From 8976d93e03b34ed68eb8364a9116dfbc396e0bac Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@jalin.de>
Date: Wed, 01 May 2013 22:23:13 +0200
Subject: [PATCH] pac migration (contains error)

---
 hsarback/test/de/hsadmin/remote/ContinuousIntegrationTest.java |    3 ++-
 hsarback/test/de/hsadmin/remote/RemoteTest.java                |    3 ++-
 hsarback/src/de/hsadmin/mods/pac/Pac.java                      |    1 +
 hsarback/test/de/hsadmin/remote/PacMigrationTest.java          |   21 +++++++++++++++++++++
 hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java            |   24 +++++++++++++++++++-----
 5 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/hsarback/src/de/hsadmin/mods/pac/Pac.java b/hsarback/src/de/hsadmin/mods/pac/Pac.java
index 23fa538..db1f988 100644
--- a/hsarback/src/de/hsadmin/mods/pac/Pac.java
+++ b/hsarback/src/de/hsadmin/mods/pac/Pac.java
@@ -10,6 +10,7 @@
 import java.util.HashSet;
 import java.util.Set;
 
+import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EntityManager;
diff --git a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
index facba6b..6b472b2 100644
--- a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
+++ b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
@@ -78,11 +78,14 @@
 		for (Object cObj : componentsList) {
 			Component comp = (Component) cObj;
 			PacComponent pacComponent = new PacComponent();
-			pacComponent.setBaseComponent(comp.getBaseComponent());
+			BaseComponent baseComponent = comp.getBaseComponent();
+			pacComponent.setBaseComponent(baseComponent);
 			pacComponent.setbasePac(comp.getBasePac());
 			pacComponent.setCreated(now);
 			pacComponent.setPac(pac);
-			pacComponent.setQuantity(comp.getDefaultQuantity());
+			String feature = baseComponent.getFeature();
+			int requestedQuantity = pac.getPacComponent(feature).getQuantity();
+			pacComponent.setQuantity(requestedQuantity);
 			pacComponents.add(pacComponent);
 		}
 		pac.setPacComponents(pacComponents);
@@ -128,7 +131,8 @@
 		EntityManager em = getTransaction().getEntityManager();
 		Pac oldState = em.find(Pac.class, newState.getId());
 		String oldBasepacName = oldState.getBasepac().getName();
-		String newBasepacName = newState.getBasepac().getName();
+		BasePac newBasepac = newState.getBasepac();
+		String newBasepacName = newBasepac.getName();
 		if (!oldBasepacName.equals(newBasepacName)) {
 			Set<PacComponent> oldPacComponents = oldState.getPacComponents();
 			Map<String, Integer> oldQuantities = new HashMap<String, Integer>();
@@ -136,17 +140,27 @@
 				String feature = c.getBaseComponent().getFeature();
 				int quantity = c.getQuantity();
 				oldQuantities.put(feature, new Integer(quantity));
+				em.remove(c);
 			}
 			oldPacComponents.clear();
+			newState.getPacComponents().clear();
 			em.flush();
-			newState.initPacComponents(em, newState.getBasepac());
+			newState.setBasepac(newBasepac);
+			newState.initPacComponents(em, newBasepac);
 			Set<PacComponent> newPacComponents = newState.getPacComponents();
 			for (PacComponent pc : newPacComponents) {
 				BaseComponent bc = pc.getBaseComponent();
 				String feature = bc.getFeature();
-				pc.setQuantity(oldQuantities.get(feature).intValue());
+				int quantity = oldQuantities.get(feature).intValue();
+				int increment = newBasepac.getComponent(feature).getIncrementQuantity();
+				if (quantity % increment != 0) {
+					int numOfIncrements = quantity / increment;
+					quantity = increment * numOfIncrements + increment;
+				}
+				pc.setQuantity(quantity);
 			}
 		}
+		em.flush();
 		return super.update(entity);
 	}
 
diff --git a/hsarback/test/de/hsadmin/remote/ContinuousIntegrationTest.java b/hsarback/test/de/hsadmin/remote/ContinuousIntegrationTest.java
index 7fdf00b..15ac6bd 100644
--- a/hsarback/test/de/hsadmin/remote/ContinuousIntegrationTest.java
+++ b/hsarback/test/de/hsadmin/remote/ContinuousIntegrationTest.java
@@ -17,7 +17,8 @@
 	DomainTest.class,
 	EMailAddressTest.class,
 	SSLCertDomainTest.class,
-	DatabaseCleanTest.class
+	DatabaseCleanTest.class,
+	PacMigrationTest.class
 //	CustomerTest.class,
 //	LongCustomerNameTest.class,
 //	QueueTaskTest.class
diff --git a/hsarback/test/de/hsadmin/remote/PacMigrationTest.java b/hsarback/test/de/hsadmin/remote/PacMigrationTest.java
index de0c0ee..e9a2b5c 100644
--- a/hsarback/test/de/hsadmin/remote/PacMigrationTest.java
+++ b/hsarback/test/de/hsadmin/remote/PacMigrationTest.java
@@ -60,6 +60,26 @@
 		try {
 			Object execute = client.execute(MODULE + ".add", params);
 			assertTrue(execute instanceof Map<?, ?>);
+			Map<String, Object> whereParams = new HashMap<String, Object>();
+			whereParams.put("name", "aaa04");
+			params = new Object[] { user, 
+					cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
+					whereParams };
+			execute = client.execute(MODULE + ".search", params);
+			assertNotNull(execute);
+			assertTrue(execute instanceof Object[]);
+			Object[] untypedResultArray = (Object[]) execute;
+			assertEquals(1, untypedResultArray.length);
+			assertTrue(untypedResultArray[0] instanceof Map<?,?>);
+			@SuppressWarnings("unchecked")
+			Map<String, ?> pacResultMap = (Map<String, ?>) untypedResultArray[0]; 
+			assertEquals(Pac.PAC_DW, pacResultMap.get("basepac"));
+			Object compMapObj = pacResultMap.get("components");
+			assertTrue(compMapObj instanceof Map<?,?>);
+			@SuppressWarnings("unchecked")
+			Map<String,String> compMap = (Map<String, String>) compMapObj;
+			assertEquals("512", compMap.get("QUOTA"));
+			assertEquals("4", compMap.get("TRAFFIC"));
 		} catch (XmlRpcException e) {
 			fail(e.getMessage());
 		}
@@ -109,6 +129,7 @@
 			assertEquals(Pac.PAC_WEB, pacResultMap.get("basepac"));
 			Object compMapObj = pacResultMap.get("components");
 			assertTrue(compMapObj instanceof Map<?,?>);
+			@SuppressWarnings("unchecked")
 			Map<String,String> compMap = (Map<String, String>) compMapObj;
 			assertEquals("512", compMap.get("QUOTA"));
 			assertEquals("5", compMap.get("TRAFFIC"));
diff --git a/hsarback/test/de/hsadmin/remote/RemoteTest.java b/hsarback/test/de/hsadmin/remote/RemoteTest.java
index e302888..537f62f 100644
--- a/hsarback/test/de/hsadmin/remote/RemoteTest.java
+++ b/hsarback/test/de/hsadmin/remote/RemoteTest.java
@@ -16,7 +16,8 @@
 	EMailAliasTest.class,
 	DomainTest.class,
 	EMailAddressTest.class,
-	DatabaseCleanTest.class
+	DatabaseCleanTest.class,
+	PacMigrationTest.class
 //  LongCustomerNameTest.class	
 //	CustomerTest.class,
 //	QueueTaskTest.class

--
Gitblit v1.9.0-SNAPSHOT