Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #128 from kuzzleio/19-scroll-profiles-users
Browse files Browse the repository at this point in the history
Add scrollProfile & scrollUsers support
  • Loading branch information
David Bengsch committed May 19, 2017
2 parents fd35d17 + 949fd01 commit dfc9c16
Show file tree
Hide file tree
Showing 6 changed files with 555 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/main/java/io/kuzzle/sdk/core/Kuzzle.java
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,14 @@ public Kuzzle query(final QueryArgs queryArgs, final JSONObject query, final Opt
if (options.getSize() != null) {
object.put("size", options.getSize());
}

if (options.getScroll() != null) {
object.put("scroll", options.getScroll());
}

if (options.getScrollId() != null) {
object.put("scrollId", options.getScrollId());
}
}

object.put("volatile", _volatile);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/io/kuzzle/sdk/responses/SecurityDocumentList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
import java.util.List;

import io.kuzzle.sdk.security.AbstractSecurityDocument;
import io.kuzzle.sdk.util.Scroll;

public class SecurityDocumentList implements KuzzleList<AbstractSecurityDocument> {
private List<AbstractSecurityDocument> documents;
private long total;
private Scroll scroll;

public SecurityDocumentList(List<AbstractSecurityDocument> roles, long total) {
public SecurityDocumentList(List<AbstractSecurityDocument> roles, long total, Scroll scroll) {
this.documents = roles;
this.total = total;
this.scroll = scroll;
}

public SecurityDocumentList(List<AbstractSecurityDocument> roles, long total) {
this(roles, total, new Scroll());
}

public List<AbstractSecurityDocument> getDocuments() {
Expand All @@ -21,4 +28,6 @@ public List<AbstractSecurityDocument> getDocuments() {
public long getTotal() {
return total;
}

public Scroll getScroll() { return scroll; }
}
173 changes: 165 additions & 8 deletions src/main/java/io/kuzzle/sdk/security/Security.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.kuzzle.sdk.listeners.ResponseListener;
import io.kuzzle.sdk.listeners.OnQueryDoneListener;
import io.kuzzle.sdk.responses.SecurityDocumentList;
import io.kuzzle.sdk.util.Scroll;


/**
Expand Down Expand Up @@ -479,7 +480,7 @@ public void onSuccess(JSONObject response) {

for (int i = 0; i < policies.length(); i++) {
JSONObject formattedPolicy = new JSONObject()
.put("roleId", policies.getJSONObject(i).getString("roleId"));
.put("roleId", policies.getJSONObject(i).getString("roleId"));
if (((JSONObject) policies.get(i)).has("restrictedTo")) {
formattedPolicy.put("restrictedTo", policies.getJSONObject(i).getJSONArray("restrictedTo"));
}
Expand Down Expand Up @@ -547,14 +548,20 @@ public void onSuccess(JSONObject response) {
JSONObject result = response.getJSONObject("result");
JSONArray documents = result.getJSONArray("hits");
int documentsLength = documents.length();
ArrayList<AbstractSecurityDocument> roles = new ArrayList<>();
ArrayList<AbstractSecurityDocument> profiles = new ArrayList<>();

for (int i = 0; i < documentsLength; i++) {
JSONObject document = documents.getJSONObject(i);
roles.add(new Profile(Security.this.kuzzle, document.getString("_id"), document.getJSONObject("_source")));
profiles.add(new Profile(Security.this.kuzzle, document.getString("_id"), document.getJSONObject("_source")));
}

listener.onSuccess(new SecurityDocumentList(roles, result.getLong("total")));
Scroll scroll = new Scroll();

if (result.has("scrollId")) {
scroll.setScrollId(result.getString("scrollId"));
}

listener.onSuccess(new SecurityDocumentList(profiles, result.getLong("total"), scroll));
} catch (JSONException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -767,6 +774,78 @@ public Security deleteProfile(@NonNull final String id) throws JSONException {
return deleteProfile(id, null, null);
}

/**
* Returns the next profiles result set with scroll query.
*
* @param scroll - Scroll object
* @param options - Optional arguments
* @param listener - Callback listener
* @throws JSONException the json exception
*/
public void scrollProfiles(final Scroll scroll, final Options options, final ResponseListener<SecurityDocumentList> listener) throws JSONException {
JSONObject request;

try {
request = new JSONObject().put("body", new JSONObject());
}
catch (JSONException e) {
throw new RuntimeException(e);
}

if (listener == null) {
throw new IllegalArgumentException("listener cannot be null");
}

if (scroll.getScrollId() == null) {
throw new IllegalArgumentException("Security.scrollProfiles: scrollId is required");
}

options.setScrollId(scroll.getScrollId());

try {
this.kuzzle.query(buildQueryArgs("scrollProfiles"), request, options, new OnQueryDoneListener() {
@Override
public void onSuccess(JSONObject object) {
try {
JSONArray hits = object.getJSONObject("result").getJSONArray("hits");
ArrayList<AbstractSecurityDocument> profiles = new ArrayList<>();

for (int i = 0; i < hits.length(); i++) {
JSONObject hit = hits.getJSONObject(i);
Profile profile = new Profile(Security.this.kuzzle, hit.getString("_id"), hit.getJSONObject("_source"));

profiles.add(profile);
}

SecurityDocumentList response = new SecurityDocumentList(profiles, hits.length(), scroll);

listener.onSuccess(response);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}

@Override
public void onError(JSONObject error) {
listener.onError(error);
}
});
} catch (JSONException e) {
throw new RuntimeException(e);
}
}

/**
* Returns the next profiles result set with scroll query.
*
* @param scroll - Scroll object
* @param listener - Callback listener
* @throws JSONException the json exception
*/
public void scrollProfiles(Scroll scroll, final ResponseListener<SecurityDocumentList> listener) throws JSONException {
this.scrollProfiles(scroll, new Options(), listener);
}

/**
* Update profile.
*
Expand Down Expand Up @@ -949,14 +1028,20 @@ public void onSuccess(JSONObject response) {
JSONObject result = response.getJSONObject("result");
JSONArray documents = result.getJSONArray("hits");
int documentsLength = documents.length();
ArrayList<AbstractSecurityDocument> roles = new ArrayList<>();
ArrayList<AbstractSecurityDocument> users = new ArrayList<>();

for (int i = 0; i < documentsLength; i++) {
JSONObject document = documents.getJSONObject(i);
roles.add(new User(Security.this.kuzzle, document.getString("_id"), document.getJSONObject("_source")));
users.add(new User(Security.this.kuzzle, document.getString("_id"), document.getJSONObject("_source")));
}

listener.onSuccess(new SecurityDocumentList(roles, result.getLong("total")));
Scroll scroll = new Scroll();

if (result.has("scrollId")) {
scroll.setScrollId(result.getString("scrollId"));
}

listener.onSuccess(new SecurityDocumentList(users, result.getLong("total"), scroll));
} catch (JSONException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -1251,6 +1336,78 @@ public Security deleteUser(@NonNull final String id) throws JSONException {
return deleteUser(id, null, null);
}

/**
* Returns the next users result set with scroll query.
*
* @param scroll - Scroll object
* @param options - Optional arguments
* @param listener - Callback listener
* @throws JSONException the json exception
*/
public void scrollUsers(final Scroll scroll, final Options options, final ResponseListener<SecurityDocumentList> listener) throws JSONException {
JSONObject request;

try {
request = new JSONObject().put("body", new JSONObject());
}
catch (JSONException e) {
throw new RuntimeException(e);
}

if (listener == null) {
throw new IllegalArgumentException("listener cannot be null");
}

if (scroll.getScrollId() == null) {
throw new IllegalArgumentException("Security.scrollUsers: scrollId is required");
}

options.setScrollId(scroll.getScrollId());

try {
this.kuzzle.query(buildQueryArgs("scrollUsers"), request, options, new OnQueryDoneListener() {
@Override
public void onSuccess(JSONObject object) {
try {
JSONArray hits = object.getJSONObject("result").getJSONArray("hits");
ArrayList<AbstractSecurityDocument> users = new ArrayList<>();

for (int i = 0; i < hits.length(); i++) {
JSONObject hit = hits.getJSONObject(i);
User user = new User(Security.this.kuzzle, hit.getString("_id"), hit.getJSONObject("_source"));

users.add(user);
}

SecurityDocumentList response = new SecurityDocumentList(users, hits.length(), scroll);

listener.onSuccess(response);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}

@Override
public void onError(JSONObject error) {
listener.onError(error);
}
});
} catch (JSONException e) {
throw new RuntimeException(e);
}
}

/**
* Returns the next users result set with scroll query.
*
* @param scroll - Scroll object
* @param listener - Callback listener
* @throws JSONException the json exception
*/
public void scrollUsers(Scroll scroll, final ResponseListener<SecurityDocumentList> listener) throws JSONException {
this.scrollUsers(scroll, new Options(), listener);
}

/**
* Update user.
*
Expand Down Expand Up @@ -1469,7 +1626,7 @@ public Security getUserRights(@NonNull final String id, final Options options, @
}
try {
JSONObject data = new JSONObject()
.put("_id", id);
.put("_id", id);
kuzzle.query(buildQueryArgs("getUserRights"), data, options, new OnQueryDoneListener() {
@Override
public void onSuccess(JSONObject response) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/kuzzle/sdk/util/Scroll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.kuzzle.sdk.util;


public class Scroll {
private String scrollId;

public void setScrollId(String scrollId) {
this.scrollId = scrollId;
}

public boolean hasScrollId() { return !(scrollId == null); }

public String getScrollId() { return scrollId; }
}
Loading

0 comments on commit dfc9c16

Please sign in to comment.