protected static Object convertReturnedValue()

in gui/jaxrs/jaxrs-client/src/main/java/org/apache/batchee/jaxrs/client/BatchEEJAXRSClientBase.java [102:198]


    protected static Object convertReturnedValue(final Object arg, final Type type) {
        if (arg == null || void.class.equals(type)) {
            return null;
        }

        // simple types are mapped 1-1 between JobOperator and JBatchResource
        if (String.class.equals(type) || int.class.equals(type) || long.class.equals(type)) {
            return arg;
        }

        if (Properties.class.equals(type)) {
            final RestProperties props = RestProperties.class.cast(arg);
            return toProperties(props);
        }
        if (JobInstance.class.equals(type)) {
            final RestJobInstance rij = RestJobInstance.class.cast(arg);
            return new JobInstanceImpl(rij.getName(), rij.getId());
        }
        if (JobExecution.class.equals(type)) {
            final RestJobExecution jobExec = RestJobExecution.class.cast(arg);
            return new JobExecutionImpl(
                jobExec.getId(),
                jobExec.getName(),
                toProperties(jobExec.getJobParameters()),
                jobExec.getBatchStatus(),
                jobExec.getExitStatus(),
                jobExec.getCreateTime(),
                jobExec.getStartTime(),
                jobExec.getEndTime(),
                jobExec.getLastUpdatedTime()
            );
        }
        if (ParameterizedType.class.isInstance(type)) {
            final ParameterizedType pt = ParameterizedType.class.cast(type);
            if (Set.class.equals(pt.getRawType())) {
                if (String.class.equals(pt.getActualTypeArguments()[0])) {
                    final String[] names = String[].class.cast(arg);
                    final Set<String> converted = new HashSet<String>(names.length);
                    Collections.addAll(converted, names);
                    return converted;
                }
            }
            if (List.class.equals(pt.getRawType())) {
                if (JobInstance.class.equals(pt.getActualTypeArguments()[0])) {
                    final RestJobInstance[] instances = RestJobInstance[].class.cast(arg);
                    final List<JobInstance> converted = new ArrayList<JobInstance>(instances.length);
                    for (final RestJobInstance rij : instances) {
                        converted.add(new JobInstanceImpl(rij.getName(), rij.getId()));
                    }
                    return converted;
                }
                if (Long.class.equals(pt.getActualTypeArguments()[0])) {
                    final Long[] ids = Long[].class.cast(arg);
                    final List<Long> converted = new ArrayList<Long>(ids.length);
                    Collections.addAll(converted, ids);
                    return converted;
                }
                if (JobExecution.class.equals(pt.getActualTypeArguments()[0])) {
                    final RestJobExecution[] executions = RestJobExecution[].class.cast(arg);
                    final List<JobExecution> converted = new ArrayList<JobExecution>(executions.length);
                    for (final RestJobExecution exec : executions) {
                        converted.add(new JobExecutionImpl(
                            exec.getId(),
                            exec.getName(),
                            toProperties(exec.getJobParameters()),
                            exec.getBatchStatus(),
                            exec.getExitStatus(),
                            exec.getCreateTime(),
                            exec.getStartTime(),
                            exec.getEndTime(),
                            exec.getLastUpdatedTime()
                        ));
                    }
                    return converted;
                }
                if (StepExecution.class.equals(pt.getActualTypeArguments()[0])) {
                    final RestStepExecution[] executions = RestStepExecution[].class.cast(arg);
                    final List<StepExecution> converted = new ArrayList<StepExecution>(executions.length);
                    for (final RestStepExecution exec : executions) {
                        converted.add(new StepExecutionImpl(
                            exec.getId(),
                            exec.getName(),
                            exec.getBatchStatus(),
                            exec.getExitStatus(),
                            exec.getStartTime(),
                            exec.getEndTime(),
                            null,
                            toMetrics(exec.getMetrics())
                        ));
                    }
                    return converted;
                }
            }
        }

        throw new IllegalArgumentException("Unexpected type " + type + " for a returned type");
    }