From ce5c545befc4cc76229479411924ee4709729bd5 Mon Sep 17 00:00:00 2001 From: Peter Hormanns Date: Tue, 30 Apr 2013 18:45:21 +0200 Subject: [PATCH] pac migration partial implementation --- .../de/hsadmin/mods/pac/PacModuleImpl.java | 22 +++++++++++++++++++ .../de/hsadmin/remote/PacMigrationTest.java | 5 +++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java index 6aae81e..f766e43 100644 --- a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java +++ b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java @@ -1,8 +1,10 @@ package de.hsadmin.mods.pac; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import javax.persistence.EntityManager; @@ -116,6 +118,26 @@ public class PacModuleImpl extends AbstractModuleImpl { throw new AuthorisationException(loginUser, "update", entity); } needsWriteAccessOn(entity, "update"); + Pac newState = (Pac) entity; + EntityManager em = getTransaction().getEntityManager(); + Pac oldState = em.find(Pac.class, newState.getId()); + if (!oldState.getBasepac().getName().equals(newState.getBasepac().getName())) { + Set oldPacComponents = oldState.getPacComponents(); + Map oldQuantities = new HashMap(); + for (PacComponent c : oldPacComponents) { + String feature = c.getBaseComponent().getFeature(); + int quantity = c.getQuantity(); + oldQuantities.put(feature, new Integer(quantity)); + } + oldPacComponents.clear(); + em.flush(); + newState.initPacComponents(em, newState.getBasepac()); + Set newPacComponents = newState.getPacComponents(); + for (PacComponent c : newPacComponents) { + String feature = c.getBaseComponent().getFeature(); + c.setQuantity(oldQuantities.get(feature).intValue()); + } + } return super.update(entity); } diff --git a/hsarback/test/de/hsadmin/remote/PacMigrationTest.java b/hsarback/test/de/hsadmin/remote/PacMigrationTest.java index b3902bf..2254aff 100644 --- a/hsarback/test/de/hsadmin/remote/PacMigrationTest.java +++ b/hsarback/test/de/hsadmin/remote/PacMigrationTest.java @@ -51,7 +51,7 @@ public class PacMigrationTest { setParams.put("curinetaddr", "176.9.242.77"); HashMap components = new HashMap(); components.put("QUOTA", "512"); - components.put("TRAFFIC", "5"); + components.put("TRAFFIC", "4"); components.put("DAEMON", "1"); setParams.put("components", components); Object[] params = new Object[] { user, @@ -87,7 +87,6 @@ public class PacMigrationTest { setParams.put("basepac", Pac.PAC_WEB); setParams.put("components", components); components.put("QUOTA", "1024"); - components.put("TRAFFIC", "5"); Map whereParams = new HashMap(); whereParams.put("name", "aaa04"); Object[] params = new Object[] { user, @@ -108,6 +107,8 @@ public class PacMigrationTest { @SuppressWarnings("unchecked") Map pacResultMap = (Map) untypedResultArray[0]; assertEquals(Pac.PAC_WEB, pacResultMap.get("basepac")); + Object compMapObj = pacResultMap.get("components"); + assertTrue(compMapObj instanceof Map); } catch (XmlRpcException e) { fail(e.getMessage()); }