From d7390031e96e14e3a9ac668662113c58d03ddf1e Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@jalin.de>
Date: Fri, 21 Mar 2014 18:44:19 +0100
Subject: [PATCH] create customer-alias and -email

---
 hsarback/test/de/hsadmin/remote/CustomerTest.java         |    5 +-
 hsarback/database/data.sql                                |    7 +++
 hsarback/test/de/hsadmin/remote/PacMigrationTest.java     |   16 ++++++--
 hsarback/src/de/hsadmin/mods/cust/CustomerModuleImpl.java |   43 +++++++++++++++++++++
 4 files changed, 65 insertions(+), 6 deletions(-)

diff --git a/hsarback/database/data.sql b/hsarback/database/data.sql
index 52b9a9d..87008c1 100644
--- a/hsarback/database/data.sql
+++ b/hsarback/database/data.sql
@@ -179,6 +179,13 @@
 		WHERE packet_name='hsh01'; 
 
 --
+-- table: domain 
+--
+INSERT INTO domain (domain_name, domain_since, domain_dns_master, domain_owner)
+	SELECT 'hostsharing.net', current_date, 'dns.hostsharing.net', unixuser_id FROM unixuser
+		WHERE unixuser.name='hsh00';
+		
+		--
 -- table: domain_option
 --
 INSERT INTO domain_option (domain_option_name) 
diff --git a/hsarback/src/de/hsadmin/mods/cust/CustomerModuleImpl.java b/hsarback/src/de/hsadmin/mods/cust/CustomerModuleImpl.java
index d961137..042983d 100644
--- a/hsarback/src/de/hsadmin/mods/cust/CustomerModuleImpl.java
+++ b/hsarback/src/de/hsadmin/mods/cust/CustomerModuleImpl.java
@@ -11,6 +11,9 @@
 import de.hsadmin.core.model.HSAdminException;
 import de.hsadmin.core.model.Transaction;
 import de.hsadmin.core.util.TextUtil;
+import de.hsadmin.mods.dom.Domain;
+import de.hsadmin.mods.email.EMailAddress;
+import de.hsadmin.mods.email.EMailAlias;
 import de.hsadmin.mods.pac.Pac;
 import de.hsadmin.mods.user.UnixUser;
 
@@ -24,9 +27,11 @@
 		}
 		Customer newCustomer = (Customer) newEntity;
 		assertNotNull("membercode", newCustomer.getName());
+		assertValidMemberCode("membercode", newCustomer.getName());
 		assertNotNull("password", newCustomer.getPassword());
 		Contact contact = newCustomer.getContacts().iterator().next();
 		assertNotNull("contact_lastname", contact.getLastName());
+		assertNotNull("contact_email", contact.getEmail());
 		String custComment = contact.getLastName();
 		if (contact.getFirstName() != null && contact.getFirstName().length() > 0) {
 			custComment = contact.getFirstName() + " " + contact.getLastName();
@@ -38,6 +43,7 @@
 			newCustomer.setBankAccount(bankAccount);
 		}
 		GenericModuleImpl helperModule = new GenericModuleImpl(getTransaction());
+		
 		UnixUser custAccount = new UnixUser();
 		custAccount.setComment(TextUtil.replaceUmlautCharacters(custComment));
 		custAccount.setName(newCustomer.getName());
@@ -46,6 +52,30 @@
 		custAccount.setQuotaSoftlimit(8);
 		custAccount.setQuotaHardlimit(12);
 		helperModule.add(custAccount);
+		
+		EMailAlias custAlias = new EMailAlias();
+		custAlias.setName(newCustomer.getName());
+		custAlias.setTarget(contact.getEmail());
+		helperModule.add(custAlias);
+		
+		String memberCode = newCustomer.getName();
+		EMailAddress custEMail1 = new EMailAddress();
+		custEMail1.setLocalpart(memberCode.substring(6));
+		Domain dom = (Domain) helperModule.findByString(Domain.class, "hostsharing.net");
+		custEMail1.setDomain(dom);
+		custEMail1.setTarget(memberCode);
+		helperModule.add(custEMail1);
+		
+		if (contact.getFirstName() != null && contact.getFirstName().length() > 0) {
+			custComment = TextUtil.replaceUmlautCharacters(contact.getFirstName().toLowerCase()) 
+					+ "." + TextUtil.replaceUmlautCharacters(contact.getLastName().toLowerCase());
+			EMailAddress custEMail2 = new EMailAddress();
+			custEMail2.setLocalpart(custComment.replace(' ',  '-'));
+			custEMail2.setDomain(dom);
+			custEMail2.setTarget(memberCode);
+			helperModule.add(custEMail2);
+		}
+
 		return super.add(newEntity);
 	}
 
@@ -92,6 +122,12 @@
 		GenericModuleImpl helper = new GenericModuleImpl(getTransaction());
 		AbstractEntity custAccount = helper.findByString(UnixUser.class, cust.getName());
 		helper.delete(custAccount);
+		AbstractEntity custAlias = helper.findByString(EMailAlias.class, cust.getName());
+		helper.delete(custAlias);
+		List<AbstractEntity> custEMailsList = helper.search(EMailAddress.class, "target='" + cust.getName() + "'", null);
+		for (AbstractEntity email : custEMailsList) {
+			helper.delete(email);
+		}
 		super.delete(existingEntity);
 	}
 
@@ -101,4 +137,11 @@
 		}
 	}
 
+	private void assertValidMemberCode(String name, String value) throws HSAdminException {
+		if (value == null || value.length() != 9 || !value.startsWith("hsh00-")) {
+			throw new HSAdminException("field '" + name + "' has to be like 'hsh00-xyz'");
+		}
+		
+	}
+
 }
diff --git a/hsarback/test/de/hsadmin/remote/CustomerTest.java b/hsarback/test/de/hsadmin/remote/CustomerTest.java
index 5df37ed..dba301f 100644
--- a/hsarback/test/de/hsadmin/remote/CustomerTest.java
+++ b/hsarback/test/de/hsadmin/remote/CustomerTest.java
@@ -50,7 +50,7 @@
 		setParams.put("contact_salut", "Herr");
 		setParams.put("contact_title", "Dr.");
 		setParams.put("contact_firstname", "Ömer Günther");
-		setParams.put("contact_lastname", "Janßen-Müller");
+		setParams.put("contact_lastname", "Müller-Zwo");
 		setParams.put("contact_street", "Hauptstr. 1");
 		setParams.put("contact_zipcode", "99998");
 		setParams.put("contact_city", "Musterstadt");
@@ -63,9 +63,10 @@
 		try {
 			client.execute(MODULE + ".add", params);
 			assertEquals(membersCount + 1, getMembersCount(user));
-			for (int idx=11; idx < 12; idx++) {
+			for (int idx=11; idx < 14; idx++) {
 				setParams.put("membercode", "hsh00-n" + idx);
 				setParams.put("memberno", "200" + idx);
+				setParams.put("contact_lastname", "Janßen-Müller" + idx);
 				setParams.put("contact_street", "Hauptstr. " + idx);	
 				params[1] = cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL());
 				client.execute(MODULE + ".add", params);
diff --git a/hsarback/test/de/hsadmin/remote/PacMigrationTest.java b/hsarback/test/de/hsadmin/remote/PacMigrationTest.java
index a0119ae..0d382e3 100644
--- a/hsarback/test/de/hsadmin/remote/PacMigrationTest.java
+++ b/hsarback/test/de/hsadmin/remote/PacMigrationTest.java
@@ -42,6 +42,18 @@
 	}
 
 	@Test
+	public void testSW() {
+		testCreateSW();
+		testUpdateSW();
+		testDeleteSW();
+	}
+	
+	@Test
+	public void testDW() {
+		testCreateDW();
+		testUpdateDW();
+	}
+
 	public void testCreateDW() {
 		int count = getPacsCount();
 		String user = "ad";
@@ -58,7 +70,6 @@
 		}
 	}
 
-	@Test
 	public void testCreateSW() {
 		int count = getPacsCount();
 		String user = "ad";
@@ -75,7 +86,6 @@
 		}
 	}
 
-	@Test
 	public void testUpdateDW() {
 		String user = "ad";
 		try {
@@ -116,7 +126,6 @@
 		}
 	}
 
-	@Test
 	public void testUpdateSW() {
 		String user = "ad";
 		try {
@@ -157,7 +166,6 @@
 		}
 	}
 
-	@Test
 	public void testDeleteSW() {
 		String user = "ad";
 		try {

--
Gitblit v1.9.0-SNAPSHOT