import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.nulabinc.backlog4j.BacklogClient;
import com.nulabinc.backlog4j.BacklogClientFactory;
import com.nulabinc.backlog4j.Category;
import com.nulabinc.backlog4j.Issue;
import com.nulabinc.backlog4j.IssueComment;
import com.nulabinc.backlog4j.IssueType;
import com.nulabinc.backlog4j.Project;
import com.nulabinc.backlog4j.Status;
import com.nulabinc.backlog4j.api.option.AddIssueCommentParams;
import com.nulabinc.backlog4j.api.option.CreateIssueParams;
import com.nulabinc.backlog4j.api.option.GetIssuesParams;
import com.nulabinc.backlog4j.api.option.UpdateIssueParams;
import com.nulabinc.backlog4j.conf.BacklogConfigure;
import com.nulabinc.backlog4j.conf.BacklogJpConfigure;
import com.nulabinc.backlog4j.conf.BacklogPackageConfigure;
public class BacklogRegist {
private BacklogClient backlog;
public BacklogRegist(String url, String apikey) throws MalformedURLException {
BacklogConfigure conf = null;
//オンプレ環境
if(url.startsWith("http")){
conf = new BacklogPackageConfigure(url);
}
//クラウド
else{
conf = new BacklogJpConfigure(url);
}
conf.apiKey(apikey);
backlog = new BacklogClientFactory(conf).newClient();
}
/**
* 課題を取得する
* @param issueKey
* */
public Issue getIssue(String issueKey){
if(issueKey == null){
return null;
}
return getIssue = backlog.getIssue(issueKey);
}
/**
* 課題を作成する
* @param projectKey
* @param issueTypeName 種別名称
* @param categoryName カテゴリ名称
* @param summary 件名
* @param description 詳細
* @return ticketID 0は登録失敗
* */
public long regist(String projectKey, String issueTypeName, String categoryName,
String summary, String description){
CreateIssueParams param = new CreateIssueParams(
getProjectID(projectKey), summary,
getIssueTypeId(projectKey, issueTypeName), Issue.PriorityType.Normal);
param.description(description);
if(StringUtils.isNotEmpty(categoryName)){
List<String> categoryIds = new ArrayList<String>();
categoryIds.add(getCategoryId(projectKey, categoryName));
param.categoryIds(categoryIds);
}
Issue issue = backlog.createIssue(param);
if(issue != null){
return issue.getId();
}
return 0;
}
/**
* 課題を更新する(コメントの追加も可能)
* @param projectKey プロジェクトKey
* @param issueIdorKey 課題IDまたはKey
* @param status ステータス名称(更新しなければセットしない)
* @param addComment 更新なければセットしない
* @return commentID 0は登録失敗
* */
public long update(String issueIdorKey, String status, String addComment){
long ret = 0;
Issue issue = null;
if(StringUtils.isNotEmpty(status)){
UpdateIssueParams param = new UpdateIssueParams(issueIdorKey);
for( Status Entity: backlog.getStatuses()){
if( Entity.getName().equals(status)){
param.status(Entity.getStatusType());
}
}
issue = backlog.updateIssue(param);
}
else{
issue = backlog.getIssue(issueIdorKey);
}
if(StringUtils.isNotEmpty(addComment)){
AddIssueCommentParams params = new AddIssueCommentParams(issue.getIssueKey(), addComment);
IssueComment cmt = backlog.addIssueComment(params);
ret = cmt.getId();
}
else{
ret = issue.getId();
}
return ret;
}
/**
* 課題にコメントを追加する
* @param projectKey
* @param issueKey
* @param addComment
* @return commentID 0は登録失敗
* */
public long addComment(String issueKey, String addComment){
Issue issue = backlog.getIssue(issueKey);
if( issue != null){
AddIssueCommentParams params = new AddIssueCommentParams(issue.getIssueKey(), addComment);
IssueComment cmt = backlog.addIssueComment(params);
return cmt.getId();
}
return 0;
}
/**
* 課題のタイプ(種別)を取得する
* @param prjKey プロジェクトキー
* @param name 種別名称
* */
public String getIssueTypeId(String projectKey, String name){
for( IssueType type: backlog.getIssueTypes(projectKey)){
if( type.getName().equals(name)){
return type.getIdAsString();
}
}
return null;
}
/**
* カテゴリを取得する
* @param prjKey プロジェクトキー
* @param name 種別名称
* */
public String getCategoryId(String projectKey, String name){
for( Category type: backlog.getCategories(projectKey)){
if( type.getName().equals(name)){
return type.getIdAsString();
}
}
return null;
}
/**
* プロジェクトIDを取得する
* @param prjKey プロジェクトキー
* @return Projectid
* */
public String getProjectID(String projectKey){
for( Project type: backlog.getProjects()){
if( type.getProjectKey().equals(projectKey)){
return type.getIdAsString();
}
}
return null;
}
}