Coverage Report - org.kuali.student.core.statement.service.impl.StatementAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
StatementAssembler
66%
213/322
51%
68/132
3.714
 
 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.List;
 21  
 import java.util.Map;
 22  
 import java.util.Set;
 23  
 
 24  
 import org.kuali.student.core.dictionary.old.dto.FieldDescriptor;
 25  
 import org.kuali.student.core.exceptions.DoesNotExistException;
 26  
 import org.kuali.student.core.exceptions.InvalidParameterException;
 27  
 import org.kuali.student.core.exceptions.MissingParameterException;
 28  
 import org.kuali.student.core.exceptions.OperationFailedException;
 29  
 import org.kuali.student.core.exceptions.VersionMismatchException;
 30  
 import org.kuali.student.core.service.impl.BaseAssembler;
 31  
 import org.kuali.student.core.statement.dao.StatementDao;
 32  
 import org.kuali.student.core.statement.dto.AbstractStatementInfo;
 33  
 import org.kuali.student.core.statement.dto.NlUsageTypeInfo;
 34  
 import org.kuali.student.core.statement.dto.RefStatementRelationInfo;
 35  
 import org.kuali.student.core.statement.dto.RefStatementRelationTypeInfo;
 36  
 import org.kuali.student.core.statement.dto.ReqCompFieldInfo;
 37  
 import org.kuali.student.core.statement.dto.ReqCompFieldTypeInfo;
 38  
 import org.kuali.student.core.statement.dto.ReqComponentInfo;
 39  
 import org.kuali.student.core.statement.dto.ReqComponentTypeInfo;
 40  
 import org.kuali.student.core.statement.dto.StatementInfo;
 41  
 import org.kuali.student.core.statement.dto.StatementTreeViewInfo;
 42  
 import org.kuali.student.core.statement.dto.StatementTypeInfo;
 43  
 import org.kuali.student.core.statement.entity.NlUsageType;
 44  
 import org.kuali.student.core.statement.entity.ObjectSubType;
 45  
 import org.kuali.student.core.statement.entity.ObjectType;
 46  
 import org.kuali.student.core.statement.entity.OrderedStatementType;
 47  
 import org.kuali.student.core.statement.entity.RefStatementRelation;
 48  
 import org.kuali.student.core.statement.entity.RefStatementRelationAttribute;
 49  
 import org.kuali.student.core.statement.entity.RefStatementRelationType;
 50  
 import org.kuali.student.core.statement.entity.ReqComponent;
 51  
 import org.kuali.student.core.statement.entity.ReqComponentField;
 52  
 import org.kuali.student.core.statement.entity.ReqComponentFieldType;
 53  
 import org.kuali.student.core.statement.entity.ReqComponentType;
 54  
 import org.kuali.student.core.statement.entity.OrderedReqComponentType;
 55  
 import org.kuali.student.core.statement.entity.Statement;
 56  
 import org.kuali.student.core.statement.entity.StatementAttribute;
 57  
 import org.kuali.student.core.statement.entity.StatementRichText;
 58  
 import org.kuali.student.core.statement.entity.StatementType;
 59  
 import org.kuali.student.core.statement.naturallanguage.NaturalLanguageTranslator;
 60  
 import org.springframework.beans.BeanUtils;
 61  
 
 62  1
 public class StatementAssembler extends BaseAssembler {
 63  
 
 64  
         private StatementDao statementDao;
 65  
         private NaturalLanguageTranslator naturalLanguageTranslator;
 66  
 
 67  
         public void setStatementDao(StatementDao dao) {
 68  1
                 this.statementDao = dao;
 69  1
         }
 70  
 
 71  
         public void setNaturalLanguageTranslator(final NaturalLanguageTranslator translator) {
 72  1
                 this.naturalLanguageTranslator = translator;
 73  1
         }
 74  
 
 75  
         public static List<RefStatementRelationInfo> toRefStatementRelationInfos(List<RefStatementRelation> entities) {
 76  1
                 List<RefStatementRelationInfo> list = new ArrayList<RefStatementRelationInfo>();
 77  1
                 for(RefStatementRelation entity : entities) {
 78  1
                         list.add(toRefStatementRelationInfo(entity));
 79  
                 }
 80  1
                 return list;
 81  
         }
 82  
 
 83  
         public static RefStatementRelationInfo toRefStatementRelationInfo(RefStatementRelation entity) {
 84  9
                 RefStatementRelationInfo dto = new RefStatementRelationInfo();
 85  
 
 86  9
         BeanUtils.copyProperties(entity, dto, new String[]{
 87  
                         "refStatementRelationType", "statement", "attributes", "metaInfo"});
 88  
 
 89  
         // Copy generic attributes
 90  9
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 91  9
         dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
 92  9
         dto.setStatementId(entity.getStatement().getId());
 93  9
         dto.setType(entity.getRefStatementRelationType().getId());
 94  
         //dto.setRefObjectTypeKey(entity.getRefStatementRelationType().getObjectSubTypeList().get(0).g)
 95  
 
 96  9
         return dto;
 97  
         }
 98  
 
 99  
         public static List<RefStatementRelationTypeInfo> toRefStatementRelationTypeInfos(List<RefStatementRelationType> entities) {
 100  1
                 List<RefStatementRelationTypeInfo> list = new ArrayList<RefStatementRelationTypeInfo>();
 101  1
                 for(RefStatementRelationType entity : entities) {
 102  2
                         list.add(toRefStatementRelationTypeInfo(entity));
 103  
                 }
 104  1
                 return list;
 105  
         }
 106  
 
 107  
         public static RefStatementRelationTypeInfo toRefStatementRelationTypeInfo(RefStatementRelationType entity) {
 108  3
                 RefStatementRelationTypeInfo dto = new RefStatementRelationTypeInfo();
 109  
 
 110  3
         BeanUtils.copyProperties(entity, dto, new String[]{
 111  
                         "attributes", "metaInfo", "objectSubTypeList", "statementTypeList"});
 112  
 
 113  3
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 114  3
         dto.setDesc(entity.getDescr());
 115  
 
 116  3
         return dto;
 117  
         }
 118  
 
 119  
         public static List<String> toRefObjectSubTypeIds(ObjectType objectType) {
 120  1
                 List<String> ids = new ArrayList<String>();
 121  1
                 for(ObjectSubType objectSubType : objectType.getObjectSubTypes()) {
 122  2
                         ids.add(objectSubType.getId());
 123  
                 }
 124  1
                 return ids;
 125  
         }
 126  
 
 127  
         public static NlUsageTypeInfo toNlUsageTypeInfo(NlUsageType entity) throws OperationFailedException {
 128  14
                 NlUsageTypeInfo info = toGenericTypeInfo(NlUsageTypeInfo.class, entity);
 129  14
                 return info;
 130  
         }
 131  
 
 132  
         public static List<NlUsageTypeInfo> toNlUsageTypeInfos(List<NlUsageType> entities) throws OperationFailedException {
 133  1
                 List<NlUsageTypeInfo> infoList = new ArrayList<NlUsageTypeInfo>();
 134  1
                 for(NlUsageType entity : entities) {
 135  5
                         NlUsageTypeInfo info = toNlUsageTypeInfo(entity);
 136  5
                         infoList.add(info);
 137  5
                 }
 138  1
                 return infoList;
 139  
         }
 140  
 
 141  
     public ReqComponent toReqComponentRelation(boolean isUpdate, ReqComponentInfo reqCompInfo)
 142  
                     throws DoesNotExistException, VersionMismatchException, InvalidParameterException {
 143  
         ReqComponent reqComp;
 144  13
         if (isUpdate) {
 145  7
             reqComp = this.statementDao.fetch(ReqComponent.class, reqCompInfo.getId());
 146  7
             if (reqComp == null) {
 147  0
                 throw new DoesNotExistException("ReqComponent does not exist for id: " + reqCompInfo.getId());
 148  
             }
 149  7
             if (!String.valueOf(reqComp.getVersionNumber()).equals(reqCompInfo.getMetaInfo().getVersionInd())) {
 150  1
                 throw new VersionMismatchException("ReqComponent to be updated is not the current version");
 151  
             }
 152  6
             for(ReqComponentField reqCompField : reqComp.getReqComponentFields()) {
 153  0
                     this.statementDao.delete(reqCompField);
 154  
             }
 155  
         } else {
 156  6
             reqComp = new ReqComponent();
 157  
         }
 158  
 
 159  12
         BeanUtils.copyProperties(reqCompInfo, reqComp, new String[]{"requiredComponentType", "reqCompField", "metaInfo", "type"});
 160  
 
 161  
         // Search for and copy the type
 162  12
         ReqComponentType reqCompType = this.statementDao.fetch(ReqComponentType.class, reqCompInfo.getType());
 163  12
         if (reqCompType == null) {
 164  0
             throw new InvalidParameterException(
 165  
                     "ReqComponentType does not exist for id: " + reqCompInfo.getType());
 166  
         }
 167  12
         reqComp.setRequiredComponentType(reqCompType);
 168  
 
 169  
         // Create and copy ReqCompFields
 170  12
         List<ReqComponentField> reqCompFieldList = new ArrayList<ReqComponentField>();
 171  12
         for(ReqCompFieldInfo reqCompFieldInfo : reqCompInfo.getReqCompFields()) {
 172  4
             ReqComponentField reqCompField = new ReqComponentField();
 173  
             //reqCompField.setId(reqCompFieldInfo.getId());
 174  4
             reqCompField.setType(reqCompFieldInfo.getType());
 175  4
             reqCompField.setValue(reqCompFieldInfo.getValue());
 176  4
             reqCompFieldList.add(reqCompField);
 177  4
         }
 178  12
         reqComp.setReqComponentFields(reqCompFieldList);
 179  
 
 180  12
         reqComp.setDescr(toRichText(StatementRichText.class, reqCompInfo.getDesc()));
 181  
 
 182  12
         return reqComp;
 183  
     }
 184  
 
 185  
     public List<ReqComponentInfo> toReqComponentInfos(List<ReqComponent> entities, String nlUsageTypeKey, String language) throws DoesNotExistException, OperationFailedException {
 186  2
         List<ReqComponentInfo> dtos = new ArrayList<ReqComponentInfo>(
 187  
                 entities.size());
 188  2
         for (ReqComponent entity : entities) {
 189  1
             dtos.add(toReqComponentInfo(entity, nlUsageTypeKey, language));
 190  
         }
 191  2
         return dtos;
 192  
 
 193  
     }
 194  
 
 195  
     public static ReqComponentInfo toReqComponentInfo(ReqComponent entity) {
 196  95
         ReqComponentInfo dto = new ReqComponentInfo();
 197  
 
 198  95
         BeanUtils.copyProperties(entity, dto, new String[] {
 199  
                 "requiredComponentType", "reqCompField", "metaInfo" });
 200  
 
 201  95
         dto.setType(entity.getRequiredComponentType().getId());
 202  95
         dto.setReqCompFields(toReqCompFieldInfos(entity.getReqComponentFields()));
 203  
         //dto.setRequiredComponentType(toReqComponentTypeInfo(entity.getRequiredComponentType()));
 204  95
         dto.setMetaInfo(toMetaInfo(entity));
 205  95
         dto.setDesc(toRichTextInfo(entity.getDescr()));
 206  
 //        if(nlUsageTypeKey != null && language != null) {
 207  
 //                String nl = this.naturalLanguageTranslator.translateReqComponent(entity, nlUsageTypeKey, language);
 208  
 //                dto.setNaturalLanguageTranslation(nl);
 209  
 //        }
 210  95
         return dto;
 211  
     }
 212  
 
 213  
     public ReqComponentInfo toReqComponentInfo(ReqComponent entity, String nlUsageTypeKey, String language) throws DoesNotExistException, OperationFailedException {
 214  24
             ReqComponentInfo dto = toReqComponentInfo(entity);
 215  24
             if(nlUsageTypeKey != null && language != null) {
 216  0
                 String nl = this.naturalLanguageTranslator.translateReqComponent(entity, nlUsageTypeKey, language);
 217  0
                 dto.setNaturalLanguageTranslation(nl);
 218  
         }
 219  24
         return dto;
 220  
     }
 221  
 
 222  
     public static List<ReqComponentTypeInfo> toReqComponentTypeInfos(List<ReqComponentType> entities) {
 223  1
         List<ReqComponentTypeInfo> dtos = new ArrayList<ReqComponentTypeInfo>(entities.size());
 224  1
         for (ReqComponentType entity : entities) {
 225  9
             dtos.add(toReqComponentTypeInfo(entity));
 226  
         }
 227  1
         return dtos;
 228  
 
 229  
     }
 230  
 
 231  
     public static List<ReqComponentTypeInfo> toReqComponentTypeInfosOrdered(List<OrderedReqComponentType> entities) {
 232  1
         List<ReqComponentTypeInfo> dtos = new ArrayList<ReqComponentTypeInfo>(entities.size());
 233  1
         for (OrderedReqComponentType entity : entities) {
 234  6
             dtos.add(toReqComponentTypeInfo(entity.getReqComponentType()));
 235  
         }
 236  1
         return dtos;
 237  
 
 238  
     }
 239  
 
 240  
     public static ReqComponentTypeInfo toReqComponentTypeInfo(ReqComponentType entity) {
 241  17
         ReqComponentTypeInfo dto = toGenericTypeInfo(ReqComponentTypeInfo.class, entity);
 242  17
         dto.setReqCompFieldTypeInfos(toReqCompFieldTypeInfos(entity.getReqCompFieldTypes()));
 243  17
         dto.setDescr(entity.getDescr());
 244  17
         return dto;
 245  
     }
 246  
 
 247  
     public static List<ReqCompFieldTypeInfo> toReqCompFieldTypeInfos(
 248  
             List<ReqComponentFieldType> entities) {
 249  17
         List<ReqCompFieldTypeInfo> dtos = new ArrayList<ReqCompFieldTypeInfo>(
 250  
                 entities.size());
 251  17
         for (ReqComponentFieldType entity : entities) {
 252  22
             dtos.add(toReqCompFieldTypeInfo(entity));
 253  
         }
 254  17
         return dtos;
 255  
     }
 256  
 
 257  
     public static ReqCompFieldTypeInfo toReqCompFieldTypeInfo(ReqComponentFieldType entity) {
 258  22
         ReqCompFieldTypeInfo dto = new ReqCompFieldTypeInfo();
 259  
 
 260  22
         BeanUtils.copyProperties(entity, dto, new String[] { "fieldDescriptor" });
 261  
 
 262  22
         FieldDescriptor fDTO = new FieldDescriptor();
 263  22
         BeanUtils.copyProperties(entity.getFieldDescriptor(), fDTO);
 264  22
         fDTO.setDesc(entity.getFieldDescriptor().getDescr());
 265  22
         dto.setFieldDescriptor(fDTO);
 266  
 
 267  22
         return dto;
 268  
     }
 269  
 
 270  
     public static List<ReqCompFieldInfo> toReqCompFieldInfos(List<ReqComponentField> entities) {
 271  95
         if(entities == null) {
 272  1
                 return null;
 273  
         }
 274  94
             List<ReqCompFieldInfo> dtos = new ArrayList<ReqCompFieldInfo>(entities.size());
 275  94
         for (ReqComponentField entity : entities) {
 276  199
             dtos.add(toReqCompFieldInfo(entity));
 277  
         }
 278  94
         return dtos;
 279  
     }
 280  
 
 281  
     public static ReqCompFieldInfo toReqCompFieldInfo(ReqComponentField entity) {
 282  199
         if (null == entity) {
 283  0
             return null;
 284  
         }
 285  
 
 286  199
         ReqCompFieldInfo dto = new ReqCompFieldInfo();
 287  199
         dto.setId(entity.getId());
 288  199
         dto.setType(entity.getType());
 289  199
         dto.setValue(entity.getValue());
 290  199
         return dto;
 291  
     }
 292  
 
 293  
     public RefStatementRelation toRefStatementRelation(boolean isUpdate, RefStatementRelationInfo refStatementRelationInfo)
 294  
                     throws DoesNotExistException, VersionMismatchException, InvalidParameterException {
 295  
             RefStatementRelation refStatement;
 296  3
         if (isUpdate) {
 297  3
             refStatement = this.statementDao.fetch(RefStatementRelation.class, refStatementRelationInfo.getId());
 298  3
             if (refStatement == null) {
 299  0
                 throw new DoesNotExistException("RefStatementRelation does not exist for id: " + refStatementRelationInfo.getId());
 300  
             }
 301  3
             if (!String.valueOf(refStatement.getVersionNumber()).equals(refStatementRelationInfo.getMetaInfo().getVersionInd())) {
 302  0
                 throw new VersionMismatchException("RefStatementRelation to be updated is not the current version");
 303  
             }
 304  
         } else {
 305  0
             refStatement = new RefStatementRelation();
 306  
         }
 307  
 
 308  3
         BeanUtils.copyProperties(refStatementRelationInfo, refStatement, new String[]{
 309  
                         "attributes", "metaInfo", "type", "statementId"});
 310  
 
 311  
         // make sure refObjectType exist
 312  3
         this.statementDao.fetch(ObjectType.class, refStatementRelationInfo.getRefObjectTypeKey());
 313  
 
 314  
         // Copy generic attributes
 315  2
         refStatement.setAttributes(toGenericAttributes(RefStatementRelationAttribute.class, refStatementRelationInfo.getAttributes(), refStatement, this.statementDao));
 316  2
         RefStatementRelationType type = this.statementDao.fetch(RefStatementRelationType.class, refStatementRelationInfo.getType());
 317  
 
 318  1
         refStatement.setRefStatementRelationType(type);
 319  1
         Statement statement = this.statementDao.fetch(Statement.class, refStatementRelationInfo.getStatementId());
 320  1
         refStatement.setStatement(statement);
 321  
 
 322  1
         return refStatement;
 323  
     }
 324  
 
 325  
     public Statement toStatementFromTree(Statement stmt, StatementTreeViewInfo treeView, Set<String> statementIdsToDelete, List<Statement> statementsToUpdate, List<ReqComponent> reqCompsToCreate) throws DoesNotExistException, VersionMismatchException, InvalidParameterException{
 326  
 
 327  0
             BeanUtils.copyProperties(treeView, stmt, new String[]{"cluIds", "statementIds",
 328  
                  "reqComponentIds", "attributes", "metaInfo", "type",
 329  
                  "parent", "children", "requiredComponents", "statementType"});
 330  
 
 331  
         // Copy generic attributes
 332  0
         stmt.setAttributes(toGenericAttributes(StatementAttribute.class, treeView.getAttributes(), stmt, this.statementDao));
 333  
 
 334  
         // Search for and copy the type
 335  0
         StatementType stmtType = this.statementDao.fetch(StatementType.class, treeView.getType());
 336  0
         if (stmtType == null) {
 337  0
             throw new InvalidParameterException(
 338  
                     "StatementType does not exist for id: " + treeView.getType());
 339  
         }
 340  0
         stmt.setStatementType(stmtType);
 341  
          
 342  
         // Copy nested requirements
 343  0
         List<ReqComponent> reqCompList = new ArrayList<ReqComponent>();
 344  0
         for(ReqComponentInfo reqComponent: treeView.getReqComponents()) {
 345  
             
 346  0
                 ReqComponent reqComp = null;
 347  0
                 if(reqComponent.getId()!=null){
 348  
                         try{
 349  0
                                  reqComp = this.statementDao.fetch(ReqComponent.class, reqComponent.getId());
 350  0
                         }catch(DoesNotExistException e){}
 351  
                 }
 352  0
             if(null == reqComp) {
 353  0
                     reqComp = toReqComponentRelation(false,reqComponent);
 354  0
                     reqCompsToCreate.add(reqComp);
 355  
             }
 356  0
             reqCompList.add(reqComp);
 357  0
         }
 358  0
         stmt.setRequiredComponents(reqCompList);
 359  
 
 360  0
         stmt.setDescr(toRichText(StatementRichText.class, treeView.getDesc()));
 361  
         
 362  0
         Map<String,Statement> stmtsToDelete = new HashMap<String,Statement>();
 363  0
         if(stmt.getChildren()!=null){
 364  0
                 for(Statement childStmt: stmt.getChildren()){
 365  0
                         stmtsToDelete.put(childStmt.getId(),childStmt);
 366  
                 }
 367  0
                 stmt.getChildren().clear();
 368  
         }else{
 369  0
                 stmt.setChildren(new ArrayList<Statement>());
 370  
         }
 371  0
         for(StatementTreeViewInfo childTreeView:treeView.getStatements()){
 372  
                 Statement childStmt;
 373  0
                 if(childTreeView.getId()!=null&&stmtsToDelete.containsKey(childTreeView.getId())){
 374  0
                         childStmt = stmtsToDelete.remove(childTreeView.getId());
 375  
                 }else{
 376  0
                         childStmt = new Statement();
 377  
                 }
 378  0
                 childStmt = toStatementFromTree(childStmt, childTreeView, statementIdsToDelete, statementsToUpdate, reqCompsToCreate);
 379  0
                     stmt.getChildren().add(childStmt);
 380  0
         }
 381  0
         for(Statement statementToDelete:stmtsToDelete.values()){
 382  0
                 deleteStatementsRecursively(statementToDelete,statementIdsToDelete,statementsToUpdate);
 383  
         }
 384  0
         statementIdsToDelete.addAll(stmtsToDelete.keySet());
 385  0
             return stmt;
 386  
     }
 387  
     
 388  
     //Recursively parse through a statement tree, clear all children and if the children were cleared add to the list of statements to be updated
 389  
     //Then add the statement id to the list of statements to be deleted so they can be updated and deleted separately.
 390  
     private void deleteStatementsRecursively(Statement statementToDelete,
 391  
                         Set<String> statementIdsToDelete, List<Statement> statementsToUpdate) {
 392  0
             if(statementToDelete.getChildren()!=null){
 393  0
                         for(Statement childStatement:statementToDelete.getChildren()){
 394  0
                                 deleteStatementsRecursively(childStatement, statementIdsToDelete, statementsToUpdate);
 395  
                         }
 396  0
                         statementToDelete.getChildren().clear();
 397  0
                         statementsToUpdate.add(statementToDelete);
 398  
             }
 399  0
             statementIdsToDelete.add(statementToDelete.getId());
 400  0
         }
 401  
 
 402  
         public Statement toStatementRelation(boolean isUpdate, StatementInfo stmtInfo) throws DoesNotExistException, VersionMismatchException, InvalidParameterException, OperationFailedException {
 403  
         Statement stmt;
 404  9
         if (isUpdate) {
 405  5
             stmt = this.statementDao.fetch(Statement.class, stmtInfo.getId());
 406  5
             if (stmt == null) {
 407  0
                 throw new DoesNotExistException("Statement does not exist for id: " + stmtInfo.getId());
 408  
             }
 409  5
             if (!String.valueOf(stmt.getVersionNumber()).equals(stmtInfo.getMetaInfo().getVersionInd())) {
 410  1
                 throw new VersionMismatchException("Statement to be updated is not the current version");
 411  
             }
 412  
         } else {
 413  4
             stmt = new Statement();
 414  
         }
 415  
 
 416  8
         BeanUtils.copyProperties(stmtInfo, stmt, new String[]{"cluIds", "statementIds",
 417  
                 "reqComponentIds", "attributes", "metaInfo", "type",
 418  
                 "parent", "children", "requiredComponents", "statementType"});
 419  
 
 420  
         // Copy generic attributes
 421  8
         stmt.setAttributes(toGenericAttributes(StatementAttribute.class, stmtInfo.getAttributes(), stmt, this.statementDao));
 422  
 
 423  
         // Search for and copy the type
 424  8
         StatementType stmtType = this.statementDao.fetch(StatementType.class, stmtInfo.getType());
 425  8
         if (stmtType == null) {
 426  0
             throw new InvalidParameterException(
 427  
                     "StatementType does not exist for id: " + stmtInfo.getType());
 428  
         }
 429  8
         stmt.setStatementType(stmtType);
 430  
 
 431  
         // Copy nested statements
 432  
         if (false) {
 433  
                 List<Statement> stmtList = new ArrayList<Statement>(stmtInfo.getStatementIds().size());
 434  
                 for(String stmtId : stmtInfo.getStatementIds()) {
 435  
                         if(stmtId.equals(stmtInfo.getId())) {
 436  
                                 throw new OperationFailedException("Statement nested within itself. Statement Id: " + stmtInfo.getId());
 437  
                         }
 438  
 
 439  
                         boolean exists = false;
 440  
                         if (stmt.getChildren() != null) {
 441  
                                 for (Statement child : stmt.getChildren()) {
 442  
                                         if (child.getId().equals(stmtId)) {
 443  
                                                 stmtList.add(child);
 444  
                                                 exists = true;
 445  
                                                 break;
 446  
                                         }
 447  
                                 }
 448  
                         }
 449  
                         if (!exists) {
 450  
                                 try {
 451  
                                         Statement nestedStmt = this.statementDao.fetch(Statement.class, stmtId);
 452  
                                         stmtList.add(nestedStmt);
 453  
                                 } catch (DoesNotExistException e) {
 454  
                                         throw new DoesNotExistException("Nested Statement does not exist for id: " + stmtId + ". Parent Statement: " + stmtInfo.getId(), e);
 455  
                                 }
 456  
                         }
 457  
                 }
 458  
                 if (stmt.getChildren() != null) {
 459  
                         stmt.getChildren().clear();
 460  
                         stmt.getChildren().addAll(stmtList);
 461  
                 } else {
 462  
                         stmt.setChildren(stmtList);
 463  
                 }
 464  
 
 465  
         } else {
 466  8
                 List<Statement> stmtList = new ArrayList<Statement>();
 467  8
                 for(String stmtId : stmtInfo.getStatementIds()) {
 468  5
                         if(stmtId.equals(stmtInfo.getId())) {
 469  0
                                 throw new OperationFailedException("Statement nested within itself. Statement Id: " + stmtInfo.getId());
 470  
                         }
 471  
 
 472  5
                         Statement nestedStmt = this.statementDao.fetch(Statement.class, stmtId);
 473  5
                         if (null == nestedStmt) {
 474  0
                                 throw new DoesNotExistException("Nested Statement does not exist for id: " + stmtId + ". Parent Statement: " + stmtInfo.getId());
 475  
                         }
 476  
 
 477  5
                         stmtList.add(nestedStmt);
 478  5
                 }
 479  8
                 stmt.setChildren(stmtList);
 480  
         }
 481  
 
 482  
         // Copy nested requirements
 483  8
         List<ReqComponent> reqCompList = new ArrayList<ReqComponent>();
 484  8
         for(String reqId: stmtInfo.getReqComponentIds()) {
 485  8
             ReqComponent reqComp = this.statementDao.fetch(ReqComponent.class, reqId);
 486  
 
 487  8
             if(null == reqComp) {
 488  0
                 throw new DoesNotExistException("Nested Requirement does not exist for id: " + reqId + ". Parent Statement Id: " + stmtInfo.getId());
 489  
             }
 490  
 
 491  8
             reqCompList.add(reqComp);
 492  8
         }
 493  8
         stmt.setRequiredComponents(reqCompList);
 494  
 
 495  8
         stmt.setDescr(toRichText(StatementRichText.class, stmtInfo.getDesc()));
 496  
 
 497  8
         return stmt;
 498  
     }
 499  
 
 500  
     public static StatementInfo toStatementInfo(Statement entity) {
 501  39
         if(entity==null){
 502  0
             return null;
 503  
         }
 504  39
         StatementInfo dto = new StatementInfo();
 505  
 
 506  39
         BeanUtils.copyProperties(entity, dto, new String[] { "parent", "children",
 507  
                 "requiredComponents", "statementType", "attributes", "metaInfo" });
 508  
 
 509  39
         List<String> statementIds = new ArrayList<String>(entity.getChildren().size());
 510  39
         for (Statement statement : entity.getChildren()) {
 511  22
             statementIds.add(statement.getId());
 512  
         }
 513  39
         dto.setStatementIds(statementIds);
 514  
 
 515  39
         List<String> componentIds = new ArrayList<String>(entity.getRequiredComponents().size());
 516  39
         for (ReqComponent reqComponent : entity.getRequiredComponents()) {
 517  50
             componentIds.add(reqComponent.getId());
 518  
         }
 519  39
         dto.setReqComponentIds(componentIds);
 520  39
         dto.setType(entity.getStatementType().getId());
 521  39
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 522  39
         dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
 523  39
         dto.setName(entity.getName());
 524  39
         dto.setOperator(entity.getOperator());
 525  39
         dto.setDesc(toRichTextInfo(entity.getDescr()));
 526  39
         return dto;
 527  
     }
 528  
 
 529  
     public static List<StatementInfo> toStatementInfos(
 530  
             List<Statement> entities) {
 531  4
         List<StatementInfo> dtos = new ArrayList<StatementInfo>(entities
 532  
                 .size());
 533  4
         for (Statement entity : entities) {
 534  19
             dtos.add(toStatementInfo(entity));
 535  
         }
 536  4
         return dtos;
 537  
 
 538  
     }
 539  
 
 540  
     public static List<StatementTypeInfo> toStatementTypeInfos(List<StatementType> entities) {
 541  1
             List<StatementTypeInfo> list = new ArrayList<StatementTypeInfo>();
 542  1
             for(StatementType type : entities) {
 543  3
                     StatementTypeInfo dto = toStatementTypeInfo(type);
 544  3
                     list.add(dto);
 545  3
             }
 546  1
             return list;
 547  
     }
 548  
 
 549  
     public static StatementTypeInfo toStatementTypeInfo(StatementType entity) {
 550  5
         if(entity==null){
 551  0
             return null;
 552  
         }
 553  5
         StatementTypeInfo stmtTypeInfo = toGenericTypeInfo(StatementTypeInfo.class, entity);
 554  
 
 555  
         // Copy allowed RequiredComponent Types
 556  5
         List<String> reqTypeIds = new ArrayList<String>(entity.getAllowedReqComponentTypes().size());
 557  5
         for (OrderedReqComponentType reqComponentTypeOrder : entity.getAllowedReqComponentTypes()) {
 558  9
             reqTypeIds.add(reqComponentTypeOrder.getReqComponentType().getId());
 559  
         }
 560  5
         stmtTypeInfo.setAllowedReqComponentTypes(reqTypeIds);
 561  
 
 562  
         // Copy allowed Statement Types
 563  5
         List<String> stmtIds = new ArrayList<String>(entity.getAllowedStatementTypes().size());
 564  5
         for (OrderedStatementType stmtType : entity.getAllowedStatementTypes()) {
 565  6
             stmtIds.add(stmtType.getChildStatementType().getId());
 566  
         }
 567  5
         stmtTypeInfo.setAllowedStatementTypes(stmtIds);
 568  
 
 569  
         // statement type header is no longer defined in specification
 570  
         //stmtTypeInfo.setHeaders(toStatementTypeHeaderTemplateInfos(entity.getHeaders()));
 571  
 
 572  5
         stmtTypeInfo.setDescr(entity.getDescr());
 573  
 
 574  5
         return stmtTypeInfo;
 575  
     }
 576  
 
 577  
     public StatementInfo toStatementInfo(final StatementTreeViewInfo statementTreeViewInfo) {
 578  6
         StatementInfo statementInfo = null;
 579  6
         if (statementTreeViewInfo == null) return null;
 580  6
         statementInfo = new StatementInfo();
 581  6
         copyValues(statementInfo, statementTreeViewInfo);
 582  
         // goes through the list of reqComponents in statementTreeViewInfo and extract the reqComponent ids
 583  6
         if (statementTreeViewInfo.getReqComponents() != null) {
 584  6
             List<String> reqCompIds = new ArrayList<String>(7);
 585  6
             for (ReqComponentInfo reqComponentInfo : statementTreeViewInfo.getReqComponents()) {
 586  8
                 reqCompIds.add(reqComponentInfo.getId());
 587  
             }
 588  6
             statementInfo.setReqComponentIds(reqCompIds);
 589  
         }
 590  6
         statementInfo.setState(statementTreeViewInfo.getState());
 591  
         // TODO goes through the list of statements in statementTreeViewInfo and extract the statement ids
 592  6
         if (statementTreeViewInfo.getStatements() != null) {
 593  6
             List<String> statementIds = new ArrayList<String>(7);
 594  6
             for (StatementTreeViewInfo subStatementTreeViewInfo : statementTreeViewInfo.getStatements()) {
 595  4
                 statementIds.add(subStatementTreeViewInfo.getId());
 596  
             }
 597  6
             statementInfo.setStatementIds(statementIds);
 598  
         }
 599  6
         statementInfo.setType(statementTreeViewInfo.getType());
 600  6
         return statementInfo;
 601  
     }
 602  
 
 603  
     /**
 604  
      * copies the values in statementInfo into statementTreeViewInfo.  Only the values of the root statement will
 605  
      * be affected.
 606  
      * @param toStatementInfo
 607  
      * @param fromStatementInfo
 608  
      */
 609  
     public void copyValues(final AbstractStatementInfo toStatementInfo, AbstractStatementInfo fromStatementInfo) {
 610  18
         toStatementInfo.setAttributes(fromStatementInfo.getAttributes());
 611  18
         toStatementInfo.setDesc(fromStatementInfo.getDesc());
 612  18
         toStatementInfo.setId(fromStatementInfo.getId());
 613  18
         toStatementInfo.setMetaInfo(fromStatementInfo.getMetaInfo());
 614  18
         toStatementInfo.setName(fromStatementInfo.getName());
 615  18
         toStatementInfo.setOperator(fromStatementInfo.getOperator());
 616  18
         toStatementInfo.setState(fromStatementInfo.getState());
 617  18
         toStatementInfo.setType(fromStatementInfo.getType());
 618  18
     }
 619  
 
 620  
     public Statement toStatement(final StatementTreeViewInfo statementTreeViewInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, VersionMismatchException {
 621  0
             return toCustomLuStatementInfo(statementTreeViewInfo);
 622  
     }
 623  
 
 624  
         /**
 625  
          * <p>Converts a {@link LuNlStatementInfo} into a {@link CustomReqComponentInfo}.</p>
 626  
          * <p>Note: LuNlStatementInfo has no id since it is only used for
 627  
          * on-the-fly translations and is not persisted.</p>
 628  
          *
 629  
          * @param statementInfo LuNlStatementInfo to convert
 630  
          * @return A custom LU statement
 631  
          * @throws VersionMismatchException
 632  
          * @throws Exception If conversion fails
 633  
          */
 634  
         public Statement toCustomLuStatementInfo(final StatementTreeViewInfo statementInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, VersionMismatchException {
 635  0
                 Statement stmt = new Statement();
 636  0
         stmt.setName(statementInfo.getName());
 637  0
         stmt.setOperator(statementInfo.getOperator());
 638  
                 //TODO: Fix with LuService RC1.4 changes
 639  
         //LuStatementTypeInfo stmtType = this.luService.getLuStatementType(statementInfo.getStatementTypeId());
 640  
         //stmt.setLuStatementType(stmtType);
 641  0
                 StatementType type = this.statementDao.fetch(StatementType.class, statementInfo.getType());
 642  0
                 stmt.setStatementType(type);
 643  
 
 644  0
         if(statementInfo.getStatements() == null || statementInfo.getStatements().isEmpty()) {
 645  
                 //setReqComponentType(statementInfo.getReqComponents());
 646  0
                 List<ReqComponent> customReqList = toReqComponents(statementInfo.getReqComponents());
 647  0
                 stmt.setRequiredComponents(customReqList);
 648  0
         } else {
 649  0
             createStatement(statementInfo, stmt);
 650  
         }
 651  
 
 652  0
         return stmt;
 653  
         }
 654  
 
 655  
         /*private void setReqComponentType(List<ReqComponentInfo> reqList) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 656  
                 for(ReqComponentInfo req : reqList) {
 657  
                         setReqComponentType(req);
 658  
                 }
 659  
         }
 660  
 
 661  
         private void setReqComponentType(ReqComponentInfo req)
 662  
                         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 663  
                 if(req.getRequiredComponentType() == null) {
 664  
                         //TODO: Fix with LuService RC1.4 changes
 665  
                         ReqComponentType type = this.statementDao.fetch(ReqComponentType.class, req.getType());
 666  
                         req.setRequiredComponentType(toReqComponentTypeInfo(type));
 667  
                 }
 668  
         }*/
 669  
 
 670  
         private void createStatement(final StatementTreeViewInfo stmtInfo, Statement rootLuStatement)
 671  
                         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, VersionMismatchException {
 672  0
                 for(StatementTreeViewInfo luNlStmt : stmtInfo.getStatements()) {
 673  0
                         Statement stmt = new Statement();
 674  0
                 stmt.setName(luNlStmt.getName());
 675  0
                 stmt.setOperator(luNlStmt.getOperator());
 676  0
                         if(luNlStmt.getType() != null) {
 677  
                                 //TODO: Fix with LuService RC1.4 changes
 678  
                         //stmt.setLuStatementType(this.luService.getLuStatementType(luNlStmt.getStatementTypeId()));
 679  0
                                 StatementType type = this.statementDao.fetch(StatementType.class, luNlStmt.getType());
 680  0
                                 stmt.setStatementType(type);
 681  
                         }
 682  
 
 683  0
                 if(rootLuStatement.getChildren() == null) {
 684  0
                         rootLuStatement.setChildren(new ArrayList<Statement>());
 685  
                 }
 686  0
                 rootLuStatement.getChildren().add(stmt);
 687  0
                 if (luNlStmt.getStatements() == null || luNlStmt.getStatements().isEmpty()) {
 688  0
                     List<ReqComponentInfo> children = luNlStmt.getReqComponents();
 689  
                         //setReqComponentType(children);
 690  0
                         List<ReqComponent> customReqList = toReqComponents(children);
 691  0
                     stmt.setRequiredComponents(customReqList);
 692  0
                 } else {
 693  0
                     createStatement(luNlStmt, stmt);
 694  
                 }
 695  0
             }
 696  0
         }
 697  
 
 698  
         private List<ReqComponent> toReqComponents(List<ReqComponentInfo> reqComponentInfoList) throws DoesNotExistException, VersionMismatchException, InvalidParameterException {
 699  0
                 List<ReqComponent> list = new ArrayList<ReqComponent>();
 700  0
                 for(ReqComponentInfo info : reqComponentInfoList) {
 701  0
                         ReqComponent req = toReqComponentRelation(false, info);
 702  0
                         list.add(req);
 703  0
                 }
 704  0
                 return list;
 705  
         }
 706  
 
 707  
         public StatementTreeViewInfo toStatementTreeViewInfo(Statement stmt){
 708  0
                 StatementTreeViewInfo treeView = new StatementTreeViewInfo();
 709  0
                 treeView.setAttributes(toAttributeMap(stmt.getAttributes()));
 710  0
                 treeView.setDesc(toRichTextInfo(stmt.getDescr()));
 711  0
                 treeView.setId(stmt.getId());
 712  0
                 treeView.setMetaInfo(toMetaInfo(stmt.getMeta(), stmt.getVersionNumber()));
 713  0
                 treeView.setName(stmt.getName());
 714  0
                 treeView.setType(stmt.getStatementType().getId());
 715  0
                 treeView.setState(stmt.getState());
 716  0
                 for(ReqComponent reqComp:stmt.getRequiredComponents()){
 717  0
                         treeView.getReqComponents().add(toReqComponentInfo(reqComp));
 718  
                 }
 719  0
                 treeView.setOperator(stmt.getOperator());
 720  0
                 for(Statement childStmt:stmt.getChildren()){
 721  0
                         treeView.getStatements().add(toStatementTreeViewInfo(childStmt));
 722  
                 }
 723  
                 
 724  0
                 return treeView;
 725  
         }
 726  
 }