Author: tchemit Date: 2012-07-15 22:50:20 +0200 (Sun, 15 Jul 2012) New Revision: 313 Url: http://nuiton.org/repositories/revision/jredmine/313 Log: improve the display data mojo (no more use of dummy enum from api Removed: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/RedmineModelEnum.java branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/RedmineModelScope.java Modified: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/DefaultRedmineServiceImplementor.java branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceImplementor.java branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/io/xpp3/RedmineXpp3Helper.java branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java branches/jredmine-1.x/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java Modified: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/DefaultRedmineServiceImplementor.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/DefaultRedmineServiceImplementor.java 2012-07-15 19:53:22 UTC (rev 312) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/DefaultRedmineServiceImplementor.java 2012-07-15 20:50:20 UTC (rev 313) @@ -109,6 +109,11 @@ } @Override + public RestClient getSession() { + return session; + } + + @Override public <T> T getData(String requestName, Class<T> type, Object... args) throws RedmineServiceException { Modified: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceImplementor.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceImplementor.java 2012-07-15 19:53:22 UTC (rev 312) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceImplementor.java 2012-07-15 20:50:20 UTC (rev 313) @@ -49,16 +49,6 @@ */ boolean isInit(); -// /** -// * Initialize the service given a redmine client already initialized. -// * -// * @param session the redmine client to be used by the service -// * @return the initialized service -// * @throws RedmineServiceException if any pb -// * @see RedmineRestClient -// */ -// RedmineServiceImplementor init(RestClient session) throws RedmineServiceException; - /** * Initialize the service given a client configuration. * @@ -76,6 +66,13 @@ void destroy() throws RedmineServiceException; /** + * Gets the transport layer based on {@link RestClient}. + * + * @return the transport layer. + */ + RestClient getSession(); + + /** * Generic method to obtain a single data from a redmine server. * * @param <T> the type of data to obtain Deleted: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/RedmineModelEnum.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/RedmineModelEnum.java 2012-07-15 19:53:22 UTC (rev 312) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/RedmineModelEnum.java 2012-07-15 20:50:20 UTC (rev 313) @@ -1,95 +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.model; - -/** - * Un énumération pour connaitre l'ensemble des classes du modèle. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0.0 - */ -public enum RedmineModelEnum { - - issueStatus(IssueStatus.class, RedmineModelScope.none, ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME), - issuePriority(IssuePriority.class, RedmineModelScope.none, ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME), - project(Project.class, RedmineModelScope.none, ModelHelper.GET_ALL_PROJECT_REQUEST_NAME), - issueCategory(IssueCategory.class, RedmineModelScope.project, ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME), - tracker(Tracker.class, RedmineModelScope.project, ModelHelper.GET_ALL_TRACKER_REQUEST_NAME), - user(User.class, RedmineModelScope.project, ModelHelper.GET_ALL_USER_REQUEST_NAME), - news(News.class, RedmineModelScope.project, ModelHelper.GET_ALL_NEWS_REQUEST_NAME), - version(Version.class, RedmineModelScope.project, ModelHelper.GET_ALL_VERSION_REQUEST_NAME), - issue(Issue.class, RedmineModelScope.version, ModelHelper.GET_ALL_ISSUES_REQUEST_NAME), - timeEntry(TimeEntry.class, RedmineModelScope.issue, ModelHelper.GET_ALL_ISSUE_TIME_ENTRY_REQUEST_NAME), - attachment(Attachment.class, RedmineModelScope.version, ModelHelper.GET_ALL_ATTACHMENTS_REQUEST_NAME); - - private final Class<?> modelType; - - private final RedmineModelScope scope; - - private final String requestAll; - - RedmineModelEnum(Class<?> modelType, RedmineModelScope scope, String requestAll) { - this.modelType = modelType; - this.scope = scope; - this.requestAll = requestAll; - } - - - public Class<?> getModelType() { - return modelType; - } - - public RedmineModelScope getScope() { - return scope; - } - - public String getRequestAll() { - return requestAll; - } - - public static RedmineModelEnum valueOf(Class<?> type) { - for (RedmineModelEnum e : values()) { - if (e.getModelType().equals(type)) { - return e; - } - } - return null; - } - - public static RedmineModelEnum safeValueOf(Class<?> type) { - if (type == null) { - throw new NullPointerException("klass parameter can not be null"); - } - - // check dataType is authorized - - RedmineModelEnum r = valueOf(type); - if (r == null) { - throw new IllegalArgumentException("the type " + type.getName() + " is not allowed!"); - } - - return r; - } -} Deleted: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/RedmineModelScope.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/RedmineModelScope.java 2012-07-15 19:53:22 UTC (rev 312) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/RedmineModelScope.java 2012-07-15 20:50:20 UTC (rev 313) @@ -1,56 +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.model; - -/** - * Une enumeration pour caracteriser le scope d'une donnée dans le modèle. - * <p/> - * <p/> - * Pour une donnée qui ne dépend de rien, elle est en scope {@link #none}. - * <p/> - * Pour une donnée qui dépend d'un projet, elle est en scope {@link #project}. - * <p/> - * Pour une donnée qui dépend d'une version, elle est en scpoe {@link #version}. - * <p/> - * Pour une donnée qui dépend d'une issue, elle est en scpoe {@link #issue}. - * <p/> - * A savoir qu'une donnée de scope {@link #version} (resp. {@link #issue}), - * elle est aussi de scope {@link #project} car la version (resp. l'issue) - * dépend en dépend. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0.0 - */ -public enum RedmineModelScope { - - /** none scope, requires nothing. */ - none, - /** project scope, requires a project. */ - project, - /** version scope, requires a project and a version. */ - version, - /** issue scope, requires a project and an issue. */ - issue -} Modified: branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/io/xpp3/RedmineXpp3Helper.java =================================================================== --- branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/io/xpp3/RedmineXpp3Helper.java 2012-07-15 19:53:22 UTC (rev 312) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/model/io/xpp3/RedmineXpp3Helper.java 2012-07-15 20:50:20 UTC (rev 313) @@ -31,7 +31,6 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.nuiton.io.xpp3.Xpp3Helper; import org.nuiton.io.xpp3.Xpp3Reader; -import org.nuiton.jredmine.model.RedmineModelEnum; import java.io.ByteArrayInputStream; import java.io.File; @@ -101,8 +100,6 @@ InputStream stream, boolean strict) throws IOException, XmlPullParserException { - RedmineModelEnum.safeValueOf(klass); - O result = readObject(klass, ReaderFactory.newXmlReader(stream), strict @@ -115,8 +112,6 @@ InputStream stream, boolean strict) throws IOException, XmlPullParserException { - RedmineModelEnum.safeValueOf(klass); - O[] results = readObjects(klass, ReaderFactory.newXmlReader(stream), strict 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 19:53:22 UTC (rev 312) +++ branches/jredmine-1.x/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java 2012-07-15 20:50:20 UTC (rev 313) @@ -50,6 +50,17 @@ */ public class RedmineRequestFactory extends AbstractRequestFactory { + public static String getRequestScope(RestRequestBuilder requestBuilder) { + String scope; + if (requestBuilder instanceof DefaultRequestBuilder) { + + scope = ((DefaultRequestBuilder) requestBuilder).getScope(); + } else { + scope = null; + } + return scope; + } + @Override public void addDefaultRequests() { @@ -240,14 +251,24 @@ protected String[] action; + protected String scope; + protected final RestMethod method; public DefaultRequestBuilder(String name, RestMethod method, String... action) { + this(name, null, method, action); + } + + public DefaultRequestBuilder(String name, + String scope, + RestMethod method, + String... action) { this.name = name; this.action = action; this.method = method; + this.scope = scope; } @Override @@ -255,6 +276,10 @@ return name; } + public final String getScope() { + return scope; + } + /** * @param args the args of the request * @return TODO @@ -326,14 +351,17 @@ private static final long serialVersionUID = 1L; - public ProjectScopeRequestBuilder(String name, String... action) { - this(name, null, action); + public ProjectScopeRequestBuilder(String name, + RestMethod method, + String... action) { + this(name, ModelHelper.PROJECT_SCOPE, method, action); } public ProjectScopeRequestBuilder(String name, + String scope, RestMethod method, String... action) { - super(name, method, action); + super(name, scope, method, action); } @Override @@ -365,12 +393,9 @@ public IssueScopeRequestBuilder(String name, RestMethod method, String... action) { - super(name, method, action); + super(name, ModelHelper.ISSUE_SCOPE, method, action); } - public IssueScopeRequestBuilder(String name, String... action) { - this(name, null, action); - } @Override public String[] getParameters(Object... args) { @@ -396,14 +421,12 @@ private static final long serialVersionUID = 1L; - public VersionScopeRequestBuilder(String name, RestMethod method, String... action) { - super(name, method, action); + public VersionScopeRequestBuilder(String name, + RestMethod method, + String... action) { + super(name, ModelHelper.VERSION_SCOPE, method, action); } - public VersionScopeRequestBuilder(String name, String... action) { - this(name, null, action); - } - @Override public String[] getParameters(Object... args) { Modified: branches/jredmine-1.x/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java =================================================================== --- branches/jredmine-1.x/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java 2012-07-15 19:53:22 UTC (rev 312) +++ branches/jredmine-1.x/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java 2012-07-15 20:50:20 UTC (rev 313) @@ -24,20 +24,34 @@ */ 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.rest.RedmineRequestFactory; 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. @@ -106,8 +120,36 @@ protected boolean anonymous; /** list of actions to perform */ - protected List<RedmineModelEnum> actions; + protected Map<String, Class<?>> actions; + private final Map<Class<?>, String> typesToRequestName; + + private final Map<String, Class<?>> namesToType; + + public DisplayDataMojo() { + typesToRequestName = Maps.newHashMap(); + typesToRequestName.put(Project.class, ModelHelper.GET_ALL_PROJECT_REQUEST_NAME); + typesToRequestName.put(Version.class, ModelHelper.GET_ALL_VERSION_REQUEST_NAME); + typesToRequestName.put(Issue.class, ModelHelper.GET_ALL_ISSUES_REQUEST_NAME); + typesToRequestName.put(News.class, ModelHelper.GET_ALL_NEWS_REQUEST_NAME); + typesToRequestName.put(IssueStatus.class, ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME); + typesToRequestName.put(IssuePriority.class, ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME); + typesToRequestName.put(IssueCategory.class, ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME); + typesToRequestName.put(Tracker.class, ModelHelper.GET_ALL_TRACKER_REQUEST_NAME); + typesToRequestName.put(User.class, ModelHelper.GET_ALL_USER_REQUEST_NAME); + + 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 protected boolean checkRunOnceDone() { return false; @@ -131,21 +173,9 @@ 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); - if (actions == null || actions.isEmpty()) { - // no need to connect, nothing to do... - return; - } super.init(); + + actions = getDownloadActions(); } @Override @@ -188,84 +218,120 @@ @Override protected boolean checkSkip() { + boolean canContinue; if (actions == null || actions.isEmpty()) { // no data to treate - getLog().warn("No data types detected, you must fill the required parameter types, will skip goal"); - return false; + getLog().warn("No data types detected, you must fill the " + + "required parameter types, will skip goal"); + canContinue = false; + } else { + + canContinue = super.checkSkip(); } - boolean b = super.checkSkip(); - return b; + return canContinue; } @Override protected void doAction() throws Exception { StringBuilder buffer = new StringBuilder("\n"); - for (RedmineModelEnum entry : actions) { + for (Map.Entry<String, Class<?>> entry : actions.entrySet()) { - RedmineModelScope scope = entry.getScope(); + Class<?> type = entry.getValue(); + String dataTypeName = entry.getKey(); + String scope = getDataScope(type); + String requestName = typesToRequestName.get(type); - String requestName = entry.getRequestAll(); - IdAble[] datas = (IdAble[]) service.getDelegateImplementor().getDatas(requestName, entry.getModelType(), projectId, versionId); + IdAble[] datas; + + RedmineServiceImplementor implementor = + service.getDelegateImplementor(); + + if (ModelHelper.VERSION_SCOPE.equals(scope)) { + datas = (IdAble[]) implementor.getDatas(requestName, type, projectId, versionId); + } else if (ModelHelper.PROJECT_SCOPE.equals(scope)) { + datas = (IdAble[]) implementor.getDatas(requestName, type, projectId); + } else { + datas = (IdAble[]) implementor.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(dataTypeName).append('\''); + if (ModelHelper.VERSION_SCOPE.equals(scope) || ModelHelper.PROJECT_SCOPE.equals(scope)) { buffer.append(" for project '").append(projectId).append("'"); } - if (scope == RedmineModelScope.version) { + if (ModelHelper.VERSION_SCOPE.equals(scope)) { buffer.append(" and version '").append(versionId).append("'"); } buffer.append('\n'); for (IdAble data : list) { - buffer.append(" - ").append(data.getId()).append(" = ").append(((I18nAble) data).getName()).append('\n'); + buffer.append(" - "); + buffer.append(data.getId()); + buffer.append(" = "); + buffer.append(((I18nAble) data).getName()); + buffer.append('\n'); } } System.out.println(buffer.toString()); } - protected List<RedmineModelEnum> getDownloadActions(String dataTypes, RedmineModelEnum... universe) throws MojoFailureException { + protected Map<String, Class<?>> getDownloadActions() throws MojoFailureException { - if (universe.length == 0) { - throw new MojoFailureException("List of accepted data types is empty!"); - } + Map<String, Class<?>> results = Maps.newTreeMap(); - List<RedmineModelEnum> results = new ArrayList<RedmineModelEnum>(); + List<Class<?>> universeList = Lists.newArrayList(typesToRequestName.keySet()); - List<RedmineModelEnum> universeList = new ArrayList<RedmineModelEnum>(Arrays.asList(universe)); + for (String dataType : types.split(",")) { - for (String dataType : dataTypes.split(",")) { + dataType = dataType.toLowerCase(Locale.ENGLISH); - 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)) { + 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); + throw new MojoFailureException( + "The data type '" + result + + "' can not be used, Accepted types are : " + + universeList); } - RedmineModelScope scope = result.getScope(); + String scope = getDataScope(result); - if (scope == RedmineModelScope.project || scope == RedmineModelScope.version) { + if (ModelHelper.VERSION_SCOPE.equals(scope) || ModelHelper.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."); + throw new MojoFailureException( + "The data type '" + result + + "', depends on a project, but the 'projectId' was " + + "not filled."); } } - if (scope == RedmineModelScope.version) { + if (ModelHelper.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."); + throw new MojoFailureException( + "The data type '" + result + + "', depends on a version, but the 'versionId' was " + + "not filled."); } } - results.add(result); + results.put(dataType, result); } return results; } + + protected String getDataScope(Class<?> type) { + + RequestFactory requestFactory = + service.getDelegateImplementor().getSession().getRequestFactory(); + + String requestName = typesToRequestName.get(type); + RestRequestBuilder requestBuilder = + requestFactory.getRequestBuilder(requestName); + + return RedmineRequestFactory.getRequestScope(requestBuilder); + } }