Coverage Report - org.kuali.student.core.statement.service.impl.StatementServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
StatementServiceImpl
71%
298/417
50%
48/96
2.16
 
 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.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashSet;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 import java.util.Set;
 23  
 
 24  
 import javax.jws.WebService;
 25  
 
 26  
 import org.kuali.student.common.validator.Validator;
 27  
 import org.kuali.student.common.validator.ValidatorFactory;
 28  
 import org.kuali.student.core.dictionary.dto.ObjectStructureDefinition;
 29  
 import org.kuali.student.core.dictionary.service.DictionaryService;
 30  
 import org.kuali.student.core.dto.StatusInfo;
 31  
 import org.kuali.student.core.exceptions.AlreadyExistsException;
 32  
 import org.kuali.student.core.exceptions.CircularReferenceException;
 33  
 import org.kuali.student.core.exceptions.DataValidationErrorException;
 34  
 import org.kuali.student.core.exceptions.DoesNotExistException;
 35  
 import org.kuali.student.core.exceptions.InvalidParameterException;
 36  
 import org.kuali.student.core.exceptions.MissingParameterException;
 37  
 import org.kuali.student.core.exceptions.OperationFailedException;
 38  
 import org.kuali.student.core.exceptions.PermissionDeniedException;
 39  
 import org.kuali.student.core.exceptions.VersionMismatchException;
 40  
 import org.kuali.student.core.search.dto.SearchCriteriaTypeInfo;
 41  
 import org.kuali.student.core.search.dto.SearchRequest;
 42  
 import org.kuali.student.core.search.dto.SearchResult;
 43  
 import org.kuali.student.core.search.dto.SearchResultTypeInfo;
 44  
 import org.kuali.student.core.search.dto.SearchTypeInfo;
 45  
 import org.kuali.student.core.search.service.SearchManager;
 46  
 import org.kuali.student.core.statement.dao.StatementDao;
 47  
 import org.kuali.student.core.statement.dto.NlUsageTypeInfo;
 48  
 import org.kuali.student.core.statement.dto.RefStatementRelationInfo;
 49  
 import org.kuali.student.core.statement.dto.RefStatementRelationTypeInfo;
 50  
 import org.kuali.student.core.statement.dto.ReqComponentInfo;
 51  
 import org.kuali.student.core.statement.dto.ReqComponentTypeInfo;
 52  
 import org.kuali.student.core.statement.dto.StatementInfo;
 53  
 import org.kuali.student.core.statement.dto.StatementTreeViewInfo;
 54  
 import org.kuali.student.core.statement.dto.StatementTypeInfo;
 55  
 import org.kuali.student.core.statement.entity.NlUsageType;
 56  
 import org.kuali.student.core.statement.entity.ObjectType;
 57  
 import org.kuali.student.core.statement.entity.RefStatementRelation;
 58  
 import org.kuali.student.core.statement.entity.RefStatementRelationAttribute;
 59  
 import org.kuali.student.core.statement.entity.RefStatementRelationType;
 60  
 import org.kuali.student.core.statement.entity.ReqComponent;
 61  
 import org.kuali.student.core.statement.entity.ReqComponentType;
 62  
 import org.kuali.student.core.statement.entity.Statement;
 63  
 import org.kuali.student.core.statement.entity.StatementType;
 64  
 import org.kuali.student.core.statement.naturallanguage.NaturalLanguageTranslator;
 65  
 import org.kuali.student.core.statement.naturallanguage.translators.ReqComponentTranslator;
 66  
 import org.kuali.student.core.statement.service.StatementService;
 67  
 import org.kuali.student.core.validation.dto.ValidationResultInfo;
 68  
 import org.slf4j.Logger;
 69  
 import org.slf4j.LoggerFactory;
 70  
 import org.springframework.beans.BeanUtils;
 71  
 import org.springframework.transaction.annotation.Transactional;
 72  
 
 73  
 @WebService(endpointInterface = "org.kuali.student.core.statement.service.StatementService", serviceName = "StatementService", portName = "StatementService", targetNamespace = "http://student.kuali.org/wsdl/statement")
 74  
 @Transactional(noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class})
 75  4
 public class StatementServiceImpl implements StatementService {
 76  
 
 77  1
         private final static Logger logger = LoggerFactory.getLogger(ReqComponentTranslator.class);
 78  
 
 79  
         private StatementDao statementDao;
 80  
         private NaturalLanguageTranslator naturalLanguageTranslator;
 81  
     private SearchManager searchManager;
 82  
     private DictionaryService dictionaryServiceDelegate;
 83  
     private StatementAssembler statementAssembler;
 84  
     private ValidatorFactory validatorFactory;
 85  
     // private StatementTreeViewAssembler statementTreeViewAssembler;
 86  
 
 87  
         public void setStatementAssembler(StatementAssembler statementAssembler) {
 88  2
                 this.statementAssembler = statementAssembler;
 89  2
         }
 90  
 
 91  
         public SearchManager getSearchManager() {
 92  0
         return searchManager;
 93  
     }
 94  
 
 95  
     public void setSearchManager(final SearchManager searchManager) {
 96  2
         this.searchManager = searchManager;
 97  2
     }
 98  
 
 99  
     public DictionaryService getDictionaryServiceDelegate() {
 100  0
         return dictionaryServiceDelegate;
 101  
     }
 102  
 
 103  
     public void setDictionaryServiceDelegate(final DictionaryService dictionaryServiceDelegate) {
 104  2
             this.dictionaryServiceDelegate = dictionaryServiceDelegate;
 105  2
     }
 106  
 
 107  
     public StatementDao getStatementDao() {
 108  0
         return statementDao;
 109  
     }
 110  
 
 111  
     public void setStatementDao(final StatementDao statementDao) {
 112  2
                 this.statementDao = statementDao;
 113  2
         }
 114  
 
 115  
     public NaturalLanguageTranslator getNaturalLanguageTranslator() {
 116  0
         return naturalLanguageTranslator;
 117  
     }
 118  
 
 119  
         public void setNaturalLanguageTranslator(final NaturalLanguageTranslator translator) {
 120  2
                 this.naturalLanguageTranslator = translator;
 121  2
         }
 122  
 
 123  
         public NlUsageTypeInfo getNlUsageType(final String nlUsageTypeKey)
 124  
                         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 125  
 
 126  11
                 checkForNullOrEmptyParameter(nlUsageTypeKey, "nlUsageTypeKey");
 127  11
                 checkForEmptyParameter(nlUsageTypeKey, "nlUsageTypeKey");
 128  
 
 129  11
                 NlUsageType entity = this.statementDao.fetch(NlUsageType.class, nlUsageTypeKey);
 130  9
                 NlUsageTypeInfo info = StatementAssembler.toNlUsageTypeInfo(entity);
 131  9
                 return info;
 132  
         }
 133  
 
 134  
         public List<NlUsageTypeInfo> getNlUsageTypes()
 135  
                         throws OperationFailedException {
 136  
 
 137  1
                 List<NlUsageType> entities = this.statementDao.find(NlUsageType.class);
 138  1
                 List<NlUsageTypeInfo> infos = StatementAssembler.toNlUsageTypeInfos(entities);
 139  1
                 return infos;
 140  
         }
 141  
 
 142  
         public List<String> getRefObjectTypes() throws OperationFailedException {
 143  1
                 List<ObjectType> objectTypes = this.statementDao.find(ObjectType.class);
 144  1
                 List<String> ids = new ArrayList<String>();
 145  1
                 for(ObjectType objectType : objectTypes) {
 146  1
                         ids.add(objectType.getId());
 147  
                 }
 148  1
                 return ids;
 149  
         }
 150  
 
 151  
         public List<String> getRefObjectSubTypes(final String objectTypeKey)
 152  
                         throws DoesNotExistException,
 153  
                         InvalidParameterException, MissingParameterException,
 154  
                         OperationFailedException {
 155  
 
 156  1
                 checkForNullOrEmptyParameter(objectTypeKey, "objectTypeKey");
 157  1
                 checkForEmptyParameter(objectTypeKey, "objectTypeKey");
 158  
 
 159  1
                 ObjectType objectType = this.statementDao.fetch(ObjectType.class, objectTypeKey);
 160  1
                 List<String> ids = StatementAssembler.toRefObjectSubTypeIds(objectType);
 161  1
                 return ids;
 162  
         }
 163  
 
 164  
         public RefStatementRelationInfo getRefStatementRelation(final String refStatementRelationId)
 165  
                         throws DoesNotExistException, InvalidParameterException,
 166  
                         MissingParameterException, OperationFailedException {
 167  
 
 168  1
                 checkForNullOrEmptyParameter(refStatementRelationId, "refStatementRelationId");
 169  1
                 checkForEmptyParameter(refStatementRelationId, "refStatementRelationId");
 170  
 
 171  1
             RefStatementRelation entity = this.statementDao.fetch(RefStatementRelation.class, refStatementRelationId);
 172  1
             RefStatementRelationInfo dto = StatementAssembler.toRefStatementRelationInfo(entity);
 173  1
                 return dto;
 174  
         }
 175  
 
 176  
         public List<RefStatementRelationInfo> getRefStatementRelationsByRef(final String refObjectTypeKey, final String refObjectId)
 177  
                         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 178  0
         checkForNullOrEmptyParameter(refObjectTypeKey, "refObjectTypeKey");
 179  0
         checkForEmptyParameter(refObjectId, "refObjectId");
 180  
 
 181  0
         List<RefStatementRelation> references = this.statementDao.getRefStatementRelations(
 182  
                 refObjectTypeKey, refObjectId);
 183  0
         List<RefStatementRelationInfo> referenceInfos = null;
 184  0
         if (references != null) {
 185  0
             for (RefStatementRelation reference : references) {
 186  0
                 RefStatementRelationInfo dto = StatementAssembler.toRefStatementRelationInfo(reference);
 187  0
                 referenceInfos = (referenceInfos == null)? new ArrayList<RefStatementRelationInfo>(7) : referenceInfos;
 188  0
                 referenceInfos.add(dto);
 189  0
             }
 190  
         }
 191  0
         return referenceInfos;
 192  
         }
 193  
 
 194  
         public List<RefStatementRelationInfo> getRefStatementRelationsByStatement(final String statementId)
 195  
                         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 196  
 
 197  3
                 checkForNullOrEmptyParameter(statementId, "statementId");
 198  
 
 199  1
                 Statement statement = this.statementDao.fetch(Statement.class, statementId);
 200  1
                 List<RefStatementRelation> entities = statement.getRefStatementRelations();
 201  1
                 List<RefStatementRelationInfo> dtoList = StatementAssembler.toRefStatementRelationInfos(entities);
 202  1
                 return dtoList;
 203  
         }
 204  
 
 205  
         /**
 206  
          * <p>Translates and retrieves a requirement component for a specific
 207  
          * usuage type (context) into natural language.</p>
 208  
          *
 209  
          * <p>If <code>language</code> is null default language is used.</p>
 210  
          *
 211  
          * @param reqComponentId Requirement component to translate
 212  
          * @param nlUsageTypeKey Natural language usage type key (context)
 213  
          * @param language Translation language
 214  
      * @throws DoesNotExistException ReqComponent not found
 215  
      * @throws InvalidParameterException Invalid nlUsageTypeKey
 216  
      * @throws MissingParameterException Missing reqComponentId or nlUsageTypeKey
 217  
      * @throws OperationFailedException Unable to complete request
 218  
      * @throws VersionMismatchException The action was attempted on an out of date version.
 219  
          */
 220  
         public String getNaturalLanguageForReqComponent(final String reqComponentId, final String nlUsageTypeKey, final String language)
 221  
                         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 222  
 
 223  8
                 checkForNullOrEmptyParameter(reqComponentId, "reqComponentId");
 224  8
                 checkForNullOrEmptyParameter(nlUsageTypeKey, "nlUsageTypeKey");
 225  8
                 checkForEmptyParameter(language, "language");
 226  
 
 227  
                 // test usage type key exists
 228  8
                 getNlUsageType(nlUsageTypeKey);
 229  
 
 230  7
                 ReqComponent reqComponent = this.statementDao.fetch(ReqComponent.class, reqComponentId);
 231  7
                 String nl = this.naturalLanguageTranslator.translateReqComponent(reqComponent, nlUsageTypeKey, language);
 232  
 
 233  7
                 if(logger.isInfoEnabled()) {
 234  0
                         logger.info("reqComponentId="+reqComponentId);
 235  0
                         logger.info("nlUsageTypeKey="+nlUsageTypeKey);
 236  0
                         logger.info("language="+language);
 237  0
                         logger.info("ReqComponent translation="+nl);
 238  
                 }
 239  
 
 240  7
                 return nl;
 241  
         }
 242  
 
 243  
         /**
 244  
          * <p>Translates and retrieves a statement directly attached to a CLU
 245  
          * for a specific usuage type (context) into natural language.
 246  
          *
 247  
          * If <code>cluId</code> is null or empty then statement header is not
 248  
          * generated</p>
 249  
          *
 250  
          * <p>If <code>language</code> is null default language is used.</p>
 251  
          *
 252  
          * <p>An <code>StatementInfo</code> can either have a list of
 253  
          * <code>StatementInfo</code>s as children or a list of
 254  
          * <code>ReqComponentInfo</code>s but not both. This means that all leaf
 255  
          * nodes must be <code>ReqComponentInfo</code>s.</p>
 256  
          *
 257  
          * @param statementId Statement to translate
 258  
          * @param nlUsageTypeKey Natural language usage type key (context)
 259  
          * @param language Translation language
 260  
      * @throws DoesNotExistException Statement not found or Clu anchor not found in statement
 261  
      * @throws InvalidParameterException Invalid nlUsageTypeKey
 262  
      * @throws MissingParameterException Missing statementId or nlUsageTypeKey
 263  
      * @throws OperationFailedException Unable to complete request
 264  
      * @throws VersionMismatchException The action was attempted on an out of date version.
 265  
          */
 266  
         public String getNaturalLanguageForStatement(final String statementId, final String nlUsageTypeKey, final String language)
 267  
                         throws DoesNotExistException, InvalidParameterException,
 268  
                         MissingParameterException, OperationFailedException {
 269  
 
 270  1
                 checkForNullOrEmptyParameter(statementId, "statementId");
 271  1
                 checkForNullOrEmptyParameter(nlUsageTypeKey, "nlUsageTypeKey");
 272  1
                 checkForEmptyParameter(language, "language");
 273  
 
 274  1
                 Statement statement = this.statementDao.fetch(Statement.class, statementId);
 275  1
                 String nl = this.naturalLanguageTranslator.translateStatement(statement, nlUsageTypeKey, language);
 276  
 
 277  1
                 if(logger.isInfoEnabled()) {
 278  0
                         logger.info("statementId="+statementId);
 279  0
                         logger.info("nlUsageTypeKey="+nlUsageTypeKey);
 280  0
                         logger.info("language="+language);
 281  0
                         logger.info("Statement translation="+nl);
 282  
                 }
 283  
 
 284  1
                 return nl;
 285  
         }
 286  
 
 287  
     public String getNaturalLanguageForRefStatementRelation(final String refStatementRelationId, final String nlUsageTypeKey, final String language) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 288  1
                 checkForNullOrEmptyParameter(refStatementRelationId, "refStatementRelationId");
 289  1
                 checkForNullOrEmptyParameter(nlUsageTypeKey, "nlUsageTypeKey");
 290  1
                 checkForEmptyParameter(language, "language");
 291  
 
 292  1
                 RefStatementRelation refStatementRelation = this.statementDao.fetch(RefStatementRelation.class, refStatementRelationId);
 293  1
                 Statement statement = refStatementRelation.getStatement();
 294  1
                 String nl = this.naturalLanguageTranslator.translateStatement(statement, nlUsageTypeKey, language);
 295  
 
 296  1
                 if(logger.isInfoEnabled()) {
 297  0
                         logger.info("refStatementRelationId="+refStatementRelationId);
 298  0
                         logger.info("nlUsageTypeKey="+nlUsageTypeKey);
 299  0
                         logger.info("language="+language);
 300  0
                         logger.info("Statement translation="+nl);
 301  
                 }
 302  
 
 303  1
                 return nl;
 304  
         }
 305  
 
 306  
         @Override
 307  
         public String translateReqComponentToNL(final ReqComponentInfo reqComponentInfo, final String nlUsageTypeKey, final String language)
 308  
                         throws InvalidParameterException, MissingParameterException, OperationFailedException {
 309  1
                 checkForMissingParameter(reqComponentInfo, "reqComponentInfo");
 310  1
                 checkForNullOrEmptyParameter(nlUsageTypeKey, "nlUsageTypeKey");
 311  1
                 checkForEmptyParameter(language, "language");
 312  
 
 313  
                 try {
 314  
                         // test usage type key exists
 315  1
                         getNlUsageType(nlUsageTypeKey);
 316  1
                         ReqComponent req = statementAssembler.toReqComponentRelation(false, reqComponentInfo);
 317  1
                         String nl = this.naturalLanguageTranslator.translateReqComponent(req, nlUsageTypeKey, language);
 318  
 
 319  1
                         if(logger.isInfoEnabled()) {
 320  0
                             logger.info("ReqComponent translation="+nl);
 321  
                     }
 322  
 
 323  1
                         return nl;
 324  0
                 } catch (DoesNotExistException e) {
 325  0
                         throw new OperationFailedException("Requirement component translation failed: " + e.getMessage());
 326  0
                 } catch (VersionMismatchException e) {
 327  0
                         throw new OperationFailedException("Requirement component translation failed: " + e.getMessage());
 328  
                 }
 329  
         }
 330  
 
 331  
         @Override
 332  
         public String translateStatementTreeViewToNL(final StatementTreeViewInfo statementTreeViewInfo, final String nlUsageTypeKey, final String language)
 333  
                         throws InvalidParameterException, MissingParameterException, OperationFailedException {
 334  0
                 checkForMissingParameter(statementTreeViewInfo, "statementTreeViewInfo");
 335  0
                 checkForNullOrEmptyParameter(nlUsageTypeKey, "nlUsageTypeKey");
 336  0
                 checkForEmptyParameter(language, "language");
 337  
 
 338  
                 try {
 339  0
                         Statement statement = statementAssembler.toStatement(statementTreeViewInfo);
 340  
 
 341  0
                         String nl = this.naturalLanguageTranslator.translateStatement(statement, nlUsageTypeKey, language);
 342  
 
 343  0
                         if(logger.isInfoEnabled()) {
 344  0
                             logger.info("StatementTreeView translation="+nl);
 345  
                     }
 346  
 
 347  0
                         return nl;
 348  0
                 } catch (DoesNotExistException e) {
 349  0
                         throw new OperationFailedException("Statement tree view translation failed: " + e.getMessage());
 350  0
                 } catch (VersionMismatchException e) {
 351  0
                         throw new OperationFailedException("Statement tree view translation failed: " + e.getMessage());
 352  
                 }
 353  
         }
 354  
 
 355  
         /**
 356  
          * Check for missing parameter and throw localized exception if missing
 357  
          *
 358  
          * @param param
 359  
          * @param parameter name
 360  
          * @throws MissingParameterException
 361  
          */
 362  
         private void checkForMissingParameter(Object param, String paramName)
 363  
                         throws MissingParameterException {
 364  83
                 if (param == null) {
 365  0
                         throw new MissingParameterException(paramName + " can not be null");
 366  
                 }
 367  83
         }
 368  
 
 369  
         /**
 370  
          * Check for missing or empty parameter and
 371  
          * throw localized exception if missing or empty
 372  
          *
 373  
          * @param param
 374  
          * @param parameter name
 375  
          * @throws MissingParameterException
 376  
          */
 377  
         private void checkForNullOrEmptyParameter(String param, String paramName)
 378  
                         throws MissingParameterException, InvalidParameterException {
 379  58
                 if (param == null) {
 380  2
                         throw new MissingParameterException(paramName + " can not be null");
 381  56
                 } else if (param.trim().isEmpty()) {
 382  2
                         throw new InvalidParameterException(paramName + " can not be empty");
 383  
                 }
 384  54
         }
 385  
 
 386  
         /**
 387  
          * Check for empty parameter and throw localized exception if empty
 388  
          *
 389  
          * @param param
 390  
          * @param parameter name
 391  
          * @throws MissingParameterException
 392  
          */
 393  
         private void checkForEmptyParameter(String param, String paramName)
 394  
                         throws MissingParameterException, InvalidParameterException {
 395  24
                 if (param != null && param.trim().isEmpty()) {
 396  0
                         throw new InvalidParameterException(paramName + " can not be empty");
 397  
                 }
 398  24
         }
 399  
 
 400  
     @Override
 401  
     public ReqComponentInfo createReqComponent(final String reqComponentType, final ReqComponentInfo reqComponentInfo) throws AlreadyExistsException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 402  5
         checkForMissingParameter(reqComponentType, "reqComponentType");
 403  5
         checkForMissingParameter(reqComponentInfo, "reqComponentInfo");
 404  
 
 405  5
         ReqComponent reqComp = null;
 406  
 
 407  
         try {
 408  5
             reqComp = statementAssembler.toReqComponentRelation(false, reqComponentInfo);
 409  0
         } catch (VersionMismatchException e) {
 410  0
             throw new OperationFailedException("Version Mismatch.", e);
 411  5
         }
 412  
 
 413  5
         reqComp = statementDao.create(reqComp);
 414  
 
 415  5
         return statementAssembler.toReqComponentInfo(reqComp, null, null);
 416  
     }
 417  
 
 418  
     @Override
 419  
     public StatementInfo createStatement(final String statementType, final StatementInfo statementInfo) throws AlreadyExistsException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 420  4
         checkForMissingParameter(statementType, "statementType");
 421  4
         checkForMissingParameter(statementInfo, "statementInfo");
 422  
 
 423  4
         Statement statement = null;
 424  
 
 425  
         try {
 426  4
             statement = statementAssembler.toStatementRelation(false, statementInfo);
 427  0
         } catch (VersionMismatchException e) {
 428  0
             throw new OperationFailedException("Version Mismatch.", e);
 429  4
         }
 430  
 
 431  4
         statementDao.create(statement);
 432  
 
 433  4
         StatementInfo info = StatementAssembler.toStatementInfo(statement);
 434  
 
 435  4
         return info;
 436  
     }
 437  
 
 438  
     @Override
 439  
     public StatementTreeViewInfo createStatementTreeView(final StatementTreeViewInfo statementTreeViewInfo) throws AlreadyExistsException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, CircularReferenceException {
 440  
             try {
 441  
             // insert statements and reqComponents if they do not already exists in database
 442  1
             updateSTVHelperCreateStatements(statementTreeViewInfo);
 443  
 
 444  1
             updateStatementTreeViewHelper(statementTreeViewInfo);
 445  1
             StatementTreeViewInfo test = getStatementTreeView(statementTreeViewInfo.getId());
 446  
 
 447  1
             return test;
 448  0
                 } catch (VersionMismatchException e) {
 449  0
                         throw new OperationFailedException("Create failed.", e);
 450  
                 }
 451  
     }
 452  
 
 453  
     
 454  
     @Override
 455  
     public StatusInfo deleteReqComponent(final String reqComponentId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 456  1
         checkForMissingParameter(reqComponentId, "reqComponentId");
 457  
 
 458  1
         ReqComponent reqComp = statementDao.fetch(ReqComponent.class, reqComponentId);
 459  
 
 460  1
         if(reqComp==null){
 461  0
             throw new DoesNotExistException("ReqComponent does not exist for id: "+reqComponentId);
 462  
         }
 463  
 
 464  1
         statementDao.delete(reqComp);
 465  
 
 466  1
         StatusInfo statusInfo = new StatusInfo();
 467  1
         statusInfo.setSuccess(true);
 468  1
         statusInfo.setMessage("Requirement component successfully deleted");
 469  1
         return statusInfo;
 470  
     }
 471  
 
 472  
     @Override
 473  
     public StatusInfo deleteStatement(final String statementId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 474  1
         checkForMissingParameter(statementId, "statementId");
 475  
 
 476  1
         Statement stmt = statementDao.fetch(Statement.class, statementId);
 477  1
         if(stmt==null){
 478  0
             throw new DoesNotExistException("Statement does not exist for id: "+statementId);
 479  
         }
 480  
 
 481  
 
 482  
                 try {
 483  1
                         Statement parent = statementDao.getParentStatement(statementId);
 484  1
                 List<Statement> children = parent.getChildren();
 485  1
                 for (int i = 0; i < children.size(); i++) {
 486  1
                         if (children.get(i).getId().equals(statementId)) {
 487  1
                                 children.remove(i);
 488  1
                                 break;
 489  
                         }
 490  
                 }
 491  1
                 statementDao.update(parent);
 492  0
                 } catch (DoesNotExistException e) {
 493  
                         // Ignore in this case
 494  1
                 }
 495  
 
 496  1
         statementDao.delete(stmt);
 497  
 
 498  1
         StatusInfo statusInfo = new StatusInfo();
 499  1
         statusInfo.setSuccess(true);
 500  1
         statusInfo.setMessage("Statement successfully deleted");
 501  1
         return statusInfo;
 502  
     }
 503  
     
 504  
     @Override
 505  
     public StatusInfo deleteStatementTreeView(final String statementId) throws DoesNotExistException{
 506  1
         Statement stmt = statementDao.fetch(Statement.class, statementId);
 507  
         
 508  
         try{
 509  1
                 Statement parent = statementDao.getParentStatement(statementId);
 510  
                 
 511  
                 //remove the child from the parent
 512  0
             if(parent.getChildren()!=null){
 513  0
                         for(Iterator<Statement> iter = parent.getChildren().iterator();iter.hasNext();){
 514  0
                                 Statement childStmt = iter.next();
 515  0
                                 if(stmt.getId().equals(childStmt.getId())){
 516  0
                                         iter.remove();
 517  0
                                         break;
 518  
                                 }
 519  0
                         }
 520  
                 }
 521  0
                 statementDao.update(parent);
 522  1
             }catch(DoesNotExistException e){
 523  
                     //Ignore in this case
 524  0
             }
 525  
         
 526  
         //delete the tree hierarchy;
 527  1
         deleteRecursively(stmt);
 528  
         
 529  1
         StatusInfo statusInfo = new StatusInfo();
 530  1
         statusInfo.setSuccess(true);
 531  1
         statusInfo.setMessage("Statement Tree successfully deleted");
 532  1
         return statusInfo;
 533  
     }
 534  
 
 535  
     private void deleteRecursively(Statement stmt) {
 536  3
             if(stmt.getChildren()!=null){
 537  3
                     List<Statement> childStmts = new ArrayList<Statement>(stmt.getChildren());
 538  3
                     stmt.getChildren().clear();
 539  3
                     stmt = statementDao.update(stmt);
 540  3
                     for(Statement childStmt:childStmts){
 541  2
                             deleteRecursively(childStmt);
 542  
                     }
 543  
             }
 544  3
             statementDao.delete(stmt);
 545  3
         }
 546  
 
 547  
         @Override
 548  
     public ReqComponentInfo getReqComponent(final String reqComponentId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 549  10
         return statementAssembler.toReqComponentInfo(statementDao.fetch(ReqComponent.class, reqComponentId), null, null);
 550  
     }
 551  
 
 552  
     @Override
 553  
     public List<ReqComponentInfo> getReqComponentsByType(final String reqComponentTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 554  2
         checkForMissingParameter(reqComponentTypeKey, "reqComponentTypeKey");
 555  
 
 556  2
         List<ReqComponent> reqComponents = statementDao.getReqComponentsByType(reqComponentTypeKey);
 557  2
         return statementAssembler.toReqComponentInfos(reqComponents, null, null);
 558  
     }
 559  
 
 560  
     @Override
 561  
     public StatementInfo getStatement(final String statementId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 562  17
         StatementInfo statementInfo = null;
 563  17
         checkForMissingParameter(statementId, "statementId");
 564  17
         statementInfo = StatementAssembler.toStatementInfo(statementDao.fetch(Statement.class, statementId));
 565  12
         return statementInfo;
 566  
     }
 567  
 
 568  
     @Override
 569  
     public List<StatementInfo> getStatementsByType(final String statementTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 570  2
         checkForMissingParameter(statementTypeKey, "statementTypeKey");
 571  
 
 572  2
         List<Statement> statements = statementDao.getStatementsForStatementType(statementTypeKey);
 573  2
         return StatementAssembler.toStatementInfos(statements);
 574  
     }
 575  
 
 576  
     @Override
 577  
     public List<StatementInfo> getStatementsUsingReqComponent(final String reqComponentId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 578  1
         checkForNullOrEmptyParameter(reqComponentId, "reqComponentId");
 579  
 
 580  1
         List<Statement> list = statementDao.getStatementsForReqComponent(reqComponentId);
 581  1
         return StatementAssembler.toStatementInfos(list);
 582  
     }
 583  
 
 584  
     /**
 585  
      * Gets child statements but does no downward recursion of child statements.
 586  
      *
 587  
      * @param statementId statement identifier
 588  
      * @return List of child statements using the specified statement
 589  
      * @throws DoesNotExistException Statement not found
 590  
      * @throws InvalidParameterException Invalid statementId
 591  
      * @throws MissingParameterException statementId not specified
 592  
      * @throws OperationFailedException Unable to complete request
 593  
      */
 594  
         public List<StatementInfo> getStatementsUsingStatement(final String statementId)
 595  
                         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 596  1
         checkForNullOrEmptyParameter(statementId, "statementId");
 597  
 
 598  1
                 Statement statement = statementDao.fetch(Statement.class, statementId);
 599  1
                 List<StatementInfo> list = StatementAssembler.toStatementInfos(statement.getChildren());
 600  1
                 return list;
 601  
         }
 602  
 
 603  
     @Override
 604  
     public StatementInfo updateStatement(final String statementId, final StatementInfo statementInfo) throws CircularReferenceException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 605  
         //Check Missing params
 606  5
         checkForMissingParameter(statementId, "statementId");
 607  5
         checkForMissingParameter(statementInfo, "statementInfo");
 608  
 
 609  
         //Set all the values on statementInfo
 610  5
         statementInfo.setId(statementId);
 611  
 
 612  
         //Update persistence entity from the statementInfo
 613  5
         Statement stmt = statementAssembler.toStatementRelation(true, statementInfo);
 614  
 
 615  
         //Update the statement
 616  4
         Statement updatedStmt = statementDao.update(stmt);
 617  
 
 618  
         //Copy back to an statementInfo and return
 619  4
         StatementInfo updStatementInfo = StatementAssembler.toStatementInfo(updatedStmt);
 620  4
         return updStatementInfo;
 621  
     }
 622  
 
 623  
     @Override
 624  
     public List<ValidationResultInfo> validateReqComponent(final String validationType, final ReqComponentInfo reqComponentInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 625  3
         checkForMissingParameter(validationType, "validationType");
 626  3
         checkForMissingParameter(reqComponentInfo, "reqComponentInfo");
 627  
 
 628  3
         ObjectStructureDefinition objStructure = this.getObjectStructure(ReqComponentInfo.class.getName());
 629  3
         Validator defaultValidator = validatorFactory.getValidator();
 630  3
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(reqComponentInfo, objStructure);
 631  
 
 632  3
         return validationResults;
 633  
     }
 634  
 
 635  
     @Override
 636  
     public List<ValidationResultInfo> validateStatement(final String validationType, final StatementInfo statementInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 637  0
         checkForMissingParameter(validationType, "validationType");
 638  0
         checkForMissingParameter(statementInfo, "statementInfo");
 639  
 
 640  0
         ObjectStructureDefinition objStructure = this.getObjectStructure(StatementInfo.class.getName());
 641  0
         Validator defaultValidator = validatorFactory.getValidator();
 642  0
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(statementInfo, objStructure);
 643  
 
 644  0
         return validationResults;
 645  
     }
 646  
 
 647  
     @Override
 648  
     public ObjectStructureDefinition getObjectStructure(String objectTypeKey) {
 649  8
         return dictionaryServiceDelegate.getObjectStructure(objectTypeKey);
 650  
     }
 651  
 
 652  
     @Override
 653  
     public SearchCriteriaTypeInfo getSearchCriteriaType(final String searchCriteriaTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 654  0
         return searchManager.getSearchCriteriaType(searchCriteriaTypeKey);
 655  
     }
 656  
 
 657  
     @Override
 658  
     public List<SearchCriteriaTypeInfo> getSearchCriteriaTypes() throws OperationFailedException {
 659  0
         return searchManager.getSearchCriteriaTypes();
 660  
     }
 661  
 
 662  
     @Override
 663  
     public SearchResultTypeInfo getSearchResultType(final String searchResultTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 664  0
         checkForMissingParameter(searchResultTypeKey, "searchResultTypeKey");
 665  0
         return searchManager.getSearchResultType(searchResultTypeKey);
 666  
     }
 667  
 
 668  
     @Override
 669  
     public List<SearchResultTypeInfo> getSearchResultTypes() throws OperationFailedException {
 670  0
         return searchManager.getSearchResultTypes();
 671  
     }
 672  
 
 673  
     @Override
 674  
     public SearchTypeInfo getSearchType(final String searchTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 675  0
         checkForMissingParameter(searchTypeKey, "searchTypeKey");
 676  0
         return searchManager.getSearchType(searchTypeKey);
 677  
     }
 678  
 
 679  
     @Override
 680  
     public List<SearchTypeInfo> getSearchTypes() throws OperationFailedException {
 681  0
         return searchManager.getSearchTypes();
 682  
     }
 683  
 
 684  
     @Override
 685  
     public List<SearchTypeInfo> getSearchTypesByCriteria(final String searchCriteriaTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 686  0
         checkForMissingParameter(searchCriteriaTypeKey, "searchCriteriaTypeKey");
 687  0
         return searchManager.getSearchTypesByCriteria(searchCriteriaTypeKey);
 688  
     }
 689  
 
 690  
     @Override
 691  
     public List<SearchTypeInfo> getSearchTypesByResult(final String searchResultTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 692  0
         checkForMissingParameter(searchResultTypeKey, "searchResultTypeKey");
 693  0
         return searchManager.getSearchTypesByResult(searchResultTypeKey);
 694  
     }
 695  
 
 696  
     @Override
 697  
     public SearchResult search(final SearchRequest searchRequest) throws MissingParameterException {
 698  1
         checkForMissingParameter(searchRequest, "searchRequest");
 699  1
         return searchManager.search(searchRequest, statementDao);
 700  
     }
 701  
 
 702  
     @Override
 703  
     public List<String> getObjectTypes() {
 704  0
         return dictionaryServiceDelegate.getObjectTypes();
 705  
     }
 706  
 
 707  
     @Override
 708  
     public StatementTypeInfo getStatementType(final String statementTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 709  1
         return StatementAssembler.toStatementTypeInfo(statementDao.fetch(StatementType.class, statementTypeKey));
 710  
     }
 711  
 
 712  
     @Override
 713  
     public List<StatementTypeInfo> getStatementTypes() throws OperationFailedException {
 714  1
         return StatementAssembler.toStatementTypeInfos(statementDao.find(StatementType.class));
 715  
     }
 716  
 
 717  
     public List<String> getStatementTypesForStatementType(final String statementTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 718  1
             StatementTypeInfo type = StatementAssembler.toStatementTypeInfo(statementDao.fetch(StatementType.class, statementTypeKey));
 719  1
             return type.getAllowedStatementTypes();
 720  
     }
 721  
 
 722  
     @Override
 723  
     public List<ReqComponentTypeInfo> getReqComponentTypes() throws OperationFailedException {
 724  1
         return StatementAssembler.toReqComponentTypeInfos(statementDao.find(ReqComponentType.class));
 725  
     }
 726  
 
 727  
     @Override
 728  
     public ReqComponentTypeInfo getReqComponentType(final String reqComponentTypeKey)
 729  
                     throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 730  3
         return StatementAssembler.toReqComponentTypeInfo(statementDao.fetch(ReqComponentType.class, reqComponentTypeKey));
 731  
     }
 732  
 
 733  
     @Override
 734  
     public List<ReqComponentTypeInfo> getReqComponentTypesForStatementType(final String statementTypeKey)
 735  
                         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 736  1
         checkForMissingParameter(statementTypeKey, "statementTypeKey");
 737  
 
 738  1
         StatementType stmtType = statementDao.fetch(StatementType.class, statementTypeKey);
 739  1
         if(null == stmtType) {
 740  0
             throw new DoesNotExistException("Statement Type: " + statementTypeKey + " does not exist.");
 741  
         }
 742  
 
 743  1
         return StatementAssembler.toReqComponentTypeInfosOrdered( stmtType.getAllowedReqComponentTypes() );
 744  
     }
 745  
 
 746  
     @Override
 747  
     public ReqComponentInfo updateReqComponent(final String reqComponentId, final ReqComponentInfo reqComponentInfo)
 748  
                     throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 749  
         //Check Missing params
 750  7
         checkForMissingParameter(reqComponentId, "reqComponentId");
 751  7
         checkForMissingParameter(reqComponentInfo, "reqComponentInfo");
 752  
 
 753  
         //Set all the values on reqComponentInfo
 754  7
         reqComponentInfo.setId(reqComponentId);
 755  
 
 756  7
         ReqComponent reqComp = null;
 757  
 
 758  
         //Update persistence entity from the reqComponentInfo
 759  7
         reqComp = statementAssembler.toReqComponentRelation(true, reqComponentInfo);
 760  
 
 761  
         //Update the reqComponen
 762  6
         ReqComponent updatedReqComp = statementDao.update(reqComp);
 763  
 
 764  
         //Copy back to an reqComponentInfo and return
 765  6
         ReqComponentInfo updReqCompInfo = StatementAssembler.toReqComponentInfo(updatedReqComp);
 766  6
         return updReqCompInfo;
 767  
     }
 768  
 
 769  
         public RefStatementRelationInfo createRefStatementRelation(final RefStatementRelationInfo refStatementRelationInfo)
 770  
                         throws AlreadyExistsException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 771  6
                 checkForMissingParameter(refStatementRelationInfo, "refStatementRelationInfo");
 772  
 
 773  6
                 Statement statement = this.statementDao.fetch(Statement.class, refStatementRelationInfo.getStatementId());
 774  6
                 RefStatementRelationType type = this.statementDao.fetch(RefStatementRelationType.class, refStatementRelationInfo.getType());
 775  
 
 776  
         // make sure refObjectType exist
 777  6
         this.statementDao.fetch(ObjectType.class, refStatementRelationInfo.getRefObjectTypeKey());
 778  
                 
 779  6
                 RefStatementRelation entity = new RefStatementRelation();
 780  
 
 781  6
                 BeanUtils.copyProperties(refStatementRelationInfo, entity, new String[] {
 782  
                                 "statementId", "attributes", "metaInfo", "type", "id"});
 783  
 
 784  6
                 entity.setRefStatementRelationType(type);
 785  6
                 entity.setStatement(statement);
 786  
 
 787  6
                 List<RefStatementRelationAttribute> attributes = StatementAssembler.toGenericAttributes(RefStatementRelationAttribute.class, refStatementRelationInfo.getAttributes(), entity, this.statementDao);
 788  6
                 entity.setAttributes(attributes);
 789  
 
 790  6
                 RefStatementRelation newEntity = this.statementDao.create(entity);
 791  
 
 792  6
                 RefStatementRelationInfo newDto = StatementAssembler.toRefStatementRelationInfo(newEntity);
 793  
 
 794  6
                 return newDto;
 795  
         }
 796  
 
 797  
         @Override
 798  
         public RefStatementRelationInfo updateRefStatementRelation(final String refStatementRelationId, final RefStatementRelationInfo refStatementRelationInfo)
 799  
                         throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 800  3
                 checkForNullOrEmptyParameter(refStatementRelationId, "refStatementRelationId");
 801  3
                 checkForMissingParameter(refStatementRelationInfo, "refStatementRelationInfo");
 802  
 
 803  3
                 refStatementRelationInfo.setId(refStatementRelationId);
 804  3
                 RefStatementRelation refStatementRel = statementAssembler.toRefStatementRelation(true, refStatementRelationInfo);
 805  1
                 RefStatementRelation updatedRefStatementRel = statementDao.update(refStatementRel);
 806  
 
 807  1
                 RefStatementRelationInfo dto = StatementAssembler.toRefStatementRelationInfo(updatedRefStatementRel);
 808  1
                 return dto;
 809  
         }
 810  
 
 811  
         @Override
 812  
         public StatusInfo deleteRefStatementRelation(final String refStatementRelationId)
 813  
                         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 814  10
                 checkForNullOrEmptyParameter(refStatementRelationId, "refStatementRelationId");
 815  8
                 this.statementDao.delete(RefStatementRelation.class, refStatementRelationId);
 816  
 
 817  6
         StatusInfo statusInfo = new StatusInfo();
 818  6
         statusInfo.setSuccess(true);
 819  6
         statusInfo.setMessage("Reference statement relation successfully deleted");
 820  6
         return statusInfo;
 821  
         }
 822  
 
 823  
         @Override
 824  
         public List<ValidationResultInfo> validateRefStatementRelation(final String validationType, RefStatementRelationInfo refStatementRelationInfo)
 825  
                         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 826  1
         ObjectStructureDefinition objStructure = this.getObjectStructure(RefStatementRelationInfo.class.getName());
 827  1
         Validator defaultValidator = validatorFactory.getValidator();
 828  1
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(refStatementRelationInfo, objStructure);
 829  
 
 830  1
                 return validationResults;
 831  
         }
 832  
 
 833  
     @Override
 834  
     public StatementTreeViewInfo getStatementTreeView(final String statementId)
 835  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 836  5
             checkForNullOrEmptyParameter("statementId", statementId);
 837  
 
 838  5
             return getStatementTreeView(statementId, null, null);
 839  
     }
 840  
 
 841  
     @Override
 842  
     public StatementTreeViewInfo getStatementTreeViewForNlUsageType(final String statementId, final String nlUsageTypeKey, final String language)
 843  
                 throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 844  0
             checkForNullOrEmptyParameter("statementId", statementId);
 845  0
             checkForNullOrEmptyParameter("nlUsageTypeKey", nlUsageTypeKey);
 846  0
             checkForNullOrEmptyParameter("language", language);
 847  
 
 848  0
             return getStatementTreeView(statementId, nlUsageTypeKey, language);
 849  
     }
 850  
 
 851  
     private StatementTreeViewInfo getStatementTreeView(final String statementId, final String nlUsageTypeKey, final String language)
 852  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 853  5
         StatementTreeViewInfo statementTreeViewInfo = null;
 854  5
         StatementInfo statementInfo = getStatement(statementId);
 855  2
         if (statementInfo == null) return null;
 856  2
         statementTreeViewInfo = new StatementTreeViewInfo();
 857  2
         getStatementTreeViewHelper(statementInfo, statementTreeViewInfo, nlUsageTypeKey, language);
 858  2
         return statementTreeViewInfo;
 859  
 
 860  
 
 861  
             /*
 862  
             Map<String, String> configuration = new HashMap<String, String>();
 863  
             configuration.put("USAGE_TYPE_KEY", nlUsageTypeKey);
 864  
             configuration.put("NL_KEY", language);
 865  
             StatementTreeViewInfo result = new StatementTreeViewInfo();
 866  
             try {
 867  
                         statementTreeViewAssembler.assemble(getStatement(statementId), result, false, configuration);
 868  
                         return result;
 869  
             } catch (AssemblyException e) {
 870  
                         throw new OperationFailedException(e.getMessage(), e);
 871  
                 }
 872  
                 */
 873  
     }
 874  
 
 875  
 
 876  
     /**
 877  
      * Goes through the list of reqComponentIds in statementInfo and retrieves the reqComponentInfos being referenced
 878  
      * @param statementInfo
 879  
      * @return list of reqComponentInfo referenced by the list of reqComponentIds in statementInfo
 880  
      * @throws DoesNotExistException
 881  
      * @throws InvalidParameterException
 882  
      * @throws MissingParameterException
 883  
      * @throws OperationFailedException
 884  
      */
 885  
     private List<ReqComponentInfo> getReqComponentInfos(final StatementInfo statementInfo, final String nlUsageTypeKey, final String language)
 886  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 887  6
         List<ReqComponentInfo> reqComponentInfos = new ArrayList<ReqComponentInfo>();
 888  6
         if (statementInfo == null) return null;
 889  6
         if (statementInfo.getReqComponentIds() != null) {
 890  6
             for (String reqComponentId : statementInfo.getReqComponentIds()) {
 891  
                 //ReqComponentInfo reqCompInfo = getReqComponent(reqComponentId);
 892  8
                     ReqComponentInfo reqCompInfo = statementAssembler.toReqComponentInfo(statementDao.fetch(ReqComponent.class, reqComponentId), nlUsageTypeKey, language);
 893  8
                 reqComponentInfos.add(reqCompInfo);
 894  8
             }
 895  
         }
 896  6
         return reqComponentInfos;
 897  
     }
 898  
 
 899  
     /**
 900  
      * Goes through the list of statementIds in statementInfo and retrieves all
 901  
      * information regarding to the current statementInfo and all the
 902  
      * sub-statements referenced by statementIds.  Data will be populated into
 903  
      * statementTreeViewInfo
 904  
      * @param statementInfo
 905  
      * @return void
 906  
      * @throws DoesNotExistException
 907  
      * @throws InvalidParameterException
 908  
      * @throws MissingParameterException
 909  
      * @throws OperationFailedException
 910  
      */
 911  
     private void getStatementTreeViewHelper(final StatementInfo statementInfo, final StatementTreeViewInfo statementTreeViewInfo,
 912  
                     final String nlUsageTypeKey, final String language)
 913  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 914  6
         if (statementInfo == null) return;
 915  
 
 916  6
         statementAssembler.copyValues(statementTreeViewInfo, statementInfo);
 917  6
         statementTreeViewInfo.setReqComponents(getReqComponentInfos(statementInfo, nlUsageTypeKey, language));
 918  
         // get statements recursively and convert them into statementTreeViewInfo
 919  6
         if (statementInfo.getStatementIds() != null) {
 920  6
             for (String statementId : statementInfo.getStatementIds()) {
 921  4
                 StatementInfo subStatement = getStatement(statementId);
 922  
 
 923  4
                 List<StatementTreeViewInfo> statements =
 924  
                     (statementTreeViewInfo.getStatements() == null) ? new ArrayList<StatementTreeViewInfo>() : statementTreeViewInfo.getStatements();
 925  4
                 StatementTreeViewInfo subStatementTreeViewInfo = new StatementTreeViewInfo();
 926  
 
 927  
                 // recursive call to get subStatementTreeViewInfo
 928  4
                 getStatementTreeViewHelper(subStatement, subStatementTreeViewInfo, nlUsageTypeKey, language);
 929  4
                 statements.add(subStatementTreeViewInfo);
 930  4
                 statementTreeViewInfo.setStatements(statements);
 931  4
             }
 932  
         }
 933  6
     }
 934  
 
 935  
     @Override
 936  
     public StatementTreeViewInfo updateStatementTreeView(final String statementId, final StatementTreeViewInfo statementTreeViewInfo)
 937  
             throws CircularReferenceException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 938  
                 
 939  0
                 Statement stmt = this.statementDao.fetch(Statement.class, statementTreeViewInfo.getId());
 940  0
             if (stmt == null) {
 941  0
                 throw new DoesNotExistException("Statement does not exist for id: " + statementTreeViewInfo.getId());
 942  
                 }
 943  0
                 if (!String.valueOf(stmt.getVersionNumber()).equals(statementTreeViewInfo.getMetaInfo().getVersionInd())) {
 944  0
                     throw new VersionMismatchException("Statement to be updated is not the current version");
 945  
                 }
 946  
                    
 947  0
             Set<String> statementIdsToDelete = new HashSet<String>();
 948  0
             List<ReqComponent> requirementComponentsToCreate = new ArrayList<ReqComponent>();
 949  0
             List<Statement> statmentsToUpdate = new ArrayList<Statement>();
 950  
             
 951  
             //Transform the tree into a statement with all of its children
 952  0
             stmt = statementAssembler.toStatementFromTree(stmt, statementTreeViewInfo, statementIdsToDelete, statmentsToUpdate, requirementComponentsToCreate);
 953  
                 
 954  
             //Create any new reqComponents 
 955  0
             for(ReqComponent reqComponent:requirementComponentsToCreate){
 956  0
                         statementDao.create(reqComponent);
 957  
                 }
 958  
             
 959  
             //Update the actual statement
 960  0
             stmt = statementDao.update(stmt);
 961  
             
 962  
             //Update statements where the join table needs to be cleared
 963  
             
 964  
             //delete orphaned statements
 965  0
             for(String statementIdToDelete:statementIdsToDelete){
 966  0
                     statementDao.delete(Statement.class, statementIdToDelete);
 967  
             }
 968  
             
 969  
             //Transform back to a dto
 970  0
             StatementTreeViewInfo result = statementAssembler.toStatementTreeViewInfo(stmt);
 971  
         
 972  0
                 return result;
 973  
 
 974  
 
 975  
     }
 976  
 
 977  
     /*private List<String> notIn(
 978  
             StatementTreeViewInfo oldTree,
 979  
             StatementTreeViewInfo newTree) {
 980  
         List<String> results = new ArrayList<String>(17);
 981  
         List<String> oldStatementIds = new ArrayList<String>(17);
 982  
         List<String> newStatementIds = new ArrayList<String>(17);
 983  
         getStatementIds(oldTree, oldStatementIds);
 984  
         getStatementIds(newTree, newStatementIds);
 985  
         if (oldStatementIds != null) {
 986  
             for (String oldStatementId : oldStatementIds) {
 987  
                 boolean inNewStatementIds = false;
 988  
                 if (newStatementIds != null) {
 989  
                     for (String newStatementId : newStatementIds) {
 990  
                         if (oldStatementId.equals(newStatementId)) {
 991  
                             inNewStatementIds = true;
 992  
                         }
 993  
                     }
 994  
                 }
 995  
                 if (!inNewStatementIds) {
 996  
                     results.add(oldStatementId);
 997  
                 }
 998  
             }
 999  
         }
 1000  
         return results;
 1001  
     }
 1002  
 
 1003  
     private void getStatementIds(StatementTreeViewInfo statementTreeViewInfo, List<String> statementIds) {
 1004  
         if (statementTreeViewInfo.getStatements() != null) {
 1005  
             for (StatementTreeViewInfo subTree : statementTreeViewInfo.getStatements()) {
 1006  
                 getStatementIds(subTree, statementIds);
 1007  
             }
 1008  
         }
 1009  
         statementIds.add(statementTreeViewInfo.getId());
 1010  
     }*/
 1011  
 
 1012  
     private void updateStatementTreeViewHelper(StatementTreeViewInfo statementTreeViewInfo) throws CircularReferenceException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 1013  3
         if (statementTreeViewInfo.getStatements() != null) {
 1014  3
             for (StatementTreeViewInfo subStatement : statementTreeViewInfo.getStatements()) {
 1015  2
                 updateStatementTreeViewHelper(subStatement);
 1016  
             }
 1017  
         }
 1018  3
         if (statementTreeViewInfo.getReqComponents() != null) {
 1019  3
             List<ReqComponentInfo> updatedReqComponentInfos = new ArrayList<ReqComponentInfo>(7);
 1020  3
             for (ReqComponentInfo reqComponentInfo : statementTreeViewInfo.getReqComponents()) {
 1021  4
                 ReqComponentInfo updatedReqComponentInfo = updateReqComponent(reqComponentInfo.getId(), reqComponentInfo);
 1022  4
                 updatedReqComponentInfos.add(updatedReqComponentInfo);
 1023  4
             }
 1024  3
             statementTreeViewInfo.setReqComponents(updatedReqComponentInfos);
 1025  
         }
 1026  3
         StatementInfo updatedStatementInfo = updateStatement(statementTreeViewInfo.getId(), statementAssembler.toStatementInfo(
 1027  
                 statementTreeViewInfo));
 1028  3
         statementAssembler.copyValues(statementTreeViewInfo, updatedStatementInfo);
 1029  3
     }
 1030  
 
 1031  
     private void updateSTVHelperCreateStatements(StatementTreeViewInfo statementTreeViewInfo) throws CircularReferenceException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 1032  3
         String statementId = null;
 1033  3
         StatementInfo origStatementInfo = null;
 1034  3
         StatementInfo newStatementInfo = null;
 1035  3
         if (statementTreeViewInfo.getStatements() != null) {
 1036  3
             for (StatementTreeViewInfo subTreeInfo : statementTreeViewInfo.getStatements()) {
 1037  2
                 updateSTVHelperCreateStatements(subTreeInfo);
 1038  
             }
 1039  
         }
 1040  3
         if (statementTreeViewInfo.getReqComponents() != null) {
 1041  3
             List<ReqComponentInfo> rcsAfterInserts = new ArrayList<ReqComponentInfo>(7);
 1042  3
             for (ReqComponentInfo reqComponentInfo : statementTreeViewInfo.getReqComponents()) {
 1043  4
                 String reqComponentId = reqComponentInfo.getId();
 1044  4
                 ReqComponentInfo origReqComponentInfo = null;
 1045  4
                 ReqComponentInfo rcAfterInsert = null;
 1046  
                 // determine the original reqComponentInfo
 1047  4
                 if (reqComponentId != null) {
 1048  
                     try {
 1049  0
                         origReqComponentInfo = getReqComponent(reqComponentId);
 1050  0
                     } catch (DoesNotExistException dnee) {
 1051  0
                         origReqComponentInfo = null;
 1052  0
                     }
 1053  
                 }
 1054  4
                 if (origReqComponentInfo == null) {
 1055  
                     // The reqComponentInfo is a new one so create it
 1056  
                     // the id here even if it is not null it is the temporary ids assigned by client
 1057  
                     // so resets the id to null to allow a new id to be generated.
 1058  4
                     reqComponentInfo.setId(null);
 1059  
                     try {
 1060  4
                         rcAfterInsert = createReqComponent(reqComponentInfo.getType(), reqComponentInfo);
 1061  0
                     } catch (AlreadyExistsException e) {
 1062  
                         // shouldn't happen because of all the check that has been done up to this point
 1063  
                         // if this exception is thrown it should be an error!
 1064  0
                         throw new OperationFailedException("Tried to create a reqComponent that already exists");
 1065  4
                     }
 1066  
                 } else {
 1067  0
                     rcAfterInsert = reqComponentInfo;
 1068  
                 }
 1069  4
                 rcsAfterInserts.add(rcAfterInsert);
 1070  4
             }
 1071  3
             statementTreeViewInfo.setReqComponents(rcsAfterInserts);
 1072  
         }
 1073  
         // check if statementTreeViewInfo already exist if not create it.
 1074  3
         statementId = statementTreeViewInfo.getId();
 1075  3
         if (statementId != null) {
 1076  
             try {
 1077  0
                 origStatementInfo = getStatement(statementId);
 1078  0
             } catch(DoesNotExistException dnee) {
 1079  0
                 origStatementInfo = null;
 1080  0
             }
 1081  
         }
 1082  3
         if (origStatementInfo == null) {
 1083  
             // the id here even if it is not null it is the temporary ids assigned by client
 1084  
             // so resets the id to null to allow a new id to be generated.
 1085  
 //            statementTreeViewInfo.setId(null);
 1086  3
             newStatementInfo = statementAssembler.toStatementInfo(statementTreeViewInfo);
 1087  
             try {
 1088  3
                 newStatementInfo = createStatement(newStatementInfo.getType(), newStatementInfo);
 1089  0
             } catch (AlreadyExistsException e) {
 1090  
                 // shouldn't happen because of all the check that has been done up to this point
 1091  
                 // if this exception is thrown it should be an error!
 1092  0
                 throw new OperationFailedException("Tried to create a statement that already exists");
 1093  3
             }
 1094  3
             statementAssembler.copyValues(statementTreeViewInfo, newStatementInfo);
 1095  
         }
 1096  3
     }
 1097  
 
 1098  
     /**
 1099  
      *
 1100  
      * @return a list of relationships in the first list but not in the second
 1101  
      */
 1102  
         @Override
 1103  
         public RefStatementRelationTypeInfo getRefStatementRelationType(final String refStatementRelationTypeKey)
 1104  
                         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 1105  1
                 checkForNullOrEmptyParameter(refStatementRelationTypeKey, "refStatementRelationTypeKey");
 1106  
 
 1107  1
                 RefStatementRelationType type = this.statementDao.fetch(RefStatementRelationType.class, refStatementRelationTypeKey);
 1108  
 
 1109  1
                 return StatementAssembler.toRefStatementRelationTypeInfo(type);
 1110  
         }
 1111  
 
 1112  
         @Override
 1113  
         public List<RefStatementRelationTypeInfo> getRefStatementRelationTypes()
 1114  
                         throws OperationFailedException {
 1115  1
                 List<RefStatementRelationType> entities = this.statementDao.find(RefStatementRelationType.class);
 1116  1
                 return StatementAssembler.toRefStatementRelationTypeInfos(entities);
 1117  
         }
 1118  
 
 1119  
         @Override
 1120  
         public List<String> getRefStatementRelationTypesForRefObjectSubType(final String refSubTypeKey)
 1121  
                 throws DoesNotExistException,InvalidParameterException, MissingParameterException, OperationFailedException {
 1122  0
                 throw new UnsupportedOperationException("Method not yet implemented!");
 1123  
         }
 1124  
 
 1125  
         @Override
 1126  
         public List<String> getStatementTypesForRefStatementRelationType(final String refStatementRelationTypeKey)
 1127  
                 throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 1128  0
                 throw new UnsupportedOperationException("Method not yet implemented!");
 1129  
         }
 1130  
 
 1131  
         public void setValidatorFactory(ValidatorFactory validatorFactory) {
 1132  2
                 this.validatorFactory = validatorFactory;
 1133  2
         }
 1134  
 }