Coverage Report - org.kuali.student.core.proposal.service.impl.ProposalAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
ProposalAssembler
96%
97/101
78%
33/42
4.571
 
 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.proposal.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import javax.persistence.NoResultException;
 22  
 
 23  
 import org.kuali.student.common.dto.ReferenceTypeInfo;
 24  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 25  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 26  
 import org.kuali.student.common.exceptions.VersionMismatchException;
 27  
 import org.kuali.student.common.service.impl.BaseAssembler;
 28  
 import org.kuali.student.core.proposal.dao.ProposalDao;
 29  
 import org.kuali.student.core.proposal.dto.ProposalInfo;
 30  
 import org.kuali.student.core.proposal.dto.ProposalTypeInfo;
 31  
 import org.kuali.student.core.proposal.entity.Proposal;
 32  
 import org.kuali.student.core.proposal.entity.ProposalAttribute;
 33  
 import org.kuali.student.core.proposal.entity.ProposalOrg;
 34  
 import org.kuali.student.core.proposal.entity.ProposalPerson;
 35  
 import org.kuali.student.core.proposal.entity.ProposalReference;
 36  
 import org.kuali.student.core.proposal.entity.ProposalReferenceType;
 37  
 import org.kuali.student.core.proposal.entity.ProposalType;
 38  
 import org.springframework.beans.BeanUtils;
 39  
 
 40  
 /**
 41  
  * Convert between DTO and enties
 42  
  *
 43  
  * @author Kuali Rice Team (kuali-rice@googlegroups.com)
 44  
  *
 45  
  */
 46  0
 public class ProposalAssembler extends BaseAssembler {
 47  
 
 48  
     public static List<ProposalInfo> toProposalInfos(List<Proposal> entities) {
 49  9
         List<ProposalInfo> dtos = new ArrayList<ProposalInfo>(entities.size());
 50  9
         for (Proposal entity : entities) {
 51  8
             dtos.add(toProposalInfo(entity));
 52  
         }
 53  9
         return dtos;
 54  
     }
 55  
 
 56  
     public static ProposalInfo toProposalInfo(Proposal entity) {
 57  15
         ProposalInfo dto = new ProposalInfo();
 58  
 
 59  
 
 60  15
         BeanUtils.copyProperties(entity, dto,
 61  
                 new String[] { "proposerPerson", "proposerOrg", "proposalReferenceType", "proposalReference", "attributes", "type" });
 62  15
         if (entity.getProposerPerson() != null) {
 63  15
             List<String> personIds = new ArrayList<String>(entity.getProposerPerson().size());
 64  15
             for (ProposalPerson person : entity.getProposerPerson()) {
 65  9
                 personIds.add(person.getPersonId());
 66  
             }
 67  15
             dto.setProposerPerson(personIds);
 68  
         }
 69  15
         if (entity.getProposerOrg() != null) {
 70  15
             List<String> orgIds = new ArrayList<String>(entity.getProposerOrg().size());
 71  15
             for (ProposalOrg org : entity.getProposerOrg()) {
 72  8
                 orgIds.add(org.getOrgId());
 73  
             }
 74  15
             dto.setProposerOrg(orgIds);
 75  
         }
 76  
 
 77  15
         if (entity.getProposalReference() != null){
 78  15
             if (entity.getProposalReference().size() > 0){
 79  15
                 dto.setProposalReferenceType(entity.getProposalReference().get(0).getType().getId());
 80  
             }
 81  15
             List<String> objectIds = new ArrayList<String>(entity.getProposalReference().size());
 82  15
             for (ProposalReference object : entity.getProposalReference()) {
 83  30
                 objectIds.add(object.getObjectReferenceId());
 84  
             }
 85  15
             dto.setProposalReference(objectIds);
 86  
         }
 87  15
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 88  15
         dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
 89  15
         dto.setType(entity.getType().getId());
 90  
 
 91  15
         return dto;
 92  
     }
 93  
 
 94  
     public static List<ProposalTypeInfo> toProposalTypeInfos(List<ProposalType> entities) {
 95  3
         List<ProposalTypeInfo> dtos = new ArrayList<ProposalTypeInfo>(entities.size());
 96  3
         for (ProposalType entity : entities) {
 97  4
             dtos.add(toProposalTypeInfo(entity));
 98  
         }
 99  3
         return dtos;
 100  
     }
 101  
 
 102  
     public static ProposalTypeInfo toProposalTypeInfo(ProposalType entity) {
 103  5
         ProposalTypeInfo dto = new ProposalTypeInfo();
 104  
 
 105  5
         BeanUtils.copyProperties(entity, dto,
 106  
                 new String[] { "attributes" });
 107  5
         dto.setDesc(entity.getDescr());
 108  5
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 109  5
         return dto;
 110  
     }
 111  
 
 112  
     public static List<ReferenceTypeInfo> toReferenceTypeInfos(List<ProposalReferenceType> entities) {
 113  1
         List<ReferenceTypeInfo> dtos = new ArrayList<ReferenceTypeInfo>(entities.size());
 114  1
         for (ProposalReferenceType entity : entities) {
 115  1
             dtos.add(toReferenceTypeInfo(entity));
 116  
         }
 117  1
         return dtos;
 118  
     }
 119  
 
 120  
     public static ReferenceTypeInfo toReferenceTypeInfo(ProposalReferenceType entity) {
 121  1
         ReferenceTypeInfo dto = new ReferenceTypeInfo();
 122  
 
 123  1
         BeanUtils.copyProperties(entity, dto,
 124  
                 new String[] { "attributes" });
 125  
 
 126  1
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 127  1
         return dto;
 128  
     }
 129  
 
 130  
     public static Proposal toProposal(String proposalTypeKey, ProposalInfo  proposalInfo, ProposalDao dao) throws DoesNotExistException, VersionMismatchException, InvalidParameterException {
 131  
         Proposal proposal;
 132  3
         if (proposalInfo.getId() != null && !proposalInfo.getId().isEmpty()) {
 133  2
             proposal = dao.fetch(Proposal.class, proposalInfo.getId());
 134  2
             if (!String.valueOf(proposal.getVersionNumber()).equals(proposalInfo.getMetaInfo().getVersionInd())){
 135  0
                 throw new VersionMismatchException("Proposal to be updated is not the current version");
 136  
             }
 137  
         } else {
 138  1
             proposal = new Proposal();
 139  1
             proposal.setProposerPerson(new ArrayList<ProposalPerson>(0));
 140  1
             proposal.setProposerOrg(new ArrayList<ProposalOrg>(0));
 141  
         }
 142  
 
 143  
         // Copy all basic properties
 144  3
         BeanUtils.copyProperties(proposalInfo, proposal, new String[] { "id", "type",
 145  
                 "attributes", "metaInfo", "proposerPerson", "proposerOrg", "proposalReference" });
 146  
 
 147  
         // Copy Attributes
 148  3
         proposal.setAttributes(toGenericAttributes(ProposalAttribute.class, proposalInfo.getAttributes(), proposal, dao));
 149  
 
 150  
         // TODO Rework when JPA gets cascading deletes (2.0)
 151  3
         List<ProposalPerson> persons = proposal.getProposerPerson();
 152  3
         for (ProposalPerson person : persons) {
 153  2
             dao.getEm().remove(person);
 154  
         }
 155  3
         List<ProposalOrg> orgs = proposal.getProposerOrg();
 156  3
         for (ProposalOrg org : orgs) {
 157  2
             dao.getEm().remove(org);
 158  
         }
 159  3
         persons.clear();
 160  3
         orgs.clear();
 161  
 
 162  3
         if (proposalInfo.getProposerPerson() != null) {
 163  
             // Copy ProposerPersons
 164  3
             for (String proposer : proposalInfo.getProposerPerson()) {
 165  
                 ProposalPerson person;
 166  
                 try {
 167  2
                     person = dao.getProposalPerson(proposer);
 168  2
                 } catch (NoResultException e) {
 169  2
                     person = new ProposalPerson();
 170  2
                     person.setPersonId(proposer);
 171  2
                     person.setProposal(proposal);
 172  0
                 }
 173  2
                 persons.add(person);
 174  2
             }
 175  
         }
 176  
 
 177  3
         if (proposalInfo.getProposerOrg() != null) {
 178  
             // Copy ProposerPersons
 179  3
             for (String proposer : proposalInfo.getProposerOrg()) {
 180  
                 ProposalOrg org;
 181  
                 try {
 182  4
                     org = dao.getProposalOrg(proposer);
 183  4
                 } catch (NoResultException e) {
 184  4
                     ProposalOrg proposerOrg = new ProposalOrg();
 185  4
                     proposerOrg.setOrgId(proposer);
 186  4
                     proposerOrg.setProposal(proposal);
 187  4
                     org = dao.create(proposerOrg);
 188  0
                 }
 189  4
                 orgs.add(org);
 190  4
             }
 191  3
             proposal.setProposerOrg(orgs);
 192  
         }
 193  
 
 194  3
         if (proposalInfo.getProposalReference() != null) {
 195  
             // Copy propsal references
 196  3
             List<ProposalReference> references = new ArrayList<ProposalReference>(proposalInfo.getProposalReference().size());
 197  3
             for (String objectReferenceId : proposalInfo.getProposalReference()) {
 198  
                 ProposalReference ref;
 199  
                 try {
 200  3
                     ref = dao.getObjectReference(objectReferenceId, proposalInfo.getProposalReferenceType());
 201  2
                 } catch (NoResultException e) {
 202  2
                     ProposalReference objectReference = new ProposalReference();
 203  2
                     objectReference.setObjectReferenceId(objectReferenceId);
 204  2
                     ProposalReferenceType refType = dao.fetch(ProposalReferenceType.class, proposalInfo.getProposalReferenceType());
 205  2
                     objectReference.setType(refType);
 206  2
                     ref = dao.create(objectReference);
 207  1
                 }
 208  3
                 references.add(ref);
 209  3
             }
 210  3
             proposal.setProposalReference(references);
 211  
         }
 212  
 
 213  3
         ProposalType proposalType = dao.fetch(ProposalType.class, proposalTypeKey);
 214  3
         proposal.setType(proposalType);
 215  3
         return proposal;
 216  
     }
 217  
 }