Author: tchemit Date: 2012-07-15 20:29:25 +0200 (Sun, 15 Jul 2012) New Revision: 306 Url: http://nuiton.org/repositories/revision/jredmine/306 Log: let request builder not public api Removed: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/DefaultRequestBuilder.java branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/IssueScopeRequestBuilder.java branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/ProjectScopeRequestBuilder.java branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/VersionScopeRequestBuilder.java Modified: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRestClient.java Deleted: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/DefaultRequestBuilder.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/DefaultRequestBuilder.java 2012-07-15 11:59:36 UTC (rev 305) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/DefaultRequestBuilder.java 2012-07-15 18:29:25 UTC (rev 306) @@ -1,124 +0,0 @@ -/* - * #%L - * JRedmine :: Client - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.jredmine.rest; - -import com.google.common.base.Joiner; -import org.apache.commons.lang3.ArrayUtils; -import org.nuiton.io.rest.RestMethod; -import org.nuiton.io.rest.RestRequest; -import org.nuiton.io.rest.RestRequestBuilder; - -import java.io.File; -import java.util.Map; - -/** - * Default {@link RestRequestBuilder} to be used in {@link RedmineRestClient}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0.0 - */ -public class DefaultRequestBuilder implements RestRequestBuilder { - - private static final long serialVersionUID = 1L; - - protected String name; - - protected String[] action; - - protected final RestMethod method; - - public DefaultRequestBuilder(String name, - RestMethod method, - String... action) { - this.name = name; - this.action = action; - this.method = method; - } - - @Override - public String getName() { - return name; - } - - /** - * @param args the args of the request - * @return TODO - */ - public String[] getParameters(Object... args) { - // by default, no parameter - return ArrayUtils.EMPTY_STRING_ARRAY; - } - - public String[] getPath(Object... args) { - // by default, path is contextPath / action - return action; - } - - public Map<String, File> getAttachments(Object... args) { - // by default, no attachments - return null; - } - - @Override - public RestRequest create(final Object... args) { - - return new RestRequest() { - - @Override - public String[] getPath() { - return DefaultRequestBuilder.this.getPath(args); - } - - @Override - public String[] getParameters() { - return DefaultRequestBuilder.this.getParameters(args); - } - - @Override - public Map<String, File> getAttachments() { - return DefaultRequestBuilder.this.getAttachments(args); - } - - @Override - public boolean containsAttachments() { - Map<String, File> attachments = getAttachments(); - return attachments != null && !attachments.isEmpty(); - } - - @Override - public String toPath(String redmineUrl) { - String result = redmineUrl + "/" + - Joiner.on('/').join(getPath()); - return result; - } - - @Override - public RestMethod getMethod() { - return method; - } - - }; - } -} Deleted: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/IssueScopeRequestBuilder.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/IssueScopeRequestBuilder.java 2012-07-15 11:59:36 UTC (rev 305) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/IssueScopeRequestBuilder.java 2012-07-15 18:29:25 UTC (rev 306) @@ -1,63 +0,0 @@ -/* - * #%L - * JRedmine :: Client - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.jredmine.rest; - -import org.nuiton.io.rest.RestMethod; - -/** - * Builder of requests which are in a project and issue context. - * <p/> - * Created: 1 janv. 2010 - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0.3 - */ -public class IssueScopeRequestBuilder extends ProjectScopeRequestBuilder { - - private static final long serialVersionUID = 1L; - - public IssueScopeRequestBuilder(String name, - RestMethod method, - String... action) { - super(name, method, action); - } - - public IssueScopeRequestBuilder(String name, String... action) { - this(name, null, action); - } - - @Override - public String[] getParameters(Object... args) { - - // args 1 = project id - // args 2 = issue id - - String issueId = (String) args[1]; - - return new String[]{ - "issue_id", issueId - }; - } -} Deleted: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/ProjectScopeRequestBuilder.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/ProjectScopeRequestBuilder.java 2012-07-15 11:59:36 UTC (rev 305) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/ProjectScopeRequestBuilder.java 2012-07-15 18:29:25 UTC (rev 306) @@ -1,61 +0,0 @@ -/* - * #%L - * JRedmine :: Client - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.jredmine.rest; - -import org.nuiton.io.rest.RestMethod; - -/** - * Builder of requests which are in a project context. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0.0 - */ -public class ProjectScopeRequestBuilder extends DefaultRequestBuilder { - - private static final long serialVersionUID = 1L; - - public ProjectScopeRequestBuilder(String name, String... action) { - this(name, null, action); - } - - public ProjectScopeRequestBuilder(String name, - RestMethod method, - String... action) { - super(name, method, action); - } - - @Override - public String[] getPath(Object... args) { - - // one args : projectName - - String projectName = (String) args[0]; - - String[] result = new String[action.length + 1]; - System.arraycopy(action, 0, result, 0, action.length); - result[action.length] = projectName; - return result; - } -} Modified: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java 2012-07-15 11:59:36 UTC (rev 305) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java 2012-07-15 18:29:25 UTC (rev 306) @@ -1,8 +1,12 @@ package org.nuiton.jredmine.rest; +import com.google.common.base.Joiner; import com.google.common.base.Strings; +import org.apache.commons.lang3.ArrayUtils; import org.nuiton.io.rest.AbstractRequestFactory; import org.nuiton.io.rest.RestMethod; +import org.nuiton.io.rest.RestRequest; +import org.nuiton.io.rest.RestRequestBuilder; import org.nuiton.jredmine.model.Attachment; import org.nuiton.jredmine.model.ModelHelper; import org.nuiton.jredmine.model.News; @@ -16,7 +20,7 @@ import java.util.Map; /** - * TODO + * Request factory for the redmine client. * * @author tchemit <chemit@codelutin.com> * @since 1.4 @@ -28,175 +32,366 @@ // misc requests - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.PING_REQUEST_NAME, RestMethod.GET, "jredmine", "ping")); - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.LOGOUT_REQUEST_NAME, RestMethod.GET, "jredmine", "logout")); - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.LOGIN_REQUEST_NAME, RestMethod.POST, "jredmine", "login") { + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.PING_REQUEST_NAME, RestMethod.GET, "jredmine", "ping")); + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.LOGOUT_REQUEST_NAME, RestMethod.GET, "jredmine", "logout")); + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.LOGIN_REQUEST_NAME, RestMethod.POST, "jredmine", "login") { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - @Override - public String[] getParameters(Object... args) { - String login = (String) args[0]; - String password = (String) args[1]; - return new String[]{"username", login, "password", password}; - } - }); + @Override + public String[] getParameters(Object... args) { + String login = (String) args[0]; + String password = (String) args[1]; + return new String[]{"username", login, "password", password}; + } + }); - // data with no scope requests + // data with no scope requests - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_PROJECT_REQUEST_NAME, RestMethod.GET, "jredmine", "get_projects.xml")); - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_USER_PROJECTS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_user_projects.xml")); - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_statuses.xml")); - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_priorities.xml")); + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_PROJECT_REQUEST_NAME, RestMethod.GET, "jredmine", "get_projects.xml")); + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_USER_PROJECTS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_user_projects.xml")); + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_statuses.xml")); + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_priorities.xml")); - // data with project scope requests + // data with project scope requests - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_PROJECT_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_issues.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_OPENED_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_opened_issues.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_CLOSED_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_closed_issues.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_VERSION_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_versions.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_categories.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_TRACKER_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_trackers.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_USER_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_users.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_NEWS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_news.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_PROJECT_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_issues.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_OPENED_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_opened_issues.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_CLOSED_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_closed_issues.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_VERSION_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_versions.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_categories.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_TRACKER_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_trackers.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_USER_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_users.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_NEWS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_news.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.ADD_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "add_version.xml") { + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.ADD_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "add_version.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + Version version = (Version) args[1]; + String date = getVersionEffectiveDate(version); + String status = getVersionStatus(version); + return new String[]{ + "version[name]", version.getName(), + "version[description]", version.getDescription(), + "version[effective_date]", date, + "version[status]", status + }; + } + }); + + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.UPDATE_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "update_version.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + Version version = (Version) args[1]; + String date = getVersionEffectiveDate(version); + String status = getVersionStatus(version); + return new String[]{ + "version[name]", version.getName(), + "version[description]", version.getDescription(), + "version[effective_date]", date, + "version[status]", status + }; + } + }); + + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.NEXT_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "next_version.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + + + Version version = (Version) args[1]; + String date = getVersionEffectiveDate(version); + String status = getVersionStatus(version); + String oldVersionName = (String) args[2]; + return new String[]{ + "oldVersionName", oldVersionName, + "version[name]", version.getName(), + "version[description]", version.getDescription(), + "version[effective_date]", date, + "version[status]", status + }; + } + }); + + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.ADD_NEWS_REQUEST_NAME, RestMethod.POST, "jredmine", "add_news.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + News news = (News) args[1]; + return new String[]{ + "news[title]", news.getTitle(), + "news[summary]", news.getSummary(), + "news[description]", news.getDescription() + }; + } + }); + + // version scope requests + + addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_VERSION_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version.xml")); + addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_ALL_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version_issues.xml")); + addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_ALL_ATTACHMENTS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version_attachments.xml")); + addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.ADD_ATTACHMENT_REQUEST_NAME, RestMethod.POST, "jredmine", "add_version_attachment.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + String versionId = (String) args[1]; + Attachment attachment = (Attachment) args[2]; + return new String[]{ + "version_name", versionId, + "attachment[description]", attachment.getDescription() + }; + } + + @Override + public Map<String, File> getAttachments(Object... args) { + Map<String, File> upload = new HashMap<String, File>(); + Attachment attachment = (Attachment) args[2]; + upload.put("attachment[file]", attachment.getToUpload()); + return upload; + } + }); + + // issue scope requests + + addRequestBuilder(new IssueScopeRequestBuilder(ModelHelper.GET_ALL_ISSUE_TIME_ENTRY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_times.xml")); + + addRequestBuilder(new IssueScopeRequestBuilder(ModelHelper.ADD_ISSUE_TIME_ENTRY_REQUEST_NAME, RestMethod.POST, "jredmine", "add_issue_time.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + String issueId = (String) args[1]; + TimeEntry timeEntry = (TimeEntry) args[2]; + Date d = timeEntry.getSpentOn(); + if (d == null) { + d = new Date(); + } + String date = DATE_FORMAT.format(d); + return new String[]{ + "issue_id", issueId, + "time_entry[activity_id]", timeEntry.getActivityId() + "", + "time_entry[spent_on]", date, + "time_entry[hours]", timeEntry.getHours() + "", + "time_entry[comments]", timeEntry.getComments() == null ? "" : timeEntry.getComments() + }; + } + }); + } + + protected static String getVersionStatus(Version version) { + String status = version.getStatus(); + if (Strings.isNullOrEmpty(status)) { + + // use default open status + status = VersionStatusEnum.open.name(); + } + return status; + } + + protected static String getVersionEffectiveDate(Version version) { + return version.getEffectiveDate() == null ? "" : + DATE_FORMAT.format(version.getEffectiveDate()); + } + + /** + * Default {@link RestRequestBuilder} to be used in {@link RedmineRestClient}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.0 + */ + protected static class DefaultRequestBuilder implements RestRequestBuilder { + private static final long serialVersionUID = 1L; + protected String name; + + protected String[] action; + + protected final RestMethod method; + + public DefaultRequestBuilder(String name, + RestMethod method, + String... action) { + this.name = name; + this.action = action; + this.method = method; + } + @Override + public String getName() { + return name; + } + + /** + * @param args the args of the request + * @return TODO + */ public String[] getParameters(Object... args) { - Version version = (Version) args[1]; - String date = getVersionEffectiveDate(version); - String status = getVersionStatus(version); - return new String[]{ - "version[name]", version.getName(), - "version[description]", version.getDescription(), - "version[effective_date]", date, - "version[status]", status - }; + // by default, no parameter + return ArrayUtils.EMPTY_STRING_ARRAY; } - }); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.UPDATE_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "update_version.xml") { - private static final long serialVersionUID = 1L; + public String[] getPath(Object... args) { + // by default, path is contextPath / action + return action; + } + public Map<String, File> getAttachments(Object... args) { + // by default, no attachments + return null; + } + @Override - public String[] getParameters(Object... args) { - Version version = (Version) args[1]; - String date = getVersionEffectiveDate(version); - String status = getVersionStatus(version); - return new String[]{ - "version[name]", version.getName(), - "version[description]", version.getDescription(), - "version[effective_date]", date, - "version[status]", status + public RestRequest create(final Object... args) { + + return new RestRequest() { + + @Override + public String[] getPath() { + return DefaultRequestBuilder.this.getPath(args); + } + + @Override + public String[] getParameters() { + return DefaultRequestBuilder.this.getParameters(args); + } + + @Override + public Map<String, File> getAttachments() { + return DefaultRequestBuilder.this.getAttachments(args); + } + + @Override + public boolean containsAttachments() { + Map<String, File> attachments = getAttachments(); + return attachments != null && !attachments.isEmpty(); + } + + @Override + public String toPath(String redmineUrl) { + String result = redmineUrl + "/" + + Joiner.on('/').join(getPath()); + return result; + } + + @Override + public RestMethod getMethod() { + return method; + } + }; } - }); + } - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.NEXT_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "next_version.xml") { + /** + * Builder of requests which are in a project context. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.0 + */ + protected static class ProjectScopeRequestBuilder extends DefaultRequestBuilder { + private static final long serialVersionUID = 1L; + public ProjectScopeRequestBuilder(String name, String... action) { + this(name, null, action); + } + + public ProjectScopeRequestBuilder(String name, + RestMethod method, + String... action) { + super(name, method, action); + } + @Override - public String[] getParameters(Object... args) { + public String[] getPath(Object... args) { + // one args : projectName - Version version = (Version) args[1]; - String date = getVersionEffectiveDate(version); - String status = getVersionStatus(version); - String oldVersionName = (String) args[2]; - return new String[]{ - "oldVersionName", oldVersionName, - "version[name]", version.getName(), - "version[description]", version.getDescription(), - "version[effective_date]", date, - "version[status]", status - }; + String projectName = (String) args[0]; + + String[] result = new String[action.length + 1]; + System.arraycopy(action, 0, result, 0, action.length); + result[action.length] = projectName; + return result; } - }); + } - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.ADD_NEWS_REQUEST_NAME, RestMethod.POST, "jredmine", "add_news.xml") { + /** + * Builder of requests which are in a project and issue context. + * <p/> + * Created: 1 janv. 2010 + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.3 + */ + protected static class IssueScopeRequestBuilder extends ProjectScopeRequestBuilder { + private static final long serialVersionUID = 1L; + public IssueScopeRequestBuilder(String name, + RestMethod method, + String... action) { + super(name, method, action); + } + + public IssueScopeRequestBuilder(String name, String... action) { + this(name, null, action); + } + @Override public String[] getParameters(Object... args) { - News news = (News) args[1]; + + // args 1 = project id + // args 2 = issue id + + String issueId = (String) args[1]; + return new String[]{ - "news[title]", news.getTitle(), - "news[summary]", news.getSummary(), - "news[description]", news.getDescription() + "issue_id", issueId }; } - }); + } - // version scope requests + /** + * Builder of requests which are in a project and version context. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.0 + */ + protected static class VersionScopeRequestBuilder extends ProjectScopeRequestBuilder { - addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_VERSION_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version.xml")); - addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_ALL_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version_issues.xml")); - addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_ALL_ATTACHMENTS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version_attachments.xml")); - addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.ADD_ATTACHMENT_REQUEST_NAME, RestMethod.POST, "jredmine", "add_version_attachment.xml") { private static final long serialVersionUID = 1L; - @Override - public String[] getParameters(Object... args) { - String versionId = (String) args[1]; - Attachment attachment = (Attachment) args[2]; - return new String[]{ - "version_name", versionId, - "attachment[description]", attachment.getDescription() - }; + public VersionScopeRequestBuilder(String name, RestMethod method, String... action) { + super(name, method, action); } - @Override - public Map<String, File> getAttachments(Object... args) { - Map<String, File> upload = new HashMap<String, File>(); - Attachment attachment = (Attachment) args[2]; - upload.put("attachment[file]", attachment.getToUpload()); - return upload; + public VersionScopeRequestBuilder(String name, String... action) { + this(name, null, action); } - }); - // issue scope requests + @Override + public String[] getParameters(Object... args) { - addRequestBuilder(new IssueScopeRequestBuilder(ModelHelper.GET_ALL_ISSUE_TIME_ENTRY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_times.xml")); + // args 1 = project id + // args 2 = version name - addRequestBuilder(new IssueScopeRequestBuilder(ModelHelper.ADD_ISSUE_TIME_ENTRY_REQUEST_NAME, RestMethod.POST, "jredmine", "add_issue_time.xml") { - private static final long serialVersionUID = 1L; + String versionName = (String) args[1]; - @Override - public String[] getParameters(Object... args) { - String issueId = (String) args[1]; - TimeEntry timeEntry = (TimeEntry) args[2]; - Date d = timeEntry.getSpentOn(); - if (d == null) { - d = new Date(); - } - String date = DATE_FORMAT.format(d); return new String[]{ - "issue_id", issueId, - //"timeEntry[issue_id]", issueId, - "time_entry[activity_id]", timeEntry.getActivityId() + "", - "time_entry[spent_on]", date, - "time_entry[hours]", timeEntry.getHours() + "", - "time_entry[comments]", timeEntry.getComments() == null ? "" : timeEntry.getComments() + "version_name", versionName }; } - }); -} - - protected static String getVersionStatus(Version version) { - String status = version.getStatus(); - if (Strings.isNullOrEmpty(status)) { - - // use default open status - status = VersionStatusEnum.open.name(); - } - return status; } - - protected static String getVersionEffectiveDate(Version version) { - return version.getEffectiveDate() == null ? "" : - DATE_FORMAT.format(version.getEffectiveDate()); - } } Modified: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRestClient.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRestClient.java 2012-07-15 11:59:36 UTC (rev 305) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRestClient.java 2012-07-15 18:29:25 UTC (rev 306) @@ -81,16 +81,15 @@ @Override protected void close(RestSession session) throws IOException { - if (session == null) { - return; + if (session != null) { + try { + RestRequest request = getRequest(ModelHelper.LOGOUT_REQUEST_NAME); + session.setOpen(false); + session.doRequest(request); + } finally { + session.close(); + } } - try { - RestRequest request = getRequest(ModelHelper.LOGOUT_REQUEST_NAME); - session.setOpen(false); - session.doRequest(request); - } finally { - session.close(); - } } protected void ping(RestSession session) throws IOException { Deleted: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/VersionScopeRequestBuilder.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/VersionScopeRequestBuilder.java 2012-07-15 11:59:36 UTC (rev 305) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/VersionScopeRequestBuilder.java 2012-07-15 18:29:25 UTC (rev 306) @@ -1,59 +0,0 @@ -/* - * #%L - * JRedmine :: Client - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.jredmine.rest; - -import org.nuiton.io.rest.RestMethod; - -/** - * Builder of requests which are in a project and version context. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0.0 - */ -public class VersionScopeRequestBuilder extends ProjectScopeRequestBuilder { - - private static final long serialVersionUID = 1L; - - public VersionScopeRequestBuilder(String name, RestMethod method, String... action) { - super(name, method, action); - } - - public VersionScopeRequestBuilder(String name, String... action) { - this(name, null, action); - } - - @Override - public String[] getParameters(Object... args) { - - // args 1 = project id - // args 2 = version name - - String versionName = (String) args[1]; - - return new String[]{ - "version_name", versionName - }; - } -}