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