use of org.cerberus.crud.entity.RobotCapability in project cerberus-source by cerberustesting.
the class RobotCapabilityService method compareListAndUpdateInsertDeleteElements.
@Override
public Answer compareListAndUpdateInsertDeleteElements(String robot, List<RobotCapability> newCapabilities) {
// Check arguments
if (robot == null || newCapabilities == null) {
return new Answer(new MessageEvent(MessageEventEnum.DATA_OPERATION_VALIDATIONS_ERROR).resolveDescription("DESCRIPTION", "null robot or capabilities"));
}
// Get the existing capabilities
AnswerList<RobotCapability> existingCapabilities = readByRobot(robot);
if (!existingCapabilities.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
return existingCapabilities;
}
List<RobotCapability> oldCapabilities = existingCapabilities.getDataList();
Answer finalAnswer = new Answer(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));
// Process smart udpate (only entities which have to be updated)
List<RobotCapability> sameKeys = new ArrayList<>();
List<RobotCapability> toUpdate = new ArrayList<>();
for (RobotCapability oldCapability : oldCapabilities) {
for (RobotCapability newCapability : newCapabilities) {
if (oldCapability.hasSameKey(newCapability)) {
sameKeys.add(oldCapability);
sameKeys.add(newCapability);
if (!oldCapability.equals(newCapability)) {
toUpdate.add(newCapability);
}
break;
}
}
}
AnswerUtil.agregateAnswer(finalAnswer, update(toUpdate));
// Process delete
List<RobotCapability> toDelete = new ArrayList<>(oldCapabilities);
toDelete.removeAll(sameKeys);
AnswerUtil.agregateAnswer(finalAnswer, delete(toDelete));
// Process create
List<RobotCapability> toCreate = new ArrayList<>(newCapabilities);
toCreate.removeAll(sameKeys);
AnswerUtil.agregateAnswer(finalAnswer, create(toCreate));
// Finally return the aggregated answer
return finalAnswer;
}
Aggregations