Search in sources :

Example 11 with RobotCapability

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;
}
Also used : Answer(org.cerberus.util.answer.Answer) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) RobotCapability(org.cerberus.crud.entity.RobotCapability)

Aggregations

RobotCapability (org.cerberus.crud.entity.RobotCapability)11 MessageEvent (org.cerberus.engine.entity.MessageEvent)5 Answer (org.cerberus.util.answer.Answer)4 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 Robot (org.cerberus.crud.entity.Robot)3 ILogEventService (org.cerberus.crud.service.ILogEventService)3 IRobotService (org.cerberus.crud.service.IRobotService)3 CerberusException (org.cerberus.exception.CerberusException)3 ApplicationContext (org.springframework.context.ApplicationContext)3 Gson (com.google.gson.Gson)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 IFactoryRobotCapability (org.cerberus.crud.factory.IFactoryRobotCapability)2 AnswerList (org.cerberus.util.answer.AnswerList)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 PrintWriter (java.io.PrintWriter)1 Connection (java.sql.Connection)1