Example usage for com.google.gson JsonArray JsonArray

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

Introduction

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

Prototype

public JsonArray() 

Source Link

Document

Creates an empty JsonArray.

Usage

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 .  j a  v a 2  s. co 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  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 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 {/*from   w  ww . ja va  2s  . c o  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;//w  w w  .  ja v  a 2s . c  om
    }

    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;//  w w w  .  ja v a 2  s  . c  om
        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  ww  w  .  ja va2 s. co m*/

                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());/*from  www  . ja va  2s.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 w w  w .j a va2s . co  m

    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();
}

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

License:Open Source License

@PreUpdate
public void preUpdate(APersisted obj) {
    OriginalTeam o = (OriginalTeam) getIfRecorded(obj);
    if (null == o || !(obj instanceof Team))
        return;//from  w  ww  .  ja  v a2 s  .c o m

    Team team = (Team) obj;
    JsonObject json = new JsonObject();

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

    if (team.isStopOnFirstMatch() != o.stopOnFirstMatch)
        json.addProperty("stopOnFirstMatch", o.stopOnFirstMatch);

    if (team.isUnmatchedEditable() != o.unmatchedEditable)
        json.addProperty("unmatchedEditable", o.unmatchedEditable);

    Set<UserAccount> currentMembers = team.getMembers();
    if (null == o.members || o.members.size() == 0) {
        if (null != currentMembers && currentMembers.size() > 0) {
            JsonArray added = new JsonArray();
            currentMembers.forEach(m -> added.add(userToJson(m)));
            json.add("newMembers", added);
        }
    } else {
        if (null == currentMembers || currentMembers.size() == 0) {
            JsonArray removed = new JsonArray();
            o.members.forEach(m -> removed.add(userToJson(m)));
            json.add("removedMembers", removed);
        } else {
            Set<UserAccount> copyOfCurrent = new HashSet<>();
            currentMembers.forEach(m -> copyOfCurrent.add(m));

            copyOfCurrent.removeAll(o.members);
            if (copyOfCurrent.size() > 0) {
                JsonArray added = new JsonArray();
                copyOfCurrent.forEach(m -> added.add(userToJson(m)));
                json.add("newMembers", added);
            }

            o.members.removeAll(currentMembers);
            if (o.members.size() > 0) {
                JsonArray removed = new JsonArray();
                o.members.forEach(m -> removed.add(userToJson(m)));
                json.add("removedMembers", removed);
            }
        }
    }

    markForNotification();
    team.diffJson = json.toString();
}

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

License:Open Source License

@PreUpdate
public void preUpdate(APersisted obj) {
    OriginalToken o = (OriginalToken) getIfRecorded(obj);
    if (null == o || !(obj instanceof Token))
        return;/*from w w w.j  a  v a  2s . c  o m*/

    Token token = (Token) obj;
    JsonObject json = new JsonObject();

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

    if (token.isActive() != o.active)
        json.addProperty("active", o.active);

    if (!Utils.equal(token.getExpires(), o.expires))
        json.addProperty("expires", null == o.expires ? "" : o.expires.toString());

    if (token.isForceKeyPushEnabled() != o.forceKeyPushEnabled)
        json.addProperty("forceKeyPushEnabled", o.forceKeyPushEnabled);

    boolean hadSps = null != o.securityProfiles && o.securityProfiles.size() > 0;
    boolean hasSps = null != token.getSecurityProfiles() && token.getSecurityProfiles().size() > 0;

    if (!hadSps && !hasSps)
        ;
    else if (hadSps && !hasSps) {
        JsonArray sps = new JsonArray();
        o.securityProfiles.forEach(sp -> sps.add(sp));
        json.add("sps", sps);
    } else if (!hadSps && hasSps) {
        json.add("sps", new JsonArray());
    } else {
        Set<String> current = new HashSet<>();
        token.getSecurityProfiles().forEach(sp -> current.add(sp.getName()));
        current.removeAll(o.securityProfiles);

        if (current.size() > 0) {
            JsonArray sps = new JsonArray();
            o.securityProfiles.forEach(sp -> sps.add(sp));
            json.add("sps", sps);
        } else {
            Set<String> clone = new HashSet<>();
            o.securityProfiles.forEach(sp -> clone.add(new String(sp)));

            current.clear();
            token.getSecurityProfiles().forEach(sp -> clone.add(sp.getName()));

            clone.removeAll(current);
            if (clone.size() > 0) {
                JsonArray sps = new JsonArray();
                o.securityProfiles.forEach(sp -> sps.add(sp));
                json.add("sps", sps);
            }
        }
    }

    if (!Utils.equal(token.getTeamRules(), o.teamRules))
        json.addProperty("rulesTeam", null == o.teamRules ? "" : o.teamRules.getName());

    if (!Utils.equal(token.getManagingTeam(), o.managingTeam))
        json.addProperty("managingTeam", null == o.managingTeam ? "" : o.managingTeam.getName());

    if (!Utils.equal(token.getUser(), o.user))
        json.addProperty("user", null == o.user ? "" : o.user.getUsername());

    if (!Utils.equal(token.getManagedBy(), o.managedBy))
        json.addProperty("managedBy", o.managedBy.name());

    markForNotification();
    token.diffJson = json.toString();
}