Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate #204

Merged
merged 6 commits into from
Sep 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
package com.epam.ta.reportportal.core.item;

import com.epam.ta.reportportal.database.dao.LaunchRepository;
import com.epam.ta.reportportal.database.dao.LogRepository;
import com.epam.ta.reportportal.database.dao.ProjectRepository;
import com.epam.ta.reportportal.database.dao.TestItemRepository;
import com.epam.ta.reportportal.database.entity.Launch;
import com.epam.ta.reportportal.database.entity.Project;
import com.epam.ta.reportportal.database.entity.item.Parameter;
import com.epam.ta.reportportal.database.entity.item.TestItem;
import com.google.common.base.Strings;
Expand Down Expand Up @@ -67,6 +70,12 @@ public class TestItemUniqueIdGenerator implements UniqueIdGenerator {

private LaunchRepository launchRepository;

@Autowired
private LogRepository logRepository;

@Autowired
private ProjectRepository projectRepository;

private MongoOperations mongoOperations;

@Autowired
Expand Down Expand Up @@ -109,19 +118,38 @@ public void generateForAll() {
while (itemIterator.hasNext()) {
TestItem next = itemIterator.next();
if (next != null) {
String item = generate(next);
next.setUniqueId(item);
testItems.add(next);
if (testItems.size() == BATCH_SIZE || !itemIterator.hasNext()) {
testItemRepository.save(testItems);
testItems = new ArrayList<>(BATCH_SIZE);
boolean isRemoved = removeIfInvalid(next);
if (!isRemoved) {
String item = generate(next);
next.setUniqueId(item);
testItems.add(next);
if (testItems.size() == BATCH_SIZE || !itemIterator.hasNext()) {
testItemRepository.save(testItems);
testItems = new ArrayList<>(BATCH_SIZE);
}
}
}
}
}
mongoOperations.getCollection(COLLECTION).drop();
}

private boolean removeIfInvalid(TestItem next) {
String launchRef = next.getLaunchRef();
Launch one = launchRepository.findOne(launchRef);
if (one == null) {
testItemRepository.delete(next.getId());
return true;
} else {
Project project = projectRepository.findByName(one.getProjectRef());
if (project == null) {
launchRepository.delete(launchRef);
return true;
}
}
return false;
}

private CloseableIterator<TestItem> getItemIterator() {
return mongoOperations.stream(new Query().addCriteria(Criteria.where("uniqueId").exists(false)), TestItem.class);
}
Expand Down