com.ibm.g11n.pipeline.tools.cli.UpdateBundleCmd Maven / Gradle / Ivy
The newest version!
/*
* Copyright IBM Corp. 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.g11n.pipeline.tools.cli;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.ibm.g11n.pipeline.client.BundleDataChangeSet;
import com.ibm.g11n.pipeline.client.ServiceException;
/**
* Updates an existing translation bundle.
*
* @author Yoshito Umaoka
*/
@Parameters(commandDescription = "Updates an existing translation bundle.")
public class UpdateBundleCmd extends BundleCmd {
@Parameter(
names = {"-l", "--languages"},
description = "List of bundle's target language ID(s) separted by comma. "
+ "Empty list \"\" will remove all existing target languages.")
private String languageIdsListStr;
@Parameter(
names = {"-n", "--note"},
description = "Translation instruction note. "
+ "Empty note \"\" will remove existing instruction note.")
private String note;
@Parameter(
names = {"-r", "--readOnly"},
description = "true to set the bundle read only.")
private String readOnlyStr;
@Override
protected void _execute() {
Set trgLangs = null;
if (languageIdsListStr != null) {
String[] langs = languageIdsListStr.split(",");
trgLangs = new HashSet<>(langs.length);
for (String lang : langs) {
if (lang.isEmpty()) {
continue;
}
trgLangs.add(lang);
}
}
List notes = null;
if (note != null) {
if (note.isEmpty()) {
// Empty note will delete the existing note
notes = Collections.emptyList();
} else {
notes = Collections.singletonList(note);
}
}
Boolean readOnly = null;
if (readOnlyStr != null) {
if (readOnlyStr.equalsIgnoreCase("true")) {
readOnly = Boolean.TRUE;
} else if (readOnlyStr.equalsIgnoreCase("false")) {
readOnly = Boolean.FALSE;
} else {
System.out.println("Bad -r (--readOnly) argument value: " + readOnlyStr
+ ". The command argument will be ignored.");
}
}
if (languageIdsListStr == null && notes == null && readOnlyStr == null) {
System.out.println("Nothing to update.");
return;
}
BundleDataChangeSet changes = new BundleDataChangeSet();
changes
.setTargetLanguages(trgLangs)
.setNotes(notes)
.setReadOnly(readOnly);
try {
getClient().updateBundle(bundleId, changes);
} catch (ServiceException e) {
throw new RuntimeException(e);
}
System.out.println("Bundle " + bundleId + " was successfully updated.");
if (trgLangs != null) {
String newTrgLangs = trgLangs.isEmpty() ? "" : trgLangs.toString();
System.out.println("- Target languages: " + newTrgLangs);
}
if (notes != null) {
String newNote = notes.isEmpty() ? "" : notes.get(0);
System.out.println("- Translation instruction note: " + newNote);
}
if (readOnly != null) {
System.out.println("- Read only: " + readOnly);
}
}
}