Skip to content

Commit 571389e

Browse files
authored
Merge pull request #109 from yumaoka/rfe-106-metadata
Supporting bundle/resource entry metadata
2 parents f1ddfbd + 1c184f3 commit 571389e

18 files changed

Lines changed: 773 additions & 12 deletions

File tree

gp-ant-task/src/main/java/com/ibm/g11n/pipeline/ant/GPDownloadTask.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ private LanguageBundle getBundle(ServiceClient client, String bundleId, String l
379379
String srcVal = data.getSourceValue();
380380
Integer seqNum = data.getSequenceNumber();
381381
List<String> notes = data.getNotes();
382+
Map<String, String> metadata = data.getMetadata();
382383

383384
if (reviewedOnly) {
384385
if (!data.isReviewed()) {
@@ -398,6 +399,9 @@ private LanguageBundle getBundle(ServiceClient client, String bundleId, String l
398399
if (notes != null) {
399400
resb.notes(notes);
400401
}
402+
if (metadata != null) {
403+
resb.metadata(metadata);
404+
}
401405
bundleBuilder.addResourceString(resb);
402406
}
403407
}

gp-ant-task/src/main/java/com/ibm/g11n/pipeline/ant/GPUploadTask.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.tools.ant.Project;
3131

3232
import com.ibm.g11n.pipeline.client.BundleData;
33+
import com.ibm.g11n.pipeline.client.BundleDataChangeSet;
3334
import com.ibm.g11n.pipeline.client.NewBundleData;
3435
import com.ibm.g11n.pipeline.client.NewResourceEntryData;
3536
import com.ibm.g11n.pipeline.client.ServiceClient;
@@ -75,6 +76,7 @@ public void execute() throws BuildException {
7576
// Checks if the bundle already exists
7677
String bundleId = bf.getBundleId();
7778
boolean createNew = false;
79+
Set<String> currentTgtLangs = null;
7880
if (bundleIds.contains(bundleId)) {
7981
getProject().log("Found bundle:" + bundleId, Project.MSG_INFO);
8082
// Checks if the source language matches.
@@ -85,6 +87,7 @@ public void execute() throws BuildException {
8587
+ ") does not match the specified language("
8688
+ srcLang + ").");
8789
}
90+
currentTgtLangs = bundle.getTargetLanguages();
8891
} else {
8992
getProject().log("bundle:" + bundleId + " does not exist, creating a new bundle.", Project.MSG_INFO);
9093
createNew = true;
@@ -108,8 +111,43 @@ public void execute() throws BuildException {
108111
}
109112
// set bundle notes
110113
newBundleData.setNotes(resBundle.getNotes());
114+
// set metadata
115+
newBundleData.setMetadata(resBundle.getMetadata());
111116
client.createBundle(bundleId, newBundleData);
112117
getProject().log("Created bundle: " + bundleId, Project.MSG_INFO);
118+
} else {
119+
BundleDataChangeSet bundleDataChanges = new BundleDataChangeSet();
120+
boolean updateBundle = false;
121+
122+
// checks if target languages need to be updated
123+
if (!tgtLangs.isEmpty()) {
124+
if (currentTgtLangs == null || !currentTgtLangs.containsAll(tgtLangs)) {
125+
// add missing target languages - we don't want to delete
126+
// existing target languages automatically here.
127+
Set<String> newTgtLangs = new TreeSet<>(tgtLangs);
128+
if (currentTgtLangs != null) {
129+
newTgtLangs.addAll(currentTgtLangs);
130+
}
131+
bundleDataChanges.setTargetLanguages(newTgtLangs);
132+
updateBundle = true;
133+
}
134+
}
135+
136+
// update bundle notes if any
137+
if (!resBundle.getNotes().isEmpty()) {
138+
bundleDataChanges.setNotes(resBundle.getNotes());
139+
updateBundle = true;
140+
}
141+
// update metadata if any - for now, this operation only appends
142+
// extra metadata key-value pairs from bundle files
143+
if (!resBundle.getMetadata().isEmpty()) {
144+
bundleDataChanges.setMetadata(resBundle.getMetadata());
145+
updateBundle = true;
146+
}
147+
if (updateBundle) {
148+
client.updateBundle(bundleId, bundleDataChanges);
149+
getProject().log("Updated bundle data: " + bundleId, Project.MSG_INFO);
150+
}
113151
}
114152
Collection<ResourceString> resStrings = resBundle.getResourceStrings();
115153
for (ResourceString resString : resStrings) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ protected void _execute() {
8585
BundleData bundleData = getClient().getBundleInfo(bundleId);
8686
List<String> bundleNotes = bundleData.getNotes();
8787
if (bundleNotes != null) {
88-
bundleBuilder.addNotes(bundleNotes);
88+
bundleBuilder.notes(bundleNotes);
89+
}
90+
Map<String, String> bundleMetadata = bundleData.getMetadata();
91+
if (bundleMetadata != null) {
92+
bundleBuilder.metadata(bundleMetadata);
8993
}
9094

9195
resEntries =
@@ -97,6 +101,7 @@ protected void _execute() {
97101
Integer seqNum = data.getSequenceNumber();
98102
String srcVal = data.getSourceValue();
99103
List<String> notes = data.getNotes();
104+
Map<String, String> metadata = data.getMetadata();
100105

101106
if (resVal == null && fallback) {
102107
resVal = srcVal;
@@ -111,6 +116,9 @@ protected void _execute() {
111116
if (notes != null) {
112117
resString.notes(notes);
113118
}
119+
if (metadata != null) {
120+
resString.metadata(metadata);
121+
}
114122
bundleBuilder.addResourceString(resString.build());
115123
}
116124
}

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import com.beust.jcommander.Parameter;
2626
import com.beust.jcommander.Parameters;
27+
import com.ibm.g11n.pipeline.client.BundleData;
28+
import com.ibm.g11n.pipeline.client.BundleDataChangeSet;
2729
import com.ibm.g11n.pipeline.client.NewResourceEntryData;
2830
import com.ibm.g11n.pipeline.client.ServiceException;
2931
import com.ibm.g11n.pipeline.resfilter.FilterOptions;
@@ -65,9 +67,17 @@ final class ImportCmd extends BundleCmd {
6567

6668
@Override
6769
protected void _execute() {
68-
// TODO: Bundle comments should be imported if the language of the resource files is
69-
// the bundle's source language.
70+
// get the specified bundle data
71+
BundleData bundleData = null;
72+
try {
73+
bundleData = getClient().getBundleInfo(bundleId);
74+
} catch (ServiceException e) {
75+
throw new RuntimeException("Failed to locate bundle: " + bundleId, e);
76+
}
77+
assert bundleData != null;
78+
boolean isSrcLang = bundleData.getSourceLanguage().equals(languageId);
7079

80+
BundleDataChangeSet bundleDataChanges = null;
7181
Map<String, NewResourceEntryData> resEntries = null;
7282
ResourceFilter filter = ResourceFilterFactory.getResourceFilter(type);
7383
if (filter == null) {
@@ -77,6 +87,25 @@ protected void _execute() {
7787
try (FileInputStream fis = new FileInputStream(f)) {
7888
LanguageBundle bundle = filter.parse(fis, new FilterOptions(Locale.forLanguageTag(languageId)));
7989

90+
if (isSrcLang) {
91+
// if the specified language is the source language, update bundle data if
92+
// notes/metadata are available in parsed result.
93+
94+
// notes
95+
if (!bundle.getNotes().isEmpty()) {
96+
bundleDataChanges = new BundleDataChangeSet();
97+
bundleDataChanges.setNotes(bundle.getNotes());
98+
}
99+
// update metadata if any - for now, this operation only appends
100+
// extra metadata key-value pairs from bundle files
101+
if (!bundle.getMetadata().isEmpty()) {
102+
if (bundleDataChanges == null) {
103+
bundleDataChanges = new BundleDataChangeSet();
104+
}
105+
bundleDataChanges.setMetadata(bundle.getMetadata());
106+
}
107+
}
108+
80109
resEntries = new HashMap<>(bundle.getResourceStrings().size());
81110
for (ResourceString resString : bundle.getResourceStrings()) {
82111
NewResourceEntryData resEntryData = new NewResourceEntryData(resString.getValue());
@@ -85,6 +114,7 @@ protected void _execute() {
85114
resEntryData.setSequenceNumber(Integer.valueOf(seqNum));
86115
}
87116
resEntryData.setNotes(resString.getNotes());
117+
resEntryData.setMetadata(resString.getMetadata());
88118
if (asReviewed) {
89119
resEntryData.setReviewed(Boolean.TRUE);
90120
}
@@ -99,6 +129,9 @@ protected void _execute() {
99129
}
100130

101131
try {
132+
if (bundleDataChanges != null) {
133+
getClient().updateBundle(bundleId, bundleDataChanges);
134+
}
102135
getClient().uploadResourceEntries(bundleId, languageId, resEntries);
103136
} catch (ServiceException e) {
104137
throw new RuntimeException(e);

gp-maven-plugin/src/main/java/com/ibm/g11n/pipeline/maven/GPDownloadMojo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ private LanguageBundle getBundle(ServiceClient client, String bundleId, String l
352352
String srcVal = data.getSourceValue();
353353
Integer seqNum = data.getSequenceNumber();
354354
List<String> notes = data.getNotes();
355+
Map<String, String> metadata = data.getMetadata();
355356

356357
if (reviewedOnly) {
357358
if (!data.isReviewed()) {
@@ -371,6 +372,9 @@ private LanguageBundle getBundle(ServiceClient client, String bundleId, String l
371372
if (notes != null) {
372373
resb.notes(notes);
373374
}
375+
if (metadata != null) {
376+
resb.metadata(metadata);
377+
}
374378
bundleBuilder.addResourceString(resb);
375379
}
376380
}

gp-maven-plugin/src/main/java/com/ibm/g11n/pipeline/maven/GPUploadMojo.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.maven.plugins.annotations.Mojo;
3131

3232
import com.ibm.g11n.pipeline.client.BundleData;
33+
import com.ibm.g11n.pipeline.client.BundleDataChangeSet;
3334
import com.ibm.g11n.pipeline.client.NewBundleData;
3435
import com.ibm.g11n.pipeline.client.NewResourceEntryData;
3536
import com.ibm.g11n.pipeline.client.ServiceClient;
@@ -74,6 +75,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
7475
// Checks if the bundle already exists
7576
String bundleId = bf.getBundleId();
7677
boolean createNew = false;
78+
Set<String> currentTgtLangs = null;
7779
if (bundleIds.contains(bundleId)) {
7880
getLog().info("Found bundle:" + bundleId);
7981
// Checks if the source language matches.
@@ -84,6 +86,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
8486
+ ") does not match the specified language("
8587
+ srcLang + ").");
8688
}
89+
currentTgtLangs = bundle.getTargetLanguages();
8790
} else {
8891
getLog().info("bundle:" + bundleId + " does not exist, creating a new bundle.");
8992
createNew = true;
@@ -107,8 +110,43 @@ public void execute() throws MojoExecutionException, MojoFailureException {
107110
}
108111
// set bundle notes
109112
newBundleData.setNotes(resBundle.getNotes());
113+
// set metadata
114+
newBundleData.setMetadata(resBundle.getMetadata());
110115
client.createBundle(bundleId, newBundleData);
111116
getLog().info("Created bundle: " + bundleId);
117+
} else {
118+
BundleDataChangeSet bundleDataChanges = new BundleDataChangeSet();
119+
boolean updateBundle = false;
120+
121+
// checks if target languages need to be updated
122+
if (!tgtLangs.isEmpty()) {
123+
if (currentTgtLangs == null || !currentTgtLangs.containsAll(tgtLangs)) {
124+
// add missing target languages - we don't want to delete
125+
// existing target languages automatically here.
126+
Set<String> newTgtLangs = new TreeSet<>(tgtLangs);
127+
if (currentTgtLangs != null) {
128+
newTgtLangs.addAll(currentTgtLangs);
129+
}
130+
bundleDataChanges.setTargetLanguages(newTgtLangs);
131+
updateBundle = true;
132+
}
133+
}
134+
135+
// update bundle notes if any
136+
if (!resBundle.getNotes().isEmpty()) {
137+
bundleDataChanges.setNotes(resBundle.getNotes());
138+
updateBundle = true;
139+
}
140+
// update metadata if any - for now, this operation only appends
141+
// extra metadata key-value pairs from bundle files
142+
if (!resBundle.getMetadata().isEmpty()) {
143+
bundleDataChanges.setMetadata(resBundle.getMetadata());
144+
updateBundle = true;
145+
}
146+
if (updateBundle) {
147+
client.updateBundle(bundleId, bundleDataChanges);
148+
getLog().info("Updated bundle data: " + bundleId);
149+
}
112150
}
113151
Collection<ResourceString> resStrings = resBundle.getResourceStrings();
114152
for (ResourceString resString : resStrings) {
@@ -118,7 +156,13 @@ public void execute() throws MojoExecutionException, MojoFailureException {
118156
resEntryData.setSequenceNumber(Integer.valueOf(seqNum));
119157
}
120158
// set resource string notes
121-
resEntryData.setNotes(resString.getNotes());
159+
if (!resString.getNotes().isEmpty()) {
160+
resEntryData.setNotes(resString.getNotes());
161+
}
162+
// set resource string metadata
163+
if (!resString.getMetadata().isEmpty()) {
164+
resEntryData.setMetadata(resString.getMetadata());
165+
}
122166
resEntries.put(resString.getKey(), resEntryData);
123167
}
124168
} catch (IOException e) {

gp-res-filter/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<attribute name="maven.pomderived" value="true"/>
77
</attributes>
88
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/resource"/>
910
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
1011
<attributes>
1112
<attribute name="maven.pomderived" value="true"/>

gp-res-filter/pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45

56
<parent>
@@ -27,6 +28,15 @@
2728
<groupId>org.apache.maven.plugins</groupId>
2829
<artifactId>maven-javadoc-plugin</artifactId>
2930
</plugin>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-surefire-plugin</artifactId>
34+
<configuration>
35+
<additionalClasspathElements>
36+
<additionalClasspathElement>${basedir}/src/test/resource</additionalClasspathElement>
37+
</additionalClasspathElements>
38+
</configuration>
39+
</plugin>
3040
</plugins>
3141
</build>
3242

gp-res-filter/src/main/java/com/ibm/g11n/pipeline/resfilter/LanguageBundle.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Collection;
2020
import java.util.Collections;
2121
import java.util.List;
22+
import java.util.Map;
23+
import java.util.TreeMap;
2224

2325
import com.ibm.g11n.pipeline.resfilter.ResourceString.ResourceStringComparator;
2426

@@ -30,6 +32,7 @@
3032
public final class LanguageBundle {
3133
private Collection<ResourceString> resourceStrings;
3234
private List<String> notes;
35+
private Map<String, String> metadata;
3336
private String embeddedLanguageCode;
3437
private String embeddedSourceLanguageCode;
3538

@@ -59,6 +62,26 @@ public List<String> getNotes() {
5962
return Collections.unmodifiableList(notes);
6063
}
6164

65+
/**
66+
* Sets the metadata key-value paris for this bundle.
67+
* @param metadata the metadata key-value pairs for this bundle.
68+
*/
69+
public void setMetadata(Map<String, String> metadata) {
70+
this.metadata = new TreeMap<>(metadata);
71+
}
72+
73+
/**
74+
* Returns an unmodifiable map of metadata key-value pairs for this language bundle.
75+
* An empty map is returned when no metadata entries are available.
76+
* @return an unmodifiable map of metadata key-value pairs for this langauge bundle.
77+
*/
78+
public Map<String, String> getMetadata() {
79+
if (metadata == null) {
80+
return Collections.emptyMap();
81+
}
82+
return Collections.unmodifiableMap(metadata);
83+
}
84+
6285
/**
6386
* Sets a collection of resource strings.
6487
* @param resourceStrings A set of resource strings.

0 commit comments

Comments
 (0)