View Javadoc
1   /*
2    * Copyright 2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.batch.admin.web.resource;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  
22  import org.springframework.batch.admin.domain.JobExecutionInfo;
23  import org.springframework.batch.admin.domain.JobExecutionInfoResource;
24  import org.springframework.batch.admin.domain.StepExecutionInfo;
25  import org.springframework.batch.admin.domain.StepExecutionInfoResource;
26  import org.springframework.batch.admin.web.BatchJobExecutionsController;
27  import org.springframework.batch.core.StepExecution;
28  import org.springframework.hateoas.mvc.ResourceAssemblerSupport;
29  
30  
31  /**
32   * Knows how to build a REST resource out of our domain model {@link org.springframework.batch.admin.domain.JobExecutionInfo}.
33   * 
34   * @author Ilayaperumal Gopinathan
35   */
36  public class JobExecutionInfoResourceAssembler extends
37  		ResourceAssemblerSupport<JobExecutionInfo, JobExecutionInfoResource> {
38  
39  	private StepExecutionInfoResourceAssembler stepExecutionInfoResourceAssembler =
40  			new StepExecutionInfoResourceAssembler();
41  
42  	public JobExecutionInfoResourceAssembler() {
43  		super(BatchJobExecutionsController.class, JobExecutionInfoResource.class);
44  	}
45  
46  	@Override
47  	public JobExecutionInfoResource toResource(JobExecutionInfo entity) {
48  		return createResourceWithId(entity.getJobExecution().getId(), entity);
49  	}
50  
51  	@Override
52  	protected JobExecutionInfoResource instantiateResource(JobExecutionInfo entity) {
53  		Collection<StepExecutionInfoResource> stepExecutionInfoResources =
54  				new ArrayList<StepExecutionInfoResource>(entity.getStepExecutionCount());
55  
56  		if(entity.getStepExecutionCount() > 0) {
57  			for (StepExecution stepExecution : entity.getJobExecution().getStepExecutions()) {
58  				stepExecutionInfoResources.add(
59  						stepExecutionInfoResourceAssembler.toResource(new StepExecutionInfo(stepExecution, entity.getTimeZone())));
60  			}
61  		}
62  
63  		JobExecutionInfoResource jobExecutionInfoResource =
64  				new JobExecutionInfoResource(entity.getJobExecution(), entity.getTimeZone());
65  
66  		jobExecutionInfoResource.setStepExecutions(stepExecutionInfoResources);
67  		return jobExecutionInfoResource;
68  	}
69  }