From 5056e46610886dd4c4ed147959e9e0fc6568ec15 Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@jalin.de>
Date: Mon, 21 Jan 2013 13:38:31 +0100
Subject: [PATCH] fix or-mapping of pac-components

---
 hsarback/src/de/hsadmin/mods/pac/BasePac.java       |    1 +
 hsarback/src/de/hsadmin/mods/pac/Pac.java           |   25 ++++++++++++++++++++++---
 hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java |    2 --
 3 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/hsarback/src/de/hsadmin/mods/pac/BasePac.java b/hsarback/src/de/hsadmin/mods/pac/BasePac.java
index a58a1e4..2698b4b 100644
--- a/hsarback/src/de/hsadmin/mods/pac/BasePac.java
+++ b/hsarback/src/de/hsadmin/mods/pac/BasePac.java
@@ -53,6 +53,7 @@
 	private boolean valid;
 
 	@OneToMany(fetch = FetchType.LAZY, cascade = ALL)
+	@JoinTable(name = "component", joinColumns = @JoinColumn(name = "basepacket_id"), inverseJoinColumns = @JoinColumn(name = "basecomponent_id"))
 	private Set<Component> components;
 
 	@OneToMany(fetch = FetchType.LAZY, cascade = ALL)
diff --git a/hsarback/src/de/hsadmin/mods/pac/Pac.java b/hsarback/src/de/hsadmin/mods/pac/Pac.java
index 746811c..d724468 100644
--- a/hsarback/src/de/hsadmin/mods/pac/Pac.java
+++ b/hsarback/src/de/hsadmin/mods/pac/Pac.java
@@ -8,6 +8,7 @@
 import java.io.Serializable;
 import java.util.Date;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import javax.persistence.Column;
@@ -18,6 +19,7 @@
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
 import javax.persistence.OneToMany;
+import javax.persistence.Query;
 import javax.persistence.SequenceGenerator;
 import javax.persistence.Table;
 import javax.persistence.Temporal;
@@ -98,7 +100,10 @@
 		this.created = new Date();
 		this.webserverGroup = "httpd";
 		this.curINetAddr = hive.getInetAddr();
+		initPacComponents(basePac);
+	}
 
+	private void initPacComponents(BasePac basePac) {
 		pacComponents = new HashSet<PacComponent>();
 		Date today = new Date();
 		for (Component comp : basePac.getComponents()) {
@@ -107,6 +112,16 @@
 					today, null);
 			pacComponents.add(pacComp);
 		}
+	}
+
+	@Override
+	public void initialize(EntityManager em, UnixUser loginUser) {
+		super.initialize(em, loginUser);
+		Query qBasepac = em.createQuery("SELECT b FROM BasePacs b WHERE b.valid = :valid ORDER BY b.name");
+		qBasepac.setParameter("valid", Boolean.TRUE);
+		List<?> resultList = qBasepac.getResultList();
+		basepac = (BasePac) resultList.get(0);
+		initPacComponents(basepac);
 	}
 
 	public static String createQueryFromStringKey(String humanKey) {
@@ -226,9 +241,13 @@
 	}
 
 	public PacComponent getPacComponent(String feature) {
-		for (PacComponent pc : getPacComponents())
-			if (feature.equals(pc.getBaseComponent().getFeature()))
-				return pc;
+		if (pacComponents != null) {
+			for (PacComponent pc : pacComponents) {
+				if (feature.equals(pc.getBaseComponent().getFeature())) {
+					return pc;
+				}
+			}
+		}
 		return null;
 	}
 
diff --git a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
index 1ed2d7d..c23525e 100644
--- a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
+++ b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
@@ -115,8 +115,6 @@
 		UnixUser loginUser = getTransaction().getLoginUser();
 		if (!(entity instanceof Pac)) {
 			throw new AuthorisationException(loginUser, "update", entity);
-//		} else {
-//			Pac pac = (Pac) entity;
 		}
 		needsWriteAccessOn(entity, "update");
 		return super.update(entity);

--
Gitblit v1.9.1