/**
 * 
 */
package org.zanata.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.zanata.dao.ProjectDAO;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.service.ValidationService;
import org.zanata.util.ZanataMessages;
import org.zanata.webtrans.shared.model.ValidationInfo;
import org.zanata.webtrans.shared.model.ValidationObject;
import org.zanata.webtrans.shared.validation.ValidationFactory;

/**
 * 
 * @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
 * 
 */
@Name("validationServiceImpl")
@AutoCreate
@Scope(ScopeType.STATELESS)
public class ValidationServiceImpl implements ValidationService
{
   @In
   private ZanataMessages zanataMessages;
   
   @In
   private ProjectDAO projectDAO;

   @In
   private ProjectIterationDAO projectIterationDAO;

   private static final String DESC_KEY = ".desc";

   @Override
   public List<ValidationObject> getValidationObject(String projectSlug)
   {
      List<ValidationObject> validationList = ValidationFactory.getAllValidationObject();
      Set<String> enabledValidations = new HashSet<String>();

      if (!StringUtils.isEmpty(projectSlug))
      {
         HProject project = projectDAO.getBySlug(projectSlug);
         enabledValidations = project.getCustomizedValidations();
      }

      for (ValidationObject  valObj: validationList)
      {
         ValidationInfo actionInfo = valObj.getValidationInfo();

         actionInfo.setDescription(zanataMessages.getMessage(actionInfo.getId().getMessagePrefix() + DESC_KEY));
         if (enabledValidations.contains(actionInfo.getId().name()))
         {
            actionInfo.setEnabled(true);
         }
      }
     
      return validationList;
   }
   
   @Override
   public List<ValidationObject> getValidationObject(String projectSlug, String versionSlug)
   {
      List<ValidationObject> validationList = ValidationFactory.getAllValidationObject();
      Set<String> enabledValidations = new HashSet<String>();

      if (!StringUtils.isEmpty(projectSlug) && !StringUtils.isEmpty(versionSlug))
      {
         HProjectIteration version = projectIterationDAO.getBySlug(projectSlug, versionSlug);
         enabledValidations = version.getCustomizedValidations();

         // Inherits validations from project if version has no defined
         // validations
         if (enabledValidations.isEmpty())
         {
            enabledValidations = version.getProject().getCustomizedValidations();
         }
      }

      for (ValidationObject valObj : validationList)
      {
         ValidationInfo actionInfo = valObj.getValidationInfo();

         actionInfo.setDescription(zanataMessages.getMessage(actionInfo.getId().getMessagePrefix() + DESC_KEY));
         if (enabledValidations.contains(actionInfo.getId().name()))
         {
            actionInfo.setEnabled(true);
            actionInfo.setLocked(true);
         }
      }
      return validationList;
   }
}

@@ -39,16 +39,71 @@
 .impl;%0A%0A
+import java.util.Collection;%0Aimport java.util.HashMap;%0A
 import j
@@ -143,16 +143,38 @@
 l.List;%0A
+import java.util.Map;%0A
 import j
@@ -503,24 +503,59 @@
 erationDAO;%0A
+import org.zanata.model.HDocument;%0A
 import org.z
@@ -615,24 +615,100 @@
 tIteration;%0A
+import org.zanata.model.HTextFlow;%0Aimport org.zanata.model.HTextFlowTarget;%0A
 import org.z
@@ -3470,11 +3470,981 @@
 st;%0A   %7D
+%0A%0A   /**%0A    * Run validation check on HTextFlow and HTextFlowTarget with specific locale%0A    * from list of HDocuments against validations rules%0A    * %0A    * @param hDocs%0A    * @param validations%0A    * @param localeId%0A    */%0A   public void runValidations(Collection%3CHDocument%3E hDocs, List%3CValidationObject%3E validations, Long localeId)%0A   %7B%0A      Map%3CString, Boolean%3E docValidationResult = new HashMap%3CString, Boolean%3E();%0A%0A      for (HDocument hDoc : hDocs)%0A      %7B%0A         for(HTextFlow textFlow: hDoc.getTextFlows())%0A         %7B%0A            HTextFlowTarget target = textFlow.getTargets().get(localeId);%0A            if (target != null)%0A            %7B%0A               for (ValidationObject validation : validations)%0A               %7B%0A                  validation.validate(textFlow.getContents().get(0), target.getContents().get(0));%0A                  if (validation.hasError())%0A                  %7B%0A%0A                  %7D%0A               %7D%0A            %7D%0A         %7D%0A      %7D%0A   %7D
 %0A%7D%0A

Work in progress validation on server side: https://bugzilla.redhat.com/show_bug.cgi?id=846314