private static final long serialVersionUID = 2363373679538578191L;
@SuppressWarnings("unchecked")
@Override
public ModelPayload add(AddAccountRequest request) {
final ModelPayload p = new ModelPayload();
final Status s = p.getStatus();
final AppContext ac = (AppContext) getServletContext().getAttribute(AppContext.KEY);
assert ac != null;
final com.tll.service.entity.account.AddAccountService svc = ac.getAddAccountService();
final PersistContext pc = (PersistContext) getServletContext().getAttribute(PersistContext.KEY);
assert svc != null && pc != null;
final Marshaler mlr = pc.getMarshaler();
Class<? extends Account> accountClass;
try {
accountClass = (Class<? extends Account>) pc.getEntityTypeResolver().resolveEntityClass(request.getEntityType());
}
catch(final ClassCastException e) {
s.addMsg("Invalid account type.", MsgLevel.ERROR, MsgAttr.STATUS.flag);
return p;
}
Model maccount = request.getAccount();
if(maccount == null) {
s.addMsg("No account specified.", MsgLevel.ERROR, MsgAttr.STATUS.flag);
return p;
}
final Collection<Model> maios = request.getAccountInterfaces();
if(maios == null) {
s.addMsg("No account interface options specified.", MsgLevel.ERROR, MsgAttr.STATUS.flag);
return p;
}
final Collection<Model> musers = request.getUsers();
try {
// un-marshal
Account account = mlr.marshalModel(maccount, accountClass);
final ArrayList<AccountInterface> aios = new ArrayList<AccountInterface>(maios.size());
for(final Model maio : maios) {
aios.add(mlr.marshalModel(maio, AccountInterface.class));
}
final ArrayList<User> users = musers == null ? null : new ArrayList<User>(musers.size());
if(musers != null) {
for(final Model muser : musers) {
users.add(mlr.marshalModel(muser, User.class));
}
}
if(Isp.class == accountClass) {
account = svc.addIsp((Isp) account, aios, users);
s.addMsg("Isp added", MsgLevel.INFO, MsgAttr.STATUS.flag);
}
else if(Merchant.class == accountClass) {
account = svc.addMerchant((Merchant) account, aios, users);
s.addMsg("Merchant added", MsgLevel.INFO, MsgAttr.STATUS.flag);
}
else if(Customer.class == accountClass) {
account = svc.addCustomer((Customer) account, aios, users);
s.addMsg("Customer added", MsgLevel.INFO, MsgAttr.STATUS.flag);
}
else {
s.addMsg("Unhandled account type: " + accountClass, MsgLevel.ERROR, MsgAttr.STATUS.flag);
return p;
}
// marshal the added account
maccount = mlr.marshalEntity(account, pc.getMarshalOptionsResolver().resolve(SmbizEntityType.ACCOUNT));
p.setModel(maccount);
return p;
}
catch(final RuntimeException e) {
RpcServlet.exceptionToStatus(e, s);