This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

fix(po file): Fix po-revision-date header (#1205)

  • Loading branch information...
aeng authored and djansen-redhat committed Jun 15, 2016
1 parent 4fc8fb5 commit 25c8fce1c281035f9e280df5c23ecf63a68a2d48
@@ -124,6 +124,10 @@
private static Properties pluralForms;
public static final String COPIED_BY_ZANATA_NAME = "Copied by Zanata";
public static final String COPIED_BY_ZANATA_NAME_EMAIL =
"copied-by-zanata@zanata.org";
@Inject
private EntityManager entityManager;
@@ -704,8 +708,7 @@ private void populateHeaderEntries(final List<HeaderEntry> headerEntries,
final List<HTextFlowTarget> hTargets, final HLocale locale) {
final Map<String, HeaderEntry> containedHeaders =
new LinkedHashMap<String, HeaderEntry>(headerEntries.size());
HTextFlowTarget lastTranslatedTarget =
this.getLastTranslatedTarget(hTargets);
HTextFlowTarget lastChangedTarget = getLastChangedTarget(hTargets);
// Collect the existing header entries
for (HeaderEntry entry : headerEntries) {
@@ -714,7 +717,7 @@ private void populateHeaderEntries(final List<HeaderEntry> headerEntries,
// Add / Replace headers
Date revisionDate =
this.getRevisionDate(headerEntries, lastTranslatedTarget);
this.getRevisionDate(headerEntries, lastChangedTarget);
HeaderEntry headerEntry = containedHeaders.get(PO_REVISION_DATE_HDR);
if (headerEntry == null) {
headerEntry =
@@ -729,11 +732,11 @@ private void populateHeaderEntries(final List<HeaderEntry> headerEntries,
if (headerEntry == null) {
headerEntry =
new HeaderEntry(LAST_TRANSLATOR_HDR,
this.getLastTranslator(lastTranslatedTarget,
this.getLastTranslator(lastChangedTarget,
headerEntries));
headerEntries.add(headerEntry);
} else {
headerEntry.setValue(this.getLastTranslator(lastTranslatedTarget,
headerEntry.setValue(this.getLastTranslator(lastChangedTarget,
headerEntries));
}
@@ -851,25 +854,21 @@ private Date getRevisionDate(final List<HeaderEntry> headerEntries,
}
/**
* @param translations
* A list of Translations for a document.
* @return The most recently translated target. If there are more than one,
* this method will return one of those, no assurances o
* @param translations - List of HTextFlowTarget
* @return last changed/updated HTextFlowTarget from the list
*/
private HTextFlowTarget getLastTranslatedTarget(
final List<HTextFlowTarget> translations) {
private HTextFlowTarget getLastChangedTarget(
final List<HTextFlowTarget> translations) {
Date lastUpdate = new Date(Long.MIN_VALUE);
HTextFlowTarget lastTranslated = null;
HTextFlowTarget lastChanged = null;
for (HTextFlowTarget trans : translations) {
if (trans.getTranslator() != null
&& trans.getLastChanged().after(lastUpdate)) {
lastTranslated = trans;
lastUpdate = trans.getLastChanged();
for (HTextFlowTarget tft : translations) {
if (tft.getLastChanged().after(lastUpdate)) {
lastChanged = tft;
lastUpdate = tft.getLastChanged();
}
}
return lastTranslated;
return lastChanged;
}
/**
@@ -891,24 +890,40 @@ private String getLastTranslator(final HTextFlowTarget lastTranslated,
HPerson lastTranslatedBy = lastTranslated.getTranslator();
Date lastModifiedDate = lastTranslated.getLastChanged();
// Last translated target is more recent than the Revision Date on
// the
// Header
if (lastTranslatedBy != null && lastModifiedDate != null
&& lastModifiedDate.after(headerRevisionDate)) {
lastTranslator =
lastTranslatedBy.getName() + " <"
+ lastTranslatedBy.getEmail() + ">";
} else if (lastTranslatedBy != null && lastModifiedDate == null) {
lastTranslator =
lastTranslatedBy.getName() + " <"
+ lastTranslatedBy.getEmail() + ">";
if (lastTranslatedBy != null) {
if (lastModifiedDate == null
|| lastModifiedDate.after(headerRevisionDate)) {
/**
* Use translator details from last translated target if
* the lastModifiedDate is null or if lastModifiedDate is
* after date in header entries
*/
lastTranslator =
generateLastTranslator(lastTranslatedBy.getName(),
lastTranslatedBy.getEmail());
}
} else {
/**
* When last translated target is being created in Zanata
* without user (e.g upload, copyTrans), set translator to be
* Zanata
*/
lastTranslator = generateLastTranslator(COPIED_BY_ZANATA_NAME,
COPIED_BY_ZANATA_NAME_EMAIL);
}
}
return lastTranslator;
}
/**
* @param name - name of person
* @param email - email of person
* @return {name} <{email}>
*/
private String generateLastTranslator(String name, String email) {
return name + " <" + email + ">";
}
/**
* Returns a string representation of a Date for use in a PO file header.
*
@@ -42,6 +42,7 @@
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;
import org.zanata.rest.service.ResourceTestUtil;
import org.zanata.rest.service.ResourceUtils;
import com.google.common.collect.Lists;
@@ -653,13 +654,24 @@ public void generatedPoHeaders() throws Exception {
headerFound, is(true));
}
// Since it is a first push with no headers, the Last Translator and
// Last Revision Date header should be empty
/**
* Since it is a first push with no headers,
* the Last Translator should contain ResourceUtils.COPIED_BY_ZANATA_NAME and ResourceUtils.COPIED_BY_ZANATA_NAME_Email
* Last Revision Date header should not be empty
*/
for (HeaderEntry entry : translations.getExtensions()
.findByType(PoTargetHeader.class).getEntries()) {
if (entry.getKey().equals(HeaderFields.KEY_LastTranslator)
|| entry.getKey().equals(HeaderFields.KEY_PoRevisionDate)) {
assertThat(entry.getValue().trim(), is(""));
String value = entry.getValue().trim();
if (entry.getKey().equals(HeaderFields.KEY_LastTranslator)) {
assertThat(value,
containsString(ResourceUtils.COPIED_BY_ZANATA_NAME));
assertThat(value, containsString(
ResourceUtils.COPIED_BY_ZANATA_NAME_EMAIL));
}
if (entry.getKey().equals(HeaderFields.KEY_PoRevisionDate)) {
assertThat(value, not(isEmptyOrNullString()));
}
}
}
@@ -757,12 +769,14 @@ public void headersFromOriginalPush() throws Exception {
// Make sure the header values are the same as the ones pushed with the
// document
for (HeaderEntry entry : header.getEntries()) {
String value = entry.getValue().trim();
if (entry.getKey().equals(HeaderFields.KEY_LastTranslator)) {
assertThat(entry.getValue().trim(),
is("Test User <test@zanata.org>"));
assertThat(value,
containsString(ResourceUtils.COPIED_BY_ZANATA_NAME));
assertThat(value, containsString(
ResourceUtils.COPIED_BY_ZANATA_NAME_EMAIL));
} else if (entry.getKey().equals(HeaderFields.KEY_PoRevisionDate)) {
assertThat(entry.getValue().trim(),
is(dateFormat.format(poRevDate.getTime())));
assertThat(value, is(dateFormat.format(poRevDate.getTime())));
}
}
}

0 comments on commit 25c8fce

Please sign in to comment.