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