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

Commit

Permalink
rhbz953734 - refactor to use DocumentInfo in context so that we can g…
Browse files Browse the repository at this point in the history
…et documentId and docId easily
  • Loading branch information
Patrick Huang committed May 17, 2013
1 parent e1a8e2a commit bfd196c
Show file tree
Hide file tree
Showing 18 changed files with 114 additions and 73 deletions.
Expand Up @@ -3,6 +3,7 @@
import org.zanata.webtrans.client.service.GetTransUnitActionContext;
import org.zanata.webtrans.client.service.NavigationService;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.DocumentInfo;
import com.google.common.base.Preconditions;
import com.google.gwt.event.shared.GwtEvent;

Expand All @@ -13,7 +14,7 @@ public class DocumentSelectionEvent extends GwtEvent<DocumentSelectionHandler> i
* Handler type.
*/
private static Type<DocumentSelectionHandler> TYPE;
private final DocumentId document;
private final DocumentInfo document;

/**
* Gets the type associated with this event.
Expand All @@ -25,14 +26,14 @@ public static Type<DocumentSelectionHandler> getType()
return TYPE != null ? TYPE : (TYPE = new Type<DocumentSelectionHandler>());
}

public DocumentSelectionEvent(DocumentId documentId)
public DocumentSelectionEvent(DocumentInfo documentInfo)
{
this.document = documentId;
this.document = documentInfo;
}

public DocumentId getDocumentId()
{
return document;
return document.getId();
}

@Override
Expand Down
Expand Up @@ -79,6 +79,7 @@
import org.zanata.webtrans.shared.rpc.RunDocValidationResult;

import com.allen_sauer.gwt.log.client.Log;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
Expand Down Expand Up @@ -444,7 +445,8 @@ public void onDocumentSelected(DocumentSelectionEvent event)
{
// match bookmarked selection, but prevent selection feedback loop
// from history
if (event.getDocumentId() != (currentDocument == null ? null : currentDocument.getId()))
DocumentId current = currentDocument == null ? null : currentDocument.getId();
if (!Objects.equal(event.getDocumentId(), current))
{
setSelection(event.getDocumentId());
}
Expand Down
Expand Up @@ -325,7 +325,7 @@ public void refreshRow(TransUnit updatedTransUnit, EditorClientId editorClientId
{
GoToRowLink goToRowLink = goToRowLinkProvider.get();

// goToRowLink.prepare("Translation has changed", );
// goToRowLink.prepare("");
// eventBus.fireEvent(new NotificationEvent(Warning, "Translation has changed", ));
}
if (updateFromCurrentUsersEditorSave(editorClientId, updateType))
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.util.List;

import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.DocumentInfo;
import org.zanata.webtrans.shared.model.TransUnitId;
import org.zanata.webtrans.shared.model.ValidationId;

Expand All @@ -38,7 +39,7 @@
*/
public class GetTransUnitActionContext
{
private DocumentId documentId;
private DocumentInfo document;
private String findMessage;
private int offset = 0;
private int count = 5; //this should be set to UserConfigHolder.getPageSize()
Expand All @@ -49,14 +50,14 @@ public class GetTransUnitActionContext
private TransUnitId targetTransUnitId;
private List<ValidationId> validationIds;

public GetTransUnitActionContext(DocumentId documentId)
public GetTransUnitActionContext(DocumentInfo document)
{
this.documentId = documentId;
this.document = document;
}

private GetTransUnitActionContext(GetTransUnitActionContext other)
{
documentId = other.getDocumentId();
document = other.getDocument();
findMessage = other.getFindMessage();
offset = other.getOffset();
count = other.getCount();
Expand All @@ -68,15 +69,15 @@ private GetTransUnitActionContext(GetTransUnitActionContext other)
validationIds = other.getValidationIds();
}

public DocumentId getDocumentId()
public DocumentInfo getDocument()
{
return documentId;
return document;
}

public GetTransUnitActionContext changeDocument(DocumentId document)
public GetTransUnitActionContext changeDocument(DocumentInfo document)
{
GetTransUnitActionContext result = new GetTransUnitActionContext(this);
result.documentId = document;
result.document = document;
return result;
}

Expand Down Expand Up @@ -204,7 +205,7 @@ public String toString()
{
// @formatter:off
return Objects.toStringHelper(this).
add("documentId", documentId).
add("document", document).
add("findMessage", findMessage).
add("offset", offset).
add("count", count).
Expand Down Expand Up @@ -245,7 +246,7 @@ public boolean needReloadNavigationIndex(GetTransUnitActionContext newContext)
|| filterUntranslated != newContext.filterUntranslated
|| filterHasError != newContext.filterHasError
|| offset != newContext.offset
|| !documentId.equals(newContext.documentId)
|| !document.equals(newContext.document)
|| !Objects.equal(findMessage, newContext.findMessage);
// @formatter:on
}
Expand Down
Expand Up @@ -2,6 +2,7 @@

import org.zanata.webtrans.client.presenter.UserConfigHolder;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.DocumentInfo;
import org.zanata.webtrans.shared.model.TransUnitId;
import com.google.inject.Inject;
import com.google.inject.Singleton;
Expand All @@ -26,10 +27,10 @@ protected boolean isContextInitialized()
return context != null;
}

protected GetTransUnitActionContext initContext(DocumentId documentId, String findMessage, TransUnitId targetTransUnitId)
protected GetTransUnitActionContext initContext(DocumentInfo document, String findMessage, TransUnitId targetTransUnitId)
{
// @formatter:off
context = new GetTransUnitActionContext(documentId)
context = new GetTransUnitActionContext(document)
.changeCount(configHolder.getState().getEditorPageSize())
.changeFindMessage(findMessage)
.changeFilterNeedReview(configHolder.getState().isFilterByNeedReview())
Expand Down
Expand Up @@ -12,6 +12,7 @@
import org.zanata.webtrans.client.presenter.SearchResultsPresenter;
import org.zanata.webtrans.client.presenter.UserConfigHolder;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.DocumentInfo;
import org.zanata.webtrans.shared.model.TransUnitId;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
Expand Down Expand Up @@ -73,10 +74,11 @@ public void onValueChange(ValueChangeEvent<String> event)
DocumentId documentId = documentListPresenter.getDocumentId(newHistoryToken.getDocumentPath());
if (!getTransUnitActionContextHolder.isContextInitialized() && documentId != null)
{
DocumentInfo documentInfo = documentListPresenter.getDocumentInfo(documentId);
// if editor is not yet initialized, we want to load document with search and target trans unit all at once
Long textFlowId = newHistoryToken.getTextFlowId();
TransUnitId transUnitId = textFlowId == null ? null : new TransUnitId(textFlowId);
getTransUnitActionContextHolder.initContext(documentId, newHistoryToken.getSearchText(), transUnitId);
getTransUnitActionContextHolder.initContext(documentInfo, newHistoryToken.getSearchText(), transUnitId);
eventBus.fireEvent(new InitEditorEvent());
}

Expand Down Expand Up @@ -104,15 +106,15 @@ protected void processForDocumentListPresenter(HistoryToken token)

protected void processForAppPresenter(DocumentId docId)
{
if (docId != null && !equal(appPresenter.getSelectedDocIdOrNull(), docId))
if (docId != null && !equal(appPresenter.getSelectedDocIdOrNull(), docId.getId()))
{
appPresenter.selectDocument(docId);
}
Log.info("[gwt-history] document id: " + docId);

if (docId != null)
{
eventBus.fireEvent(new DocumentSelectionEvent(docId));
eventBus.fireEvent(new DocumentSelectionEvent(documentListPresenter.getDocumentInfo(docId)));
}
}

Expand Down
Expand Up @@ -249,7 +249,7 @@ private void loadPageAndGoToRow(int pageIndex, TransUnitId transUnitId)
@Override
public void onTransUnitUpdated(TransUnitUpdatedEvent event)
{
if (Objects.equal(event.getUpdateInfo().getDocumentId(), contextHolder.getContext().getDocumentId()))
if (Objects.equal(event.getUpdateInfo().getDocumentId(), contextHolder.getContext().getDocument().getId()))
{
TransUnit updatedTU = event.getUpdateInfo().getTransUnit();
boolean updated = updateDataModel(updatedTU);
Expand Down
Expand Up @@ -43,7 +43,7 @@ private GetTransUnitList(DocumentId id, int offset, int count, String phrase, bo

public static GetTransUnitList newAction(GetTransUnitActionContext context)
{
return new GetTransUnitList(context.getDocumentId(), context.getOffset(), context.getCount(), context.getFindMessage(), context.isFilterTranslated(), context.isFilterNeedReview(), context.isFilterUntranslated(), context.isFilterHasError(), context.getTargetTransUnitId(), context.getValidationIds());
return new GetTransUnitList(context.getDocument().getId(), context.getOffset(), context.getCount(), context.getFindMessage(), context.isFilterTranslated(), context.isFilterNeedReview(), context.isFilterUntranslated(), context.isFilterHasError(), context.getTargetTransUnitId(), context.getValidationIds());
}

public boolean isNeedReloadIndex()
Expand Down
Expand Up @@ -46,7 +46,7 @@ public GetTransUnitsNavigation(Long id, String phrase, boolean isNewState, boole

public static GetTransUnitsNavigation newAction(GetTransUnitActionContext context)
{
return new GetTransUnitsNavigation(context.getDocumentId().getId(), context.getFindMessage(), context.isFilterUntranslated(), context.isFilterNeedReview(), context.isFilterTranslated());
return new GetTransUnitsNavigation(context.getDocument().getId().getId(), context.getFindMessage(), context.isFilterUntranslated(), context.isFilterNeedReview(), context.isFilterTranslated());
}

public Long getId()
Expand Down
Expand Up @@ -9,7 +9,9 @@
<changeSet id="1" author="pahuang@redhat.com">
<comment>Add need review column to project iteration</comment>
<addColumn tableName="HProjectIteration">
<column name="requireTranslationReview" type="boolean" defaultValueBoolean="false"/>
<column name="requireTranslationReview" type="boolean" defaultValueBoolean="false">
<constraints nullable="true" />
</column>
</addColumn>
</changeSet>

Expand Down
24 changes: 24 additions & 0 deletions zanata-war/src/test/java/org/zanata/model/TestFixture.java
Expand Up @@ -22,12 +22,16 @@
package org.zanata.model;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.zanata.common.ContentState;
import org.zanata.common.ContentType;
import org.zanata.common.LocaleId;
import org.zanata.common.ProjectType;
import org.zanata.webtrans.shared.model.AuditInfo;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.DocumentInfo;
import org.zanata.webtrans.shared.model.Person;
import org.zanata.webtrans.shared.model.PersonId;
import org.zanata.webtrans.shared.model.ProjectIterationId;
Expand Down Expand Up @@ -181,4 +185,24 @@ public Long apply(TransUnitId input)
}
});
}

public static DocumentInfo documentInfo()
{
return documentInfo(0, "", "name0");
}

public static DocumentInfo documentInfo(long id, String docId)
{
return documentInfo(id, "", docId);
}

public static DocumentInfo documentInfo(long id, String path, String name)
{
return new DocumentInfo(new DocumentId(id, path + name), name, path, LocaleId.EN_US, null, new AuditInfo(new Date(), "Translator"), null, new AuditInfo(new Date(), "last translator"));
}

public static DocumentInfo documentInfo(long id)
{
return documentInfo(id, "");
}
}
@@ -1,11 +1,14 @@
package org.zanata.webtrans.client.service;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.zanata.model.TestFixture.*;

import org.hamcrest.Matchers;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.model.TestFixture;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.DocumentInfo;
import org.zanata.webtrans.shared.model.TransUnitId;

/**
Expand All @@ -21,17 +24,17 @@ public class GetTransUnitActionContextTest
@BeforeMethod
public void setUp() throws Exception
{
context = new GetTransUnitActionContext(new DocumentId(new Long(1), ""));
context = new GetTransUnitActionContext(documentInfo(1, ""));
}

@Test
public void testChangeDocument() throws Exception
{
DocumentId documentId = context.getDocumentId();
GetTransUnitActionContext newContext = context.changeDocument(new DocumentId(new Long(99), ""));
DocumentInfo document = context.getDocument();
GetTransUnitActionContext newContext = context.changeDocument(documentInfo(99, ""));

assertThat(context.getDocumentId(), Matchers.sameInstance(documentId));
assertThat(newContext.getDocumentId(), Matchers.equalTo(new DocumentId(new Long(99), "")));
assertThat(context.getDocument(), Matchers.sameInstance(document));
assertThat(newContext.getDocument(), Matchers.equalTo(documentInfo(99, "")));
}

@Test
Expand Down Expand Up @@ -77,7 +80,7 @@ public void testNeedReloadList() throws Exception
verifyNeedReloadTransUnits(context, context.changeFilterNeedReview(true), NEEDED);
verifyNeedReloadTransUnits(context, context.changeFilterUntranslated(true), NEEDED);
verifyNeedReloadTransUnits(context, context.changeFilterTranslated(true), NEEDED);
verifyNeedReloadTransUnits(context, context.changeDocument(new DocumentId(new Long(99), "")), NEEDED);
verifyNeedReloadTransUnits(context, context.changeDocument(documentInfo(99, "")), NEEDED);
verifyNeedReloadTransUnits(context, context.changeFindMessage("find message"), NEEDED);
verifyNeedReloadTransUnits(context, context.changeCount(2), NEEDED);
verifyNeedReloadTransUnits(context, context.changeOffset(2), NEEDED);
Expand All @@ -96,7 +99,7 @@ public void testNeedReloadNavigationIndex() throws Exception
verifyNeedReloadNavigationIndex(context, context.changeFilterNeedReview(true), NEEDED);
verifyNeedReloadNavigationIndex(context, context.changeFilterUntranslated(true), NEEDED);
verifyNeedReloadNavigationIndex(context, context.changeFilterTranslated(true), NEEDED);
verifyNeedReloadNavigationIndex(context, context.changeDocument(new DocumentId(new Long(99), "")), NEEDED);
verifyNeedReloadNavigationIndex(context, context.changeDocument(documentInfo(99, "")), NEEDED);
verifyNeedReloadNavigationIndex(context, context.changeFindMessage("find message"), NEEDED);

verifyNeedReloadNavigationIndex(context, context.changeCount(2), NO_NEED);
Expand Down
Expand Up @@ -2,6 +2,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.*;
import static org.zanata.model.TestFixture.*;
import net.customware.gwt.presenter.client.EventBus;

import org.hamcrest.Matchers;
Expand All @@ -26,6 +27,7 @@
import org.zanata.webtrans.client.presenter.SearchResultsPresenter;
import org.zanata.webtrans.client.presenter.UserConfigHolder;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.DocumentInfo;
import org.zanata.webtrans.shared.model.TransUnitId;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
Expand Down Expand Up @@ -100,15 +102,15 @@ public void onProcessForAppPresenter()
{
HistoryToken token = new HistoryToken();
token.setDocumentPath("doc/a.po");
DocumentId documentId = new DocumentId(new Long(1), "");
when(documentListPresenter.getDocumentId("doc/a.po")).thenReturn(documentId);
DocumentInfo document = TestFixture.documentInfo(99, "doc/a.po");
when(documentListPresenter.getDocumentId("doc/a.po")).thenReturn(document.getId());

service.processForAppPresenter(documentId);
service.processForAppPresenter(document.getId());

verify(appPresenter).selectDocument(documentId);
verify(appPresenter).selectDocument(document.getId());
verify(eventBus).fireEvent(eventCaptor.capture());
DocumentSelectionEvent documentSelectionEvent = TestFixture.extractFromEvents(eventCaptor.getAllValues(), DocumentSelectionEvent.class);
assertThat(documentSelectionEvent.getDocumentId(), Matchers.equalTo(documentId));
assertThat(documentSelectionEvent.getDocumentId(), Matchers.equalTo(document.getId()));

}

Expand Down Expand Up @@ -191,7 +193,7 @@ public void processHistoryTokenWithInitializedContext()
DocumentId documentId = new DocumentId(new Long(1), "");
when(documentListPresenter.getDocumentId("doc/path")).thenReturn(documentId);
when(appPresenter.getSelectedDocIdOrNull()).thenReturn(new DocumentId(new Long(99), ""));
contextHolder.updateContext(new GetTransUnitActionContext(documentId));
contextHolder.updateContext(new GetTransUnitActionContext(documentInfo(99, "")));

// When:
service.onValueChange(historyChangeEvent);
Expand Down Expand Up @@ -296,7 +298,7 @@ public void processBookmarkedTextFlowWithInvalidTextFlowId()
public void processBookmarkedTextFlow()
{
// Given: everything works
contextHolder.updateContext(new GetTransUnitActionContext(new DocumentId(new Long(9), "")));
contextHolder.updateContext(new GetTransUnitActionContext(TestFixture.documentInfo(99, "")));
HistoryToken token = new HistoryToken();
token.setTextFlowId("111");
when(stateHolder.getPageCount()).thenReturn(10);
Expand Down

0 comments on commit bfd196c

Please sign in to comment.