Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
set project type when selecting version in zanata init
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Jan 7, 2015
1 parent 5e30fc9 commit 69bb0ea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Expand Up @@ -20,6 +20,7 @@
*/
package org.zanata.client.commands.init;

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.zanata.client.commands.ConsoleInteractor.DisplayMode.Confirmation;
import static org.zanata.client.commands.ConsoleInteractor.DisplayMode.Hint;
import static org.zanata.client.commands.ConsoleInteractor.DisplayMode.Question;
Expand All @@ -29,6 +30,7 @@
import java.util.List;

import org.zanata.client.commands.ConsoleInteractor;
import org.zanata.client.commands.ConsoleInteractorImpl;
import org.zanata.common.EntityStatus;
import org.zanata.rest.client.ProjectClient;
import org.zanata.rest.client.ProjectIterationClient;
Expand All @@ -37,7 +39,9 @@
import org.zanata.rest.dto.ProjectIteration;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.sun.jersey.api.client.UniformInterfaceException;
Expand Down Expand Up @@ -93,11 +97,28 @@ protected void selectVersion() {
consoleInteractor.printf(Question, _("select.version.prompt"));
String selection =
consoleInteractor.expectAnswerWithRetry(expect(versionIndexes));
String versionId =
Iterables.get(activeIterations,
(Integer.valueOf(selection) - 1))
.getId();
ProjectIteration projectIteration = Iterables.get(activeIterations,
(Integer.valueOf(selection) - 1));
String versionId = projectIteration.getId();
opts.setProjectVersion(versionId);
opts.setProjectType(resolveProjectType(project, projectIteration));
}

private String resolveProjectType(Project project,
ProjectIteration projectIteration) {
if (!isNullOrEmpty(projectIteration.getProjectType())) {
return projectIteration.getProjectType().toLowerCase();
} else if (!isNullOrEmpty(project.getDefaultType())) {
return project.getDefaultType().toLowerCase();
} else {
String projectTypes =
Joiner.on(", ").join(ProjectPrompt.PROJECT_TYPE_LIST);
consoleInteractor.printfln(Question, _("project.type.prompt"),
projectTypes);
return consoleInteractor.expectAnswerWithRetry(
ConsoleInteractorImpl.AnswerValidatorImpl
.expect(ProjectPrompt.PROJECT_TYPE_LIST));
}
}

@VisibleForTesting
Expand Down
Expand Up @@ -60,6 +60,9 @@ class ProjectPrompt {
// state variables
private List<Project> allProjects = Collections.emptyList();
private List<Project> filteredProjects = Collections.emptyList();
public static final List<String> PROJECT_TYPE_LIST =
Lists.transform(Lists.newArrayList(ProjectType.values()),
new ProjectTypeToStringFunction());

ProjectPrompt(ConsoleInteractor consoleInteractor, InitOptions opts,
ProjectIterationPrompt projectIterationPrompt,
Expand Down Expand Up @@ -190,14 +193,11 @@ protected void createNewProject() {
String projectId = consoleInteractor.expectAnyAnswer();
consoleInteractor.printfln(Question, _("project.name.prompt"));
String projectName = consoleInteractor.expectAnyAnswer();
List<String> projectTypeList =
Lists.transform(Lists.newArrayList(ProjectType.values()),
new ProjectTypeToStringFunction());
String projectTypes = Joiner.on(", ").join(projectTypeList);
String projectTypes = Joiner.on(", ").join(PROJECT_TYPE_LIST);
consoleInteractor.printfln(Question, _("project.type.prompt"), projectTypes);
String projectType =
consoleInteractor.expectAnswerWithRetry(
AnswerValidatorImpl.expect(projectTypeList));
AnswerValidatorImpl.expect(PROJECT_TYPE_LIST));
ProjectClient projectClient = clientFactory.getProjectClient(projectId);
Project project = new Project(projectId, projectName, projectType);
try {
Expand Down
Expand Up @@ -72,11 +72,13 @@ public void willGuideUserToSelectVersion() {

verify(projectClient).get();
assertThat(opts.getProjectVersion(), Matchers.equalTo("4.8.2"));
assertThat(opts.getProjectType(), Matchers.equalTo("gettext"));
}

private static ProjectIteration newIteration(String id, EntityStatus status) {
ProjectIteration iteration = new ProjectIteration(id);
iteration.setStatus(status);
iteration.setProjectType("gettext");
return iteration;
}

Expand Down

0 comments on commit 69bb0ea

Please sign in to comment.