Example usage for com.google.gson JsonArray add

List of usage examples for com.google.gson JsonArray add

Introduction

In this page you can find the example usage for com.google.gson JsonArray add.

Prototype

public void add(JsonElement element) 

Source Link

Document

Adds the specified element to self.

Usage

From source file:com.confighub.api.util.GsonHelper.java

License:Open Source License

public static JsonArray getTeams(Set<Team> teams) {
    JsonArray teamsJ = new JsonArray();
    if (null != teams) {
        for (Team team : teams) {
            JsonObject teamJ = new JsonObject();
            teamJ.addProperty("name", team.getName());
            teamJ.addProperty("description", Utils.jsonString(team.getDescription()));
            teamJ.addProperty("stopOnFirstMatch", team.isStopOnFirstMatch());
            teamJ.addProperty("unmatchedEditable", team.isUnmatchedEditable());

            teamsJ.add(teamJ);
        }/*from   www  .j  a v a  2s  . c  o  m*/
    }
    return teamsJ;
}

From source file:com.confighub.api.util.GsonHelper.java

License:Open Source License

public static JsonArray teamMembers(Team team) {
    JsonArray membersJ = new JsonArray();
    Set<UserAccount> members = team.getMembers();
    if (null == members)
        return membersJ;

    for (UserAccount member : members) {
        JsonObject memberJ = new JsonObject();
        memberJ.addProperty("un", member.getUsername());
        memberJ.addProperty("name", member.getName());

        membersJ.add(memberJ);
    }/*from   w ww.j av a 2 s.  c o m*/

    return membersJ;
}

From source file:com.confighub.api.util.GsonHelper.java

License:Open Source License

public static JsonArray allMembers(Repository repository) {
    Set<Team> teams = repository.getTeams();
    JsonArray json = new JsonArray();

    if (null == teams)
        return json;

    for (Team team : teams) {
        for (UserAccount member : team.getMembers()) {
            JsonObject u = new JsonObject();
            u.addProperty("un", member.getUsername());
            u.addProperty("name", member.getName());
            u.addProperty("team", team.getName());

            json.add(u);
        }//from w w w .ja  v a 2s . c o  m
    }

    return json;
}

From source file:com.confighub.api.util.GsonHelper.java

License:Open Source License

public static JsonArray ruleToJson(Team team) {
    List<AccessRule> accessRules = team.getAccessRules();
    JsonArray json = new JsonArray();

    if (null != accessRules) {
        for (AccessRule accessRule : accessRules)
            json.add(accessRuleToJson(accessRule));
    }/*from w  ww.j a  v a  2 s  . c  o m*/

    return json;
}

From source file:com.confighub.api.util.GsonHelper.java

License:Open Source License

public static JsonObject tokenToJson(Token token, boolean includeToken) {
    JsonObject json = new JsonObject();

    if (includeToken)
        json.addProperty("token", token.getToken());

    json.addProperty("id", token.getId());
    json.addProperty("createdOn", token.getCreatedOn());
    json.addProperty("expires", token.getExpires());
    json.addProperty("expired", token.isExpired());
    json.addProperty("name", Utils.jsonString(token.getName()));
    json.addProperty("active", token.isActive());
    json.addProperty("forceKeyPushEnabled", token.isForceKeyPushEnabled());
    json.addProperty("user", null == token.getUser() ? null : token.getUser().getUsername());
    json.addProperty("rulesTeam", null == token.getTeamRules() ? null : token.getTeamRules().getName());
    json.addProperty("managedBy", null == token.getManagedBy() ? "All" : token.getManagedBy().name());
    json.addProperty("managingTeam",
            null == token.getManagingTeam() ? null : token.getManagingTeam().getName());

    Set<SecurityProfile> securityProfiles = token.getSecurityProfiles();
    if (null != securityProfiles && securityProfiles.size() > 0) {
        try {//  w ww .  jav  a  2 s.  co  m
            JsonArray sps = new JsonArray();
            for (SecurityProfile sp : securityProfiles)
                sps.add(sp.getName());

            json.add("sps", sps);
        } catch (Exception e) {
            json.addProperty("error", "Token error:  " + e.getMessage());
        }
    }

    return json;
}

From source file:com.confighub.core.repository.AContextAwarePersistent.java

License:Open Source License

void checkPropertyCircularReference(final Context context, final Property property,
        final HashMap<RepoFile, Property> breadcrumbs) throws ConfigException {
    if (null == property.getAbsoluteFilePath()) {
        return;//from   ww w .j  av a 2 s . co m
    }

    Collection<RepoFile> files = context.resolvePartialContextFilePath(property.getAbsoluteFilePath());

    if (null == files || files.size() == 0) {
        return;
    }

    for (RepoFile file : files) {
        if (!file.isActive()) {
            continue;
        }

        if (breadcrumbs.containsKey(file)) {
            Gson gson = new Gson();
            JsonArray steps = new JsonArray();

            for (RepoFile f : breadcrumbs.keySet()) {
                Property bp = breadcrumbs.get(f);
                JsonObject step = new JsonObject();

                step.addProperty("filePath", f.getAbsPath());
                step.add("fileContext", gson.fromJson(f.contextJson, JsonArray.class));
                step.addProperty("key", bp.getKey());
                step.add("valueContext", gson.fromJson(bp.contextJson, JsonArray.class));
                step.addProperty("value", bp.getValue());

                steps.add(step);
            }

            JsonObject crumbs = new JsonObject();
            crumbs.add("crumbs", steps);
            throw new ConfigException(Error.Code.FILE_CIRCULAR_REFERENCE, crumbs);
        } else {
            for (PropertyKey key : file.getKeys()) {
                if (!PropertyKey.ValueDataType.FileEmbed.equals(key.getValueDataType())) {
                    continue;
                }

                Collection<Property> properties = context.partialContextKeyResolver(key);
                for (Property p : properties) {
                    breadcrumbs.put(file, p);
                    checkPropertyCircularReference(context, p, breadcrumbs);
                    breadcrumbs.remove(file);
                }
            }
        }
    }
}

From source file:com.confighub.core.repository.AContextAwarePersistent.java

License:Open Source License

public final void updateContextString() {
    this.contextWeight = 0;
    JsonArray json = new JsonArray();
    for (Depth depth : this.repository.getDepth().getDepths()) {
        CtxLevel l;// ww  w. j  a  va2 s  .  co  m
        JsonObject ljson = new JsonObject();
        ljson.addProperty("p", depth.getPlacement());

        if (null != (l = getLevelAt(depth, this.context))) {
            ljson.addProperty("n", l.getName());
            ljson.addProperty("t", l.isStandalone() ? 0 : l.isMember() ? 1 : 2);
            ljson.addProperty("w", l.getContextScore());

            this.contextWeight += l.getContextScore();
        }

        json.add(ljson);
    }

    this.contextJson = json.toString();
}

From source file:com.confighub.core.rules.AccessRule.java

License:Open Source License

public JsonObject contextToJson() {
    JsonObject contextJ = new JsonObject();
    if (null != this.getContext()) {
        TreeMap<Depth, Collection<CtxLevel>> contextMap = this.getDepthMap(false);

        if (null != contextMap) {
            for (Depth depth : contextMap.keySet()) {
                JsonArray depthLevelsJ = new JsonArray();
                for (CtxLevel ctxLevel : contextMap.get(depth)) {
                    depthLevelsJ.add(ctxLevel.getName());
                }//from  www .j a  v  a  2s. c om

                contextJ.add(String.valueOf(depth.getPlacement()), depthLevelsJ);
            }
        }
    }

    return contextJ;
}

From source file:com.confighub.core.security.Token.java

License:Open Source License

public JsonObject toJson() {
    JsonObject json = new JsonObject();

    json.addProperty("name", this.name);
    json.addProperty("expires", this.expires);
    json.addProperty("forceKeyPushEnabled", this.forceKeyPushEnabled);

    if (null != this.securityProfiles) {
        JsonArray sps = new JsonArray();
        for (SecurityProfile sp : this.securityProfiles) {
            sps.add(sp.getName());
        }//  w w  w .  j  av  a2  s  .c  o m
        json.add("sps", sps);
    }

    return json;
}

From source file:com.confighub.core.store.diff.LevelDiffTracker.java

License:Open Source License

@PreUpdate
public void preUpdate(APersisted obj) {
    OriginalLevel o = (OriginalLevel) getIfRecorded(obj);
    if (null == o || !(obj instanceof CtxLevel))
        return;/*from  ww w  .  j  a  va2  s  .com*/

    CtxLevel ctxLevel = (CtxLevel) obj;

    JsonObject json = new JsonObject();

    if (!Utils.equal(ctxLevel.getName(), o.name))
        json.addProperty("name", o.name);

    if (ctxLevel.getDepth() != o.depth)
        json.addProperty("o.depth", o.depth.getPlacement());

    if (ctxLevel.getType() != o.levelType) {
        json.addProperty("type", o.levelType.name());
    }

    boolean hadAssignments = null != o.assigned && o.assigned.size() > 0;
    boolean hasAssignments = null != ctxLevel.getMembers() && ctxLevel.getMembers().size() > 0;

    // did not have assignments, and still does not
    // ---> do nothing
    if (!hadAssignments && !hasAssignments)
        ;

    // had assignments, but now it does not
    // ---> add old assignments to JSON
    else if (hadAssignments && !hasAssignments) {
        JsonArray assignments = new JsonArray();
        o.assigned.forEach(c -> assignments.add(c));
        json.add("assignments", assignments);
    }

    // did not have assignments, but now it does
    // ---> add empty list for assignments
    else if (!hadAssignments && hasAssignments) {
        json.add("assignments", new JsonArray());
    }

    else {
        Set<String> current = new HashSet<>();
        ctxLevel.getMembers().forEach(k -> current.add(k.getName()));
        current.removeAll(o.assigned);

        // different assignments
        // ---> add old assignments to JSON
        if (current.size() > 0) {
            JsonArray assignments = new JsonArray();
            o.assigned.forEach(c -> assignments.add(c));
            json.add("assignments", assignments);
        } else {
            Set<String> clone = new HashSet<>();
            o.assigned.forEach(ln -> clone.add(new String(ln)));

            current.clear();
            ctxLevel.getMembers().forEach(k -> current.add(k.getName()));

            clone.removeAll(current);
            if (clone.size() > 0) {
                JsonArray assignments = new JsonArray();
                o.assigned.forEach(c -> assignments.add(c));
                json.add("assignments", assignments);
            }

            // assignments the same
            // ---> do nothing
        }
    }

    ctxLevel.diffJson = json.toString();
}