Skip to content

Commit

Permalink
Merge pull request #83 from MicroFocus/US##232587-refactor_agile_sdk4
Browse files Browse the repository at this point in the history
US#232587-refactor
  • Loading branch information
Elva-Zhu authored May 11, 2018
2 parents 77f515d + 9b7c317 commit 9926872
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

import javax.ws.rs.HttpMethod;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import com.ppm.integration.agilesdk.ValueSet;
import com.ppm.integration.agilesdk.connector.octane.client.ClientPublicAPI;
import com.ppm.integration.agilesdk.connector.octane.client.OctaneClientException;
Expand All @@ -28,6 +32,8 @@
import com.ppm.integration.agilesdk.model.AgileEntity;
import com.ppm.integration.agilesdk.model.AgileEntityFieldInfo;
import com.ppm.integration.agilesdk.model.AgileEntityInfo;
import com.ppm.integration.agilesdk.provider.Providers;
import com.ppm.integration.agilesdk.provider.UserProvider;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
Expand All @@ -41,6 +47,8 @@
*/
public class OctaneRequestIntegration extends RequestIntegration {

private UserProvider up = null;

@Override
public List<AgileEntityInfo> getAgileEntitiesInfo(final String agileProjectValue,
final ValueSet instanceConfigurationParameters)
Expand Down Expand Up @@ -126,9 +134,11 @@ public List<AgileEntity> getEntities(final String agileProjectValue, final Strin
String sharedSpaceId = workspaceJson.getString(OctaneConstants.SHARED_SPACE_ID);

if (OctaneConstants.SUB_TYPE_FEATURE.equals(entityType)) {
entities = client.getFeaturesAfterDate(sharedSpaceId, workSpaceId, entityIds, lastUpdateTime);
List<JSONObject> featureJson = client.getFeaturesAfterDate(sharedSpaceId, workSpaceId, entityIds, lastUpdateTime);
entities = getAgileEntities(client, featureJson, sharedSpaceId, workSpaceId, "feature");
} else if (OctaneConstants.SUB_TYPE_STORY.equals(entityType)) {
entities = client.getUserStoriesAfterDate(sharedSpaceId, workSpaceId, entityIds, lastUpdateTime);
List<JSONObject> userStoryJson = client.getUserStoriesAfterDate(sharedSpaceId, workSpaceId, entityIds, lastUpdateTime);
entities = getAgileEntities(client, userStoryJson, sharedSpaceId, workSpaceId, "story");
}

return entities;
Expand All @@ -146,9 +156,18 @@ public AgileEntity getEntity(final String agileProjectValue, final String entity
String sharedSpaceId = workspaceJson.getString(OctaneConstants.SHARED_SPACE_ID);

if (OctaneConstants.SUB_TYPE_FEATURE.equals(entityType)) {
entity = client.getFeature(sharedSpaceId, workSpaceId, entityId);
List<JSONObject> featureJson = client.getFeature(sharedSpaceId, workSpaceId, entityId);
if (featureJson.size() > 0) {
Map<String, FieldInfo> fieldInfoMap = getFieldInfoMap(client, sharedSpaceId, workSpaceId, "feature");
entity = wrapperEntity(featureJson.get(0), fieldInfoMap);
}

} else if (OctaneConstants.SUB_TYPE_STORY.equals(entityType)) {
entity = client.getUserStory(sharedSpaceId, workSpaceId, entityId);
List<JSONObject> userStoryJson = client.getUserStory(sharedSpaceId, workSpaceId, entityId);
if (userStoryJson.size() > 0) {
Map<String, FieldInfo> fieldInfoMap = getFieldInfoMap(client, sharedSpaceId, workSpaceId, "story");
entity = wrapperEntity(userStoryJson.get(0), fieldInfoMap);
}
}

return entity;
Expand All @@ -170,11 +189,17 @@ private AgileEntity saveOrUpdateEntity(final String agileProjectValue, final Str
}
if (OctaneConstants.SUB_TYPE_FEATURE.equals(entityType)) {
String entityStr = buildEntity(client, sharedSpaceId, fieldInfos, entityType, entity, null);
agileEntity = client.saveFeatureInWorkspace(sharedSpaceId, workSpaceId, entityStr, method);
JSONObject feature = client.saveFeatureInWorkspace(sharedSpaceId, workSpaceId, entityStr, method);
agileEntity = wrapperEntity(feature, null);
} else if (OctaneConstants.SUB_TYPE_STORY.equals(entityType)) {
SimpleEntity root = client.getWorkItemRoot(Integer.parseInt(sharedSpaceId), Integer.parseInt(workSpaceId));
String entityStr = buildEntity(client, sharedSpaceId, fieldInfos, entityType, entity, root);
agileEntity = client.saveStoryInWorkspace(sharedSpaceId, workSpaceId, entityStr, method);
JSONObject userStory = client.saveStoryInWorkspace(sharedSpaceId, workSpaceId, entityStr, method);
agileEntity = wrapperEntity(userStory, null);
}
if (agileEntity != null) {
agileEntity.setEntityUrl(
String.format(ClientPublicAPI.DEFAULT_ENTITY_ITEM_URL, client.getBaseURL(), sharedSpaceId, workSpaceId, agileEntity.getId()));
}
return agileEntity;
}
Expand All @@ -194,9 +219,14 @@ private String buildEntity(final ClientPublicAPI client, final String sharedSpce
while (it.hasNext()) {
Entry<String, DataField> entry = it.next();
String key = entry.getKey();
FieldInfo fieldInfo = fieldInfoMap.get(entry.getKey());
FieldInfo fieldInfo = fieldInfoMap.get(key);
DataField field = entry.getValue();
if (field == null) {
if (fieldInfo.isMultiValue()) {
entityObj.put(key, createNullJSONObject(true));
} else {
entityObj.put(key, createNullJSONObject(false));
}
continue;
}
switch (field.getType()) {
Expand All @@ -207,37 +237,39 @@ 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;
}
}


if (entity.getId() != null) {
entityObj.put(OctaneConstants.KEY_FIELD_ID, entity.getId());
}

JSONObject complexObj = new JSONObject();
if (OctaneConstants.SUB_TYPE_STORY.equals(entityType)) {
complexObj.put("id", "phase.story.new");
} else {
complexObj.put("id", "phase.feature.new");
}
complexObj.put("type", "phase");
entityObj.put("phase", complexObj);

if (root != null) {
JSONObject parent = new JSONObject();
parent.put("id", root.id);
parent.put("type", root.type);
entityObj.put("parent", parent);
JSONObject complexObj = new JSONObject();
if (OctaneConstants.SUB_TYPE_STORY.equals(entityType)) {
complexObj.put("id", "phase.story.new");
} else {
complexObj.put("id", "phase.feature.new");
}
complexObj.put("type", "phase");
entityObj.put("phase", complexObj);

if (root != null) {
JSONObject parent = new JSONObject();
parent.put("id", root.id);
parent.put("type", root.type);
entityObj.put("parent", parent);
}
}

entityList.add(entityObj);

return entityList.toString();
Expand All @@ -263,61 +295,179 @@ 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++;
if (users != null && !users.isEmpty()) {
List<String> emails = new ArrayList<String>();

for (User user : users) {
if (user.getEmail() != null) {
emails.add(user.getEmail());
}
}
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));
}
}
}
}
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);
return createNullJSONObject(true);
} else {
return createNullJSONObject(false);
}
}

private JSONObject getUserJsonObject(JSONObject tempObj) {
String id = tempObj.getString("id");
JSONObject obj = new JSONObject();
obj.put("type", "workspace_user");
obj.put("id", id);
return obj;
}

private synchronized UserProvider getUserProvider() {
if (up == null) {
up = Providers.getUserProvider(OctaneIntegrationConnector.class);
}
return up;
}

private Date parserDate(String dateStr) {
DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
DateTime dateTimeHere = parser.parseDateTime(dateStr);
return dateTimeHere.toDate();

}

private AgileEntity wrapperEntity(JSONObject item, Map<String, FieldInfo> fieldInfoMap) {

AgileEntity entity = new AgileEntity();
Iterator<String> sIterator = item.keys();
while (sIterator.hasNext()) {
String key = sIterator.next();
if (key.equals(ClientPublicAPI.KEY_LAST_UPDATE_DATE)) {
String value = item.getString(key);
entity.setLastUpdateTime(parserDate(value));
} else if (key.equals("id")) {
String value = item.getString(key);
entity.setId(value);
} else if (fieldInfoMap != null && fieldInfoMap.get(key) != null) {
FieldInfo info = fieldInfoMap.get(key);
if (info.getFieldType().equals("userList")) {
JSONObject value = item.getJSONObject(key);
if (info.isMultiValue()) {
JSONArray users = value.getJSONArray("data");
if (!users.isEmpty()) {
List<User> userList = new ArrayList<User>();
for (int i = 0; i < users.size(); i++) {
JSONObject userObj = users.getJSONObject(i);
User user = buildUser(userObj);
if (user != null) {
userList.add(user);
}
}
MultiUserField multiUserField = new MultiUserField();
multiUserField.set(userList);
entity.addField(key, multiUserField);
} else {
entity.addField(key, null);
}

} else {
if (canParseJson(value, "name")) {
User user = buildUser(value);
if (user != null) {
UserField userField = new UserField();
userField.set(user);
entity.addField(key, userField);
}
} else {
entity.addField(key, null);
}

}

} else if (info.getFieldType().equals("string")) {
String value = item.getString(key);
if (value == null || value.equals("null")) {
value = "";
}
StringField stringField = new StringField();
stringField.set(value);
entity.addField(key, stringField);
}
}
}
return entity;
}

private User buildUser(JSONObject userJson) {
User user = null;
if (userJson.containsKey("name")) {
String email = userJson.getString("name");
com.hp.ppm.user.model.User userModel = getUserProvider().getByEmail(email);
if (userModel != null) {
user = new User();
user.setUserId(userModel.getUserId());
user.setEmail(userModel.getEmail());
user.setFullName(userModel.getFullName());
user.setUsername(userModel.getUserName());
}
userList.put("data", userArr);
return userList;
}
return user;
}

private boolean canParseJson(JSONObject jsonObj, String key) {
if (jsonObj != null && jsonObj.containsKey(key)) {
return 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 false;
}
}

private JSONObject transformSingleUser(ClientPublicAPI client, FieldInfo userFieldInfo, User user,
String shareSpaceId)
{
if (user == null) {
return null;
private JSONObject createNullJSONObject(boolean isMulti) {
if (isMulti) {
JSONObject nullObj = new JSONObject();
nullObj.put("data", new JSONArray());
return nullObj;
} else {
return new JSONObject(true);
}
String[] fullNames = new String[]{user.getFullName()};
}

net.sf.json.JSONArray jsonArray = client.getUsersByFullName(shareSpaceId, fullNames);
if (jsonArray.size() < 1)
return null;
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);
for (FieldInfo info : fieldInfos) {
fieldInfoMap.put(info.getName(), info);
}
return fieldInfoMap;
}

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;
private List<AgileEntity> getAgileEntities(ClientPublicAPI client, List<JSONObject> workItemsJson, String sharedspaceId, String workspaceId, String entityType) {
List<AgileEntity> agileEntities = new ArrayList<>();
if (workItemsJson != null && workItemsJson.size() > 0) {
Map<String, FieldInfo> fieldInfoMap = getFieldInfoMap(client, sharedspaceId, workspaceId, entityType);
for (JSONObject workItemJson : workItemsJson) {
AgileEntity entity = wrapperEntity(workItemJson, fieldInfoMap);
agileEntities.add(entity);
}
}
return agileEntities;
}

}

class AgileFieldComparator implements Comparator<AgileEntityFieldInfo> {
Expand Down
Loading

0 comments on commit 9926872

Please sign in to comment.