Coverage Report - org.kuali.student.core.statement.naturallanguage.translators.ReqComponentTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
ReqComponentTranslator
83%
35/42
80%
16/20
3.429
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.core.statement.naturallanguage.translators;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.List;
 20  
 import java.util.Locale;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 24  
 import org.kuali.student.common.exceptions.OperationFailedException;
 25  
 import org.kuali.student.core.statement.dto.ReqComponentInfo;
 26  
 import org.kuali.student.core.statement.entity.ReqComponent;
 27  
 import org.kuali.student.core.statement.entity.ReqComponentType;
 28  
 import org.kuali.student.core.statement.entity.ReqComponentTypeNLTemplate;
 29  
 import org.kuali.student.core.statement.naturallanguage.Context;
 30  
 import org.kuali.student.core.statement.naturallanguage.ContextRegistry;
 31  
 import org.kuali.student.core.statement.service.impl.StatementAssembler;
 32  
 import org.slf4j.Logger;
 33  
 import org.slf4j.LoggerFactory;
 34  
 
 35  
 /**
 36  
  * This class translates requirement components into a specific
 37  
  * natural language. This class is not thread safe.
 38  
  */
 39  
 public class ReqComponentTranslator {
 40  
     /** SLF4J logging framework */
 41  1
         private final static Logger logger = LoggerFactory.getLogger(ReqComponentTranslator.class);
 42  
 
 43  
     private String language;
 44  
     private ContextRegistry<Context<ReqComponentInfo>> contextRegistry;
 45  32
     private TemplateTranslator templateTranslator = new TemplateTranslator();
 46  
 
 47  
     /**
 48  
          * Constructs a new natural language translator in the
 49  
          * default language locale.
 50  
      */
 51  32
     public ReqComponentTranslator() {
 52  32
                 this.language = Locale.getDefault().getLanguage();
 53  32
     }
 54  
 
 55  
         /**
 56  
          * Sets requirement component translation language.
 57  
          *
 58  
          * @param language Language translation
 59  
          */
 60  
     public void setLanguage(final String language) {
 61  34
                 this.language = language;
 62  34
         }
 63  
 
 64  
     /**
 65  
      * Sets the template context registry.
 66  
      *
 67  
      * @param contextRegistry Template context registry
 68  
      */
 69  
     public void setContextRegistry(final ContextRegistry<Context<ReqComponentInfo>> contextRegistry) {
 70  32
             this.contextRegistry = contextRegistry;
 71  32
     }
 72  
 
 73  
     /**
 74  
      * Translates an <code>reqComponent</code> for a specific
 75  
      * <code>nlUsageTypeKey</code> into natural language.
 76  
      * This method is not thread safe.
 77  
      *
 78  
      * @param reqComponent
 79  
      *            Requirement component to translate
 80  
      * @param nlUsageTypeKey
 81  
      *            Natural language usuage type key (context)
 82  
      * @return Natural language translation
 83  
      * @throws DoesNotExistException
 84  
      *             Natural language usuage type key does not exist
 85  
      * @throws OperationFailedException
 86  
      *             Translation failed
 87  
      */
 88  
     public String translate(final ReqComponent reqComponent, final String nlUsageTypeKey) throws DoesNotExistException, OperationFailedException {
 89  20
             return translate(reqComponent, nlUsageTypeKey, this.language);
 90  
     }
 91  
 
 92  
     /**
 93  
      * Translates an <code>reqComponent</code> for a specific
 94  
      * <code>nlUsageTypeKey</code> into natural language.
 95  
      * This method is not thread safe.
 96  
      *
 97  
      * @param reqComponent
 98  
      *            Requirement component to translate
 99  
      * @param nlUsageTypeKey
 100  
      *            Natural language usuage type key (context)
 101  
      * @param language
 102  
      *            Translation language
 103  
      * @return
 104  
      * @throws DoesNotExistException
 105  
      *             Natural language usuage type key does not exist
 106  
      * @throws OperationFailedException
 107  
      *             Translation failed
 108  
      */
 109  
     public String translate(final ReqComponent reqComponent, final String nlUsageTypeKey, final String language) throws DoesNotExistException, OperationFailedException {
 110  67
             if(reqComponent == null) {
 111  2
                     throw new DoesNotExistException("ReqComponent cannot be null");
 112  
             }
 113  
 
 114  65
             ReqComponentType reqComponentType = reqComponent.getRequiredComponentType();
 115  65
             ReqComponentInfo reqComponentInfo = StatementAssembler.toReqComponentInfo(reqComponent);
 116  65
             Map<String, Object> contextMap = buildContextMap(reqComponentInfo);
 117  64
         ReqComponentTypeNLTemplate template = getTemplate(reqComponentType, nlUsageTypeKey, language);
 118  
 
 119  
         try {
 120  62
                         String nl = this.templateTranslator.translate(contextMap, template.getTemplate());
 121  
 
 122  62
                         if(logger.isInfoEnabled()) {
 123  0
                             logger.info("nl="+nl);
 124  
                     }
 125  
 
 126  62
                         return nl;
 127  0
                 } catch (OperationFailedException e) {
 128  0
                         String msg = "Generating template for requirement component failed: "+reqComponent;
 129  0
                         logger.error(msg, e);
 130  0
                         throw new OperationFailedException(msg);
 131  
                 }
 132  
     }
 133  
 
 134  
     /**
 135  
      * Builds a requirement component type context map.
 136  
      *
 137  
      * @param reqComponent Requirement component
 138  
      * @throws DoesNotExistException Requirement component context not found in registry
 139  
      * @throws OperationFailedException Creating context map failed
 140  
      */
 141  
     private Map<String, Object> buildContextMap(ReqComponentInfo reqComponent) throws DoesNotExistException, OperationFailedException {
 142  65
         List<Context<ReqComponentInfo>> contextList = this.contextRegistry.get(reqComponent.getType());
 143  65
         if(contextList == null || contextList.isEmpty()) {
 144  1
                 throw new DoesNotExistException("Requirement component context not found in registry for requirement component type id: " + reqComponent.getType());
 145  
         }
 146  64
         Map<String, Object> contextMap = new HashMap<String, Object>();
 147  64
         for(Context<ReqComponentInfo> context : contextList) {
 148  66
                     Map<String, Object> cm = context.createContextMap(reqComponent);
 149  66
                     contextMap.putAll(cm);
 150  66
             }
 151  
 
 152  64
         if(logger.isInfoEnabled()) {
 153  0
                         logger.info("contextMap="+contextMap);
 154  
                 }
 155  
 
 156  64
         return contextMap;
 157  
     }
 158  
 
 159  
     /**
 160  
      * Gets the requirement component type template for the <code>nlUsageTypeKey</code>.
 161  
      *
 162  
      * @param reqComponentType
 163  
      *            Requirement component type
 164  
      * @param nlUsageTypeKey
 165  
      *            Natural language usuage type key (context)
 166  
      * @return Requirement component type template
 167  
      * @throws DoesNotExistException Template does not exist
 168  
      */
 169  
     private ReqComponentTypeNLTemplate getTemplate(ReqComponentType reqComponentType, String nlUsageTypeKey, String language) throws DoesNotExistException {
 170  64
             List<ReqComponentTypeNLTemplate> templateList = reqComponentType.getNlUsageTemplates();
 171  64
         for (ReqComponentTypeNLTemplate template : templateList) {
 172  78
             if (nlUsageTypeKey.equals(template.getNlUsageTypeKey()) && language.equals(template.getLanguage())) {
 173  
 
 174  62
                     if(logger.isInfoEnabled()) {
 175  0
                                 logger.info("template="+template);
 176  
                         }
 177  
 
 178  62
                 return template;
 179  
             }
 180  
         }
 181  2
         throw new DoesNotExistException("Natural language usage type key '" + nlUsageTypeKey + "'" +
 182  
                         " and language code '" + language + "' for requirement component type " + reqComponentType.getId() + " template not found");
 183  
     }
 184  
 }