Coverage Report - org.kuali.student.core.proposal.service.impl.ProposalAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
ProposalAssembler
0%
0/105
0%
0/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  0
         List<ProposalInfo> dtos = new ArrayList<ProposalInfo>(entities.size());
 52  0
         for (Proposal entity : entities) {
 53  0
             dtos.add(toProposalInfo(entity));
 54  
         }
 55  0
         return dtos;
 56  
     }
 57  
 
 58  
     public static ProposalInfo toProposalInfo(Proposal entity) {
 59  0
         ProposalInfo dto = new ProposalInfo();
 60  
 
 61  
 
 62  0
         BeanUtils.copyProperties(entity, dto,
 63  
                 new String[] { "proposerPerson", "proposerOrg", "proposalReferenceType", "proposalReference", "attributes", "type" });
 64  0
         if (entity.getProposerPerson() != null) {
 65  0
             List<String> personIds = new ArrayList<String>(entity.getProposerPerson().size());
 66  0
             for (ProposalPerson person : entity.getProposerPerson()) {
 67  0
                 personIds.add(person.getPersonId());
 68  
             }
 69  0
             dto.setProposerPerson(personIds);
 70  
         }
 71  0
         if (entity.getProposerOrg() != null) {
 72  0
             List<String> orgIds = new ArrayList<String>(entity.getProposerOrg().size());
 73  0
             for (ProposalOrg org : entity.getProposerOrg()) {
 74  0
                 orgIds.add(org.getOrgId());
 75  
             }
 76  0
             dto.setProposerOrg(orgIds);
 77  
         }
 78  
 
 79  0
         if (entity.getProposalReference() != null){
 80  0
             if (entity.getProposalReference().size() > 0){
 81  0
                 dto.setProposalReferenceType(entity.getProposalReference().get(0).getType().getId());
 82  
             }
 83  0
             List<String> objectIds = new ArrayList<String>(entity.getProposalReference().size());
 84  0
             for (ProposalReference object : entity.getProposalReference()) {
 85  0
                 objectIds.add(object.getObjectReferenceId());
 86  
             }
 87  0
             dto.setProposalReference(objectIds);
 88  
         }
 89  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 90  0
         dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
 91  0
         dto.setType(entity.getType().getId());
 92  
 
 93  0
         return dto;
 94  
     }
 95  
 
 96  
     public static List<ProposalTypeInfo> toProposalTypeInfos(List<ProposalType> entities) {
 97  0
         List<ProposalTypeInfo> dtos = new ArrayList<ProposalTypeInfo>(entities.size());
 98  0
         for (ProposalType entity : entities) {
 99  0
             dtos.add(toProposalTypeInfo(entity));
 100  
         }
 101  0
         return dtos;
 102  
     }
 103  
 
 104  
     public static ProposalTypeInfo toProposalTypeInfo(ProposalType entity) {
 105  0
         ProposalTypeInfo dto = new ProposalTypeInfo();
 106  
 
 107  0
         BeanUtils.copyProperties(entity, dto,
 108  
                 new String[] { "attributes" });
 109  0
         dto.setDesc(entity.getDescr());
 110  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 111  0
         return dto;
 112  
     }
 113  
 
 114  
     public static List<ReferenceTypeInfo> toReferenceTypeInfos(List<ProposalReferenceType> entities) {
 115  0
         List<ReferenceTypeInfo> dtos = new ArrayList<ReferenceTypeInfo>(entities.size());
 116  0
         for (ProposalReferenceType entity : entities) {
 117  0
             dtos.add(toReferenceTypeInfo(entity));
 118  
         }
 119  0
         return dtos;
 120  
     }
 121  
 
 122  
     public static ReferenceTypeInfo toReferenceTypeInfo(ProposalReferenceType entity) {
 123  0
         ReferenceTypeInfo dto = new ReferenceTypeInfo();
 124  
 
 125  0
         BeanUtils.copyProperties(entity, dto,
 126  
                 new String[] { "attributes" });
 127  
 
 128  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 129  0
         return dto;
 130  
     }
 131  
 
 132  
     public static Proposal toProposal(String proposalTypeKey, ProposalInfo  proposalInfo, ProposalDao dao) throws DoesNotExistException, VersionMismatchException, InvalidParameterException {
 133  
         Proposal proposal;
 134  0
         if (proposalInfo.getId() != null && !proposalInfo.getId().isEmpty()) {
 135  0
             proposal = dao.fetch(Proposal.class, proposalInfo.getId());
 136  0
             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  0
             proposal = new Proposal();
 141  0
             proposal.setProposerPerson(new ArrayList<ProposalPerson>(0));
 142  0
             proposal.setProposerOrg(new ArrayList<ProposalOrg>(0));
 143  
         }
 144  
 
 145  
         // Copy all basic properties
 146  0
         BeanUtils.copyProperties(proposalInfo, proposal, new String[] { "id", "type",
 147  
                 "attributes", "metaInfo", "proposerPerson", "proposerOrg", "proposalReference" });
 148  
 
 149  
         // Copy Attributes
 150  0
         proposal.setAttributes(toGenericAttributes(ProposalAttribute.class, proposalInfo.getAttributes(), proposal, dao));
 151  
 
 152  
         //Update proposal to person relations
 153  0
         Map<String,ProposalPerson> existingPersons = new HashMap<String,ProposalPerson>();
 154  0
         if(proposal.getProposerPerson()==null){
 155  0
                 proposal.setProposerPerson(new ArrayList<ProposalPerson>(proposalInfo.getProposerPerson().size()));
 156  
         }
 157  0
         for(ProposalPerson person:proposal.getProposerPerson()){
 158  0
                 existingPersons.put(person.getPersonId(), person);
 159  
         }
 160  0
         proposal.getProposerPerson().clear();
 161  0
         for (String proposerId : proposalInfo.getProposerPerson()) {
 162  
                 ProposalPerson person;
 163  0
                 if(existingPersons.containsKey(proposerId)){
 164  0
                         person = existingPersons.get(proposerId);
 165  0
                         existingPersons.remove(proposerId);
 166  
                 }else{
 167  0
                         person = new ProposalPerson();
 168  
                 }
 169  0
             person.setPersonId(proposerId);
 170  0
             person.setProposal(proposal);
 171  0
             proposal.getProposerPerson().add(person);
 172  0
         }
 173  0
         for (ProposalPerson person : existingPersons.values()) {
 174  0
             dao.delete(person);
 175  
         }
 176  
         
 177  
         //Update proposal to org relations
 178  0
         Map<String,ProposalOrg> existingOrgs = new HashMap<String,ProposalOrg>();
 179  0
         if(proposal.getProposerOrg()==null){
 180  0
                 proposal.setProposerOrg(new ArrayList<ProposalOrg>(proposalInfo.getProposerOrg().size()));
 181  
         }
 182  0
         for(ProposalOrg org:proposal.getProposerOrg()){
 183  0
                 existingOrgs.put(org.getOrgId(), org);
 184  
         }
 185  0
         proposal.getProposerOrg().clear();
 186  0
         for (String orgId: proposalInfo.getProposerOrg()) {
 187  
                 ProposalOrg org;
 188  0
                 if(existingOrgs.containsKey(orgId)){
 189  0
                         org = existingOrgs.get(orgId);
 190  0
                         existingOrgs.remove(orgId);
 191  
                 }else{
 192  0
                         org = new ProposalOrg();
 193  
                 }
 194  0
                 org.setOrgId(orgId);
 195  0
                 org.setProposal(proposal);
 196  0
             proposal.getProposerOrg().add(org);
 197  0
         }
 198  0
         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  0
         if (proposalInfo.getProposalReference() != null) {
 229  
             // Copy propsal references
 230  0
             List<ProposalReference> references = new ArrayList<ProposalReference>(proposalInfo.getProposalReference().size());
 231  0
             for (String objectReferenceId : proposalInfo.getProposalReference()) {
 232  
                 ProposalReference ref;
 233  
                 try {
 234  0
                     ref = dao.getObjectReference(objectReferenceId, proposalInfo.getProposalReferenceType());
 235  0
                 } catch (NoResultException e) {
 236  0
                     ProposalReference objectReference = new ProposalReference();
 237  0
                     objectReference.setObjectReferenceId(objectReferenceId);
 238  0
                     ProposalReferenceType refType = dao.fetch(ProposalReferenceType.class, proposalInfo.getProposalReferenceType());
 239  0
                     objectReference.setType(refType);
 240  0
                     ref = dao.create(objectReference);
 241  0
                 }
 242  0
                 references.add(ref);
 243  0
             }
 244  0
             proposal.setProposalReference(references);
 245  
         }
 246  
         
 247  0
         ProposalType proposalType = dao.fetch(ProposalType.class, proposalTypeKey);
 248  0
         proposal.setType(proposalType);
 249  0
         return proposal;
 250  
     }
 251  
 }