Skip to content

Commit

Permalink
Merge pull request #27 from yumaoka/rfe-tr-updates
Browse files Browse the repository at this point in the history
Supports new translation request status CANCELLED
  • Loading branch information
yumaoka authored Sep 21, 2017
2 parents bdb1462 + 29d2f6c commit c265dd1
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class NewTranslationRequestData {
private EnumSet<IndustryDomain> domains;
private List<String> notes;
private Map<String, String> metadata;
private Map<String, String> partnerParameters;
private boolean submit;

/**
Expand Down Expand Up @@ -233,6 +234,29 @@ public Map<String, String> getMetadata() {
return metadata;
}

/**
* Sets a map containing key-value pairs specifying configuration parameters
* passed to professional human post editing service provider.
*
* @param partnerParameters A map containing key-value pairs specifying configuration parameters.
* @return This object.
*/
public NewTranslationRequestData setPartnerParameters(Map<String, String> partnerParameters) {
this.partnerParameters = partnerParameters;
return this;
}

/**
* Returns a map containing key-value pairs specifying configuration parameters
* passed to professional human post editing service provider.
*
* @return A map containing key-value pairs specifying configuration parameters
* passed to professional human post editing service provider.
*/
public Map<String, String> getPartnerParameters() {
return partnerParameters;
}

/**
* Sets whether this new translation request will be submitted immediately
* after creation. The default value is false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ public final Date getUpdatedAt() {
*/
public abstract Map<String, String> getMetadata();

/**
* Returns a map containing key-value pairs specifying configuration parameters
* passed to professional human post editing service provider.
*
* <p>Configuration parameters may vary depending on a service provider.
* This field should be empty unless a service provider asks a Globalization Pipeline
* user to provide some translation request specific information.
*
* @return A map containing key-value pairs specifying configuration parameters
* passed to professional human post editing service provider.
*/
public abstract Map<String, String> getPartnerParameters();

/**
* Returns the word count information of this translation request.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class TranslationRequestDataChangeSet {
private EnumSet<IndustryDomain> domains;
private List<String> notes;
private Map<String, String> metadata;
private Map<String, String> partnerParameters;
private boolean submit;

/**
Expand Down Expand Up @@ -218,6 +219,9 @@ public List<String> getNotes() {
* Sets a map containing the metadata of this translation request represented
* by key-value pairs.
*
* <p>This map should specify only delta from the current map. To remove a specific
* key from existing metadta filed, specify the key to be removed with an empty value.
*
* @param metadata A map containing the metadata of this translation request represented
* by key-value pairs.
* @return This object.
Expand All @@ -238,6 +242,33 @@ public Map<String, String> getMetadata() {
return metadata;
}

/**
* Sets a map containing key-value pairs specifying configuration parameters
* passed to professional human post editing service provider.
*
* <p>This map should specify only delta from the current map. To remove a specific
* key from existing partnerParameters filed, specify the key to be removed with an
* empty value.
*
* @param partnerParameters A map containing key-value pairs specifying configuration parameters.
* @return This object.
*/
public TranslationRequestDataChangeSet setPartnerParameters(Map<String, String> partnerParameters) {
this.partnerParameters = partnerParameters;
return this;
}

/**
* Returns a map containing key-value pairs specifying configuration parameters
* passed to professional human post editing service provider.
*
* @return A map containing key-value pairs specifying configuration parameters
* passed to professional human post editing service provider.
*/
public Map<String, String> getPartnerParameters() {
return partnerParameters;
}

/**
* Sets whether this new translation request will be submitted immediately
* after this change set is applied. The default value is false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,48 @@ public enum TranslationRequestStatus {
/**
* Draft status. The translation request is not yet submitted for
* professional translation post editing service. Globalization Pipeline
* user can modify the contents of the translation request.
* user can edit the contents of the translation request or delete the
* request in this state.
*/
DRAFT,
/**
* Submitted status. The translation request is submitted by user
* for professional translation post editing service. At this point,
* Globalization Pipeline user cannot modify the contents of the translation
* request.
* for professional translation post editing service. Globalization Pipeline
* user cannot modify the contents of the translation request in this state.
*/
SUBMITTED,
/**
* Started status. The professional translation post editing service
* provider acknowledged the translation request and started working on
* the request.
* the request. Globalization Pipeline user cannot modify the contents
* of the translation request in this state.
*/
STARTED,
/**
* Translated status. The professional translation post editing service
* provider finished editing the translation.
* provider finished editing the translation. Globalization Pipeline user
* cannot modify the contents of the translation request in this state.
*/
TRANSLATED,
/**
* Merged status. The final translation results from the professional translation
* post editing provider were merged to the original Globalization Pipeline
* bundle.
* bundle. Globalization Pipeline user can modify only specific field(s) such
* as metadata field in this state.
*/
MERGED;
MERGED,
/**
* Cancelled status. The translation request is cancelled. Globalization Pipeline
* service or assigned professional translation post editing service provider may
* cancel a translation request when the service or the provider cannot handle
* the request for some reasons. Globalization Pipeline user cannot modify
* the contents of the translation request in this state, but can delete it.
*/
CANCELLED,
/**
* Unknown status. This is a special status only used when Globalization Pipeline
* service returns a status not supported by this SDK. This status should not be
* used for updating an existing translation request.
*/
UNKNOWN;
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import com.ibm.g11n.pipeline.client.TranslationConfigData;
import com.ibm.g11n.pipeline.client.TranslationRequestData;
import com.ibm.g11n.pipeline.client.TranslationRequestDataChangeSet;
import com.ibm.g11n.pipeline.client.TranslationRequestStatus;
import com.ibm.g11n.pipeline.client.TranslationStatus;
import com.ibm.g11n.pipeline.client.UserData;
import com.ibm.g11n.pipeline.client.UserDataChangeSet;
Expand Down Expand Up @@ -1414,33 +1415,44 @@ private static String getHmacCredential(String uid, String secret, String method
}

//
// Custom JSON deserialization code supporting Java Enum
// Custom JSON serialization/deserialization supporting Java Enum
//

// Custom type adapter for TranslationStatus enum. This adapter implementation
// maps unknown status type to TranslationStatus.UNKNOWN. This fallback mapping
// allow GP REST server to introduce a new status without breaking Java SDK
// client code.
private static class TranslationStatusAdapter extends TypeAdapter<TranslationStatus> {
@Override
public TranslationStatus read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
// Custom type adapter for serializing/deserializing Java enum with mapping.
// This method assumes all enum constants are in upper case.
static class EnumWithFallbackAdapter<T extends Enum<T>> extends TypeAdapter<T> {
private final T fallback;

public EnumWithFallbackAdapter(T fallback) {
this.fallback = fallback;
}

T toEnumWithFallback(String s) {
if (s == null) {
return null;
}
TranslationStatus status = null;
String statusStr = in.nextString();

T value = null;
try {
status = TranslationStatus.valueOf(statusStr.toUpperCase());
value = (T) Enum.valueOf(fallback.getDeclaringClass(), s.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
// use UNKNOWN as fallback
status = TranslationStatus.UNKNOWN;
// fallback
value = fallback;
}
return status;
return value;
}

@Override
public void write(JsonWriter out, TranslationStatus value) throws IOException {
public T read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
return toEnumWithFallback(in.nextString());
}

@Override
public void write(JsonWriter out, T value) throws IOException {
if (value == null) {
out.nullValue();
return;
Expand Down Expand Up @@ -1513,7 +1525,10 @@ private static Gson createGson(String className) {
builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");

builder.registerTypeAdapter(TranslationStatus.class,
new TranslationStatusAdapter());
new EnumWithFallbackAdapter<TranslationStatus>(TranslationStatus.UNKNOWN));

builder.registerTypeAdapter(TranslationRequestStatus.class,
new EnumWithFallbackAdapter<TranslationRequestStatus>(TranslationRequestStatus.UNKNOWN));

builder.registerTypeAdapter(
new TypeToken<EnumMap<TranslationStatus, Integer>>() {}.getType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ public Map<String, String> getMetadata() {
return Collections.unmodifiableMap(metadata);
}

@Override
public Map<String, String> getPartnerParameters() {
Map<String, String> partnerParameters = translationRequest.getPartnerParameters();
if (partnerParameters == null) {
return null;
}
return Collections.unmodifiableMap(partnerParameters);
}

@Override
public Map<String, WordCountData> getWordCountData() {
Map<String, WordCountData> wcDataByBundle = new TreeMap<>();
Expand Down Expand Up @@ -191,6 +200,7 @@ static class RestTranslationRequest extends RestObject {
private Set<String> domains;
private List<String> notes;
private Map<String, String> metadata;
private Map<String, String> partnerParameters;
private String status;
private Map<String, RestWordCountData> wordCountsByBundle;
private Date estimatedCompletion;
Expand Down Expand Up @@ -236,6 +246,10 @@ public Map<String, String> getMetadata() {
return metadata;
}

public Map<String, String> getPartnerParameters() {
return partnerParameters;
}

public String getStatus() {
return status;
}
Expand Down Expand Up @@ -303,7 +317,8 @@ static class RestInputTranslationRequestData {
private Set<String> domains;
private List<String> notes;
private String status;
// private Map<String, String> metadata;
private Map<String, String> metadata;
private Map<String, String> partnerParameters;

public RestInputTranslationRequestData(NewTranslationRequestData newTrData) {
this.targetLanguagesByBundle = newTrData.getTargetLanguagesByBundle();
Expand All @@ -313,6 +328,8 @@ public RestInputTranslationRequestData(NewTranslationRequestData newTrData) {
this.emails = newTrData.getEmails();
this.phones = newTrData.getPhones();
this.notes = newTrData.getNotes();
this.metadata = newTrData.getMetadata();
this.partnerParameters = newTrData.getPartnerParameters();

this.status = newTrData.isSubmit() ? "SUBMITTED" : "DRAFT";

Expand All @@ -334,6 +351,8 @@ public RestInputTranslationRequestData(TranslationRequestDataChangeSet trChangeS
this.emails = trChangeSet.getEmails();
this.phones = trChangeSet.getPhones();
this.notes = trChangeSet.getNotes();
this.metadata = trChangeSet.getMetadata();
this.partnerParameters = trChangeSet.getPartnerParameters();

this.status = trChangeSet.isSubmit() ? "SUBMITTED" : "DRAFT";

Expand Down Expand Up @@ -379,13 +398,16 @@ public List<String> getNotes() {
return notes;
}

// public Map<String, String> getMetadata() {
// return metadata;
// }
public Map<String, String> getMetadata() {
return metadata;
}

public Map<String, String> getPartnerParameters() {
return partnerParameters;
}

public String getStatus() {
return status;
}
}

}
Loading

0 comments on commit c265dd1

Please sign in to comment.