Object oldInvocationId = req.getSession().getAttribute("oldInteractionListId");
if (oldInvocationId == null || !(oldInvocationId instanceof Long)){
throw new InputException(getResources(req).getMessage(locale, "admin.entryNotAvailable"),"old invocation entry not found in session",null,null);
}
Invocation oldInvocation = new SmsDbManager(locale,session).getInvocation((Long)oldInvocationId);
if (oldInvocation == null){
throw new InputException(getResources(req).getMessage(locale, "admin.entryNotAvailable"),"old invocation entry #" + oldInvocationId + " not found",null,null);
}
checkAccessRights(req,oldInvocation.getGroup(),null);
Object newEntry = req.getSession().getAttribute("newInteractionList");
if (newEntry == null || !(newEntry instanceof Invocation)){
throw new InputException(getResources(req).getMessage(locale, "admin.entryNotAvailable"),"new invocation entry not found in session",null,null);
}
// reload references in current session
Invocation newInvocation = (Invocation)newEntry;
f.initialize(newInvocation,locale,session,getResources(req));
newInvocation = new Invocation();
f.toEntry(newInvocation, locale, session, getResources(req));
checkAccessRights(req, newInvocation.getGroup(),null);
// group assignment is not editable => perform source to destination automatically
mappingSet.add(new Entry(oldInvocation.getGroup()), new Entry(newInvocation.getGroup()));
// Create duplicated command entries according to the original execution order
CommandEntryManager commandEntryManager = new CommandEntryManager(locale,session);
SortedSet<CommandEntry> newCommandEntries = new TreeSet<CommandEntry>(new CommandEntryManager.CommandEntryComparator());
Set<CommandEntry> oldCommandEntries = oldInvocation.getCommandEntries();
SortedSet<CommandEntry> sortedOldCommandEntries = new TreeSet<CommandEntry>(new CommandEntryManager.CommandEntryComparator());
sortedOldCommandEntries.addAll(oldCommandEntries);
for (CommandEntry oldCommandEntry : sortedOldCommandEntries) {
CommandEntryExtender extender = commandEntryManager.createCommandEntryExtender(oldCommandEntry);
CommandEntry duplicatedCommandEntry = extender.duplicateCommandEntry(newInvocation.getGroup(),mappingSet);
Set<InteractionList> invocations = new TreeSet<InteractionList>();
invocations.add(newInvocation);
duplicatedCommandEntry.setInteractionLists(invocations);
commandEntryManager.create(duplicatedCommandEntry); // also sets the ID for sorting
newCommandEntries.add(duplicatedCommandEntry);
}
newInvocation.setCommandEntries(newCommandEntries);
SmsDbManager smsDbManager = new SmsDbManager(locale,session);
smsDbManager.createInvocation(newInvocation);
return mapping.findForward("duplicated");
}