hs.hsadmin/hsarback/src/de/hsadmin/remote/PacRemote.java

121 lines
4.3 KiB
Java
Raw Normal View History

2011-03-22 23:02:06 +01:00
package de.hsadmin.remote;
import java.util.Date;
2012-09-21 14:16:22 +02:00
import java.util.HashMap;
2011-03-22 23:02:06 +01:00
import java.util.Map;
2012-09-21 14:16:22 +02:00
import java.util.Set;
2011-03-22 23:02:06 +01:00
import de.hsadmin.core.model.AbstractEntity;
2013-04-29 20:01:09 +02:00
import de.hsadmin.core.model.HSAdminException;
import de.hsadmin.core.model.Transaction;
2012-07-27 18:02:24 +02:00
import de.hsadmin.core.util.TextUtil;
2011-03-22 23:02:06 +01:00
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;
2011-03-31 21:24:02 +02:00
import de.hsadmin.mods.pac.PacComponent;
2011-03-22 23:02:06 +01:00
public class PacRemote extends AbstractRemote {
@Override
2013-04-29 20:01:09 +02:00
protected void entity2map(Transaction tx, AbstractEntity entity, Map<String, Object> resultMap) {
2011-03-22 23:02:06 +01:00
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());
2012-07-27 18:02:24 +02:00
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));
}
2011-03-31 21:24:02 +02:00
resultMap.put("basepac", pac.getBasepac().getName());
2012-09-21 14:16:22 +02:00
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));
2011-03-31 21:24:02 +02:00
}
}
2012-09-21 14:16:22 +02:00
resultMap.put("components", components);
2011-03-22 23:02:06 +01:00
}
@Override
protected Class<? extends AbstractEntity> getEntityClass() {
return Pac.class;
}
@Override
2013-04-29 20:01:09 +02:00
protected void map2entity(Transaction tx, Map<String, Object> setParams, AbstractEntity entity) throws HSAdminException {
2011-03-22 23:02:06 +01:00
Pac pac = (Pac) entity;
BasePac basePac = pac.getBasepac();
2011-10-28 16:08:18 +02:00
String basePacName = (String) setParams.get("basepac");
if (assertNotNull(basePacName)) {
2011-03-22 23:02:06 +01:00
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);
}
2011-03-22 23:02:06 +01:00
INetAddress curINetAddr = pac.getCurINetAddr();
2011-10-28 16:08:18 +02:00
String inetAddrString = (String) setParams.get("curinetaddr");
2011-05-19 20:16:11 +02:00
if ((curINetAddr == null || curINetAddr.getInetAddr() == null) && assertNotNull(inetAddrString)) {
2011-03-22 23:02:06 +01:00
curINetAddr = new INetAddress(inetAddrString);
pac.setCurINetAddr(curINetAddr);
}
Customer customer = pac.getCustomer();
2011-10-28 16:08:18 +02:00
String memberCode = (String) setParams.get("customer");
2011-03-22 23:02:06 +01:00
if (customer == null && assertNotNull(memberCode)) {
customer = new Customer();
customer.setName(memberCode);
pac.setCustomer(customer);
}
Hive hive = pac.getHive();
2011-10-28 16:08:18 +02:00
String hiveName = (String) setParams.get("hive");
2011-05-19 19:37:29 +02:00
if ((hive == null || hive.getName() == null) && assertNotNull(hiveName)) {
2011-03-22 23:02:06 +01:00
hive = new Hive();
hive.setName(hiveName);
pac.setHive(hive);
}
2013-05-06 19:59:23 +02:00
pac.initPacComponents(tx.getEntityManager(), basePac, false);
2012-09-21 14:16:22 +02:00
Object componentsObj = setParams.get("components");
if (componentsObj != null && componentsObj instanceof Map) {
Map<?, ?> componentsMap = (Map<?, ?>) componentsObj;
for (Object key : componentsMap.keySet()) {
2013-04-29 20:01:09 +02:00
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()));
2012-09-21 14:16:22 +02:00
}
}
Object nameParamObj = setParams.get("name");
if (nameParamObj != null && nameParamObj instanceof String) {
pac.setName((String) nameParamObj);
}
2011-03-22 23:02:06 +01:00
}
@Override
protected void regularizeKeys(Map<String, String> whereParams) {
replaceKey(whereParams, "customer", "customer.name");
}
}