Coverage Report - org.kuali.student.core.atp.service.impl.AtpAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
AtpAssembler
90%
90/99
77%
28/36
3.636
 
 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.atp.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 22  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 23  
 import org.kuali.student.common.exceptions.VersionMismatchException;
 24  
 import org.kuali.student.common.service.impl.BaseAssembler;
 25  
 import org.kuali.student.core.atp.dao.AtpDao;
 26  
 import org.kuali.student.core.atp.dto.AtpInfo;
 27  
 import org.kuali.student.core.atp.dto.AtpTypeInfo;
 28  
 import org.kuali.student.core.atp.dto.DateRangeInfo;
 29  
 import org.kuali.student.core.atp.dto.MilestoneInfo;
 30  
 import org.kuali.student.core.atp.entity.Atp;
 31  
 import org.kuali.student.core.atp.entity.AtpAttribute;
 32  
 import org.kuali.student.core.atp.entity.AtpRichText;
 33  
 import org.kuali.student.core.atp.entity.AtpType;
 34  
 import org.kuali.student.core.atp.entity.DateRange;
 35  
 import org.kuali.student.core.atp.entity.DateRangeAttribute;
 36  
 import org.kuali.student.core.atp.entity.DateRangeType;
 37  
 import org.kuali.student.core.atp.entity.Milestone;
 38  
 import org.kuali.student.core.atp.entity.MilestoneAttribute;
 39  
 import org.kuali.student.core.atp.entity.MilestoneType;
 40  
 import org.springframework.beans.BeanUtils;
 41  
 
 42  0
 public class AtpAssembler extends BaseAssembler{
 43  
 
 44  
         public static Atp toAtp(boolean isUpdate, AtpInfo atpInfo, AtpDao dao)
 45  
                         throws InvalidParameterException, DoesNotExistException, VersionMismatchException {
 46  
                 Atp atp;
 47  3
                 if (isUpdate) {
 48  2
                         atp = dao.fetch(Atp.class, atpInfo.getId());
 49  2
                         if (atp == null) {
 50  0
                                 throw new DoesNotExistException("Atp does not exist for key: " + atpInfo.getId());
 51  
                         }
 52  2
                         if (!String.valueOf(atp.getVersionNumber()).equals(atpInfo.getMetaInfo().getVersionInd())){
 53  1
                                 throw new VersionMismatchException("Atp to be updated is not the current version");
 54  
                         }
 55  
                 } else {
 56  1
                         atp = new Atp();
 57  
                 }
 58  
 
 59  
                 // Copy all basic properties
 60  2
                 BeanUtils.copyProperties(atpInfo, atp, new String[] { "type",
 61  
                                 "attributes", "metaInfo", "desc" });
 62  
 
 63  
                 // Copy Attributes
 64  2
                 atp.setAttributes(toGenericAttributes(AtpAttribute.class, atpInfo.getAttributes(), atp, dao));
 65  
 
 66  
                 // Search for and copy the type
 67  2
                 AtpType atpType = dao.fetch(AtpType.class, atpInfo.getType());
 68  2
                 if (atpType == null) {
 69  0
                         throw new InvalidParameterException(
 70  
                                         "AtpType does not exist for key: " + atpInfo.getType());
 71  
                 }
 72  2
                 atp.setType(atpType);
 73  
 
 74  
                 //Copy RichText
 75  2
                 atp.setDescr(toRichText(AtpRichText.class, atpInfo.getDesc()));
 76  
                 
 77  2
                 return atp;
 78  
         }
 79  
 
 80  
         public static AtpInfo toAtpInfo(Atp atp) {
 81  6
                 AtpInfo atpInfo = new AtpInfo();
 82  
 
 83  6
                 BeanUtils.copyProperties(atp, atpInfo, new String[] { "type",
 84  
                                 "attributes", "metaInfo", "desc" });
 85  
 
 86  
                 // copy attributes, metadata, Atp, and Type
 87  6
                 atpInfo.setAttributes(toAttributeMap(atp.getAttributes()));
 88  6
                 atpInfo.setMetaInfo(toMetaInfo(atp.getMeta(), atp.getVersionNumber()));
 89  6
                 atpInfo.setType(atp.getType().getId());
 90  6
                 atpInfo.setDesc(toRichTextInfo(atp.getDescr()));
 91  
                 
 92  6
                 return atpInfo;
 93  
         }
 94  
 
 95  
 
 96  
 
 97  
         public static List<AtpInfo> toAtpInfoList(List<Atp> atps) {
 98  3
                 List<AtpInfo> atpInfoList = new ArrayList<AtpInfo>();
 99  3
                 for (Atp atp : atps) {
 100  4
                         atpInfoList.add(toAtpInfo(atp));
 101  
                 }
 102  3
                 return atpInfoList;
 103  
         }
 104  
 
 105  
 
 106  
 
 107  
         public static AtpTypeInfo toAtpTypeInfo(AtpType atpType) {
 108  
 
 109  2
                 AtpTypeInfo atpTypeInfo = new AtpTypeInfo();
 110  
 
 111  2
                 BeanUtils.copyProperties(atpType, atpTypeInfo, new String[] {
 112  
                                 "seasonalType", "durationType", "attributes" });
 113  
 
 114  
                 // Copy attributes and duration/seasonal types
 115  2
                 atpTypeInfo.setAttributes(toAttributeMap(atpType.getAttributes()));
 116  
 
 117  2
                 atpTypeInfo.setDurationType(atpType.getDurationType().getId());
 118  2
                 atpTypeInfo.setSeasonalType(atpType.getSeasonalType().getId());
 119  
 
 120  2
                 return atpTypeInfo;
 121  
         }
 122  
 
 123  
         public static List<AtpTypeInfo> toAtpTypeInfoList(List<AtpType> atpTypes) {
 124  1
                 List<AtpTypeInfo> typeInfoList = new ArrayList<AtpTypeInfo>();
 125  1
                 for (AtpType atpType : atpTypes) {
 126  2
                         typeInfoList.add(toAtpTypeInfo(atpType));
 127  
                 }
 128  1
                 return typeInfoList;
 129  
         }
 130  
 
 131  
         public static DateRange toDateRange(boolean isUpdate,
 132  
                         DateRangeInfo dateRangeInfo, AtpDao dao)
 133  
                         throws InvalidParameterException, DoesNotExistException, VersionMismatchException {
 134  
 
 135  
                 DateRange dateRange;
 136  3
                 if (isUpdate) {
 137  2
                         dateRange = dao.fetch(DateRange.class, dateRangeInfo.getId());
 138  2
                         if (dateRange == null) {
 139  0
                                 throw new DoesNotExistException("DateRange does not exist for key: " + dateRangeInfo.getId());
 140  
                         }
 141  2
                         if (!String.valueOf(dateRange.getVersionNumber()).equals(dateRangeInfo.getMetaInfo().getVersionInd())){
 142  1
                                 throw new VersionMismatchException("DateRange to be updated is not the current version");
 143  
                         }
 144  
                 } else {
 145  1
                         dateRange = new DateRange();
 146  
                 }
 147  
 
 148  2
                 BeanUtils.copyProperties(dateRangeInfo, dateRange, new String[] {
 149  
                                 "atpKey", "type", "attributes", "metaInfo","desc" });
 150  
 
 151  
                 // Copy the attributes
 152  2
                 dateRange.setAttributes(toGenericAttributes(DateRangeAttribute.class,
 153  
                                 dateRangeInfo.getAttributes(), dateRange, dao));
 154  
 
 155  
                 // Search for and copy the associated Atp
 156  2
                 Atp atp = dao.fetch(Atp.class, dateRangeInfo.getAtpId());
 157  2
                 if (atp == null) {
 158  0
                         throw new InvalidParameterException("Atp does not exist for key: "
 159  
                                         + dateRangeInfo.getAtpId());
 160  
                 }
 161  2
                 dateRange.setAtp(atp);
 162  
 
 163  
                 // Search for and copy the Type
 164  2
                 DateRangeType dateRangeType = dao.fetch(DateRangeType.class,
 165  
                                 dateRangeInfo.getType());
 166  2
                 if (dateRangeType == null) {
 167  0
                         throw new InvalidParameterException(
 168  
                                         "DateRangeType does not exist for key: "
 169  
                                                         + dateRangeInfo.getType());
 170  
                 }
 171  2
                 dateRange.setType(dateRangeType);
 172  
 
 173  2
                 dateRange.setDescr(toRichText(AtpRichText.class, dateRangeInfo.getDesc()));
 174  
                 
 175  2
                 return dateRange;
 176  
         }
 177  
 
 178  
         public static DateRangeInfo toDateRangeInfo(DateRange dateRange) {
 179  
 
 180  4
                 DateRangeInfo dateRangeInfo = new DateRangeInfo();
 181  
 
 182  4
                 BeanUtils.copyProperties(dateRange, dateRangeInfo, new String[] {
 183  
                                 "atp", "type", "attributes", "metaInfo", "desc" });
 184  
 
 185  
                 // copy attributes, metadata, Atp, and Type
 186  4
                 dateRangeInfo
 187  
                                 .setAttributes(toAttributeMap(dateRange.getAttributes()));
 188  4
                 dateRangeInfo.setMetaInfo(toMetaInfo(dateRange.getMeta(), dateRange
 189  
                                 .getVersionNumber()));
 190  4
                 dateRangeInfo.setType(dateRange.getType().getId());
 191  4
                 dateRangeInfo.setAtpId(dateRange.getAtp().getId());
 192  4
                 dateRangeInfo.setDesc(toRichTextInfo(dateRange.getDescr()));
 193  
                 
 194  4
                 return dateRangeInfo;
 195  
         }
 196  
 
 197  
 
 198  
 
 199  
         public static List<DateRangeInfo> toDateRangeInfoList(
 200  
                         List<DateRange> dateRanges) {
 201  2
                 List<DateRangeInfo> dateRangeInfoList = new ArrayList<DateRangeInfo>();
 202  2
                 for (DateRange dateRange : dateRanges) {
 203  2
                         dateRangeInfoList.add(toDateRangeInfo(dateRange));
 204  
                 }
 205  2
                 return dateRangeInfoList;
 206  
         }
 207  
 
 208  
         public static Milestone toMilestone(boolean isUpdate,
 209  
                         MilestoneInfo milestoneInfo, AtpDao dao)
 210  
                         throws InvalidParameterException, DoesNotExistException, VersionMismatchException {
 211  
 
 212  
                 Milestone milestone;
 213  3
                 if (isUpdate) {
 214  2
                         milestone = dao.fetch(Milestone.class, milestoneInfo.getId());
 215  2
                         if (milestone == null) {
 216  0
                                 throw new DoesNotExistException("Milestone does not exist for key: " + milestoneInfo.getId());
 217  
                         }
 218  2
                         if (!String.valueOf(milestone.getVersionNumber()).equals(milestoneInfo.getMetaInfo().getVersionInd())){
 219  1
                                 throw new VersionMismatchException("Milestone to be updated is not the current version");
 220  
                         }
 221  
                 } else {
 222  1
                         milestone = new Milestone();
 223  
                 }
 224  
 
 225  2
                 BeanUtils.copyProperties(milestoneInfo, milestone, new String[] {
 226  
                                 "atpKey", "type", "attributes", "metaInfo", "desc" });
 227  
 
 228  
                 // Copy the attributes
 229  2
                 milestone.setAttributes(toGenericAttributes(MilestoneAttribute.class,
 230  
                                 milestoneInfo.getAttributes(), milestone, dao));
 231  
 
 232  
                 // Search for and copy the associated Atp
 233  2
                 Atp atp = dao.fetch(Atp.class, milestoneInfo.getAtpId());
 234  2
                 if (atp == null) {
 235  0
                         throw new InvalidParameterException("Atp does not exist for key: "
 236  
                                         + milestoneInfo.getAtpId());
 237  
                 }
 238  2
                 milestone.setAtp(atp);
 239  
 
 240  
                 // Search for and copy the Type
 241  2
                 MilestoneType milestoneType = dao.fetch(MilestoneType.class,
 242  
                                 milestoneInfo.getType());
 243  2
                 if (milestoneType == null) {
 244  0
                         throw new InvalidParameterException(
 245  
                                         "MilestoneType does not exist for key: "
 246  
                                                         + milestoneInfo.getType());
 247  
                 }
 248  2
                 milestone.setType(milestoneType);
 249  
 
 250  2
                 milestone.setDescr(toRichText(AtpRichText.class, milestoneInfo.getDesc()));
 251  
                 
 252  2
                 return milestone;
 253  
 
 254  
         }
 255  
 
 256  
         public static MilestoneInfo toMilestoneInfo(Milestone milestone) {
 257  
 
 258  5
                 MilestoneInfo milestoneInfo = new MilestoneInfo();
 259  
 
 260  5
                 BeanUtils.copyProperties(milestone, milestoneInfo, new String[] {
 261  
                                 "atp", "type", "attributes", "metaInfo", "desc" });
 262  
 
 263  
                 // copy attributes, metadata, Atp, and Type
 264  5
                 milestoneInfo
 265  
                                 .setAttributes(toAttributeMap(milestone.getAttributes()));
 266  5
                 milestoneInfo.setMetaInfo(toMetaInfo(milestone.getMeta(), milestone
 267  
                                 .getVersionNumber()));
 268  5
                 milestoneInfo.setType(milestone.getType().getId());
 269  5
                 milestoneInfo.setAtpId(milestone.getAtp().getId());
 270  5
                 milestoneInfo.setDesc(toRichTextInfo(milestone.getDescr()));
 271  
                 
 272  5
                 return milestoneInfo;
 273  
         }
 274  
 
 275  
         public static List<MilestoneInfo> toMilestoneInfoList(
 276  
                         List<Milestone> milestones) {
 277  3
                 List<MilestoneInfo> milestoneInfoList = new ArrayList<MilestoneInfo>();
 278  3
                 for (Milestone milestone : milestones) {
 279  3
                         milestoneInfoList.add(toMilestoneInfo(milestone));
 280  
                 }
 281  3
                 return milestoneInfoList;
 282  
         }
 283  
         
 284  
 }