hs.hsadmin/hsarback/src/de/hsadmin/remote/PacRemote.java
2013-05-06 19:59:23 +02:00

121 lines
4.3 KiB
Java

package de.hsadmin.remote;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.HSAdminException;
import de.hsadmin.core.model.Transaction;
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(Transaction tx, AbstractEntity entity, Map<String, Object> 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("free", TextUtil.format(pac.isFree()));
Date cancelled = pac.getCancelled();
if (cancelled != null) {
resultMap.put("cancelled", TextUtil.format(cancelled));
}
resultMap.put("basepac", pac.getBasepac().getName());
Map<String, Object> components = new HashMap<String, Object>();
Set<PacComponent> pacComponents = pac.getPacComponents();
for (PacComponent comp : pacComponents) {
int quantity = comp.getQuantity();
if (quantity > 0) {
components.put(comp.getBaseComponent().getFeature(), Integer.toString(quantity));
}
}
resultMap.put("components", components);
}
@Override
protected Class<? extends AbstractEntity> getEntityClass() {
return Pac.class;
}
@Override
protected void map2entity(Transaction tx, Map<String, Object> setParams, AbstractEntity entity) throws HSAdminException {
Pac pac = (Pac) entity;
BasePac basePac = pac.getBasepac();
String basePacName = (String) setParams.get("basepac");
if (assertNotNull(basePacName)) {
basePac = new BasePac();
basePac.setName(basePacName);
pac.setBasepac(basePac);
}
String createdStr = (String) setParams.get("created");
if (createdStr != null && !createdStr.isEmpty()) {
Date createdDate = TextUtil.parseDate(createdStr);
pac.setCreated(createdDate);
}
String cancelledStr = (String) setParams.get("cancelled");
if (cancelledStr != null && !cancelledStr.isEmpty()) {
Date cancelledDate = TextUtil.parseDate(cancelledStr);
pac.setCancelled(cancelledDate);
}
String isFreeString = (String) setParams.get("free");
if (isFreeString != null && !isFreeString.isEmpty()) {
boolean isFree = TextUtil.parseBool(isFreeString);
pac.setFree(isFree);
}
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.initPacComponents(tx.getEntityManager(), basePac, false);
Object componentsObj = setParams.get("components");
if (componentsObj != null && componentsObj instanceof Map) {
Map<?, ?> componentsMap = (Map<?, ?>) componentsObj;
for (Object key : componentsMap.keySet()) {
PacComponent pacComponent = pac.getPacComponent(key.toString());
if (pacComponent == null) {
throw new HSAdminException("component " + key.toString() + " not found");
}
pacComponent.setQuantity(Integer.parseInt(componentsMap.get(key).toString()));
}
}
Object nameParamObj = setParams.get("name");
if (nameParamObj != null && nameParamObj instanceof String) {
pac.setName((String) nameParamObj);
}
}
@Override
protected void regularizeKeys(Map<String, String> whereParams) {
replaceKey(whereParams, "customer", "customer.name");
}
}