Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ZNTA-1597): User can search for a glossary entry
- Loading branch information
1 parent
2065abe
commit 8d3ab3c
Showing
5 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...mands/src/main/java/org/zanata/client/commands/glossary/search/GlossarySearchCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package org.zanata.client.commands.glossary.search; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.codehaus.jackson.JsonNode; | ||
import org.codehaus.jackson.JsonParser; | ||
import org.codehaus.jackson.map.ObjectMapper; | ||
import org.codehaus.jackson.type.TypeReference; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.zanata.client.commands.ConfigurableCommand; | ||
import org.zanata.client.commands.OptionsUtil; | ||
import org.zanata.rest.client.GlossaryClient; | ||
import org.zanata.rest.client.RestClientFactory; | ||
|
||
import javax.ws.rs.client.ResponseProcessingException; | ||
import javax.ws.rs.core.Response; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
/** | ||
* | ||
* @author Damian Jansen <a href="mailto:djansen@redhat.com">djansen@redhat.com</a> | ||
* | ||
**/ | ||
public class GlossarySearchCommand extends ConfigurableCommand<GlossarySearchOptions> { | ||
|
||
private static final Logger log = LoggerFactory | ||
.getLogger(GlossarySearchCommand.class); | ||
private final GlossaryClient client; | ||
|
||
public GlossarySearchCommand(GlossarySearchOptions opts, | ||
RestClientFactory clientFactory) { | ||
super(opts, clientFactory); | ||
client = getClientFactory().getGlossaryClient(); | ||
} | ||
|
||
public GlossarySearchCommand(GlossarySearchOptions opts) { | ||
this(opts, OptionsUtil.createClientFactory(opts)); | ||
} | ||
|
||
@Override | ||
public void run() throws Exception { | ||
log.info("Server: {}", getOpts().getUrl()); | ||
log.info("Username: {}", getOpts().getUsername()); | ||
|
||
if (StringUtils.isNotBlank(getOpts().getProject())) { | ||
log.info("Project: {}", getOpts().getProject()); | ||
} | ||
|
||
String project = getOpts().getProject(); | ||
String qualifiedName; | ||
try { | ||
qualifiedName = StringUtils.isBlank(project) | ||
? client.getGlobalQualifiedName() | ||
: client.getProjectQualifiedName(project); | ||
} catch (ResponseProcessingException rpe) { | ||
if (rpe.getResponse().getStatus() == 404) { | ||
log.error("Project {} not found", project); | ||
return; | ||
} else { | ||
throw rpe; | ||
} | ||
} | ||
if (StringUtils.isBlank(getOpts().getFilter())) { | ||
log.error("Filter query parameter required"); | ||
return; | ||
} | ||
Response response = client.find(getOpts().getFilter(), qualifiedName); | ||
String responseText = response.readEntity(String.class); | ||
if (getOpts().getRaw()) { | ||
log.info("Raw JSON:\n{}", responseText); | ||
} else { | ||
printFind(responseText); | ||
} | ||
log.info("Search complete"); | ||
} | ||
|
||
private void printFind(String jsonResponse) throws Exception { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); | ||
TypeReference<HashMap<String, JsonNode>> typeRef | ||
= new TypeReference<HashMap<String, JsonNode>>() { | ||
}; | ||
|
||
HashMap<String, JsonNode> jsonMap = mapper.readValue(jsonResponse, typeRef); | ||
log.debug("Entries {}", jsonMap); | ||
log.info("Entries found: {}", jsonMap.get("totalCount").asText()); | ||
JsonNode results = jsonMap.get("results"); | ||
if (results.isArray()) { | ||
for (JsonNode resultNode : results) { | ||
log.info(""); | ||
log.debug("Entry: {}", resultNode); | ||
log.info("Result:"); | ||
log.info("ID: {}", resultNode.get("id").asText()); | ||
log.info("Part of Speech: {}", resultNode.get("pos").asText()); | ||
List<String> contents = new ArrayList<>(); | ||
JsonNode content = resultNode.get("glossaryTerms"); | ||
if (content.isArray()) { | ||
for (JsonNode contentNode : content) { | ||
contents.add(contentNode.get("content").asText()); | ||
} | ||
} | ||
log.info("Content: {}", contents); | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...mands/src/main/java/org/zanata/client/commands/glossary/search/GlossarySearchOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.zanata.client.commands.glossary.search; | ||
|
||
import org.zanata.client.commands.ConfigurableGlossaryOptions; | ||
|
||
public interface GlossarySearchOptions extends ConfigurableGlossaryOptions { | ||
|
||
public String getFilter(); | ||
public String getProject(); | ||
public boolean getRaw(); | ||
} |
61 changes: 61 additions & 0 deletions
61
...s/src/main/java/org/zanata/client/commands/glossary/search/GlossarySearchOptionsImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.zanata.client.commands.glossary.search; | ||
|
||
import org.kohsuke.args4j.Option; | ||
import org.zanata.client.commands.ConfigurableGlossaryOptionsImpl; | ||
import org.zanata.client.commands.ZanataCommand; | ||
|
||
public class GlossarySearchOptionsImpl extends ConfigurableGlossaryOptionsImpl | ||
implements GlossarySearchOptions { | ||
|
||
private String filter; | ||
private String project; | ||
private boolean raw; | ||
|
||
@Override | ||
public String getFilter() { | ||
return filter; | ||
} | ||
|
||
@Override | ||
public String getProject() { | ||
return this.project; | ||
} | ||
|
||
@Override | ||
public boolean getRaw() { | ||
return this.raw; | ||
} | ||
|
||
@Option(name = "--filter", metaVar = "FILTER", required = true, | ||
usage = "Search term for glossary entries.") | ||
public void setFilter(String filter) { | ||
this.filter = filter; | ||
} | ||
|
||
@Option(name = "--project-glossary", metaVar = "PROJECT", | ||
usage = "Project Glossary ID to restrict search.") | ||
public void setProject(String project) { | ||
this.project = project; | ||
} | ||
|
||
@Option(name = "--raw", metaVar = "RAW", | ||
usage = "Print the raw JSON response.") | ||
public void setRaw(boolean raw) { | ||
this.raw = raw; | ||
} | ||
|
||
@Override | ||
public ZanataCommand initCommand() { | ||
return new GlossarySearchCommand(this); | ||
} | ||
|
||
@Override | ||
public String getCommandName() { | ||
return "glossary-search"; | ||
} | ||
|
||
@Override | ||
public String getCommandDescription() { | ||
return "Find glossary entries in Zanata"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters