3030import org .apache .maven .plugins .annotations .Mojo ;
3131
3232import com .ibm .g11n .pipeline .client .BundleData ;
33+ import com .ibm .g11n .pipeline .client .BundleDataChangeSet ;
3334import com .ibm .g11n .pipeline .client .NewBundleData ;
3435import com .ibm .g11n .pipeline .client .NewResourceEntryData ;
3536import 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 ) {
0 commit comments