if (profile == null) {
profile = _profileService.findProfile(req, profileName);
}
Repository repository = profile.getRepository();
Map<String, WorkDefinitionImpl> workitemsFromRepo = WorkItemRepository.getWorkDefinitions(repoURL);
if(action != null && action.equalsIgnoreCase(displayRepoContent)) {
if(workitemsFromRepo != null && workitemsFromRepo.size() > 0) {
Map<String, List<String>> retMap = new HashMap<String, List<String>>();
for(String key : workitemsFromRepo.keySet()) {
WorkDefinitionImpl wd = workitemsFromRepo.get(key);
List<String> keyList = new ArrayList<String>();
keyList.add(wd.getName() == null ? "" : wd.getName());
keyList.add(wd.getDisplayName() == null ? "" : wd.getDisplayName());
keyList.add(repoURL + "/" + wd.getName() + "/" + wd.getIcon());
keyList.add(wd.getCategory() == null ? "" : wd.getCategory());
keyList.add(wd.getExplanationText() == null ? "" : wd.getExplanationText());
keyList.add(repoURL + "/" + wd.getName() + "/" + wd.getDocumentation());
StringBuffer bn = new StringBuffer();
if(wd.getParameterNames() != null) {
String delim = "";
for (String name : wd.getParameterNames()) {
bn.append(delim).append(name);
delim = ",";
}
}
keyList.add(bn.toString());
StringBuffer br = new StringBuffer();
if(wd.getResultNames() != null) {
String delim = "";
for (String resName : wd.getResultNames()) {
br.append(delim).append(resName);
delim = ",";
}
}
keyList.add(br.toString());
retMap.put(key, keyList);
}
JSONObject jsonObject = new JSONObject();
for (Entry<String,List<String>> retMapKey : retMap.entrySet()) {
try {
jsonObject.put(retMapKey.getKey(), retMapKey.getValue());
} catch (Exception e) {
e.printStackTrace();
}
}
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/json");
resp.getWriter().write(jsonObject.toString());
} else {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/json");
resp.getWriter().write("false");
return;
}
} else if(action != null && action.equalsIgnoreCase(installRepoContent)) {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/json");
if(workitemsFromRepo != null && workitemsFromRepo.size() > 0) {
boolean gotPackage = false;
String pkg = "";
for(String key : workitemsFromRepo.keySet()) {
if(key.equals(assetsToInstall) && categoryToInstall.equals(workitemsFromRepo.get(key).getCategory())) {
String workitemDefinitionURL = workitemsFromRepo.get(key).getPath() + "/" + workitemsFromRepo.get(key).getName() + ".wid";
String iconFileURL = workitemsFromRepo.get(key).getPath() + "/" + workitemsFromRepo.get(key).getIcon();
String workItemDefinitionContent = ConfFileUtils.URLContentsToString(new URL(workitemDefinitionURL));
String iconName = workitemsFromRepo.get(key).getIcon();
String widName = workitemsFromRepo.get(key).getName();
byte[] iconContent = null;
try {
iconContent = getImageBytes(new URL(iconFileURL)
.openStream());
} catch (Exception e1) {
_logger.error("Could not read icon image: " + e1.getMessage());
}
// install wid and icon
repository.deleteAsset(profile.getRepositoryGlobalDir( uuid ) + "/" + widName + ".wid");
AssetBuilder widAssetBuilder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
widAssetBuilder.name(widName)
.location(profile.getRepositoryGlobalDir( uuid ))
.type("wid")
.content(workItemDefinitionContent);
repository.createAsset(widAssetBuilder.getAsset());
AssetBuilder iconAssetBuilder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Byte);
String iconExtension = iconName.substring(iconName.lastIndexOf(".") + 1);
String iconFileName = iconName.substring(0, iconName.lastIndexOf("."));
repository.deleteAsset(profile.getRepositoryGlobalDir( uuid ) + "/" + iconFileName + "." + iconExtension);
iconAssetBuilder.name(iconFileName)
.location(profile.getRepositoryGlobalDir( uuid ))
.type(iconExtension)
.content(iconContent);
repository.createAsset(iconAssetBuilder.getAsset());
}
}
} else {
_logger.error("Invalid or empty service repository.");
resp.setCharacterEncoding("UTF-8");