1   package com.trendmicro.grid.acl.ds.dummy;
2   
3   import com.trendmicro.grid.acl.ds.JobRepository;
4   import com.trendmicro.grid.acl.ds.datatypes.SharedJob;
5   import com.trendmicro.grid.acl.ds.datatypes.SharedJobDetails;
6   import com.trendmicro.grid.acl.l0.datatypes.Job;
7   import com.trendmicro.grid.acl.l0.datatypes.JobDetails;
8   import com.trendmicro.grid.acl.l0.datatypes.Source;
9   import com.trendmicro.grid.acl.l0.datatypes.UUIDListPage;
10  import net.sf.tinyjee.concurrent.LockingMap;
11  import org.springframework.stereotype.Repository;
12  
13  import java.util.*;
14  
15  import static net.sf.tinyjee.util.Assert.assertNotNull;
16  
17  /**
18   * Implements a dummy in-memory repository for jobs.
19   *
20   * @author juergen_kellerer, 2010-05-07
21   * @version 1.0
22   */
23  @Repository
24  public class DummyJobRepository implements JobRepository {
25  
26  	Map<UUID, SharedJobDetails> jobDetails = new LockingMap<UUID, SharedJobDetails>();
27  
28  	/**
29  	 * {@inheritDoc}
30  	 */
31  	public UUID createJob() {
32  		return createSubJob(null);
33  	}
34  
35  	/**
36  	 * {@inheritDoc}
37  	 */
38  	public UUID createSubJob(UUID parentJobId) {
39  		UUID key = UUID.randomUUID();
40  		SharedJob job = new SharedJob(key, parentJobId, Job.State.PREPARED, 0, new Date(), null, null, null);
41  		jobDetails.put(key, new SharedJobDetails(job, null, null));
42  
43  		if (parentJobId != null) {
44  			JobDetails parent = jobDetails.get(parentJobId);
45  			parent.getSubJobs().add(job);
46  		}
47  
48  		return key;
49  	}
50  
51  	/**
52  	 * {@inheritDoc}
53  	 */
54  	public void addJobSources(UUID jobId, List<Source> sources) {
55  		JobDetails details = jobDetails.get(jobId);
56  		assertNotNull("job", details);
57  		details.getAssignedSources().addAll(sources);
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 */
63  	@Override
64  	public void addJobSources(Job job, List<Source> sources) {
65  		addJobSources(job.getJobId(), sources);
66  	}
67  
68  	/**
69  	 * {@inheritDoc}
70  	 */
71  	public void updateJob(Job job) {
72  		JobDetails details = jobDetails.get(job.getJobId());
73  		Job existing = details == null ? null : details.getJob();
74  		if (existing == null)
75  			return;
76  		existing.setLastUpdated(new Date());
77  		existing.setStagesPassed(job.getStagesPassed());
78  		existing.setMetadata(job.getMetadata());
79  		existing.setState(job.getState());
80  	}
81  
82  	/**
83  	 * {@inheritDoc}
84  	 */
85  	public void finalizeJob(UUID jobId, Job.State finalJobState) throws IllegalArgumentException {
86  		JobDetails details = jobDetails.get(jobId);
87  		if (details == null)
88  			return;
89  
90  		if (details.getJob().getState() == Job.State.PREPARED) {
91  			if (finalJobState != null) {
92  				throw new IllegalArgumentException(
93  						"finalJobState is not 'null' of a job that is still in prepared state.");
94  			}
95  			jobDetails.remove(jobId);
96  		} else {
97  			if (finalJobState == null || !Job.FINAL_STATES.contains(finalJobState))
98  				throw new IllegalArgumentException("finalJobState not in " + Job.FINAL_STATES);
99  
100 			details.getJob().setState(finalJobState);
101 		}
102 	}
103 
104 	/**
105 	 * {@inheritDoc}
106 	 */
107 	@Override
108 	public void finalizeJob(Job job, Job.State finalJobState) throws IllegalArgumentException {
109 		finalizeJob(job.getJobId(), finalJobState);
110 	}
111 
112 	/**
113 	 * {@inheritDoc}
114 	 */
115 	public Job.State getJobState(UUID jobId) {
116 		JobDetails details = jobDetails.get(jobId);
117 		return details == null ? null : details.getJob().getState();
118 	}
119 
120 	/**
121 	 * {@inheritDoc}
122 	 */
123 	public UUIDListPage getJobsByStateInRange(Job.State jobState, Date from, Date to, int pageNumber) {
124 		if (pageNumber > 0)
125 			return null;
126 
127 		Collection<UUID> ids = new ArrayList<UUID>();
128 		for (JobDetails details : jobDetails.values()) {
129 			if (details.getJob().getState() == jobState)
130 				ids.add(details.getJob().getJobId());
131 		}
132 
133 		return new UUIDListPage(0, true, ids);
134 	}
135 
136 	/**
137 	 * {@inheritDoc}
138 	 */
139 	public Collection<SharedJob> getJobs(Collection<UUID> jobIds) {
140 		Collection<SharedJob> jobs = new ArrayList<SharedJob>(jobIds.size());
141 		for (UUID jobId : jobIds) {
142 			SharedJobDetails details = jobDetails.get(jobId);
143 			jobs.add(details == null ? null : (SharedJob) details.getJob());
144 		}
145 		return jobs;
146 	}
147 
148 	/**
149 	 * {@inheritDoc}
150 	 */
151 	public SharedJobDetails getJobDetails(UUID job) {
152 		return jobDetails.get(job);
153 	}
154 }