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

Commit

Permalink
Check plural contents when verifying ContentState for TextFlowTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Apr 19, 2012
1 parent aa88e50 commit ff8baff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
Expand Up @@ -197,6 +197,17 @@ public void testContentStateNewPlural4()
assertThat(actual1, is(ContentState.New));
}

// FIXME test where plurals < nplurals
// public void testContentStateNewPluralTooFew()
// {
// // TODO set nplurals=2
// Message m = new Message();
// m.setMsgidPlural("plural");
// m.addMsgstrPlural("s0", 0);
// ContentState actual1 = PoReader2.getContentState(m);
// assertThat(actual1, is(ContentState.New));
// }

public void testContentStateNeedReviewSingle()
{
Message m = new Message();
Expand Down
Expand Up @@ -20,6 +20,9 @@
*/
package org.zanata.rest.service;

import static org.zanata.util.StringUtil.allEmpty;
import static org.zanata.util.StringUtil.allNonEmpty;

import java.lang.reflect.Type;
import java.net.URI;
import java.util.ArrayList;
Expand Down Expand Up @@ -893,15 +896,7 @@ public Response putTranslations(@PathParam("id") String idNoSlash, @PathParam("l
}
else
{
if (incomingTarget.getContent().isEmpty() && incomingTarget.getState() != ContentState.New)
{
return Response.status(Status.BAD_REQUEST).entity("empty TextFlowTarget " + incomingTarget.getResId() + " must have ContentState New").build();
}
if (incomingTarget.getState() == ContentState.New && !incomingTarget.getContent().isEmpty())
{
return Response.status(Status.BAD_REQUEST).entity("ContentState New is illegal for non-empty TextFlowTarget " + incomingTarget.getResId()).build();
}

checkTargetState(incomingTarget.getResId(), incomingTarget.getState(), incomingTarget.getContents());
HTextFlowTarget hTarget = textFlow.getTargets().get(hLocale);
boolean targetChanged = false;
if (hTarget == null)
Expand Down Expand Up @@ -1010,6 +1005,37 @@ else if (incomingTarget.getState() == ContentState.Approved)
return Response.ok("warning: unknown resIds: " + unknownResIds).tag(etag).build();
}

private void checkTargetState(String resId, ContentState state, List<String> contents)
{
switch (state)
{
case NeedReview:
if (allEmpty(contents))
{
String entity = "ContentState NeedsReview is illegal for TextFlowTarget " + resId + " with no contents";
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(entity).build());
}
break;
case New:
if (allNonEmpty(contents))
{
String entity = "ContentState New is illegal for non-empty TextFlowTarget " + resId;
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(entity).build());
}
break;
case Approved:
// FIXME what if plurals < nplurals ?
if (!allNonEmpty(contents))
{
String entity = "ContentState Approved is illegal for TextFlowTarget " + resId + " with one or more empty strings";
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(entity).build());
}
break;
default:
throw new RuntimeException("unknown ContentState " + state);
}
}

private HProjectIteration retrieveAndCheckIteration(boolean writeOperation)
{
HProjectIteration hProjectIteration = projectIterationDAO.getBySlug(projectSlug, iterationSlug);
Expand Down

0 comments on commit ff8baff

Please sign in to comment.