/*
 * Copyright 2012, Red Hat, Inc. and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.zanata.webtrans.server.rpc;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowTarget;

import com.google.common.collect.Lists;

/**
 * @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
 *
 */
class GwtRpcUtil
{
   private static final Logger log = LoggerFactory.getLogger(GwtRpcUtil.class);

   static ArrayList<String> getSourceContents(HTextFlow textFlow)
   {
      ArrayList<String> sourceContents = Lists.newArrayList(textFlow.getContents());
      assert textFlow.isPlural() || sourceContents.size() < 2 : "non-plural textflow with multiple strings: " + textFlow.getResId();
      return sourceContents;
   }

   /**
    * Gets the string contents of target (unless null), padding the ArrayList
    * to nPlurals (if the textFlow allows plurals), or to 1 otherwise.
    * @param textFlow
    * @param target
    * @param nPlurals
    * @return
    */
   static ArrayList<String> getTargetContentsWithPadding(HTextFlow textFlow, HTextFlowTarget target, int nPlurals)
   {
      int resultSize = textFlow.isPlural() ? nPlurals : 1;
      ArrayList<String> result = new ArrayList<String>(resultSize);
      if (target != null)
      {
         List<String> targetContents = target.getContents();
         if (targetContents.size() > resultSize)
         {
            log.warn("TextFlowTarget {} has {} strings: trimming excess plurals", target.getId(), targetContents.size());
         }
         result.addAll(targetContents.subList(0, resultSize));
      }
      while (result.size() < resultSize)
      {
         result.add("");
      }
      return result;
   }
}

@@ -2132,17 +2132,25 @@
     int 
-r
+expectedR
 esultSiz
@@ -2244,17 +2244,25 @@
 String%3E(
-r
+expectedR
 esultSiz
@@ -2397,17 +2397,25 @@
 ize() %3E 
-r
+expectedR
 esultSiz
@@ -2553,24 +2553,16 @@
 ());%0A   
-      %7D%0A
         
@@ -2602,17 +2602,25 @@
 List(0, 
-r
+expectedR
 esultSiz
@@ -2620,24 +2620,114 @@
 sultSize));%0A
+         %7D%0A         else%0A         %7B%0A            result.addAll(targetContents);%0A         %7D%0A
       %7D%0A    
@@ -2751,17 +2751,25 @@
 ize() %3C 
-r
+expectedR
 esultSiz

fix up gwt rpc sublist bug