alternative to emailaddress-remote

This commit is contained in:
Peter Hormanns 2023-04-24 18:14:00 +02:00
parent 200a649091
commit 53516fbc38
2 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,121 @@
package de.hsadmin.remote;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.Transaction;
import de.hsadmin.mods.dom.Domain;
import de.hsadmin.mods.email.EMailAddress;
public class EMailRemote extends AbstractRemote {
@Override
protected void entity2map(Transaction tx, AbstractEntity entity, Map<String, Object> map) {
EMailAddress adr = (EMailAddress) entity;
long id = adr.getId();
String domain = adr.getDomain().getName();
String admin = adr.getDomain().getUser().getName();
String pac = adr.getDomain().getUser().getPac().getName();
String target = adr.getTarget();
String localpart = adr.getLocalpart();
String subdomain = adr.getSubdomain();
map.put("id", Long.toString(id));
if (assertNotNull(domain)) map.put("domain", domain);
if (assertNotNull(admin)) map.put("admin", admin);
if (assertNotNull(pac)) map.put("pac", pac);
if (assertNotNull(target)) {
StringTokenizer tokenizer = new StringTokenizer(target, ",");
List<String> tokenList = new ArrayList<String>();
while (tokenizer.hasMoreTokens()) {
tokenList.add(tokenizer.nextToken().trim());
}
map.put("target", tokenList);
}
final StringBuffer name = new StringBuffer();
if (assertNotNull(localpart)) name.append(localpart);
name.append("@");
if (assertNotNull(subdomain)) name.append(subdomain + ":");
if (assertNotNull(domain)) name.append(domain);
map.put("name", name.toString());
}
@Override
protected void map2entity(Transaction tx, Map<String, Object> map, AbstractEntity entity) {
EMailAddress adr = (EMailAddress) entity;
String name = (String) map.get("name");
String[] addressParts = name.split("@");
String localpart = addressParts[0];
String fulldomain = addressParts[1];
String domain = null;
String subdomain = null;
if (fulldomain.contains(":")) {
String[] fulldomainParts = fulldomain.split("\\:");
domain = fulldomainParts[1];
subdomain = fulldomainParts[0];
} else {
domain = fulldomain;
}
if (assertNotNull(localpart)) {
adr.setLocalpart(localpart);
}
if (assertNotNull(subdomain)) {
adr.setSubdomain(subdomain);
}
Object l = map.get("target");
if (l != null && l instanceof Object[]) {
final StringBuffer tBuff = new StringBuffer();
final Object[] targetObjArray = (Object[]) l;
for (int idx=0; idx<targetObjArray.length; idx++) {
if (targetObjArray[idx] instanceof String) {
if (tBuff.length() > 0) {
tBuff.append(',');
}
tBuff.append((String) targetObjArray[idx]);
}
}
adr.setTarget(tBuff.toString());
} else if ((l != null && l instanceof Map<?, ?>)) {
final StringBuffer tBuff = new StringBuffer();
final Map<? ,?> targetObjMap = (Map<?, ?>) l;
for (Object targetObj : targetObjMap.values()) {
if (targetObj instanceof String) {
if (tBuff.length() > 0) {
tBuff.append(',');
}
tBuff.append((String) targetObj);
}
}
adr.setTarget(tBuff.toString());
} else {
if (l != null && l instanceof String) {
adr.setTarget((String) l);
}
}
if (assertNotNull(domain)) {
Domain dom = new Domain();
dom.setName(domain);
adr.setDomain(dom);
}
}
@Override
protected Class<? extends AbstractEntity> getEntityClass() {
return EMailAddress.class;
}
@Override
protected void regularizeKeys(Map<String, String> whereParams) {
replaceKey(whereParams, "domain", "domain.name");
replaceKey(whereParams, "pac", "domain.user.pac.name");
replaceKey(whereParams, "admin", "domain.user.name");
}
@Override
protected String getOrderBy() {
return "ORDER BY obj.domain.name ASC, obj.subdomain ASC, obj.localpart ASC";
}
}

View File

@ -3,6 +3,7 @@ user=de.hsadmin.remote.UnixUserRemote
domain=de.hsadmin.remote.DomainRemote
emailalias=de.hsadmin.remote.EMailAliasRemote
emailaddress=de.hsadmin.remote.EMailAddressRemote
email=de.hsadmin.remote.EMailRemote
q=de.hsadmin.remote.QueueTaskRemote
role=de.hsadmin.remote.RoleRemote
mysqluser=de.hsadmin.remote.MysqlUserRemote