View Javadoc
1   /*
2    * Copyright 2013-2014 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  import java.util.TimeZone;
22  
23  import org.springframework.batch.admin.domain.JobExecutionInfo;
24  import org.springframework.batch.admin.domain.JobInstanceInfoResource;
25  import org.springframework.batch.admin.web.BatchJobInstancesController;
26  import org.springframework.batch.admin.web.JobInstanceInfo;
27  import org.springframework.batch.core.JobExecution;
28  import org.springframework.hateoas.mvc.ResourceAssemblerSupport;
29  
30  
31  /**
32   * Resource assembler that builds the REST resource {@link JobInstanceInfoResource} out of domain model
33   * {@link org.springframework.batch.admin.web.JobInstanceInfo}.
34   * 
35   * @author Ilayaperumal Gopinathan
36   */
37  public class JobInstanceInfoResourceAssembler extends
38  		ResourceAssemblerSupport<JobInstanceInfo, JobInstanceInfoResource> {
39  
40  	JobExecutionInfoResourceAssembler jobExecutionInfoResourceAssembler = new JobExecutionInfoResourceAssembler();
41  
42  	public JobInstanceInfoResourceAssembler() {
43  		super(BatchJobInstancesController.class, JobInstanceInfoResource.class);
44  	}
45  
46  	@Override
47  	public JobInstanceInfoResource toResource(JobInstanceInfo entity) {
48  		return createResourceWithId(entity.getJobInstance().getId(), entity);
49  	}
50  
51  	@Override
52  	protected JobInstanceInfoResource instantiateResource(JobInstanceInfo entity) {
53  		Collection<JobExecution> jobExecutions = entity.getJobExecutions();
54  		Collection<JobExecutionInfo> infos = new ArrayList<JobExecutionInfo>(jobExecutions.size());
55  
56  		for (JobExecution jobExecution : jobExecutions) {
57  			infos.add(new JobExecutionInfo(jobExecution, TimeZone.getTimeZone("UTC")));
58  		}
59  
60  		return new JobInstanceInfoResource(entity.getJobInstance(),
61  				jobExecutionInfoResourceAssembler.toResources(infos));
62  	}
63  }