Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Add revision comment from TM Merge action
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Jun 25, 2015
1 parent 92b62a6 commit ae9cd70
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 28 deletions.
Expand Up @@ -416,6 +416,7 @@ public void clear() {
setLastModifiedBy(null);
setTranslator(null);
setReviewer(null);
setRevisionComment(null);
}

protected boolean logPersistence() {
Expand Down
Expand Up @@ -42,6 +42,7 @@
import org.zanata.service.TranslationMemoryService;
import org.zanata.service.TranslationService;
import org.zanata.util.Event;
import org.zanata.util.MessageGenerator;
import org.zanata.webtrans.server.rpc.TransMemoryMergeStatusResolver;
import org.zanata.webtrans.shared.model.TransMemoryDetails;
import org.zanata.webtrans.shared.model.TransMemoryResultItem;
Expand Down Expand Up @@ -182,13 +183,14 @@ private TransUnitUpdateRequest createRequest(TransMemoryMerge action,

Long tmSourceId = tmResult.getSourceIdList().get(0);
ContentState statusToSet;
String comment;
String comment, revisionComment;
if (tmResult.getMatchType() == TransMemoryResultItem.MatchType.Imported) {
TransMemoryUnit tu = transMemoryUnitDAO.findById(tmSourceId);
statusToSet =
TransMemoryMergeStatusResolver.newInstance().decideStatus(
action, tmResult, oldTarget);
comment = buildTargetComment(tu);
revisionComment = MessageGenerator.getTMMergeMessage(tu);
} else {
HTextFlow tmSource = textFlowDAO.findById(tmSourceId, false);
TransMemoryDetails tmDetail =
Expand All @@ -199,16 +201,19 @@ private TransUnitUpdateRequest createRequest(TransMemoryMerge action,
action, hTextFlowToBeFilled, tmDetail, tmResult,
oldTarget);
comment = buildTargetComment(tmDetail);
revisionComment = MessageGenerator.getTMMergeMessage(tmDetail);
}

if (statusToSet != null) {
TransUnitUpdateRequest unfilledRequest =
requestMap.get(hTextFlowToBeFilled.getId());

TransUnitUpdateRequest request =
new TransUnitUpdateRequest(
unfilledRequest.getTransUnitId(),
tmResult.getTargetContents(), statusToSet,
unfilledRequest.getBaseTranslationVersion());
unfilledRequest.getBaseTranslationVersion(),
revisionComment);
request.addTargetComment(comment);
log.debug("auto translate from translation memory {}", request);
return request;
Expand Down
Expand Up @@ -233,10 +233,16 @@ private List<TranslationResult> translate(LocaleId localeId,
.getVersionNum()) {
try {
int nPlurals = getNumPlurals(hLocale, hTextFlow);
Optional<String> revisionComment =
StringUtils.isEmpty(request.getRevisionComment()) ? Optional
.<String> absent() : Optional.of(request
.getRevisionComment());
result.targetChanged =
translate(hTextFlowTarget,
request.getNewContents(),
request.getNewContentState(), nPlurals);
request.getNewContentState(),
nPlurals,
revisionComment);
result.isSuccess = true;
} catch (HibernateException e) {
result.isSuccess = false;
Expand Down Expand Up @@ -330,7 +336,7 @@ private void signalPostTranslateEvent(Long actorId,

private boolean translate(@Nonnull HTextFlowTarget hTextFlowTarget,
@Nonnull List<String> contentsToSave, ContentState requestedState,
int nPlurals) {
int nPlurals, Optional<String> revisionComment) {
boolean targetChanged = false;
ContentState currentState = hTextFlowTarget.getState();
targetChanged |= setContentIfChanged(hTextFlowTarget, contentsToSave);
Expand All @@ -343,6 +349,9 @@ private boolean translate(@Nonnull HTextFlowTarget hTextFlowTarget,
hTextFlowTarget.setVersionNum(hTextFlowTarget.getVersionNum() + 1);
hTextFlowTarget.setTextFlowRevision(textFlow.getRevision());
hTextFlowTarget.setLastModifiedBy(authenticatedAccount.getPerson());
if(revisionComment.isPresent()) {
hTextFlowTarget.setRevisionComment(revisionComment.get());
}
log.debug("last modified by :{}", authenticatedAccount.getPerson()
.getName());
}
Expand Down
82 changes: 58 additions & 24 deletions zanata-war/src/main/java/org/zanata/util/MessageGenerator.java
Expand Up @@ -23,7 +23,10 @@

import org.apache.commons.lang.StringUtils;
import org.zanata.model.HDocument;
import org.zanata.model.HPerson;
import org.zanata.model.HTextFlowTarget;
import org.zanata.model.tm.TransMemoryUnit;
import org.zanata.webtrans.shared.model.TransMemoryDetails;

/**
* Generate messages/comments of Zanata business actions
Expand All @@ -38,6 +41,7 @@ public class MessageGenerator {
public static final String PREFIX_MERGE_TRANS = "Merge translations";
public static final String PREFIX_COPY_TRANS = "Copy translation";
public static final String PREFIX_COPY_VERSION = "Copy version";
public static final String PREFIX_TM_MERGE = "TM Merge";

/**
* Create revision comment for translation that is copied by merge
Expand All @@ -50,15 +54,11 @@ public class MessageGenerator {
public static final String getMergeTranslationMessage(
HTextFlowTarget tft) {
HDocument document = tft.getTextFlow().getDocument();
String author = "";
if (tft.getLastModifiedBy() != null) {
author = tft.getLastModifiedBy().getName();
}

return generateAutoCopiedMessage(PREFIX_MERGE_TRANS,
document.getProjectIteration().getProject().getName(),
document.getProjectIteration()
.getSlug(), document.getDocId(), author);
.getSlug(), document.getDocId(),
getAuthor(tft.getLastModifiedBy()));
}

/**
Expand All @@ -69,14 +69,11 @@ public static final String getMergeTranslationMessage(
*/
public static final String getCopyTransMessage(HTextFlowTarget tft) {
HDocument document = tft.getTextFlow().getDocument();
String author = "";
if (tft.getLastModifiedBy() != null) {
author = tft.getLastModifiedBy().getName();
}
return generateAutoCopiedMessage(PREFIX_COPY_TRANS,
document.getProjectIteration()
.getProject().getName(), document.getProjectIteration()
.getSlug(), document.getDocId(), author);
.getSlug(), document.getDocId(),
getAuthor(tft.getLastModifiedBy()));
}

/**
Expand All @@ -87,30 +84,67 @@ public static final String getCopyTransMessage(HTextFlowTarget tft) {
*/
public static final String getCopyVersionMessage(HTextFlowTarget tft) {
HDocument document = tft.getTextFlow().getDocument();
String author = "";
if (tft.getLastModifiedBy() != null) {
author = tft.getLastModifiedBy().getName();
}
return generateAutoCopiedMessage(PREFIX_COPY_VERSION,
document.getProjectIteration().getProject().getName(),
document.getProjectIteration()
.getSlug(), document.getDocId(), author);
.getSlug(), document.getDocId(),
getAuthor(tft.getLastModifiedBy()));
}

/**
* Create revision comment for translation that is copied by TM Merge
* @see org.zanata.service.TransMemoryMergeService
*
* @param tu
*/
public static final String getTMMergeMessage(TransMemoryUnit tu) {
StringBuilder comment = new StringBuilder();

comment.append(PREFIX_TM_MERGE)
.append(": translation auto-copied from Translation memory '")
.append(tu.getTranslationMemory().getSlug())
.append("', description '")
.append(tu.getTranslationMemory().getDescription())
.append("'");

return comment.toString();
}

/**
* Create revision comment for translation that is copied by TM Merge
* @see org.zanata.service.TransMemoryMergeService
*
* @param tu
*/
public static final String getTMMergeMessage(TransMemoryDetails tmDetails) {
return generateAutoCopiedMessage(PREFIX_TM_MERGE,
tmDetails.getProjectName(),
tmDetails.getIterationName(), tmDetails.getDocId(),
tmDetails.getLastModifiedBy());
}

private static String getAuthor(HPerson person) {
if (person != null) {
return person.getName();
}
return null;
}

private static final String generateAutoCopiedMessage(String prefix,
String projectName, String versionSlug, String docId, String author) {
StringBuilder comment = new StringBuilder();

comment.append(prefix + ": translation auto-copied from project ")
.append(projectName)
.append(", version ")
.append(versionSlug)
.append(", document ")
.append(docId);
comment.append(prefix)
.append(": translation auto-copied from project '")
.append(projectName)
.append("', version '")
.append(versionSlug)
.append("', document '")
.append(docId)
.append("'");

if (!StringUtils.isEmpty(author)) {
comment.append(", author ")
.append(author);
comment.append(", author '").append(author).append("'");
}
return comment.toString();
}
Expand Down
Expand Up @@ -42,6 +42,7 @@ public class TransUnitUpdateRequest implements IsSerializable {
private ContentState newContentState;
private int baseTranslationVersion;
private String targetComment;
private String revisionComment;

// required for GWT rpc serialization
@SuppressWarnings("unused")
Expand All @@ -57,6 +58,13 @@ public TransUnitUpdateRequest(TransUnitId transUnitId,
this.baseTranslationVersion = baseTranslationVersion;
}

public TransUnitUpdateRequest(TransUnitId transUnitId,
List<String> newContents, ContentState newContentState,
int baseTranslationVersion, String revisionComment) {
this(transUnitId, newContents, newContentState, baseTranslationVersion);
this.revisionComment = revisionComment;
}

public TransUnitUpdateRequest addTargetComment(String comment) {
targetComment = comment;
return this;
Expand All @@ -78,6 +86,10 @@ public int getBaseTranslationVersion() {
return baseTranslationVersion;
}

public String getRevisionComment() {
return revisionComment;
}

public boolean hasTargetComment() {
return !Strings.isNullOrEmpty(targetComment);
}
Expand All @@ -95,6 +107,7 @@ public String toString() {
add("newContentState", newContentState).
add("baseTranslationVersion", baseTranslationVersion).
add("targetComment", targetComment).
add("revisionComment", revisionComment).
toString();
// @formatter:on
}
Expand Down

0 comments on commit ae9cd70

Please sign in to comment.