Skip to content

Commit

Permalink
Prevent adding the same job target multiple times
Browse files Browse the repository at this point in the history
Fixes #903

Signed-off-by: Claudio Mezzasalma <claudio.mezzasalma@eurotech.com>
  • Loading branch information
Claudio Mezzasalma committed Nov 8, 2017
1 parent 5c5de10 commit 33466e1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void doSubmit(List<GwtDevice> targets) {
creator.setJobTargetId(target.getId());
creatorList.add(creator);
}
GWT_JOB_TARGET_SERVICE.create(xsrfToken, creatorList, new AsyncCallback<List<GwtJobTarget>>() {
GWT_JOB_TARGET_SERVICE.create(xsrfToken, currentSession.getSelectedAccountId(), jobId, creatorList, new AsyncCallback<List<GwtJobTarget>>() {

@Override
public void onSuccess(List<GwtJobTarget> arg0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ public PagingLoadResult<GwtJobTarget> query(PagingLoadConfig loadConfig, GwtJobT
// If there are results
if (!jobTargetList.isEmpty()) {
// count
if (jobTargetList.getSize() >= loadConfig.getLimit()) {
totalLength = Long.valueOf(jobTargetService.count(jobTargetQuery)).intValue();
} else {
totalLength = jobTargetList.getSize();
}

totalLength = Long.valueOf(jobTargetService.count(jobTargetQuery)).intValue();
// Converto to GWT entity
for (JobTarget jt : jobTargetList.getItems()) {
gwtJobTargetList.add(KapuaGwtJobModelConverter.convertJobTarget(jt));
Expand All @@ -75,7 +70,7 @@ public PagingLoadResult<GwtJobTarget> query(PagingLoadConfig loadConfig, GwtJobT
KapuaExceptionHandler.handle(t);
}

return new BasePagingLoadResult<GwtJobTarget>(gwtJobTargetList, loadConfig.getOffset(), totalLength);
return new BasePagingLoadResult<GwtJobTarget>(gwtJobTargetList, loadConfig != null ? loadConfig.getOffset() : 0, totalLength);
}

@Override
Expand All @@ -102,17 +97,20 @@ public List<GwtJobTarget> findByJobId(String scopeId, String jobId, boolean fetc
}

@Override
public List<GwtJobTarget> create(GwtXSRFToken xsrfToken, List<GwtJobTargetCreator> gwtJobTargetCreatorList) throws GwtKapuaException {
public List<GwtJobTarget> create(GwtXSRFToken xsrfToken, String scopeId, String jobId, List<GwtJobTargetCreator> gwtJobTargetCreatorList) throws GwtKapuaException {
checkXSRFToken(xsrfToken);

List<GwtJobTarget> existingTargets = findByJobId(scopeId, jobId, false);
List<GwtJobTarget> gwtJobTargetList = new ArrayList<GwtJobTarget>();
try {
KapuaLocator locator = KapuaLocator.getInstance();
JobTargetFactory jobTargetFactory = locator.getFactory(JobTargetFactory.class);

for (GwtJobTargetCreator gwtJobTargetCreator : gwtJobTargetCreatorList) {
KapuaId scopeId = KapuaEid.parseCompactId(gwtJobTargetCreator.getScopeId());
JobTargetCreator jobTargetCreator = jobTargetFactory.newCreator(scopeId);
if (findExtistingTarget(gwtJobTargetCreator.getJobTargetId(), existingTargets)) {
continue;
}
KapuaId creatorScopeId = KapuaEid.parseCompactId(gwtJobTargetCreator.getScopeId());
JobTargetCreator jobTargetCreator = jobTargetFactory.newCreator(creatorScopeId);
jobTargetCreator.setJobId(GwtKapuaCommonsModelConverter.convertKapuaId(gwtJobTargetCreator.getJobId()));
jobTargetCreator.setJobTargetId(GwtKapuaCommonsModelConverter.convertKapuaId(gwtJobTargetCreator.getJobTargetId()));

Expand All @@ -131,6 +129,15 @@ public List<GwtJobTarget> create(GwtXSRFToken xsrfToken, List<GwtJobTargetCreato
return gwtJobTargetList;
}

private boolean findExtistingTarget(String jobTargetId, List<GwtJobTarget> existingTargets) {
for (GwtJobTarget existingTarget : existingTargets) {
if (existingTarget.getJobTargetId().equals(jobTargetId)) {
return true;
}
}
return false;
}

@Override
public void delete(GwtXSRFToken xsrfToken, String gwtScopeId, String gwtJobTargetId) throws GwtKapuaException {
checkXSRFToken(xsrfToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ List<GwtJobTarget> findByJobId(String scopeId, String jobId, boolean fetchDetail
/**
* Creates a new job target under the account specified in the JobTargetCreator.
*
* @param scopeId
* @param jobId
* @param gwtJobTargetCreatorList
* @return
* @throws GwtKapuaException
*/
List<GwtJobTarget> create(GwtXSRFToken xsrfToken, List<GwtJobTargetCreator> gwtJobTargetCreatorList)
List<GwtJobTarget> create(GwtXSRFToken xsrfToken, String scopeId, String jobId, List<GwtJobTargetCreator> gwtJobTargetCreatorList)
throws GwtKapuaException;

/**
Expand Down

0 comments on commit 33466e1

Please sign in to comment.