|
| 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 java.util.Collections; |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import com.beust.jcommander.Parameter; |
| 24 | +import com.beust.jcommander.Parameters; |
| 25 | +import com.ibm.g11n.pipeline.client.BundleDataChangeSet; |
| 26 | +import com.ibm.g11n.pipeline.client.ServiceException; |
| 27 | + |
| 28 | +/** |
| 29 | + * Updates an existing translation bundle. |
| 30 | + * |
| 31 | + * @author Yoshito Umaoka |
| 32 | + */ |
| 33 | +@Parameters(commandDescription = "Updates an existing translation bundle.") |
| 34 | +public class UpdateBundleCmd extends BundleCmd { |
| 35 | + |
| 36 | + @Parameter( |
| 37 | + names = {"-l", "--languages"}, |
| 38 | + description = "List of bundle's target language ID(s) separted by comma. " |
| 39 | + + "Empty list \"\" will remove all existing target languages.") |
| 40 | + private String languageIdsListStr; |
| 41 | + |
| 42 | + @Parameter( |
| 43 | + names = {"-n", "--note"}, |
| 44 | + description = "Translation instruction note. " |
| 45 | + + "Empty note \"\" will remove existing instruction note.") |
| 46 | + private String note; |
| 47 | + |
| 48 | + @Parameter( |
| 49 | + names = {"-r", "--readOnly"}, |
| 50 | + description = "true to set the bundle read only.") |
| 51 | + private String readOnlyStr; |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void _execute() { |
| 55 | + Set<String> trgLangs = null; |
| 56 | + if (languageIdsListStr != null) { |
| 57 | + String[] langs = languageIdsListStr.split(","); |
| 58 | + trgLangs = new HashSet<>(langs.length); |
| 59 | + for (String lang : langs) { |
| 60 | + if (lang.isEmpty()) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + trgLangs.add(lang); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + List<String> notes = null; |
| 68 | + if (note != null) { |
| 69 | + if (note.isEmpty()) { |
| 70 | + // Empty note will delete the existing note |
| 71 | + notes = Collections.emptyList(); |
| 72 | + } else { |
| 73 | + notes = Collections.singletonList(note); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + Boolean readOnly = null; |
| 78 | + if (readOnlyStr != null) { |
| 79 | + if (readOnlyStr.equalsIgnoreCase("true")) { |
| 80 | + readOnly = Boolean.TRUE; |
| 81 | + } else if (readOnlyStr.equalsIgnoreCase("false")) { |
| 82 | + readOnly = Boolean.FALSE; |
| 83 | + } else { |
| 84 | + System.out.println("Bad -r (--readOnly) argument value: " + readOnlyStr |
| 85 | + + ". The command argument will be ignored."); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (languageIdsListStr == null && notes == null && readOnlyStr == null) { |
| 90 | + System.out.println("Nothing to update."); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + BundleDataChangeSet changes = new BundleDataChangeSet(); |
| 95 | + changes |
| 96 | + .setTargetLanguages(trgLangs) |
| 97 | + .setNotes(notes) |
| 98 | + .setReadOnly(readOnly); |
| 99 | + |
| 100 | + try { |
| 101 | + getClient().updateBundle(bundleId, changes); |
| 102 | + } catch (ServiceException e) { |
| 103 | + throw new RuntimeException(e); |
| 104 | + } |
| 105 | + |
| 106 | + System.out.println("Bundle " + bundleId + " was successfully updated."); |
| 107 | + if (trgLangs != null) { |
| 108 | + String newTrgLangs = trgLangs.isEmpty() ? "<removed>" : trgLangs.toString(); |
| 109 | + System.out.println("- Target languages: " + newTrgLangs); |
| 110 | + } |
| 111 | + if (notes != null) { |
| 112 | + String newNote = notes.isEmpty() ? "<removed>" : notes.get(0); |
| 113 | + System.out.println("- Translation instruction note: " + newNote); |
| 114 | + } |
| 115 | + if (readOnly != null) { |
| 116 | + System.out.println("- Read only: " + readOnly); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments