Skip to content

Package: JobTargets

JobTargets

nameinstructionbranchcomplexitylinemethod
JobTargets()
M: 34 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
count(ScopeId, EntityId, JobTargetQuery)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
create(ScopeId, EntityId, JobTargetCreator)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
deleteJobTarget(ScopeId, EntityId)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
executionsByTarget(ScopeId, EntityId, EntityId, int, int)
M: 32 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
find(ScopeId, EntityId, EntityId)
M: 52 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
query(ScopeId, EntityId, JobTargetQuery)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
simpleQuery(ScopeId, EntityId, String, SortOrder, boolean, int, int)
M: 39 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.app.api.resources.v1.resources;
14:
15: import javax.ws.rs.Consumes;
16: import javax.ws.rs.DELETE;
17: import javax.ws.rs.DefaultValue;
18: import javax.ws.rs.GET;
19: import javax.ws.rs.POST;
20: import javax.ws.rs.Path;
21: import javax.ws.rs.PathParam;
22: import javax.ws.rs.Produces;
23: import javax.ws.rs.QueryParam;
24: import javax.ws.rs.core.MediaType;
25: import javax.ws.rs.core.Response;
26:
27: import org.eclipse.kapua.KapuaEntityNotFoundException;
28: import org.eclipse.kapua.KapuaException;
29: import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
30: import org.eclipse.kapua.app.api.core.model.CountResult;
31: import org.eclipse.kapua.app.api.core.model.EntityId;
32: import org.eclipse.kapua.app.api.core.model.ScopeId;
33: import org.eclipse.kapua.locator.KapuaLocator;
34: import org.eclipse.kapua.model.KapuaEntityAttributes;
35: import org.eclipse.kapua.model.id.KapuaId;
36: import org.eclipse.kapua.model.query.SortOrder;
37: import org.eclipse.kapua.service.KapuaService;
38: import org.eclipse.kapua.service.job.Job;
39: import org.eclipse.kapua.service.job.execution.JobExecutionAttributes;
40: import org.eclipse.kapua.service.job.execution.JobExecutionFactory;
41: import org.eclipse.kapua.service.job.execution.JobExecutionListResult;
42: import org.eclipse.kapua.service.job.execution.JobExecutionQuery;
43: import org.eclipse.kapua.service.job.execution.JobExecutionService;
44: import org.eclipse.kapua.service.job.targets.JobTarget;
45: import org.eclipse.kapua.service.job.targets.JobTargetAttributes;
46: import org.eclipse.kapua.service.job.targets.JobTargetCreator;
47: import org.eclipse.kapua.service.job.targets.JobTargetFactory;
48: import org.eclipse.kapua.service.job.targets.JobTargetListResult;
49: import org.eclipse.kapua.service.job.targets.JobTargetQuery;
50: import org.eclipse.kapua.service.job.targets.JobTargetService;
51:
52: import com.google.common.base.Strings;
53:
54: @Path("{scopeId}/jobs/{jobId}/targets")
55: public class JobTargets extends AbstractKapuaResource {
56:
57: private final KapuaLocator locator = KapuaLocator.getInstance();
58: private final JobTargetService jobTargetService = locator.getService(JobTargetService.class);
59: private final JobExecutionService jobExecutionService = locator.getService(JobExecutionService.class);
60: private final JobTargetFactory jobTargetFactory = locator.getFactory(JobTargetFactory.class);
61: private final JobExecutionFactory jobExecutionFactory = locator.getFactory(JobExecutionFactory.class);
62:
63: /**
64: * Gets the {@link JobTarget} list for a given {@link Job}.
65: *
66: * @param scopeId The {@link ScopeId} in which to search results.
67: * @param jobId The {@link Job} id to filter results
68: * @param sortParam The name of the parameter that will be used as a sorting key
69: * @param sortDir The sort direction. Can be ASCENDING (default), DESCENDING. Case-insensitive.
70: * @param askTotalCount Ask for the total count of the matched entities in the result
71: * @param offset The result set offset.
72: * @param limit The result set limit.
73: * @return The {@link JobTargetListResult} of all the jobs targets associated to the current selected job.
74: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
75: * @since 1.0.0
76: */
77: @GET
78: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
79: public JobTargetListResult simpleQuery(
80: @PathParam("scopeId") ScopeId scopeId,
81: @PathParam("jobId") EntityId jobId,
82: @QueryParam("sortParam") String sortParam,
83: @QueryParam("sortDir") @DefaultValue("ASCENDING") SortOrder sortDir,
84: @QueryParam("askTotalCount") boolean askTotalCount,
85: @QueryParam("offset") @DefaultValue("0") int offset,
86: @QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
87: JobTargetQuery query = jobTargetFactory.newQuery(scopeId);
88:
89: query.setPredicate(query.attributePredicate(JobTargetAttributes.JOB_ID, jobId));
90:
91:• if (!Strings.isNullOrEmpty(sortParam)) {
92: query.setSortCriteria(query.fieldSortCriteria(sortParam, sortDir));
93: }
94:
95: query.setAskTotalCount(askTotalCount);
96: query.setOffset(offset);
97: query.setLimit(limit);
98:
99: return query(scopeId, jobId, query);
100: }
101:
102: /**
103: * Queries the results with the given {@link JobTargetQuery} parameter.
104: *
105: * @param scopeId The {@link ScopeId} in which to search results.
106: * @param query The {@link JobTargetQuery} to use to filter results.
107: * @return The {@link JobTargetListResult} of all the result matching the given {@link JobTargetQuery} parameter.
108: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
109: * @since 1.0.0
110: */
111: @POST
112: @Path("_query")
113: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
114: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
115: public JobTargetListResult query(
116: @PathParam("scopeId") ScopeId scopeId,
117: @PathParam("jobId") EntityId jobId,
118: JobTargetQuery query) throws KapuaException {
119: query.setScopeId(scopeId);
120: query.setPredicate(query.attributePredicate(JobTargetAttributes.JOB_ID, jobId));
121: return jobTargetService.query(query);
122: }
123:
124: /**
125: * Counts the results with the given {@link JobTargetQuery} parameter.
126: *
127: * @param scopeId The {@link ScopeId} in which to search results.
128: * @param query The {@link JobTargetQuery} to use to filter results.
129: * @return The count of all the result matching the given {@link JobTargetQuery} parameter.
130: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
131: * @since 1.0.0
132: */
133: @POST
134: @Path("_count")
135: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
136: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
137: public CountResult count(
138: @PathParam("scopeId") ScopeId scopeId,
139: @PathParam("jobId") EntityId jobId,
140: JobTargetQuery query) throws KapuaException {
141: query.setScopeId(scopeId);
142: query.setPredicate(query.attributePredicate(JobTargetAttributes.JOB_ID, jobId));
143:
144: return new CountResult(jobTargetService.count(query));
145: }
146:
147: /**
148: * Returns the Job specified by the "jobId" path parameter.
149: *
150: * @param scopeId The {@link ScopeId} of the requested {@link Job}.
151: * @param jobId The id of the requested Job.
152: * @param targetId The id of the requested JobTarget.
153: * @return The requested Job object.
154: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
155: * @since 1.0.0
156: */
157: @GET
158: @Path("{targetId}")
159: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
160: public JobTarget find(
161: @PathParam("scopeId") ScopeId scopeId,
162: @PathParam("jobId") EntityId jobId,
163: @PathParam("targetId") EntityId targetId) throws KapuaException {
164: JobTargetQuery jobTargetQuery = jobTargetFactory.newQuery(scopeId);
165: jobTargetQuery.setPredicate(jobTargetQuery.andPredicate(
166: jobTargetQuery.attributePredicate(JobTargetAttributes.JOB_ID, jobId),
167: jobTargetQuery.attributePredicate(KapuaEntityAttributes.ENTITY_ID, targetId)
168: ));
169: jobTargetQuery.setOffset(0);
170: jobTargetQuery.setLimit(1);
171: JobTargetListResult jobTargetListResult = jobTargetService.query(jobTargetQuery);
172:
173:• if (jobTargetListResult.isEmpty()) {
174: throw new KapuaEntityNotFoundException(JobTarget.TYPE, targetId);
175: }
176:
177: return jobTargetListResult.getFirstItem();
178: }
179:
180: @GET
181: @Path("{targetId}/executions")
182: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
183: public JobExecutionListResult executionsByTarget(
184: @PathParam("scopeId") ScopeId scopeId,
185: @PathParam("jobId") EntityId jobId,
186: @PathParam("targetId") EntityId targetId,
187: @QueryParam("offset") @DefaultValue("0") int offset,
188: @QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
189: JobExecutionQuery jobExecutionQuery = jobExecutionFactory.newQuery(scopeId);
190: jobExecutionQuery.setPredicate(jobExecutionQuery.attributePredicate(JobExecutionAttributes.TARGET_IDS, new KapuaId[]{ targetId }));
191: JobExecutionListResult jobExecutionListResult = jobExecutionService.query(jobExecutionQuery);
192:
193: jobExecutionQuery.setOffset(offset);
194: jobExecutionQuery.setLimit(limit);
195:
196: return jobExecutionListResult;
197: }
198:
199: /**
200: * Creates a new {@link JobTarget} based on the information provided in {@link JobTargetCreator}
201: * parameter.
202: *
203: * @param scopeId The {@link ScopeId} in which to create the {@link JobTarget}
204: * @param jobId The ID of the {@link Job} to attach the {@link JobTarget} to
205: * @param jobTargetCreator Provides the information for the new {@link JobTarget} to be created.
206: * @return The newly created {@link JobTarget} object.
207: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
208: * @since 1.5.0
209: */
210:
211: @POST
212: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
213: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
214: public Response create(
215: @PathParam("scopeId") ScopeId scopeId,
216: @PathParam("jobId") EntityId jobId,
217: JobTargetCreator jobTargetCreator) throws KapuaException {
218: jobTargetCreator.setScopeId(scopeId);
219: jobTargetCreator.setJobId(jobId);
220:
221: return returnCreated(jobTargetService.create(jobTargetCreator));
222: }
223:
224: /**
225: * Deletes the JobTarget specified by the "targetId" path parameter.
226: *
227: * @param scopeId The ScopeId of the requested {@link JobTarget}.
228: * @param targetId The id of the JobTarget to be deleted.
229: * @return HTTP 201 if operation has completed successfully.
230: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
231: * @since 1.5.0
232: */
233:
234: @DELETE
235: @Path("{targetId}")
236: public Response deleteJobTarget(
237: @PathParam("scopeId") ScopeId scopeId,
238: @PathParam("targetId") EntityId targetId) throws KapuaException {
239: jobTargetService.delete(scopeId, targetId);
240:
241: return returnNoContent();
242: }
243:
244: }