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