Skip to content

Commit

Permalink
US#232587-refactor agile sdk to support multi users
Browse files Browse the repository at this point in the history
  • Loading branch information
chunqi.lu committed May 11, 2018
1 parent a558631 commit 9b7c317
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,9 @@ private String buildEntity(final ClientPublicAPI client, final String sharedSpce
DataField field = entry.getValue();
if (field == null) {
if (fieldInfo.isMultiValue()) {
JSONObject emptyField = new JSONObject();
emptyField.put("data", new JSONArray());
entityObj.put(key, emptyField);
entityObj.put(key, createNullJSONObject(true));
} else {
entityObj.put(key, new JSONObject(true));
entityObj.put(key, createNullJSONObject(false));
}
continue;
}
Expand All @@ -239,11 +237,13 @@ private String buildEntity(final ClientPublicAPI client, final String sharedSpce
case USER:
if (field.isList()) {
MultiUserField userField = (MultiUserField) field;
JSONObject obj = transformUsers(client, fieldInfo, userField.get(), sharedSpceId);
JSONObject obj = transformUsers(client, fieldInfo, userField.get(), sharedSpceId);
entityObj.put(entry.getKey(), obj);
} else {
UserField userField = (UserField) field;
JSONObject obj = transformSingleUser(client, fieldInfo, userField.get(), sharedSpceId);
List<User> userList = new ArrayList<User>();
userList.add(userField.get());
JSONObject obj = transformUsers(client, fieldInfo, userList, sharedSpceId);
entityObj.put(entry.getKey(), obj);
}
break;
Expand Down Expand Up @@ -295,56 +295,42 @@ public void postDeleteRequest(final String agileProjectValue, final String entit
private JSONObject transformUsers(ClientPublicAPI client, FieldInfo userFieldInfo, List<User> users,
String shareSpaceId)
{
if (users == null || users.isEmpty()) {
return null;
}
String[] fullNames = new String[users.size()];
int index = 0;
for (User user : users) {
fullNames[index] = user.getFullName();
index++;
}
net.sf.json.JSONArray jsonArray = client.getUsersByFullName(shareSpaceId, fullNames);
if (jsonArray.size() < 1)
return null;
if (userFieldInfo.isMultiValue()) {
JSONObject userList = new JSONObject();
JSONArray userArr = new JSONArray();
for (int i = 0; i < jsonArray.size(); i++) {
net.sf.json.JSONObject tempObj = jsonArray.getJSONObject(i);
String id = (String)tempObj.get("id");
JSONObject obj = new JSONObject();
obj.put("type", "workspace_user");
obj.put("id", id);
userArr.add(obj);
if (users != null && !users.isEmpty()) {
List<String> emails = new ArrayList<String>();

for (User user : users) {
if (user.getEmail() != null) {
emails.add(user.getEmail());
}
}
userList.put("data", userArr);
return userList;
if (!emails.isEmpty()) {
JSONArray emailArray = client.getUsersByEmail(shareSpaceId, emails.toArray(new String[emails.size()]));
if (emailArray.size() > 0) {
if (userFieldInfo.isMultiValue()) {
JSONObject userList = new JSONObject();
JSONArray userArr = new JSONArray();
for (int i = 0; i < emailArray.size(); i++) {
JSONObject tempObj = emailArray.getJSONObject(i);
userArr.add(getUserJsonObject(tempObj));
}
userList.put("data", userArr);
return userList;
} else {
return getUserJsonObject(emailArray.getJSONObject(0));
}
}
}
}

if (userFieldInfo.isMultiValue()) {
return createNullJSONObject(true);
} else {
net.sf.json.JSONObject tempObj = jsonArray.getJSONObject(0);
String id = (String)tempObj.get("id");
JSONObject obj = new JSONObject();
obj.put("type", "workspace_user");
obj.put("id", id);
return obj;
return createNullJSONObject(false);
}
}

private JSONObject transformSingleUser(ClientPublicAPI client, FieldInfo userFieldInfo, User user,
String shareSpaceId)
{
if (user == null) {
return null;
}
String[] fullNames = new String[]{user.getFullName()};

net.sf.json.JSONArray jsonArray = client.getUsersByFullName(shareSpaceId, fullNames);
if (jsonArray.size() < 1)
return null;

net.sf.json.JSONObject tempObj = jsonArray.getJSONObject(0);
String id = (String)tempObj.get("id");
private JSONObject getUserJsonObject(JSONObject tempObj) {
String id = tempObj.getString("id");
JSONObject obj = new JSONObject();
obj.put("type", "workspace_user");
obj.put("id", id);
Expand Down Expand Up @@ -451,6 +437,16 @@ private boolean canParseJson(JSONObject jsonObj, String key) {
}
}

private JSONObject createNullJSONObject(boolean isMulti) {
if (isMulti) {
JSONObject nullObj = new JSONObject();
nullObj.put("data", new JSONArray());
return nullObj;
} else {
return new JSONObject(true);
}
}

private Map<String, FieldInfo> getFieldInfoMap(ClientPublicAPI client, String sharedspaceId, String workspaceId, String entityType) {
Map<String, FieldInfo> fieldInfoMap = new HashMap<String, FieldInfo>();
List<FieldInfo> fieldInfos = client.getEntityFields(sharedspaceId, workspaceId, entityType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,27 @@ public JSONArray getUsersByFullName(String sharedspaceId, String[] names) {
return null;
}
try {
query = URLEncoder.encode(query, "gb2312");
query = URLEncoder.encode(query, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url = String.format("%s/api/shared_spaces/%s/users?query=%s", baseURL, sharedspaceId, query);
RestResponse response = sendGet(url);
JSONObject dataObj = JSONObject.fromObject(response.getData());
JSONArray userList = JSONArray.fromObject(dataObj.get("data"));
return userList;
}

public JSONArray getUsersByEmail(String sharedspaceId, String[] emails) {
String query = "";
if (null != emails && emails.length > 0) {
query += "\"name IN '" + StringUtils.join(emails, "','") + "'\"";
} else {
return null;
}
try {
query = URLEncoder.encode(query, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand Down

0 comments on commit 9b7c317

Please sign in to comment.