* @throws CoreException if the wave generation has failed
*/
@SuppressWarnings("unchecked")
private void sendReturnWave(final T res) throws CoreException {
Wave returnWave = null;
// Try to retrieve the return Wave type, could be null
final WaveType responseWaveType = this.service.getReturnWaveType(this.wave.getWaveType());
if (responseWaveType != null) {
// No service result type defined into a WaveItem
if (((WaveTypeBase) responseWaveType).getWaveItemList().isEmpty()) {
LOGGER.log(NO_RETURNED_WAVE_ITEM);
throw new CoreException(NO_RETURNED_WAVE_ITEM);
}
// Get the first (and unique) WaveItem used to define the service result type
final WaveItem<T> resultWaveItem = (WaveItem<T>) ((WaveTypeBase) responseWaveType).getWaveItemList().get(0);
// Try to retrieve the command class, could be null
final Class<? extends Command> responseCommandClass = this.service.getReturnCommand(this.wave.getWaveType());
if (responseCommandClass != null) {
// If a Command Class is provided, call it with the right WaveItem to get the real result type
returnWave = WaveBuilder.create()
.waveGroup(WaveGroup.CALL_COMMAND)
.fromClass(this.service.getClass())
.relatedClass(responseCommandClass)
.data(WaveData.build(resultWaveItem, res))
.build();
} else {
// Otherwise send a generic wave that can be handled by any component
returnWave = WaveBuilder.create()
.waveType(responseWaveType)
.fromClass(this.service.getClass())
.data(WaveData.build(resultWaveItem, res))
.build();
}
returnWave.setRelatedWave(this.wave);
returnWave.addWaveListener(new ServiceTaskReturnWaveListener());
// Send the return wave to interested components
this.service.sendWave(returnWave);
} else {
// No service return wave Type defined