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

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

Introduction

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

Prototype

public String getString(int pos) 

Source Link

Document

Get the String at position pos in the array,

Usage

From source file:org.entcore.cas.services.EnglishAttackRegisteredService.java

License:Open Source License

@Override
protected void prepareUserCas20(User user, final String userId, String service, final JsonObject data,
        final Document doc, final List<Element> additionnalAttributes) {
    user.setUser(data.getString(principalAttributeName));

    if (log.isDebugEnabled()) {
        log.debug("START : prepareUserCas20 userId : " + userId);
    }/* ww w.  j  a  va  2s  . c  o m*/

    try {
        if (log.isDebugEnabled()) {
            log.debug("DATA : prepareUserCas20 data : " + data);
        }

        String query = "MATCH (u:`User` { id : {id}}) return u";
        JsonObject params = new JsonObject().put("id", userId);
        neo.execute(query, params, new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> m) {

                // Uid
                if (data.containsKey("externalId")) {
                    additionnalAttributes.add(createTextElement(EA_ID, data.getString("externalId"), doc));
                }

                // Structures
                Element rootStructures = createElement(EA_STRUCTURE + "s", doc);
                for (Object o : data.getJsonArray("structures", new fr.wseduc.webutils.collections.JsonArray())
                        .getList()) {
                    if (o == null || !(o instanceof JsonObject))
                        continue;
                    JsonObject structure = (JsonObject) o;
                    Element rootStructure = createElement(EA_STRUCTURE, doc);

                    if (structure.containsKey("UAI")) {
                        rootStructure.appendChild(
                                createTextElement(EA_STRUCTURE_UAI, structure.getString("UAI"), doc));
                    }
                    if (structure.containsKey("name")) {
                        rootStructure.appendChild(
                                createTextElement(EA_STRUCTURE_NAME, structure.getString("name"), doc));
                    }
                    rootStructures.appendChild(rootStructure);
                }
                additionnalAttributes.add(rootStructures);

                // Profile
                switch (data.getString("type")) {
                case "Student":
                    additionnalAttributes.add(createTextElement(EA_PROFILES, "National_1", doc));
                    addStringArray(EA_CLASSE, "classes", "name", data, doc, additionnalAttributes,
                            new Mapper<String, String>() {
                                String map(String input) {
                                    Matcher m = classGroupPattern.matcher(input);
                                    if (m.matches() && m.groupCount() >= 1) {
                                        return m.group(1);
                                    }
                                    return input;
                                }
                            });
                    // Email
                    if (data.containsKey("email")) {
                        additionnalAttributes.add(createTextElement(EA_EMAIL, data.getString("email"), doc));
                    }
                    break;
                case "Teacher":
                    additionnalAttributes.add(createTextElement(EA_PROFILES, "National_3", doc));
                    addStringArray(EA_CLASSE, "classes", "name", data, doc, additionnalAttributes,
                            new Mapper<String, String>() {
                                String map(String input) {
                                    Matcher m = classGroupPattern.matcher(input);
                                    if (m.matches() && m.groupCount() >= 1) {
                                        return m.group(1);
                                    }
                                    return input;
                                }
                            });

                    //Discipline
                    Either<String, JsonObject> res = validUniqueResult(m);
                    if (res.isRight()) {
                        JsonObject j = res.right().getValue();
                        if (null != j.getJsonObject("u") && null != j.getJsonObject("u").getJsonObject("data")
                                && null != j.getJsonObject("u").getJsonObject("data")
                                        .getJsonArray("functions")) {
                            JsonArray jsonArrayFunctions = j.getJsonObject("u").getJsonObject("data")
                                    .getJsonArray("functions");
                            if (jsonArrayFunctions.size() > 0) {
                                Element rootDisciplines = createElement(EA_DISCIPLINE + "s", doc);
                                List<String> vTempListDiscipline = new ArrayList<>();
                                for (int i = 0; i < jsonArrayFunctions.size(); i++) {
                                    String fonction = jsonArrayFunctions.getString(i);
                                    String[] elements = fonction.split("\\$");
                                    if (elements.length > 1) {
                                        String discipline = elements[elements.length - 2] + "$"
                                                + elements[elements.length - 1];
                                        if (!vTempListDiscipline.contains(discipline)) {
                                            vTempListDiscipline.add(discipline);
                                            rootDisciplines.appendChild(
                                                    createTextElement(EA_DISCIPLINE, discipline, doc));
                                        }
                                    } else {
                                        log.error("Failed to get User functions userID, : " + userId
                                                + " fonction : " + fonction);
                                    }
                                }
                                additionnalAttributes.add(rootDisciplines);
                            }
                        } else {
                            log.error("Failed to get User functions userID, user empty : " + userId + " j : "
                                    + j);
                        }
                    } else {
                        log.error("Failed to get User functions userID : " + userId);
                    }
                    // Email
                    if (data.containsKey("emailAcademy")) {
                        additionnalAttributes
                                .add(createTextElement(EA_EMAIL, data.getString("emailAcademy"), doc));
                    } else if (data.containsKey("email")) {
                        additionnalAttributes.add(createTextElement(EA_EMAIL, data.getString("email"), doc));

                    }
                    break;
                case "Relative":
                    additionnalAttributes.add(createTextElement(EA_PROFILES, "National_2", doc));
                    break;
                case "Personnel":
                    additionnalAttributes.add(createTextElement(EA_PROFILES, "National_4", doc));
                    break;
                }

                // Lastname
                if (data.containsKey("lastName")) {
                    additionnalAttributes.add(createTextElement(EA_LASTNAME, data.getString("lastName"), doc));
                }

                // Firstname
                if (data.containsKey("firstName")) {
                    additionnalAttributes
                            .add(createTextElement(EA_FIRSTNAME, data.getString("firstName"), doc));
                }
            }
        });

    } catch (Exception e) {
        log.error("Failed to transform User for EnglishAttack", e);
    }

    if (log.isDebugEnabled()) {
        log.debug("START : prepareUserCas20 userId : " + userId);
    }
}

From source file:org.entcore.common.elasticsearch.ElasticSearch.java

License:Open Source License

public void init(Vertx vertx, JsonObject config) {
    this.vertx = vertx;
    JsonArray serverUris = config.getJsonArray("server-uris");
    String serverUri = config.getString("server-uri");
    if (serverUris == null && serverUri != null) {
        serverUris = new fr.wseduc.webutils.collections.JsonArray().add(serverUri);
    }//from   w w  w.jav  a 2s . com

    if (serverUris != null) {
        try {
            URI[] uris = new URI[serverUris.size()];
            for (int i = 0; i < serverUris.size(); i++) {
                uris[i] = new URI(serverUris.getString(i));
            }
            init(uris, vertx, config.getInteger("poolSize", 16), config.getBoolean("keepAlive", true), config);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    } else {
        log.error("Invalid ElasticSearch URI");
    }
}

From source file:org.entcore.common.neo4j.Neo4j.java

License:Open Source License

public void init(Vertx vertx, JsonObject config) {
    this.eb = Server.getEventBus(vertx);
    JsonArray serverUris = config.getJsonArray("server-uris");
    String serverUri = config.getString("server-uri");
    if (serverUris == null && serverUri != null) {
        serverUris = new fr.wseduc.webutils.collections.JsonArray().add(serverUri);
    }/*from  w ww  .  j  av  a2  s  .c om*/

    if (serverUris != null) {
        try {
            URI[] uris = new URI[serverUris.size()];
            for (int i = 0; i < serverUris.size(); i++) {
                uris[i] = new URI(serverUris.getString(i));
            }
            database = new Neo4jRest(uris, config.getBoolean("slave-readonly", false), vertx,
                    config.getLong("checkDelay", 3000l), config.getInteger("poolSize", 16),
                    config.getBoolean("keepAlive", true), config);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    } else {
        log.error("Invalid Neo4j URI");
    }
}

From source file:org.entcore.common.neo4j.Neo4jRest.java

License:Open Source License

private JsonArray transformJson(JsonObject json) {
    final JsonArray columns = json.getJsonArray("columns");
    final JsonArray data = json.getJsonArray("data");
    final JsonArray out = new fr.wseduc.webutils.collections.JsonArray();

    if (data != null && columns != null) {
        for (Object r : data) {
            JsonArray row;//  www .  ja  v a2 s  .c  om
            if (r instanceof JsonArray) {
                row = (JsonArray) r;
            } else if (r instanceof JsonObject) {
                row = ((JsonObject) r).getJsonArray("row");
            } else {
                continue;
            }
            JsonObject outRow = new fr.wseduc.webutils.collections.JsonObject();
            out.add(outRow);
            for (int j = 0; j < row.size(); j++) {
                Object value = row.getValue(j);
                if (value == null) {
                    outRow.put(columns.getString(j), (String) null);
                } else if (value instanceof String) {
                    outRow.put(columns.getString(j), (String) value);
                } else if (value instanceof JsonArray) {
                    outRow.put(columns.getString(j), (JsonArray) value);
                } else if (value instanceof JsonObject) {
                    outRow.put(columns.getString(j), (JsonObject) value);
                } else if (value instanceof Boolean) {
                    outRow.put(columns.getString(j), (Boolean) value);
                } else if (value instanceof Number) {
                    outRow.put(columns.getString(j), (Number) value);
                } else {
                    outRow.put(columns.getString(j), value.toString());
                }
            }
        }
    }
    return out;
}

From source file:org.entcore.common.share.impl.SqlShareService.java

License:Open Source License

@Override
public void shareInfos(final String userId, String resourceId, final String acceptLanguage, final String search,
        final Handler<Either<String, JsonObject>> handler) {
    if (userId == null || userId.trim().isEmpty()) {
        handler.handle(new Either.Left<String, JsonObject>("Invalid userId."));
        return;//ww  w  .java 2s  . c  o  m
    }
    if (resourceId == null || resourceId.trim().isEmpty()) {
        handler.handle(new Either.Left<String, JsonObject>("Invalid resourceId."));
        return;
    }
    final JsonArray actions = getResoureActions(securedActions);
    String query = "SELECT s.member_id, s.action, m.group_id FROM " + shareTable + " AS s " + "JOIN " + schema
            + "members AS m ON s.member_id = m.id WHERE resource_id = ?";
    sql.prepared(query, new fr.wseduc.webutils.collections.JsonArray().add(Sql.parseId(resourceId)),
            new Handler<Message<JsonObject>>() {
                @Override
                public void handle(Message<JsonObject> message) {
                    if ("ok".equals(message.body().getString("status"))) {
                        JsonArray r = message.body().getJsonArray("results");
                        JsonObject groupCheckedActions = new JsonObject();
                        JsonObject userCheckedActions = new JsonObject();
                        for (Object o : r) {
                            if (!(o instanceof JsonArray))
                                continue;
                            JsonArray row = (JsonArray) o;
                            final String memberId = row.getString(0);
                            if (memberId == null || memberId.equals(userId))
                                continue;
                            final JsonObject checkedActions = (row.getValue(2) != null) ? groupCheckedActions
                                    : userCheckedActions;
                            JsonArray m = checkedActions.getJsonArray(memberId);
                            if (m == null) {
                                m = new fr.wseduc.webutils.collections.JsonArray();
                                checkedActions.put(memberId, m);
                            }
                            m.add(row.getValue(1));
                        }
                        getShareInfos(userId, actions, groupCheckedActions, userCheckedActions, acceptLanguage,
                                search, new Handler<JsonObject>() {
                                    @Override
                                    public void handle(JsonObject event) {
                                        if (event != null && event.size() == 3) {
                                            handler.handle(new Either.Right<String, JsonObject>(event));
                                        } else {
                                            handler.handle(new Either.Left<String, JsonObject>(
                                                    "Error finding shared resource."));
                                        }
                                    }
                                });
                    }
                }
            });
}

From source file:org.entcore.common.sql.SqlResult.java

License:Open Source License

private static JsonArray transform(JsonObject body) {
    JsonArray f = body.getJsonArray("fields");
    JsonArray r = body.getJsonArray("results");
    JsonArray result = new fr.wseduc.webutils.collections.JsonArray();
    if (f != null && r != null) {
        JsonArray jsonbAttributes = body.getJsonArray("jsonb_fields");
        List ja = (jsonbAttributes != null) ? jsonbAttributes.getList() : new ArrayList<>();
        for (Object o : r) {
            if (!(o instanceof JsonArray))
                continue;
            JsonArray a = (JsonArray) o;
            JsonObject j = new fr.wseduc.webutils.collections.JsonObject();
            for (int i = 0; i < f.size(); i++) {
                Object item = a.getValue(i);
                if (item instanceof Boolean) {
                    j.put(f.getString(i), (Boolean) item);
                } else if (item instanceof Number) {
                    j.put(f.getString(i), (Number) item);
                } else if (item instanceof JsonArray) {
                    j.put(f.getString(i), (JsonArray) item);
                } else if (item != null && ja.contains(f.getValue(i))) {
                    String stringRepresentation = item.toString().trim();
                    if (stringRepresentation.startsWith("[")) {
                        j.put(f.getString(i), new fr.wseduc.webutils.collections.JsonArray(item.toString()));
                    } else {
                        j.put(f.getString(i), new fr.wseduc.webutils.collections.JsonObject(item.toString()));
                    }//  w  ww  .j ava2s.c o m
                } else if (item != null) {
                    j.put(f.getString(i), item.toString());
                } else {
                    j.put(f.getString(i), (String) null);
                }
            }
            result.add(j);
        }
    }
    return result;
}

From source file:org.entcore.common.storage.impl.GridfsStorage.java

License:Open Source License

@Override
public void removeFile(String id, Handler<JsonObject> handler) {
    JsonArray ids = new fr.wseduc.webutils.collections.JsonArray().add(id);
    JsonObject find = new JsonObject();
    find.put("action", "remove");
    JsonObject query = new JsonObject();
    if (ids != null && ids.size() == 1) {
        query.put("_id", ids.getString(0));
    } else {/*w w w. j  a v a 2s . co  m*/
        query.put("_id", new JsonObject().put("$in", ids));
    }
    find.put("query", query);
    byte[] header = null;
    try {
        header = find.toString().getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        handler.handle(new JsonObject().put("status", "error"));
    }
    if (header != null) {
        Buffer buf = Buffer.buffer(header);
        buf.appendInt(header.length);
        eb.send(gridfsAddress, buf, handlerToAsyncHandler(new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> res) {
                if (handler != null) {
                    handler.handle(res.body());
                }
            }
        }));
    }
}

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 2  s.co  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.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);/* w w w  .jav  a2  s.c  o  m*/
    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.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;/*w ww.  j a va2 s.co  m*/
    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);
            }
        }
    });
}