Example usage for io.vertx.core.json JsonObject getString

List of usage examples for io.vertx.core.json JsonObject getString

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject getString.

Prototype

public String getString(String key) 

Source Link

Document

Get the string value with the specified key, special cases are addressed for extended JSON types Instant , byte[] and Enum which can be converted to String.

Usage

From source file:org.entcore.feeder.aaf1d.PersonnelImportProcessing1d.java

License:Open Source License

@Override
public void process(JsonObject object) {
    String profile = detectProfile(object);
    object.put("profiles", new fr.wseduc.webutils.collections.JsonArray()
            .add((TEACHER_PROFILE_EXTERNAL_ID.equals(profile) ? "Teacher" : "Personnel")));
    String email = object.getString("email");
    if (email != null && !email.trim().isEmpty()) {
        object.put("emailAcademy", email);
    }//from   w w w . j a va2  s  . c  o m
    JsonArray functions = object.getJsonArray("functions");
    JsonArray structuresByFunctions = null;
    if (functions != null) {
        Set<String> s = new HashSet<>();
        for (Object o : functions) {
            if (!(o instanceof String) || !o.toString().contains("$"))
                continue;
            s.add(o.toString().substring(0, o.toString().indexOf('$')));
        }
        structuresByFunctions = new fr.wseduc.webutils.collections.JsonArray(new ArrayList<>(s));
    }
    importer.createOrUpdatePersonnel(object, profile, structuresByFunctions, null, null, true, true);
}

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

License:Open Source License

private void parse(final Importer importer, final String p, final Handler<Message<JsonObject>> handler) {
    vertx.fileSystem().readDir(p, new Handler<AsyncResult<List<String>>>() {
        @Override//from   w  w w.  j a v a2s . co m
        public void handle(AsyncResult<List<String>> event) {
            if (event.succeeded() && event.result().size() == 1) {
                final String path = event.result().get(0);
                final Structure s;
                try {
                    JsonObject structure = CSVUtil.getStructure(path);
                    final String overrideClass = structure.getString("overrideClass");
                    //         final boolean isUpdate = importer.getStructure(structure.getString("externalId")) != null;
                    s = importer.createOrUpdateStructure(structure);
                    if (s == null) {
                        log.error("Structure error with directory " + path + ".");
                        handler.handle(new ResultMessage().error("structure.error"));
                        return;
                    }
                    if (isNotEmpty(overrideClass)) {
                        s.setOverrideClass(overrideClass);
                    }
                } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
                    log.error("Structure error with directory " + path + ".");
                    handler.handle(new ResultMessage().error("structure.error"));
                    return;
                }
                vertx.fileSystem().readDir(path, new Handler<AsyncResult<List<String>>>() {
                    @Override
                    public void handle(final AsyncResult<List<String>> event) {
                        if (event.succeeded()) {
                            checkNotModifiableExternalId(event.result(), path,
                                    new Handler<Message<JsonObject>>() {
                                        @Override
                                        public void handle(Message<JsonObject> m) {
                                            if ("ok".equals(m.body().getString("status"))) {
                                                launchFiles(path, event.result(), s, importer, handler);
                                            } else {
                                                handler.handle(m);
                                            }
                                        }
                                    });
                        } else {
                            handler.handle(new ResultMessage().error("error.list.files"));
                        }
                    }
                });
            } else {
                handler.handle(new ResultMessage().error("error.list.files"));
            }
        }
    });
}

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

License:Open Source License

public void start(final String profile, final Structure structure, String file, String charset,
        final Importer importer, final Handler<Message<JsonObject>> handler) {
    importer.getReport().addProfile(profile);
    importer.createOrUpdateProfile(STUDENT_PROFILE);
    importer.createOrUpdateProfile(RELATIVE_PROFILE);
    importer.createOrUpdateProfile(PERSONNEL_PROFILE);
    importer.createOrUpdateProfile(TEACHER_PROFILE);
    importer.createOrUpdateProfile(GUEST_PROFILE);
    DefaultFunctions.createOrUpdateFunctions(importer);

    final Validator validator = ManualFeeder.profiles.get(profile);
    //      final Structure structure = importer.getStructure(structureExternalId);
    //      if (structure == null) {
    //         handler.handle(new ResultMessage().error("invalid.structure"));
    //         return;
    //      }//from ww  w. ja  v a  2s. c  o  m

    try {
        CSVReader csvParser = getCsvReader(file, charset);
        final List<String> columns = new ArrayList<>();
        int nbColumns = 0;
        String[] strings;
        int i = 0;
        csvParserWhile: while ((strings = csvParser.readNext()) != null) {
            if (i == 0) {
                columnsMapper.getColumsNames(strings, columns, profile, handler);
                nbColumns = columns.size();
            } else if (!columns.isEmpty()) {
                if (emptyLine(strings)) {
                    i++;
                    continue;
                }
                if (strings.length > nbColumns) {
                    strings = Arrays.asList(strings).subList(0, nbColumns).toArray(new String[nbColumns]);
                }
                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<>();

                // Class Admin
                if (isNotEmpty(structure.getOverrideClass())) {
                    String eId = structure.getOverrideClass(); // structure.getExternalId() + '$' + structure.getOverrideClass();
                    //structure.createClassIfAbsent(eId, structure.getOverrideClass());
                    final String[] classId = new String[3];
                    classId[0] = structure.getExternalId();
                    classId[1] = eId;
                    classId[2] = "";
                    classes.add(classId);
                }

                for (int j = 0; j < strings.length; j++) {
                    final String c = columns.get(j);
                    final String v = strings[j].trim();
                    if ((v.isEmpty() && !c.startsWith("child"))
                            || ("classes".equals(c) && isNotEmpty(structure.getOverrideClass())))
                        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[] cc = v.split("\\$");
                        if (cc.length == 2 && !cc[1].matches("[0-9]+")) {
                            final String fosEId = importer.getFieldOfStudy().get(cc[1]);
                            if (fosEId != null) {
                                cc[1] = fosEId;
                            }
                        }
                        String eId = structure.getExternalId() + '$' + cc[0];
                        structure.createClassIfAbsent(eId, cc[0]);
                        final String[] classId = new String[3];
                        classId[0] = structure.getExternalId();
                        classId[1] = eId;
                        classId[2] = (cc.length == 2) ? cc[1] : "";
                        classes.add(classId);
                    }
                }
                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();
                }
                generateUserExternalId(user, ca, structure, seed);
                if ("Student".equals(profile)) {
                    studentExternalIdMapping.put(getHashMapping(user, ca, structure, seed),
                            user.getString("externalId"));
                }
                switch (profile) {
                case "Teacher":
                    importer.createOrUpdatePersonnel(user, TEACHER_PROFILE_EXTERNAL_ID,
                            user.getJsonArray("structures"), classes.toArray(new String[classes.size()][2]),
                            null, true, true);
                    break;
                case "Personnel":
                    importer.createOrUpdatePersonnel(user, PERSONNEL_PROFILE_EXTERNAL_ID,
                            user.getJsonArray("structures"), classes.toArray(new String[classes.size()][2]),
                            null, true, true);
                    break;
                case "Student":
                    importer.createOrUpdateStudent(user, STUDENT_PROFILE_EXTERNAL_ID, null, null,
                            classes.toArray(new String[classes.size()][2]), null, null, true, true);
                    break;
                case "Relative":
                    if (("Intitul".equals(strings[0]) && "Adresse Organisme".equals(strings[1]))
                            || ("".equals(strings[0]) && "Intitul".equals(strings[1])
                                    && "Adresse Organisme".equals(strings[2]))) {
                        break csvParserWhile;
                    }
                    JsonArray linkStudents = new fr.wseduc.webutils.collections.JsonArray();
                    for (String attr : user.fieldNames()) {
                        if ("childExternalId".equals(attr)) {
                            Object o = user.getValue(attr);
                            if (o instanceof JsonArray) {
                                for (Object c : (JsonArray) o) {
                                    if (c instanceof String && !((String) c).trim().isEmpty()) {
                                        linkStudents.add(c);
                                    }
                                }
                            } else if (o instanceof String && !((String) o).trim().isEmpty()) {
                                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 {
                                handler.handle(new ResultMessage().error("invalid.child.mapping"));
                                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).trim()
                                            + ((JsonArray) childFirstName).getString(j).trim()
                                            + ((JsonArray) childClasses).getString(j).trim()
                                            + 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 {
                                handler.handle(new ResultMessage().error("invalid.child.mapping"));
                                return;
                            }
                        }
                    }
                    importer.createOrUpdateUser(user, linkStudents, true);
                    break;
                case "Guest":
                    importer.createOrUpdateGuest(user, classes.toArray(new String[classes.size()][2]));
                    break;
                }
            }
            i++;
        }

        switch (profile) {
        case "Teacher":
            importer.getPersEducNat().createAndLinkSubjects(structure.getExternalId());
            break;
        case "Relative":
            importer.linkRelativeToClass(RELATIVE_PROFILE_EXTERNAL_ID, null, structure.getExternalId());
            importer.linkRelativeToStructure(RELATIVE_PROFILE_EXTERNAL_ID, null, structure.getExternalId());
            importer.addRelativeProperties(getSource());
            break;
        }
    } catch (Exception e) {
        handler.handle(new ResultMessage().error("csv.exception"));
        log.error("csv.exception", e);
    }
    //      importer.markMissingUsers(structure.getExternalId(), new Handler<Void>() {
    //         @Override
    //         public void handle(Void event) {
    //            importer.persist(handler);
    //         }
    //      });
    //   importer.persist(handler);
}

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

License:Open Source License

public static void generateUserExternalId(JsonObject props, String c, Structure structure, long seed) {
    String externalId = props.getString("externalId");
    if (externalId != null && !externalId.trim().isEmpty()) {
        return;//w  w w .  j a va 2s  . com
    }
    String hash = getHashMapping(props, c, structure, seed);
    if (hash != null) {
        props.put("externalId", hash);
    }
}

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

License:Open Source License

public CsvImportsLauncher(Vertx vertx, String path, JsonObject config, PostImport postImport) {
    this.vertx = vertx;
    this.path = path;
    this.profiles = config.getJsonObject("profiles");
    this.namePattern = Pattern.compile(config.getString("namePattern"));
    this.postImport = postImport;
    this.preDelete = config.getBoolean("preDelete", false);
}

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 w  w.j  ava2 s  .c o  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 validateFile(final String path, final String profile, final List<String> columns,
        final JsonArray existExternalId, final String charset, final Handler<JsonObject> handler) {
    addProfile(profile);/*from   w w w. j  a  v a2s  . c om*/
    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   www .j a  v  a 2  s .  c om*/
                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.dictionary.structures.DefaultFunctions.java

License:Open Source License

public static void createOrUpdateFunctions(Importer importer) {
    Profile p = importer.getProfile(PERSONNEL_PROFILE_EXTERNAL_ID);
    if (p != null) {
        JsonObject f = DefaultFunctions.ADMIN_LOCAL;
        p.createFunctionIfAbsent(f.getString("externalId"), f.getString("name"));
    }/* ww  w .  j  a v a2s .  com*/
    Profile t = importer.getProfile(TEACHER_PROFILE_EXTERNAL_ID);
    if (t != null) {
        JsonObject f = DefaultFunctions.CLASS_ADMIN;
        t.createFunctionIfAbsent(f.getString("externalId"), f.getString("name"));
    }
}

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

License:Open Source License

private void mergeDuplicate(final JsonObject r, final Message<JsonObject> message, final TransactionHelper tx) {
    final String source1 = r.getString("source1");
    final String source2 = r.getString("source2");
    final boolean activatedU1 = r.getBoolean("activatedU1", false);
    final boolean activatedU2 = r.getBoolean("activatedU2", false);
    final String userId1 = r.getString("userId1");
    final String userId2 = r.getString("userId2");
    final boolean missing1 = r.getLong("disappearanceDate1") != null || r.getLong("deleteDate1") != null;
    final boolean missing2 = r.getLong("disappearanceDate2") != null || r.getLong("deleteDate2") != null;
    final JsonObject error = new JsonObject().put("status", "error");
    if (source1 != null && source1.equals(source2) && notDeduplicateSource.contains(source1) && !missing1
            && !missing2) {// ww  w.ja v  a  2  s . c om
        message.reply(error.put("message", "two.users.in.same.source"));
        return;
    }
    String query;
    JsonObject params = new JsonObject();
    if ((activatedU1 && prioritySource(source1) >= prioritySource(source2))
            || (activatedU2 && prioritySource(source1) <= prioritySource(source2))
            || (!activatedU1 && !activatedU2)) {
        query = SIMPLE_MERGE_QUERY;
        if (prioritySource(source1) == prioritySource(source2) && notDeduplicateSource.contains(source1)) {
            if (!missing1 && activatedU1) {
                params.put("userId1", userId1).put("userId2", userId2);
            } else if (!missing2 && activatedU2) {
                params.put("userId1", userId2).put("userId2", userId1);
            } else {
                query = SWITCH_MERGE_QUERY;
                if (activatedU1) {
                    params.put("userId1", userId1).put("userId2", userId2);
                } else {
                    params.put("userId1", userId2).put("userId2", userId1);
                }
            }
        } else {
            if (activatedU1) {
                params.put("userId1", userId1).put("userId2", userId2);
            } else if (activatedU2) {
                params.put("userId1", userId2).put("userId2", userId1);
            } else {
                if (prioritySource(source1) > prioritySource(source2)) {
                    params.put("userId1", userId1).put("userId2", userId2);
                } else {
                    params.put("userId1", userId2).put("userId2", userId1);
                }
            }
        }
    } else if ((activatedU1 && prioritySource(source1) < prioritySource(source2))
            || (activatedU2 && prioritySource(source1) > prioritySource(source2))) {
        query = SWITCH_MERGE_QUERY;
        if (activatedU1) {
            params.put("userId1", userId1).put("userId2", userId2);
        } else {
            params.put("userId1", userId2).put("userId2", userId1);
        }
    } else {
        message.reply(error.put("message", "invalid.merge.case"));
        return;
    }
    if (tx != null) {
        tx.add(INCREMENT_RELATIVE_SCORE, params);
        tx.add(query, params);
        sendMergedEvent(params.getString("userId1"), params.getString("userId2"));
        message.reply(new JsonObject().put("status", "ok"));
    } else {
        try {
            TransactionHelper txl = TransactionManager.getTransaction();
            txl.add(INCREMENT_RELATIVE_SCORE, params);
            txl.add(query, params);
            txl.commit(new Handler<Message<JsonObject>>() {
                @Override
                public void handle(Message<JsonObject> event) {
                    if ("ok".equals(event.body().getString("status"))) {
                        log.info("Merge duplicates : " + r.encode());
                        if (updateCourses) {
                            AbstractTimetableImporter.updateMergedUsers(event.body().getJsonArray("results"));
                        }
                        sendMergedEvent(params.getString("userId1"), params.getString("userId2"));
                    }
                    message.reply(event.body());
                }
            });
        } catch (TransactionException e) {
            message.reply(error.put("message", "invalid.transaction"));
        }
    }
}