package de.hsadmin.remote; import java.util.Comparator; import java.util.Date; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; import de.hsadmin.core.model.AbstractEntity; import de.hsadmin.core.util.TextUtil; import de.hsadmin.mods.cust.Customer; import de.hsadmin.mods.pac.BasePac; import de.hsadmin.mods.pac.Hive; import de.hsadmin.mods.pac.INetAddress; import de.hsadmin.mods.pac.Pac; import de.hsadmin.mods.pac.PacComponent; public class PacRemote extends AbstractRemote { @Override protected void entity2map(AbstractEntity entity, Map resultMap) { Pac pac = (Pac) entity; resultMap.put("name", pac.getName()); resultMap.put("id", Long.toString(pac.getId())); resultMap.put("hive", pac.getHiveName()); resultMap.put("customer", pac.getCustomer().getName()); resultMap.put("curinetaddr", pac.getCurINetAddr().getInetAddr()); resultMap.put("created", TextUtil.format(pac.getCreated())); resultMap.put("basepac", pac.getBasepac().getName()); SortedSet sortedComponents = new TreeSet(new Comparator() { @Override public int compare(PacComponent o1, PacComponent o2) { return o2.getBaseComponent().getSorting() - o1.getBaseComponent().getSorting(); } }); sortedComponents.addAll(pac.getPacComponents()); StringBuffer bf = new StringBuffer(); boolean empty = true; for (PacComponent comp : sortedComponents) { if (comp.getQuantity() > 0) { if (!empty) { bf.append("; "); } bf.append(comp.getBaseComponent().getFeature()); bf.append("="); bf.append(comp.getQuantity()); empty = false; } } resultMap.put("components", bf.toString()); } @Override protected Class getEntityClass() { return Pac.class; } @Override protected void map2entity(Map setParams, AbstractEntity entity) { Pac pac = (Pac) entity; BasePac basePac = pac.getBasepac(); String basePacName = (String) setParams.get("basepac"); if ((basePac == null || basePac.getName() == null) && assertNotNull(basePacName)) { basePac = new BasePac(); basePac.setName(basePacName); pac.setBasepac(basePac); } pac.setCreated(new Date()); INetAddress curINetAddr = pac.getCurINetAddr(); String inetAddrString = (String) setParams.get("curinetaddr"); if ((curINetAddr == null || curINetAddr.getInetAddr() == null) && assertNotNull(inetAddrString)) { curINetAddr = new INetAddress(inetAddrString); pac.setCurINetAddr(curINetAddr); } Customer customer = pac.getCustomer(); String memberCode = (String) setParams.get("customer"); if (customer == null && assertNotNull(memberCode)) { customer = new Customer(); customer.setName(memberCode); pac.setCustomer(customer); } Hive hive = pac.getHive(); String hiveName = (String) setParams.get("hive"); if ((hive == null || hive.getName() == null) && assertNotNull(hiveName)) { hive = new Hive(); hive.setName(hiveName); pac.setHive(hive); } pac.setName((String) setParams.get("name")); } @Override protected void regularizeKeys(Map whereParams) { replaceKey(whereParams, "customer", "customer.name"); } }