Coverage Report - org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageTranslatorImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
NaturalLanguageTranslatorImpl
82%
24/29
58%
7/12
1.857
 
 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 org.apache.log4j.Logger;
 19  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 20  
 import org.kuali.student.common.exceptions.OperationFailedException;
 21  
 import org.kuali.student.core.statement.entity.ReqComponent;
 22  
 import org.kuali.student.core.statement.entity.Statement;
 23  
 import org.kuali.student.core.statement.naturallanguage.NaturalLanguageTranslator;
 24  
 
 25  
 /**
 26  
  * This class translates requirement components and LU (learning unit) 
 27  
  * statements into a specific natural language and into a 
 28  
  * statement/requirement component tree.
 29  
  */
 30  
 public class NaturalLanguageTranslatorImpl implements NaturalLanguageTranslator {
 31  
 
 32  1
         private final static Logger logger = Logger.getLogger(NaturalLanguageTranslatorImpl.class);
 33  
         
 34  
         private ReqComponentTranslator reqComponentTranslator;
 35  
         private StatementTranslator statementTranslator;
 36  
         
 37  
         /**
 38  
          * Constructs a new natural language translator in the 
 39  
          * default language locale.
 40  
          */
 41  28
         public NaturalLanguageTranslatorImpl() {
 42  28
         }
 43  
 
 44  
         /**
 45  
          * Sets the requirement component translator.
 46  
          * 
 47  
          * @param reqComponentTranslator Requirement component translator
 48  
          */
 49  
         public void setReqComponentTranslator(final ReqComponentTranslator reqComponentTranslator) {
 50  28
                 this.reqComponentTranslator = reqComponentTranslator;
 51  28
         }
 52  
 
 53  
         /**
 54  
          * Sets the statement translator.
 55  
          * 
 56  
          * @param statementTranslator Statement translator
 57  
          */
 58  
         public void setStatementTranslator(final StatementTranslator statementTranslator) {
 59  28
                 this.statementTranslator = statementTranslator;
 60  28
         }
 61  
         
 62  
         /**
 63  
          * Translates a requirement component in the default language locale for a 
 64  
          * specific natural language, usuage type (context) into natural language.
 65  
          * 
 66  
          * @param reqComponent Requirement component to be translated
 67  
          * @param nlUsageTypeKey Natural language usage type key (context)
 68  
          * @return Natural language requirement translation
 69  
          * @throws DoesNotExistException Requirement component id does not exists
 70  
          * @throws OperationFailedException
 71  
          */
 72  
         public synchronized String translateReqComponent(final ReqComponent reqComponent, final String nlUsageTypeKey) throws DoesNotExistException, OperationFailedException {
 73  4
                 String nl = this.reqComponentTranslator.translate(reqComponent, nlUsageTypeKey);
 74  2
                 if(logger.isInfoEnabled()) {
 75  0
                         logger.info("ReqComponent translation="+nl);
 76  
                 }
 77  2
                 return nl;
 78  
         }
 79  
 
 80  
         /**
 81  
          * Translates a requirement component for a specific natural language, 
 82  
          * usuage type (context) and language locale (e.g. 'en' for English, 
 83  
          * 'de' for German) into natural language.
 84  
          * 
 85  
          * @param reqComponent Requirement component to be translated
 86  
          * @param nlUsageTypeKey Natural language usage type key (context)
 87  
          * @param language Translation language
 88  
          * @return
 89  
          * @throws DoesNotExistException
 90  
          * @throws OperationFailedException
 91  
          */
 92  
         public synchronized String translateReqComponent(final ReqComponent reqComponent, final String nlUsageTypeKey, final String language) throws DoesNotExistException, OperationFailedException {
 93  9
                 String nl = null;
 94  9
                 if(language == null) {
 95  2
                         nl = this.reqComponentTranslator.translate(reqComponent, nlUsageTypeKey);
 96  
                 } else {
 97  7
                         nl = this.reqComponentTranslator.translate(reqComponent, nlUsageTypeKey, language);
 98  
                 }
 99  9
                 if(logger.isInfoEnabled()) {
 100  0
                         logger.info("ReqComponent translation="+nl);
 101  
                 }
 102  9
                 return nl;
 103  
         }
 104  
         
 105  
         /**
 106  
          * Translates a statement for a specific natural language, 
 107  
          * usuage type (context) into natural language.
 108  
          * 
 109  
          * @param statement Statement to be translated 
 110  
          * @param nlUsageTypeKey Natural language usage type key (context)
 111  
          * @return Natural language statement translation
 112  
          * @throws DoesNotExistException CLU does not exists
 113  
          * @throws OperationFailedException Translation fails
 114  
          */
 115  
         public synchronized String translateStatement(final Statement statement, final String nlUsageTypeKey) throws DoesNotExistException, OperationFailedException {
 116  5
                 String nl = this.statementTranslator.translate(statement, nlUsageTypeKey);
 117  4
                 if(logger.isInfoEnabled()) {
 118  0
                         logger.info("Statement translation="+nl);
 119  
                 }
 120  4
                 return nl;
 121  
         }
 122  
 
 123  
         /**
 124  
          * Translates a statement for a specific natural language, 
 125  
          * usuage type (context) and language locale into natural language.
 126  
          * 
 127  
          * @param statement Statement to be translated 
 128  
          * @param nlUsageTypeKey Natural language usage type key (context)
 129  
          * @param language Translation language
 130  
          * @return Natural language statement translation
 131  
          * @throws DoesNotExistException CLU id does not exists
 132  
          * @throws OperationFailedException Translation fails
 133  
          */
 134  
         public synchronized String translateStatement(Statement statement, String nlUsageTypeKey, String language) throws DoesNotExistException, OperationFailedException {
 135  3
                 String nl = null;
 136  3
                 if(language == null) {
 137  0
                         nl = this.statementTranslator.translate(statement, nlUsageTypeKey);
 138  
                 } else {
 139  3
                         nl = this.statementTranslator.translate(statement, nlUsageTypeKey, language);
 140  
                 }
 141  3
                 if(logger.isInfoEnabled()) {
 142  0
                         logger.info("Statement translation="+nl);
 143  
                 }
 144  3
                 return nl;
 145  
         }
 146  
 }