Author: tchemit Date: 2012-07-13 18:29:42 +0200 (Fri, 13 Jul 2012) New Revision: 284 Url: http://nuiton.org/repositories/revision/jredmine/284 Log: remove usage of dummy enums Removed: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/model/ Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java 2012-07-13 16:29:20 UTC (rev 283) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java 2012-07-13 16:29:42 UTC (rev 284) @@ -24,21 +24,36 @@ */ package org.nuiton.jredmine.plugin; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.nuiton.io.rest.RequestFactory; +import org.nuiton.io.rest.RestRequestBuilder; import org.nuiton.jredmine.RedmineServiceImplementor; import org.nuiton.jredmine.model.I18nAble; import org.nuiton.jredmine.model.IdAble; +import org.nuiton.jredmine.model.Issue; +import org.nuiton.jredmine.model.IssueCategory; +import org.nuiton.jredmine.model.IssuePriority; +import org.nuiton.jredmine.model.IssueStatus; import org.nuiton.jredmine.model.ModelHelper; -import org.nuiton.jredmine.model.RedmineModelEnum; -import org.nuiton.jredmine.model.RedmineModelScope; +import org.nuiton.jredmine.model.News; +import org.nuiton.jredmine.model.Project; +import org.nuiton.jredmine.model.Tracker; +import org.nuiton.jredmine.model.User; +import org.nuiton.jredmine.model.Version; +import org.nuiton.jredmine.request.ProjectScopeRedmineRequestBuilder; +import org.nuiton.jredmine.request.VersionScopeRedmineRequestBuilder; +import org.nuiton.jredmine.v1_3_x.RedmineRequestFactoryImpl; import org.nuiton.plugin.PluginHelper; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Locale; +import java.util.Map; /** * Display in console some data from redmine's server. @@ -51,6 +66,10 @@ @Mojo(name = "display-data", requiresOnline = true, requiresProject = true) public class DisplayDataMojo extends AbstractRedmineMojo { + public static final String VERSION_SCOPE = "version"; + + public static final String PROJECT_SCOPE = "project"; + /** * The data types to display : {@code issueSatus}, {@code issuePriority}, * {@code issueCategory}, {@code tracker} and {@code user}. @@ -91,10 +110,35 @@ protected boolean anonymous; /** list of actions to perform */ - protected List<RedmineModelEnum> actions; + protected List<Class<?>> actions; + private final Map<Class<?>, String> typesToRequestName; + + private final Map<String, Class<?>> namesToType; + public DisplayDataMojo() { super(false, false, false); + typesToRequestName = Maps.newHashMap(); + typesToRequestName.put(Project.class, RedmineRequestFactoryImpl.GET_PROJECT_LIST); + typesToRequestName.put(Version.class, RedmineRequestFactoryImpl.GET_VERSION_LIST); + typesToRequestName.put(Issue.class, RedmineRequestFactoryImpl.GET_ISSUE_LIST); + typesToRequestName.put(News.class, RedmineRequestFactoryImpl.GET_NEWS_LIST); + typesToRequestName.put(IssueStatus.class, RedmineRequestFactoryImpl.GET_ISSUE_STATUS_LIST); + typesToRequestName.put(IssuePriority.class, RedmineRequestFactoryImpl.GET_ISSUE_PRIORITY_LIST); + typesToRequestName.put(IssueCategory.class, RedmineRequestFactoryImpl.GET_ISSUE_CATEGORY_LIST); + typesToRequestName.put(Tracker.class, RedmineRequestFactoryImpl.GET_TRACKER_LIST); + typesToRequestName.put(User.class, RedmineRequestFactoryImpl.GET_USER_LIST); + + namesToType = Maps.newHashMap(); + namesToType.put("project", Project.class); + namesToType.put("version", Version.class); + namesToType.put("issue", Issue.class); + namesToType.put("news", News.class); + namesToType.put("issuestatus", IssueStatus.class); + namesToType.put("issuepriority", IssuePriority.class); + namesToType.put("issuecategory", IssueCategory.class); + namesToType.put("tracker", Tracker.class); + namesToType.put("user", User.class); } @Override @@ -120,16 +164,7 @@ versionId = PluginHelper.removeSnapshotSuffix(versionId); } - actions = getDownloadActions(types, - RedmineModelEnum.project, - RedmineModelEnum.version, - RedmineModelEnum.issue, - RedmineModelEnum.news, - RedmineModelEnum.issueStatus, - RedmineModelEnum.issuePriority, - RedmineModelEnum.issueCategory, - RedmineModelEnum.tracker, - RedmineModelEnum.user); + actions = getDownloadActions(); if (actions == null || actions.isEmpty()) { // no need to connect, nothing to do... return; @@ -162,21 +197,31 @@ protected void doAction() throws Exception { StringBuilder buffer = new StringBuilder("\n"); - for (RedmineModelEnum entry : actions) { + RequestFactory requestFactory = + ((RedmineServiceImplementor) service).getSession().getRequestFactory(); + for (Class<?> type : actions) { - RedmineModelScope scope = entry.getScope(); + String scope = getScope(requestFactory, type); + String requestName = typesToRequestName.get(type); - String requestName = entry.getRequestAll(); - IdAble[] datas = (IdAble[]) ((RedmineServiceImplementor) service).getDatas(requestName, entry.getModelType(), projectId, versionId); + IdAble[] datas; + + if (VERSION_SCOPE.equals(scope)) { + datas = (IdAble[]) ((RedmineServiceImplementor) service).getDatas(requestName, type, projectId, versionId); + } else if (PROJECT_SCOPE.equals(scope)) { + datas = (IdAble[]) ((RedmineServiceImplementor) service).getDatas(requestName, type, projectId); + } else { + datas = (IdAble[]) ((RedmineServiceImplementor) service).getDatas(requestName, type); + } List<IdAble> list = Arrays.asList(datas); if (sortById) { Collections.sort(list, ModelHelper.ID_ABLE_COMPARATOR); } - buffer.append("\nValues of '").append(entry).append('\''); - if (scope == RedmineModelScope.project || scope == RedmineModelScope.version) { + buffer.append("\nValues of '").append(requestName).append('\''); + if (VERSION_SCOPE.equals(scope) || PROJECT_SCOPE.equals(scope)) { buffer.append(" for project '").append(projectId).append("'"); } - if (scope == RedmineModelScope.version) { + if (VERSION_SCOPE.equals(scope)) { buffer.append(" and version '").append(versionId).append("'"); } buffer.append('\n'); @@ -187,39 +232,53 @@ System.out.println(buffer.toString()); } - protected List<RedmineModelEnum> getDownloadActions(String dataTypes, RedmineModelEnum... universe) throws MojoFailureException { + protected String getScope(RequestFactory requestFactory, Class<?> type) { + String scope; - if (universe.length == 0) { - throw new MojoFailureException("List of accepted data types is empty!"); + String requestName = typesToRequestName.get(type); + RestRequestBuilder requestBuilder = + requestFactory.getRequestBuilder(requestName); + + if (requestBuilder instanceof VersionScopeRedmineRequestBuilder) { + scope = VERSION_SCOPE; + } else if (requestBuilder instanceof ProjectScopeRedmineRequestBuilder) { + scope = PROJECT_SCOPE; + } else { + scope = null; } + return scope; + } - List<RedmineModelEnum> results = new ArrayList<RedmineModelEnum>(); + protected List<Class<?>> getDownloadActions() throws MojoFailureException { - List<RedmineModelEnum> universeList = new ArrayList<RedmineModelEnum>(Arrays.asList(universe)); + List<Class<?>> results = Lists.newArrayList(); - for (String dataType : dataTypes.split(",")) { + List<Class<?>> universeList = Lists.newArrayList(typesToRequestName.keySet()); - RedmineModelEnum result; - try { - result = RedmineModelEnum.valueOf(dataType.trim()); - } catch (Exception e) { - throw new MojoFailureException("Unkown data type '" + dataType + "', Accepted types are : " + universeList); - } - if (!universeList.contains(result)) { + RequestFactory requestFactory = + ((RedmineServiceImplementor) service).getSession().getRequestFactory(); + + for (String dataType : types.split(",")) { + + dataType = dataType.toLowerCase(Locale.ENGLISH); + + Class<?> result = namesToType.get(dataType); + + if (result == null || !universeList.contains(result)) { // can not accept these value throw new MojoFailureException("The data type '" + result + "' can not be used, Accepted types are : " + universeList); } - RedmineModelScope scope = result.getScope(); + String scope = getScope(requestFactory, result); - if (scope == RedmineModelScope.project || scope == RedmineModelScope.version) { + if (VERSION_SCOPE.equals(scope) || PROJECT_SCOPE.equals(scope)) { // check projectId used if (projectId == null || projectId.isEmpty()) { throw new MojoFailureException("The data type '" + result + "', depends on a project, but the 'projectId' was not filled."); } } - if (scope == RedmineModelScope.version) { + if (VERSION_SCOPE.equals(scope)) { // check versionId use if (versionId == null || versionId.isEmpty()) { throw new MojoFailureException("The data type '" + result + "', depends on a version, but the 'versionId' was not filled."); @@ -229,4 +288,47 @@ } return results; } + +// protected List<RedmineModelEnum> getDownloadActions(String dataTypes, RedmineModelEnum... universe) throws MojoFailureException { +// +// if (universe.length == 0) { +// throw new MojoFailureException("List of accepted data types is empty!"); +// } +// +// List<RedmineModelEnum> results = new ArrayList<RedmineModelEnum>(); +// +// List<RedmineModelEnum> universeList = new ArrayList<RedmineModelEnum>(Arrays.asList(universe)); +// +// for (String dataType : dataTypes.split(",")) { +// +// RedmineModelEnum result; +// try { +// result = RedmineModelEnum.valueOf(dataType.trim()); +// } catch (Exception e) { +// throw new MojoFailureException("Unkown data type '" + dataType + "', Accepted types are : " + universeList); +// } +// if (!universeList.contains(result)) { +// // can not accept these value +// throw new MojoFailureException("The data type '" + result + "' can not be used, Accepted types are : " + universeList); +// } +// +// RedmineModelScope scope = result.getScope(); +// +// if (scope == RedmineModelScope.project || scope == RedmineModelScope.version) { +// +// // check projectId used +// if (projectId == null || projectId.isEmpty()) { +// throw new MojoFailureException("The data type '" + result + "', depends on a project, but the 'projectId' was not filled."); +// } +// } +// if (scope == RedmineModelScope.version) { +// // check versionId use +// if (versionId == null || versionId.isEmpty()) { +// throw new MojoFailureException("The data type '" + result + "', depends on a version, but the 'versionId' was not filled."); +// } +// } +// results.add(result); +// } +// return results; +// } }