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