Skip to content

Commit

Permalink
feat(ZNTA-975): add filter fields to status list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed Aug 8, 2017
1 parent 328b418 commit a2578a9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
Expand Up @@ -239,7 +239,7 @@ public Response getDocuments(@PathParam("projectSlug") String projectSlug,
@PathParam("versionSlug") String versionSlug);

/**
* Retrieves a list translation unit with status in a document.
* Retrieves a list of translation unit id with status in a document.
*
* @param projectSlug
* Project identifier
Expand Down Expand Up @@ -268,6 +268,13 @@ public Response getTransUnitStatus(
@PathParam("projectSlug") String projectSlug,
@PathParam("versionSlug") String versionSlug,
@PathParam("docId") String docId,
@DefaultValue("en-US") @PathParam("localeId") String localeId);

@DefaultValue("en-US") @PathParam("localeId") String localeId,
@QueryParam("searchString") String searchString,
@QueryParam("resId") String resId,
@QueryParam("changedBefore") String changedBefore,
@QueryParam("changedAfter") String changedAfter,
@QueryParam("lastModifiedByUser") String lastModifiedByUser,
@QueryParam("sourceComment") String sourceComment,
@QueryParam("transComment") String transComment,
@QueryParam("msgContext") String msgContext);
}
Expand Up @@ -3,6 +3,7 @@
import static org.zanata.common.EntityStatus.ACTIVE;
import static org.zanata.common.EntityStatus.OBSOLETE;
import static org.zanata.common.EntityStatus.READONLY;
import static org.zanata.util.DateUtil.parseQueryDate;
import static org.zanata.webtrans.server.rpc.GetTransUnitsNavigationService.TextFlowResultTransformer;

import java.util.ArrayList;
Expand All @@ -17,6 +18,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.GenericEntity;
Expand Down Expand Up @@ -56,6 +58,8 @@
import org.zanata.rest.dto.resource.ResourceMeta;
import org.zanata.rest.editor.service.TransMemoryMergeManager;
import org.zanata.rest.editor.service.UserService;
import org.zanata.util.StringUtil;
import org.zanata.webtrans.shared.search.FilterConstraints;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.ConfigurationService;
import org.zanata.service.LocaleService;
Expand Down Expand Up @@ -305,7 +309,15 @@ public Response getTransUnitStatus(
@PathParam("projectSlug") String projectSlug,
@PathParam("versionSlug") String versionSlug,
@PathParam("docId") String noSlashDocId,
@DefaultValue("en-US") @PathParam("localeId") String localeId) {
@DefaultValue("en-US") @PathParam("localeId") String localeId,
@QueryParam("searchString") String searchString,
@QueryParam("resId") String resId,
@QueryParam("changedBefore") String changedBefore,
@QueryParam("changedAfter") String changedAfter,
@QueryParam("lastModifiedByUser") String lastModifiedByUser,
@QueryParam("sourceComment") String sourceComment,
@QueryParam("transComment") String transComment,
@QueryParam("msgContext") String msgContext) {
if (StringUtils.isEmpty(noSlashDocId)) {
return Response.status(Response.Status.NOT_FOUND).build();
}
Expand All @@ -321,8 +333,18 @@ public Response getTransUnitStatus(
}
TextFlowResultTransformer resultTransformer =
new TextFlowResultTransformer(hLocale);
FilterConstraints filterConstraints =
FilterConstraints.builder().build();

FilterConstraints filterConstraints = FilterConstraints.builder()
.filterBy(searchString)
.resourceIdIs(resId)
.targetChangedBefore(parseQueryDate(changedBefore))
.targetChangedAfter(parseQueryDate(changedAfter))
.lastModifiedBy(lastModifiedByUser)
.sourceCommentContains(sourceComment)
.targetCommentContains(transComment)
.msgContext(msgContext)
.build();

List<HTextFlow> textFlows = textFlowDAO.getNavigationByDocumentId(
new DocumentId(document.getId(), document.getDocId()), hLocale,
resultTransformer, filterConstraints);
Expand All @@ -331,8 +353,7 @@ public Response getTransUnitStatus(
for (HTextFlow textFlow : textFlows) {
ContentState state =
textFlow.getTargets().get(hLocale.getId()).getState();
statusList.add(new TransUnitStatus(textFlow.getId(),
textFlow.getResId(), state));
statusList.add(new TransUnitStatus(textFlow.getId(), textFlow.getResId(), state));
}
Object entity = new GenericEntity<List<TransUnitStatus>>(statusList){};
return Response.ok(entity).build();
Expand Down
18 changes: 18 additions & 0 deletions server/services/src/main/java/org/zanata/util/DateUtil.java
Expand Up @@ -6,6 +6,8 @@
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import com.google.common.base.Strings;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Period;
Expand All @@ -22,6 +24,8 @@ public class DateUtil {

private static final String DATE_TIME_SHORT_PATTERN = "dd/MM/yy HH:mm";
private static final String TIME_SHORT_PATTERN = "hh:mm:ss";
// Used for advanced editor search queries
private static final String DATE_SHORT_QUERY_PATTERN = "yyyy-MM-dd";
// Period Formatters are thread safe and immutable according to joda time
// docs
private static final PeriodFormatter TIME_REMAINING_FORMATTER =
Expand Down Expand Up @@ -232,4 +236,18 @@ public static boolean isDatesInRange(Date from, Date to, int days) {
Days d = Days.daysBetween(fromDate, toDate);
return d.getDays() <= days;
}

/**
* Parse a yyyy-mm-dd string to a date.
*
* @param dateString in form "yyyy-mm-dd" or empty string or null
* @return the parsed date or null.
*/
public static DateTime parseQueryDate(String dateString) {
if (Strings.isNullOrEmpty(dateString)) {
return null;
}
return DateTimeFormat.forPattern(DATE_SHORT_QUERY_PATTERN)
.parseDateTime(dateString);
}
}
Expand Up @@ -26,9 +26,6 @@
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.zanata.dao.TextFlowDAO;
import org.zanata.exception.ZanataServiceException;
import org.zanata.model.HLocale;
Expand All @@ -37,6 +34,7 @@
import org.zanata.security.ZanataIdentity;
import org.zanata.service.LocaleService;
import org.zanata.service.ValidationService;
import static org.zanata.util.DateUtil.parseQueryDate;
import org.zanata.webtrans.server.ActionHandlerFor;
import org.zanata.webtrans.shared.model.TransUnit;
import org.zanata.webtrans.shared.rpc.EditorFilter;
Expand All @@ -46,7 +44,6 @@
import org.zanata.webtrans.shared.rpc.GetTransUnitsNavigationResult;
import org.zanata.webtrans.shared.util.FindByTransUnitIdPredicate;
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

Expand All @@ -70,8 +67,6 @@ public class GetTransUnitListHandler extends
private ValidationService validationServiceImpl;
@Inject
private GetTransUnitsNavigationService getTransUnitsNavigationService;
private DateTimeFormatter dateFormatter =
DateTimeFormat.forPattern("yyyy-MM-dd");

@Override
public GetTransUnitListResult execute(GetTransUnitList action,
Expand All @@ -86,10 +81,10 @@ public GetTransUnitListResult execute(GetTransUnitList action,
FilterConstraints constraints = FilterConstraints.builder()
.filterBy(editorFilter.getTextInContent())
.lastModifiedBy(editorFilter.getLastModifiedByUser())
.targetChangedBefore(parseDateIfPresent(
.targetChangedBefore(parseQueryDate(
editorFilter.getLastModifiedBefore()))
.targetChangedAfter(
parseDateIfPresent(editorFilter.getLastModifiedAfter()))
parseQueryDate(editorFilter.getLastModifiedAfter()))
.resourceIdIs(editorFilter.getResId())
.msgContext(editorFilter.getMsgContext())
.sourceCommentContains(editorFilter.getSourceComment())
Expand Down Expand Up @@ -127,11 +122,6 @@ public GetTransUnitListResult execute(GetTransUnitList action,
return result;
}

private DateTime parseDateIfPresent(String dateInString) {
return Strings.isNullOrEmpty(dateInString) ? null
: dateFormatter.parseDateTime(dateInString);
}

private int getTotalPageIndex(int indexListSize, int countPerPage) {
int totalPageNumber =
(int) Math.ceil((float) indexListSize / countPerPage);
Expand Down
Expand Up @@ -238,7 +238,9 @@ public void getTransUnitStatusWillReturnNotFoundIfDocumentNotFound() {
.thenReturn(null);

Response response =
service.getTransUnitStatus("a", "1", "authors", "de");
service.getTransUnitStatus("a", "1", "authors", "de",
"", "", "", "", "", "",
"", "");
assertThat(response.getStatus()).isEqualTo(404);
}

Expand All @@ -249,7 +251,9 @@ public void getTransUnitStatusWillReturnNotFoundIfLocaletNotFound() {
when(localeService.getByLocaleId("de")).thenReturn(null);

Response response =
service.getTransUnitStatus("a", "1", "authors", "de");
service.getTransUnitStatus("a", "1", "authors", "de",
"", "", "", "", "", "",
"", "");
assertThat(response.getStatus()).isEqualTo(404);
}

Expand All @@ -261,7 +265,9 @@ public void getTransUnitStatusWillGetResults() {
new HLocale(LocaleId.DE));

Response response =
service.getTransUnitStatus("a", "1", "authors", "de");
service.getTransUnitStatus("a", "1", "authors", "de",
"", "", "", "", "", "",
"", "");
assertThat(response.getStatus()).isEqualTo(200);
verify(textFlowDAO).getNavigationByDocumentId(isA(DocumentId.class), isA(HLocale.class), isA(
GetTransUnitsNavigationService.TextFlowResultTransformer.class), isA(FilterConstraints.class));
Expand Down

0 comments on commit a2578a9

Please sign in to comment.