Example usage for io.vertx.core.json JsonArray size

List of usage examples for io.vertx.core.json JsonArray size

Introduction

In this page you can find the example usage for io.vertx.core.json JsonArray size.

Prototype

public int size() 

Source Link

Document

Get the number of values in this JSON array

Usage

From source file:org.entcore.feeder.csv.CsvValidator.java

License:Open Source License

private void findUsers(final String path, final String profile, List<String> columns, int eii,
        final String charset, final Handler<JsonObject> handler) {
    mappingFinder.findExternalIds(structureId, path, profile, columns, eii, charset, new Handler<JsonArray>() {
        @Override//from  w  ww. j a  va  2  s  .  co m
        public void handle(JsonArray errors) {
            if (errors.size() > 0) {
                for (Object o : errors) {
                    if (!(o instanceof JsonObject))
                        continue;
                    JsonObject j = (JsonObject) o;
                    JsonArray p = j.getJsonArray("params");
                    log.info(j.encode());
                    if (p != null && p.size() > 0) {
                        addErrorByFile(profile, j.getString("key"), p.encode()
                                .substring(1, p.encode().length() - 1).replaceAll("\"", "").split(","));
                    } else if (j.getString("key") != null) {
                        addError(profile, j.getString("key"));
                    } else {
                        addError(profile, "mapping.unknown.error");
                    }
                }
                handler.handle(result);
            } else {
                //validateFile(path, profile, columns, null, handler);
                checkFile(path, profile, charset, handler);
            }
        }
    });
}

From source file:org.entcore.feeder.csv.CsvValidator.java

License:Open Source License

private void filterExternalIdExists(JsonArray externalIds, final Handler<JsonArray> handler) {
    String query = "MATCH (u:User) where u.externalId in {externalIds} return collect(u.externalId) as ids";
    TransactionManager.getNeo4jHelper().execute(query, new JsonObject().put("externalIds", externalIds),
            new Handler<Message<JsonObject>>() {
                @Override/*from   www.ja  v  a 2s. c o m*/
                public void handle(Message<JsonObject> event) {
                    JsonArray result = event.body().getJsonArray("result");
                    if ("ok".equals(event.body().getString("status")) && result.size() == 1) {
                        handler.handle(result.getJsonObject(0).getJsonArray("ids"));
                    } else {
                        handler.handle(null);
                    }
                }
            });
}

From source file:org.entcore.feeder.csv.CsvValidator.java

License:Open Source License

private void validateFile(final String path, final String profile, final List<String> columns,
        final JsonArray existExternalId, final String charset, final Handler<JsonObject> handler) {
    addProfile(profile);/*from  www  .j  ava  2 s. com*/
    final Validator validator = profiles.get(profile);
    getStructure(path.substring(0, path.lastIndexOf(File.separator)), new Handler<Structure>() {
        @Override
        public void handle(final Structure structure) {
            if (structure == null) {
                addError(profile, "invalid.structure");
                handler.handle(result);
                return;
            }
            final JsonObject checkChildExists = new JsonObject();
            try {
                CSVReader csvParser = getCsvReader(path, charset, 1);
                final int nbColumns = columns.size();
                String[] strings;
                int i = 1;
                csvParserWhile: while ((strings = csvParser.readNext()) != null) {
                    if (emptyLine(strings)) {
                        i++;
                        continue;
                    }
                    if (strings.length > nbColumns) {
                        strings = Arrays.asList(strings).subList(0, nbColumns).toArray(new String[nbColumns]);
                    }
                    //                  if (strings.length != nbColumns) {
                    //                     addErrorByFile(profile, "bad.columns.number", "" + ++i);
                    //                     continue;
                    //                  }
                    final JsonArray classesNames = new fr.wseduc.webutils.collections.JsonArray();
                    JsonObject user = new JsonObject();
                    user.put("structures",
                            new fr.wseduc.webutils.collections.JsonArray().add(structure.getExternalId()));
                    user.put("profiles", new fr.wseduc.webutils.collections.JsonArray().add(profile));
                    List<String[]> classes = new ArrayList<>();
                    for (int j = 0; j < strings.length; j++) {
                        if (j >= columns.size()) {
                            addErrorByFile(profile, "out.columns", "" + i);
                            return;
                        }
                        final String c = columns.get(j);
                        final String v = strings[j].trim();
                        if (v.isEmpty() && !c.startsWith("child"))
                            continue;
                        switch (validator.getType(c)) {
                        case "string":
                            if ("birthDate".equals(c)) {
                                Matcher m = frenchDatePatter.matcher(v);
                                if (m.find()) {
                                    user.put(c, m.group(3) + "-" + m.group(2) + "-" + m.group(1));
                                } else {
                                    user.put(c, v);
                                }
                            } else {
                                user.put(c, v);
                            }
                            break;
                        case "array-string":
                            JsonArray a = user.getJsonArray(c);
                            if (a == null) {
                                a = new fr.wseduc.webutils.collections.JsonArray();
                                user.put(c, a);
                            }
                            if (("classes".equals(c) || "subjectTaught".equals(c) || "functions".equals(c))
                                    && !v.startsWith(structure.getExternalId() + "$")) {
                                a.add(structure.getExternalId() + "$" + v);
                            } else {
                                a.add(v);
                            }
                            break;
                        case "boolean":
                            user.put(c, "true".equals(v.toLowerCase()));
                            break;
                        default:
                            Object o = user.getValue(c);
                            final String v2;
                            if ("childClasses".equals(c) && !v.startsWith(structure.getExternalId() + "$")) {
                                v2 = structure.getExternalId() + "$" + v;
                            } else {
                                v2 = v;
                            }
                            if (o != null) {
                                if (o instanceof JsonArray) {
                                    ((JsonArray) o).add(v2);
                                } else {
                                    JsonArray array = new fr.wseduc.webutils.collections.JsonArray();
                                    array.add(o).add(v2);
                                    user.put(c, array);
                                }
                            } else {
                                user.put(c, v2);
                            }
                        }
                        if ("classes".equals(c)) {
                            String eId = structure.getExternalId() + '$' + v;
                            String[] classId = new String[2];
                            classId[0] = structure.getExternalId();
                            classId[1] = eId;
                            classes.add(classId);
                            classesNames.add(v);
                        }
                    }
                    String ca;
                    long seed;
                    JsonArray classesA;
                    Object co = user.getValue("classes");
                    if (co != null && co instanceof JsonArray) {
                        classesA = (JsonArray) co;
                    } else if (co instanceof String) {
                        classesA = new fr.wseduc.webutils.collections.JsonArray().add(co);
                    } else {
                        classesA = null;
                    }
                    if ("Student".equals(profile) && classesA != null && classesA.size() == 1) {
                        seed = defaultStudentSeed;
                        ca = classesA.getString(0);
                    } else {
                        ca = String.valueOf(i);
                        seed = System.currentTimeMillis();
                    }
                    final State state;
                    final String externalId = user.getString("externalId");
                    if (externalId == null || externalId.trim().isEmpty()) {
                        generateUserExternalId(user, ca, structure, seed);
                        state = State.NEW;
                    } else {
                        if (existExternalId.contains(externalId)) {
                            state = State.UPDATED;
                            studentExternalIdMapping.put(getHashMapping(user, ca, structure, seed), externalId);
                        } else {
                            state = State.NEW;
                        }
                    }
                    switch (profile) {
                    case "Relative":
                        boolean checkChildMapping = true;
                        JsonArray linkStudents = new fr.wseduc.webutils.collections.JsonArray();
                        if ("Intitul".equals(strings[0]) && "Adresse Organisme".equals(strings[1])) {
                            break csvParserWhile;
                        }
                        user.put("linkStudents", linkStudents);
                        for (String attr : user.fieldNames()) {
                            if ("childExternalId".equals(attr)) {
                                if (checkChildMapping) {
                                    checkChildMapping = false;
                                }
                                Object o = user.getValue(attr);
                                if (o instanceof JsonArray) {
                                    for (Object c : (JsonArray) o) {
                                        linkStudents.add(c);
                                    }
                                } else {
                                    linkStudents.add(o);
                                }
                            } else if ("childUsername".equals(attr)) {
                                Object childUsername = user.getValue(attr);
                                Object childLastName = user.getValue("childLastName");
                                Object childFirstName = user.getValue("childFirstName");
                                Object childClasses;
                                if (isNotEmpty(structure.getOverrideClass())) {
                                    childClasses = structure.getOverrideClass();
                                } else {
                                    childClasses = user.getValue("childClasses");
                                }
                                if (childUsername instanceof JsonArray && childLastName instanceof JsonArray
                                        && childFirstName instanceof JsonArray
                                        && childClasses instanceof JsonArray
                                        && ((JsonArray) childClasses).size() == ((JsonArray) childLastName)
                                                .size()
                                        && ((JsonArray) childFirstName).size() == ((JsonArray) childLastName)
                                                .size()) {
                                    for (int j = 0; j < ((JsonArray) childUsername).size(); j++) {
                                        String mapping = structure.getExternalId()
                                                + ((JsonArray) childUsername).getString(j).trim()
                                                + ((JsonArray) childLastName).getString(j).trim()
                                                + ((JsonArray) childFirstName).getString(j).trim()
                                                + ((JsonArray) childClasses).getString(j).trim()
                                                + defaultStudentSeed;
                                        relativeStudentMapping(linkStudents, mapping);
                                    }
                                } else if (childUsername instanceof String && childLastName instanceof String
                                        && childFirstName instanceof String && childClasses instanceof String) {
                                    String mapping = structure.getExternalId() + childLastName.toString().trim()
                                            + childFirstName.toString().trim() + childClasses.toString().trim()
                                            + defaultStudentSeed;
                                    relativeStudentMapping(linkStudents, mapping);
                                } else {
                                    addErrorByFile(profile, "invalid.child.mapping", "" + (i + 1),
                                            "childLUsername");
                                    handler.handle(result);
                                    return;
                                }
                            } else if ("childLastName".equals(attr)
                                    && !user.fieldNames().contains("childUsername")) {
                                Object childLastName = user.getValue(attr);
                                Object childFirstName = user.getValue("childFirstName");
                                Object childClasses;
                                if (isNotEmpty(structure.getOverrideClass())) {
                                    childClasses = structure.getOverrideClass();
                                } else {
                                    childClasses = user.getValue("childClasses");
                                }
                                if (childLastName instanceof JsonArray && childFirstName instanceof JsonArray
                                        && childClasses instanceof JsonArray
                                        && ((JsonArray) childClasses).size() == ((JsonArray) childLastName)
                                                .size()
                                        && ((JsonArray) childFirstName).size() == ((JsonArray) childLastName)
                                                .size()) {
                                    for (int j = 0; j < ((JsonArray) childLastName).size(); j++) {
                                        String mapping = structure.getExternalId()
                                                + ((JsonArray) childLastName).getString(j)
                                                + ((JsonArray) childFirstName).getString(j)
                                                + ((JsonArray) childClasses).getString(j) + defaultStudentSeed;
                                        relativeStudentMapping(linkStudents, mapping);
                                    }
                                } else if (childLastName instanceof String && childFirstName instanceof String
                                        && childClasses instanceof String) {
                                    String mapping = structure.getExternalId() + childLastName.toString().trim()
                                            + childFirstName.toString().trim() + childClasses.toString().trim()
                                            + defaultStudentSeed;
                                    relativeStudentMapping(linkStudents, mapping);
                                } else {
                                    addErrorByFile(profile, "invalid.child.mapping", "" + (i + 1),
                                            "childLastName & childFirstName");
                                    handler.handle(result);
                                    return;
                                }
                            }
                        }
                        if (checkChildMapping || classesNamesMapping.size() > 0) {
                            for (Object o : linkStudents) {
                                if (!(o instanceof String))
                                    continue;
                                if (classesNamesMapping.get(o) != null) {
                                    classesNames.add(classesNamesMapping.get(o));
                                }
                            }
                            if (classesNames.size() == 0) {
                                addSoftErrorByFile(profile, "missing.student.soft", "" + (i + 1),
                                        user.getString("firstName"), user.getString("lastName"));
                            }
                        } else {
                            Object c = user.getValue("childExternalId");
                            JsonObject u = new JsonObject().put("lastName", user.getString("lastName"))
                                    .put("firstName", user.getString("firstName")).put("line", i);
                            if (c instanceof String) {
                                c = new JsonArray().add(c);
                            }
                            if (c instanceof JsonArray) {
                                for (Object ceId : ((JsonArray) c)) {
                                    if (isEmpty((String) ceId))
                                        continue;
                                    JsonArray jr = checkChildExists.getJsonArray((String) ceId);
                                    if (jr == null) {
                                        jr = new JsonArray();
                                        checkChildExists.put((String) ceId, jr);
                                    }
                                    jr.add(u);
                                }
                            }
                        }
                        break;
                    }
                    String error = validator.validate(user, acceptLanguage);
                    if (error != null) {
                        log.warn(error);
                        addErrorByFile(profile, "validator.errorWithLine", "" + (i + 1), error); // Note that 'error' is already translated
                    } else {
                        final String classesStr = Joiner.on(", ").join(classesNames);
                        if (!"Relative".equals(profile)) {
                            classesNamesMapping.put(user.getString("externalId"), classesStr);
                        }
                        addUser(profile, user.put("state", translate(state.name()))
                                .put("translatedProfile", translate(profile)).put("classesStr", classesStr));
                    }
                    i++;
                }
            } catch (Exception e) {
                addError(profile, "csv.exception");
                log.error("csv.exception", e);
            }
            if (!checkChildExists.isEmpty()) {
                final String query = "MATCH (u:User) " + "WHERE u.externalId IN {childExternalIds} "
                        + "RETURN COLLECT(u.externalId) as childExternalIds ";
                final JsonObject params = new JsonObject().put("childExternalIds",
                        new JsonArray(new ArrayList<>(checkChildExists.fieldNames())));
                Neo4j.getInstance().execute(query, params, new Handler<Message<JsonObject>>() {
                    @Override
                    public void handle(Message<JsonObject> event) {
                        if ("ok".equals(event.body().getString("status"))) {
                            JsonArray existsChilds = getOrElse(
                                    getOrElse(getOrElse(event.body().getJsonArray("result"), new JsonArray())
                                            .getJsonObject(0), new JsonObject())
                                                    .getJsonArray("childExternalIds"),
                                    new JsonArray());
                            for (String cexternalId : checkChildExists.fieldNames()) {
                                if (!existsChilds.contains(cexternalId)) {
                                    for (Object o : checkChildExists.getJsonArray(cexternalId)) {
                                        if (!(o instanceof JsonObject))
                                            continue;
                                        JsonObject user = (JsonObject) o;
                                        addSoftErrorByFile(profile, "missing.student.soft",
                                                "" + (user.getInteger("line") + 1), user.getString("firstName"),
                                                user.getString("lastName"));
                                    }
                                }
                            }
                        }
                        handler.handle(result);
                    }
                });
            } else {
                handler.handle(result);
            }
        }
    });

}

From source file:org.entcore.feeder.csv.CsvValidator.java

License:Open Source License

private void getStructure(final String path, final Handler<Structure> handler) {
    String query = "MATCH (s:Structure {externalId:{id}})"
            + "return s.id as id, s.externalId as externalId, s.UAI as UAI, s.name as name";
    TransactionManager.getNeo4jHelper().execute(query, new JsonObject().put("id", structureId),
            new Handler<Message<JsonObject>>() {
                @Override/*from ww  w  . j  a v  a 2 s .com*/
                public void handle(Message<JsonObject> event) {
                    JsonArray result = event.body().getJsonArray("result");
                    if ("ok".equals(event.body().getString("status")) && result != null && result.size() == 1) {
                        final Structure s = new Structure(result.getJsonObject(0));
                        try {
                            final JsonObject structure = CSVUtil.getStructure(path);
                            final String overrideClass = structure.getString("overrideClass");
                            if (isNotEmpty(overrideClass)) {
                                s.setOverrideClass(overrideClass);
                            }
                        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
                            log.error("Error parsing structure path : " + path, e);
                        }
                        handler.handle(s);
                    } else {
                        try {
                            final JsonObject structure = CSVUtil.getStructure(path);
                            final String overrideClass = structure.getString("overrideClass");
                            final Structure s = new Structure(structure);
                            if (isNotEmpty(overrideClass)) {
                                s.setOverrideClass(overrideClass);
                            }
                            handler.handle(s);
                        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
                            handler.handle(null);
                        }
                    }
                }
            });
}

From source file:org.entcore.feeder.csv.MappingFinder.java

License:Open Source License

public void findExternalIds(final String structureId, final String path, final String profile,
        final List<String> columns, final int eII, final String charset, final Handler<JsonArray> handler) {
    final boolean additionalColumn;
    final int externalIdIdx;
    if (eII >= 0) {
        additionalColumn = false;//w  ww .j  ava 2 s .co m
        externalIdIdx = eII;
    } else {
        additionalColumn = true;
        externalIdIdx = 0;
    }
    final JsonArray errors = new fr.wseduc.webutils.collections.JsonArray();
    String filter = "";
    if ("Student".equals(profile)) {
        filter = "AND u.birthDate = {birthDate} ";
    }
    final String query = "MATCH (s:Structure {externalId : {id}})<-[:DEPENDS]-(:ProfileGroup)<-[:IN]-(u:User) "
            + "WHERE u.firstNameSearchField = {firstName} AND u.lastNameSearchField = {lastName} AND head(u.profiles) = {profile} "
            + filter + "RETURN DISTINCT u.externalId as externalId";
    final List<String[]> lines = new ArrayList<>();
    final TransactionHelper tx;
    try {
        tx = TransactionManager.getTransaction();
    } catch (TransactionException e) {
        addError(errors, "transaction.error");
        handler.handle(errors);
        return;
    }

    try {
        CSVReader csvReader = getCsvReader(path, charset);
        final int nbColumns = columns.size();
        String[] values;
        int rowIdx = 0;
        while ((values = csvReader.readNext()) != null) {
            if (emptyLine(values)) {
                continue;
            }
            if (values.length > nbColumns) {
                values = Arrays.asList(values).subList(0, nbColumns).toArray(new String[nbColumns]);
            } else if (values.length < nbColumns) {
                values = Arrays.copyOf(values, nbColumns);
            }
            final List<String> line = new LinkedList<>(Arrays.asList(values));
            if (additionalColumn) {
                line.add(0, "");
            }
            lines.add(line.toArray(new String[line.size()]));
            if (rowIdx == 0) {
                if (additionalColumn) {
                    lines.get(0)[externalIdIdx] = "externalId";
                }
                rowIdx++;
                continue;
            }

            final JsonObject params = new JsonObject();
            if (!additionalColumn && values[externalIdIdx] != null && !values[externalIdIdx].isEmpty()) {
                tx.add(NOP_QUERY, params.put("externalId", values[externalIdIdx]));
            } else {
                params.put("id", structureId).put("profile", profile);
                try {
                    int i = 0;
                    for (String c : columns) {
                        //   if (i >=  values.length) break;
                        switch (c) {
                        case "lastName":
                            params.put("lastName", sanitize(values[i]));
                            break;
                        case "firstName":
                            params.put("firstName", sanitize(values[i]));
                            break;
                        case "birthDate":
                            if ("Student".equals(profile)) {
                                Matcher m;

                                if (values[i] != null
                                        && (m = CsvFeeder.frenchDatePatter.matcher(values[i])).find()) {
                                    params.put("birthDate", m.group(3) + "-" + m.group(2) + "-" + m.group(1));
                                } else {
                                    params.put("birthDate", values[i]);
                                }
                            }
                            break;
                        }
                        i++;
                    }
                } catch (Exception e) {
                    errors.add(new JsonObject().put("key", "parse.line.error").put("params",
                            new fr.wseduc.webutils.collections.JsonArray().add(Integer.toString(rowIdx))));
                }

                tx.add(query, params);
            }
            rowIdx++;
        }
    } catch (Exception e) {
        addError(errors, "error.read.file", path);
        handler.handle(errors);
    }
    tx.commit(new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> event) {
            JsonArray results = event.body().getJsonArray("results");
            if ("ok".equals(event.body().getString("status")) && results != null
                    && results.size() + 1 == lines.size()) {
                for (int i = 0; i < results.size(); i++) {
                    JsonArray line = results.getJsonArray(i);
                    if (line.size() == 1) { // Si 0 ou plusieurs utilisateurs, on laisse la ligne d'origine
                        String eId = line.getJsonObject(0).getString("externalId", "");
                        lines.get(i + 1)[externalIdIdx] = eId;
                    }
                }
                vertx.fileSystem().deleteBlocking(path);

                try {
                    CSVWriter writer = getCsvWriter(path, charset);
                    writer.writeAll(lines);
                    writer.close();
                } catch (IOException e) {
                    log.error("Error writing file.", e);
                    addError(errors, "error.write.file", path);
                }
                if ("Relative".equals(profile) && columns.contains("childLastName")
                        && !columns.contains("childExternalId")) {
                    if (additionalColumn) {
                        columns.add(0, "externalId");
                    }
                    findChildExternalIds(structureId, path, charset, columns, errors, handler);
                } else {
                    handler.handle(errors);
                }
            } else {
                addError(errors, "error.find.ids");
                handler.handle(errors);
            }
        }
    });
}

From source file:org.entcore.feeder.csv.MappingFinder.java

License:Open Source License

private void findChildExternalIds(final String structureId, final String path, final String charset,
        final List<String> columns, final JsonArray errors, final Handler<JsonArray> handler) {
    final List<String[]> lines = new ArrayList<>();

    final JsonArray childLastNameIndex = new fr.wseduc.webutils.collections.JsonArray();
    final JsonArray childUsernameIndex = new fr.wseduc.webutils.collections.JsonArray();
    int idx = 0;//from  ww  w  .  j a v a2  s.com
    for (String c : columns) {
        if ("childLastName".equals(c)) {
            childLastNameIndex.add(idx);
        } else if ("childUsername".equals(c)) {
            childUsernameIndex.add(idx);
        }
        idx++;
    }
    if (childLastNameIndex.size() == 0) {
        addError(errors, "missing.childLastName");
        handler.handle(errors);
        return;
    } else if (childUsernameIndex.size() != 0 && childLastNameIndex.size() != childUsernameIndex.size()) {
        addError(errors, "mismatch.childLastName.childUsername");
        handler.handle(errors);
        return;
    }

    final int maxNbChild = childLastNameIndex.size();
    final int appendIdx;
    if (childUsernameIndex.size() > 0) {
        appendIdx = childLastNameIndex.getInteger(0) > childUsernameIndex.getInteger(0)
                ? childUsernameIndex.getInteger(0)
                : childLastNameIndex.getInteger(0);
    } else {
        appendIdx = childLastNameIndex.getInteger(0);
    }
    final String query = "MATCH (s:Structure {externalId : {id}})<-[:DEPENDS]-(:ProfileGroup)<-[:IN]-(u:User) "
            + "WHERE u.firstNameSearchField = {firstName} AND u.lastNameSearchField = {lastName} AND head(u.profiles) = 'Student' "
            + "RETURN DISTINCT u.externalId as externalId, {rowIdx} as line, {itemIdx} as item ";
    final TransactionHelper tx;
    try {
        tx = TransactionManager.getTransaction();
    } catch (TransactionException e) {
        addError(errors, "transaction.error");
        handler.handle(errors);
        return;
    }
    try {
        CSVReader csvReader = getCsvReader(path, charset);
        String[] values;
        int rowIdx = 0;
        while ((values = csvReader.readNext()) != null) {
            if (emptyLine(values)) {
                continue;
            }

            final List<String> line = new LinkedList<>(Arrays.asList(values));
            for (int i = 0; i < maxNbChild; i++) {
                if (rowIdx == 0) {
                    line.add(appendIdx, "childExternalId");
                } else {
                    line.add(appendIdx, "");
                }
            }
            lines.add(line.toArray(new String[line.size()]));
            if (rowIdx == 0) {
                rowIdx++;
                continue;
            }

            final JsonArray firstNames = new fr.wseduc.webutils.collections.JsonArray();
            final JsonArray lastNames = new fr.wseduc.webutils.collections.JsonArray();
            try {
                int i = 0;
                for (String c : columns) {
                    if (i >= values.length)
                        break;
                    switch (c) {
                    case "childLastName":
                        lastNames.add(sanitize(values[i]));
                        break;
                    case "childFirstName":
                        firstNames.add(sanitize(values[i]));
                        break;
                    }
                    i++;
                }
            } catch (Exception e) {
                errors.add(new JsonObject().put("key", "parse.line.error").put("params",
                        new fr.wseduc.webutils.collections.JsonArray().add(Integer.toString(rowIdx))));
            }
            final int fns = firstNames.size();
            if (fns != lastNames.size()) {
                errors.add(new JsonObject().put("key", "child.lastName.firstName.mismatch").put("params",
                        new fr.wseduc.webutils.collections.JsonArray().add(Integer.toString(rowIdx))));
            } else if (fns > 0) {
                //               if (fns > maxNbChild) {
                //                  maxNbChild = fns;
                //               }
                for (int i = 0; i < fns; i++) {
                    JsonObject params = new JsonObject().put("id", structureId)
                            .put("firstName", firstNames.getString(i)).put("lastName", lastNames.getString(i))
                            .put("rowIdx", rowIdx).put("itemIdx", i);
                    tx.add(query, params);
                }
            }
            rowIdx++;
        }
    } catch (Exception e) {
        addError(errors, "error.read.file", path);
        handler.handle(errors);
    }
    tx.commit(new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> event) {
            JsonArray results = event.body().getJsonArray("results");
            if ("ok".equals(event.body().getString("status")) && results != null) {
                for (int i = 0; i < results.size(); i++) {
                    JsonArray item = results.getJsonArray(i);
                    if (item.size() == 1) { // Si 0 ou plusieurs utilisateurs, on laisse la ligne d'origine
                        String eId = item.getJsonObject(0).getString("externalId", "");
                        int lineIdx = item.getJsonObject(0).getInteger("line", -1);
                        int itemIdx = item.getJsonObject(0).getInteger("item", -1);
                        if (lineIdx > 0 && itemIdx >= 0) {
                            String[] line = lines.get(lineIdx);
                            line[itemIdx + appendIdx] = eId;
                            line[childLastNameIndex.getInteger(itemIdx) + maxNbChild] = "";
                            if (childUsernameIndex.size() > 0) {
                                line[childUsernameIndex.getInteger(itemIdx) + maxNbChild] = "";
                            }
                        }
                    }
                }
                vertx.fileSystem().deleteBlocking(path);

                try {
                    CSVWriter writer = getCsvWriter(path, charset);
                    writer.writeAll(lines);
                    writer.close();
                } catch (IOException e) {
                    log.error("Error writing file.", e);
                    addError(errors, "error.write.file", path);
                }
                handler.handle(errors);
            } else {
                addError(errors, "error.find.ids");
                handler.handle(errors);
            }
        }
    });
}

From source file:org.entcore.feeder.dictionary.structures.DuplicateUsers.java

License:Open Source License

public DuplicateUsers(JsonArray sourcesPriority, boolean updateCourses, boolean autoMergeOnlyInSameStructure,
        EventBus eb) {//from  w ww .j  av  a  2 s .co  m
    if (sourcesPriority == null) {
        sourcesPriority = new fr.wseduc.webutils.collections.JsonArray().add("AAF").add("AAF1D").add("CSV")
                .add("EDT").add("UDT").add("MANUAL");
    }
    final int size = sourcesPriority.size();
    for (int i = 0; i < size; i++) {
        sourcePriority.put(sourcesPriority.getString(i), size - i);
    }
    this.eb = eb;
    this.updateCourses = updateCourses;
    this.autoMergeOnlyInSameStructure = autoMergeOnlyInSameStructure;
}

From source file:org.entcore.feeder.dictionary.structures.DuplicateUsers.java

License:Open Source License

public void markDuplicates(final Message<JsonObject> message, final Handler<JsonObject> handler) {
    final String now = DateTime.now().toString();
    final String query = "MATCH (s:System {name : 'Starter'}) return s.lastSearchDuplicates as lastSearchDuplicates ";
    TransactionManager.getNeo4jHelper().execute(query, new JsonObject(), new Handler<Message<JsonObject>>() {
        @Override/* w w w.j  av  a  2  s.c  o m*/
        public void handle(Message<JsonObject> event) {
            JsonArray res = event.body().getJsonArray("result");
            if ("ok".equals(event.body().getString("status")) && res != null && res.size() == 1
                    && res.getJsonObject(0).getString("lastSearchDuplicates") != null) {
                final String last = res.getJsonObject(0).getString("lastSearchDuplicates");
                final String[] profiles = ManualFeeder.profiles.keySet()
                        .toArray(new String[ManualFeeder.profiles.keySet().size()]);
                final Handler[] handlers = new Handler[profiles.length + 1];
                final long start = System.currentTimeMillis();
                handlers[handlers.length - 1] = new Handler<Void>() {
                    @Override
                    public void handle(Void v) {
                        final String updateDate = "MATCH (s:System {name : 'Starter'}) set s.lastSearchDuplicates = {now} ";
                        TransactionManager.getNeo4jHelper().execute(updateDate,
                                new JsonObject().put("now", now), new Handler<Message<JsonObject>>() {
                                    @Override
                                    public void handle(Message<JsonObject> event) {
                                        if (!"ok".equals(event.body().getString("status"))) {
                                            log.error("Error updating last search duplicate date : "
                                                    + event.body().getString("message"));
                                        }
                                    }
                                });
                        log.info("Mark duplicates users finished - elapsed time "
                                + (System.currentTimeMillis() - start) + " ms.");
                        if (message != null) {
                            message.reply(new JsonObject().put("status", "ok"));
                        }
                        if (handler != null) {
                            handler.handle(new JsonObject().put("status", "ok"));
                        }
                    }
                };
                for (int i = profiles.length - 1; i >= 0; i--) {
                    final int j = i;
                    handlers[i] = new Handler<Void>() {
                        @Override
                        public void handle(Void v) {
                            searchDuplicatesByProfile(last, profiles[j], handlers[j + 1]);
                        }
                    };
                }
                handlers[0].handle(null);
            } else {
                log.warn("lastSearchDuplicates not found.");
                message.reply(new JsonObject().put("status", "ok"));
            }
        }
    });
}

From source file:org.entcore.feeder.dictionary.structures.DuplicateUsers.java

License:Open Source License

public void listDuplicates(final Message<JsonObject> message) {
    JsonArray structures = message.body().getJsonArray("structures");
    boolean inherit = message.body().getBoolean("inherit");
    final Integer minScore = message.body().getInteger("minScore");
    final boolean inSameStructure = message.body().getBoolean("inSameStructure", false);

    final String filter = (minScore != null) ? ((inSameStructure) ? "AND" : "WHERE") + " r.score >= {minScore} "
            : "";
    String query;//from  w  ww . j a  v a2  s.co  m
    if (structures != null && structures.size() > 0) {
        if (inherit) {
            query = "MATCH (s:Structure)<-[:HAS_ATTACHMENT*0..]-(so:Structure)<-[:DEPENDS]-(pg:ProfileGroup) ";
        } else {
            query = "MATCH (s:Structure)<-[:DEPENDS]-(pg:ProfileGroup) ";
        }
        query += "WHERE s.id IN {structures} " + "WITH COLLECT(pg.id) as groupIds "
                + "MATCH (g1:ProfileGroup)<-[:IN]-(u1:User)-[r:DUPLICATE]->(u2:User)-[:IN]->(g2:ProfileGroup) "
                + "WHERE g1.id IN groupIds AND g2.id IN groupIds " + "MATCH (s1:Structure)<-[:DEPENDS]-(g1) "
                + filter + "OPTIONAL MATCH (s2:Structure)<-[:DEPENDS]-(g2) ";
        query += "RETURN r.score as score, "
                + "{id: u1.id, firstName: u1.firstName, lastName: u1.lastName, birthDate: u1.birthDate, email: u1.email, profiles: u1.profiles, structures: collect(distinct s1.id)} as user1, "
                + "{id: u2.id, firstName: u2.firstName, lastName: u2.lastName, birthDate: u2.birthDate, email: u2.email, profiles: u2.profiles, structures: collect(distinct s2.id)} as user2 "
                + "ORDER BY score DESC";
    } else {
        if (inSameStructure) {
            query = "match (s:Structure)<-[:DEPENDS]-(:ProfileGroup)<-[:IN]-(u1:User)-[r:DUPLICATE]-(u2:User) WHERE u2-[:IN]->(:ProfileGroup)-[:DEPENDS]->(s) ";
        } else {
            query = "MATCH (u1:User)-[r:DUPLICATE]->(u2:User) ";
        }
        query += filter + "RETURN r.score as score, "
                + "{id: u1.id, firstName: u1.firstName, lastName: u1.lastName, birthDate: u1.birthDate, email: u1.email, profiles: u1.profiles} as user1, "
                + "{id: u2.id, firstName: u2.firstName, lastName: u2.lastName, birthDate: u2.birthDate, email: u2.email, profiles: u2.profiles} as user2 "
                + "ORDER BY score DESC";
    }
    JsonObject params = new JsonObject().put("structures", structures).put("minScore", minScore);
    TransactionManager.getNeo4jHelper().execute(query, params, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> event) {
            message.reply(event.body());
        }
    });
}

From source file:org.entcore.feeder.dictionary.structures.DuplicateUsers.java

License:Open Source License

public void mergeDuplicate(final Message<JsonObject> message, final TransactionHelper tx) {
    String userId1 = message.body().getString("userId1");
    String userId2 = message.body().getString("userId2");
    if (userId1 == null || userId2 == null || userId1.trim().isEmpty() || userId2.trim().isEmpty()) {
        message.reply(new JsonObject().put("status", "error").put("message", "invalid.id"));
        return;//from  w  w  w .java2  s  .c o m
    }
    String query = "MATCH (u1:User {id: {userId1}})-[r:DUPLICATE]-(u2:User {id: {userId2}}) "
            + "RETURN DISTINCT u1.id as userId1, u1.source as source1, NOT(HAS(u1.activationCode)) as activatedU1, "
            + "u1.disappearanceDate as disappearanceDate1, u1.deleteDate as deleteDate1, "
            + "u2.id as userId2, u2.source as source2, NOT(HAS(u2.activationCode)) as activatedU2, "
            + "u2.disappearanceDate as disappearanceDate2, u2.deleteDate as deleteDate2";
    JsonObject params = new JsonObject().put("userId1", userId1).put("userId2", userId2);
    TransactionManager.getNeo4jHelper().execute(query, params, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> event) {
            JsonArray res = event.body().getJsonArray("result");
            JsonObject error = new JsonObject().put("status", "error");
            if ("ok".equals(event.body().getString("status")) && res != null && res.size() == 1) {
                JsonObject r = res.getJsonObject(0);
                if (r.getBoolean("activatedU1", true) && r.getBoolean("activatedU2", true)) {
                    message.reply(error.put("message", "two.users.activated"));
                } else {
                    mergeDuplicate(r, message, tx);
                }
            } else if ("ok".equals(event.body().getString("status"))) {
                message.reply(error.put("message", "not.found.duplicate"));
            } else {
                message.reply(event.body());
            }
        }
    });
}