Skip to content

Commit cc5026a

Browse files
authored
Merge pull request #117 from JCEmmons/gpdocs
Updates to CLI tools to support documents
2 parents df2098b + 8f59b62 commit cc5026a

12 files changed

Lines changed: 578 additions & 3 deletions

gp-cli/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
<dependency>
9797
<groupId>com.ibm.g11n.pipeline</groupId>
9898
<artifactId>gp-java-client</artifactId>
99-
<version>1.1.6</version>
99+
<version>1.2.0</version>
100100
</dependency>
101101

102102
<dependency>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright IBM Corp. 2017, 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ibm.g11n.pipeline.tools.cli;
17+
18+
import java.util.HashSet;
19+
import java.util.Set;
20+
21+
import com.beust.jcommander.Parameter;
22+
import com.beust.jcommander.Parameters;
23+
import com.ibm.g11n.pipeline.client.NewDocumentData;
24+
import com.ibm.g11n.pipeline.client.ServiceException;
25+
26+
/**
27+
* Creates a new document for translation.
28+
*
29+
* @author John Emmons
30+
*/
31+
@Parameters(commandDescription = "Creates a new document for translation.")
32+
final class CreateDocumentCmd extends DocumentCmd {
33+
@Parameter(
34+
names = {"-l", "--language"},
35+
description = "Language ID(s) separted by comma. "
36+
+ "The first element will be used as source language",
37+
required = true)
38+
private String languageIds;
39+
40+
@Override
41+
protected void _execute() {
42+
String[] langs = languageIds.split(",");
43+
NewDocumentData newDocumentData = new NewDocumentData(langs[0].trim());
44+
if (langs.length > 1) {
45+
Set<String> targetLangs = new HashSet<String>(langs.length - 1);
46+
for (int i = 1; i < langs.length; i++) {
47+
targetLangs.add(langs[i].trim());
48+
}
49+
newDocumentData.setTargetLanguages(targetLangs);
50+
}
51+
52+
try {
53+
getClient().createDocument(type, documentId, newDocumentData);
54+
} catch (ServiceException e) {
55+
throw new RuntimeException(e);
56+
}
57+
58+
System.out.println("A new document '" + documentId + "' was successfully created.");
59+
}
60+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright IBM Corp. 2017, 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ibm.g11n.pipeline.tools.cli;
17+
18+
import com.beust.jcommander.Parameters;
19+
import com.ibm.g11n.pipeline.client.ServiceException;
20+
21+
/**
22+
* Deletes a translatable document.
23+
*
24+
* @author John Emmons
25+
*/
26+
@Parameters(commandDescription = "Deletes a translatable document.")
27+
final class DeleteDocumentCmd extends DocumentCmd {
28+
@Override
29+
protected void _execute() {
30+
try {
31+
getClient().deleteDocument(type, documentId);
32+
} catch (ServiceException e) {
33+
throw new RuntimeException(e);
34+
}
35+
36+
System.out.println("Document '" + documentId + "' was successfully deleted.");
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright IBM Corp. 2017
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ibm.g11n.pipeline.tools.cli;
17+
18+
import com.beust.jcommander.Parameter;
19+
20+
/**
21+
* A base class of commands taking document ID as an input.
22+
*
23+
* @author John Emmons
24+
*/
25+
abstract class DocumentCmd extends DocumentTypeCmd {
26+
@Parameter(
27+
names = { "-d", "--document"},
28+
description = "Document ID",
29+
required = true)
30+
protected String documentId;
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright IBM Corp. 2017, 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ibm.g11n.pipeline.tools.cli;
17+
18+
import com.beust.jcommander.Parameter;
19+
import com.ibm.g11n.pipeline.client.DocumentType;
20+
21+
/**
22+
* A base class of commands taking document type as an input.
23+
*
24+
* @author John Emmons
25+
*/
26+
abstract class DocumentTypeCmd extends BaseCmd {
27+
@Parameter(
28+
names = { "-t", "--type"},
29+
description = "Document type",
30+
converter = DocumentTypeConverter.class,
31+
required = true)
32+
protected DocumentType type;
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright IBM Corp. 2017, 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ibm.g11n.pipeline.tools.cli;
17+
18+
import java.util.Locale;
19+
20+
import com.beust.jcommander.IStringConverter;
21+
import com.beust.jcommander.ParameterException;
22+
import com.ibm.g11n.pipeline.client.DocumentType;
23+
24+
/**
25+
* Convert document type parameter String to DocumentType.
26+
*
27+
* @author John Emmons
28+
*/
29+
public class DocumentTypeConverter implements IStringConverter<DocumentType>{
30+
31+
@Override
32+
public DocumentType convert(String type) {
33+
try {
34+
return DocumentType.valueOf(type.toUpperCase(Locale.ENGLISH));
35+
} catch (IllegalArgumentException e) {
36+
throw new ParameterException("Parameter value " + type +
37+
" is not valid for the document type option.");
38+
}
39+
}
40+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright IBM Corp. 2017, 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ibm.g11n.pipeline.tools.cli;
17+
18+
import java.io.File;
19+
import java.io.FileOutputStream;
20+
import java.io.IOException;
21+
22+
import com.beust.jcommander.Parameter;
23+
import com.beust.jcommander.Parameters;
24+
import com.ibm.g11n.pipeline.client.ServiceException;
25+
26+
/**
27+
* Exports document content from a translatable document.
28+
*
29+
* @author John Emmons
30+
*/
31+
@Parameters(commandDescription = "Exports document content from a translatable document.")
32+
final class ExportDocumentCmd extends DocumentCmd {
33+
@Parameter(
34+
names = {"-l", "--language"},
35+
description = "Language ID",
36+
required = true)
37+
private String languageId;
38+
39+
@Parameter(
40+
names = {"-f", "--file"},
41+
description = "File name to be exported",
42+
required = true)
43+
private String fileName;
44+
45+
46+
@Override
47+
protected void _execute() {
48+
byte[] content;
49+
try {
50+
content = getClient().getDocumentContent(type, documentId, languageId);
51+
} catch (ServiceException e) {
52+
throw new RuntimeException(e);
53+
}
54+
55+
File f = new File(fileName);
56+
try {
57+
FileOutputStream fos = new FileOutputStream(f);
58+
fos.write(content);
59+
fos.flush();
60+
fos.close();
61+
} catch (IOException e) {
62+
throw new RuntimeException("Failed to write the document content to " + fileName + ": " + e.getMessage(), e);
63+
}
64+
65+
System.out.println("Document content from document:" + documentId
66+
+ ", language: " + languageId + " was successfully saved to file "
67+
+ fileName);
68+
}
69+
}

gp-cli/src/main/java/com/ibm/g11n/pipeline/tools/cli/GPCmd.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corp. 2015,2016,2017
2+
* Copyright IBM Corp. 2015, 2018
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@ public static void main(String[] args) {
4343

4444
jc.addCommand("help", new HelpCmd());
4545

46+
// bundles
4647
jc.addCommand("list-bundle", new ListBundlesCmd(), "list");
4748
jc.addCommand("show-bundle", new ShowBundleCmd(), "show");
4849
jc.addCommand("create-bundle", new CreateBundleCmd(), "create");
@@ -54,12 +55,21 @@ public static void main(String[] args) {
5455
jc.addCommand("import", new ImportCmd());
5556
jc.addCommand("list-mt-languages", new ListMTLanguagesCmd());
5657

57-
//users
58+
// users
5859
jc.addCommand("list-users", new ListUsersCmd());
5960
jc.addCommand("create-user", new CreateUserCmd());
6061
jc.addCommand("delete-user", new DeleteUserCmd());
6162
jc.addCommand("reset-user-password" , new ResetUserPasswordCmd());
6263

64+
// documents
65+
jc.addCommand("list-documents", new ListDocumentsCmd());
66+
jc.addCommand("show-document", new ShowDocumentCmd());
67+
jc.addCommand("create-document", new CreateDocumentCmd());
68+
jc.addCommand("delete-document", new DeleteDocumentCmd());
69+
jc.addCommand("update-document", new UpdateDocumentCmd());
70+
jc.addCommand("export-document", new ExportDocumentCmd());
71+
jc.addCommand("import-document", new ImportDocumentCmd());
72+
6373
try {
6474
jc.parse(args);
6575
String cmdName = jc.getParsedCommand();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright IBM Corp. 2017, 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ibm.g11n.pipeline.tools.cli;
17+
18+
import java.io.File;
19+
20+
import com.beust.jcommander.Parameter;
21+
import com.beust.jcommander.Parameters;
22+
import com.ibm.g11n.pipeline.client.ServiceException;
23+
24+
/**
25+
* Imports source language data to a translatable document.
26+
*
27+
* @author John Emmons
28+
*/
29+
@Parameters(commandDescription = "Imports source language content to a translatable document.")
30+
final class ImportDocumentCmd extends DocumentCmd {
31+
@Parameter(
32+
names = {"-f", "--file"},
33+
description = "File name to be imported",
34+
required = true)
35+
private String fileName;
36+
37+
@Parameter(
38+
names = {"-l", "--language"},
39+
description = "Language ID",
40+
required = true)
41+
private String languageId;
42+
43+
@Override
44+
protected void _execute() {
45+
File f = new File(fileName);
46+
47+
try {
48+
getClient().updateDocumentContent(type, documentId, languageId, f);
49+
} catch (ServiceException e) {
50+
throw new RuntimeException(e);
51+
}
52+
53+
System.out.println("Document content from " + fileName
54+
+ " was successfully imported to document:" + documentId);
55+
}
56+
}

0 commit comments

Comments
 (0)