| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 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 | 0 | if (isUpdate) { |
| 48 | 0 | atp = dao.fetch(Atp.class, atpInfo.getId()); |
| 49 | 0 | if (atp == null) { |
| 50 | 0 | throw new DoesNotExistException("Atp does not exist for key: " + atpInfo.getId()); |
| 51 | |
} |
| 52 | 0 | if (!String.valueOf(atp.getVersionNumber()).equals(atpInfo.getMetaInfo().getVersionInd())){ |
| 53 | 0 | throw new VersionMismatchException("Atp to be updated is not the current version"); |
| 54 | |
} |
| 55 | |
} else { |
| 56 | 0 | atp = new Atp(); |
| 57 | |
} |
| 58 | |
|
| 59 | |
|
| 60 | 0 | BeanUtils.copyProperties(atpInfo, atp, new String[] { "type", |
| 61 | |
"attributes", "metaInfo", "desc" }); |
| 62 | |
|
| 63 | |
|
| 64 | 0 | atp.setAttributes(toGenericAttributes(AtpAttribute.class, atpInfo.getAttributes(), atp, dao)); |
| 65 | |
|
| 66 | |
|
| 67 | 0 | AtpType atpType = dao.fetch(AtpType.class, atpInfo.getType()); |
| 68 | 0 | if (atpType == null) { |
| 69 | 0 | throw new InvalidParameterException( |
| 70 | |
"AtpType does not exist for key: " + atpInfo.getType()); |
| 71 | |
} |
| 72 | 0 | atp.setType(atpType); |
| 73 | |
|
| 74 | |
|
| 75 | 0 | atp.setDescr(toRichText(AtpRichText.class, atpInfo.getDesc())); |
| 76 | |
|
| 77 | 0 | return atp; |
| 78 | |
} |
| 79 | |
|
| 80 | |
public static AtpInfo toAtpInfo(Atp atp) { |
| 81 | 0 | AtpInfo atpInfo = new AtpInfo(); |
| 82 | |
|
| 83 | 0 | BeanUtils.copyProperties(atp, atpInfo, new String[] { "type", |
| 84 | |
"attributes", "metaInfo", "desc" }); |
| 85 | |
|
| 86 | |
|
| 87 | 0 | atpInfo.setAttributes(toAttributeMap(atp.getAttributes())); |
| 88 | 0 | atpInfo.setMetaInfo(toMetaInfo(atp.getMeta(), atp.getVersionNumber())); |
| 89 | 0 | atpInfo.setType(atp.getType().getId()); |
| 90 | 0 | atpInfo.setDesc(toRichTextInfo(atp.getDescr())); |
| 91 | |
|
| 92 | 0 | return atpInfo; |
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
public static List<AtpInfo> toAtpInfoList(List<Atp> atps) { |
| 98 | 0 | List<AtpInfo> atpInfoList = new ArrayList<AtpInfo>(); |
| 99 | 0 | for (Atp atp : atps) { |
| 100 | 0 | atpInfoList.add(toAtpInfo(atp)); |
| 101 | |
} |
| 102 | 0 | return atpInfoList; |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public static AtpTypeInfo toAtpTypeInfo(AtpType atpType) { |
| 108 | |
|
| 109 | 0 | AtpTypeInfo atpTypeInfo = new AtpTypeInfo(); |
| 110 | |
|
| 111 | 0 | BeanUtils.copyProperties(atpType, atpTypeInfo, new String[] { |
| 112 | |
"seasonalType", "durationType", "attributes" }); |
| 113 | |
|
| 114 | |
|
| 115 | 0 | atpTypeInfo.setAttributes(toAttributeMap(atpType.getAttributes())); |
| 116 | |
|
| 117 | 0 | atpTypeInfo.setDurationType(atpType.getDurationType().getId()); |
| 118 | 0 | atpTypeInfo.setSeasonalType(atpType.getSeasonalType().getId()); |
| 119 | |
|
| 120 | 0 | return atpTypeInfo; |
| 121 | |
} |
| 122 | |
|
| 123 | |
public static List<AtpTypeInfo> toAtpTypeInfoList(List<AtpType> atpTypes) { |
| 124 | 0 | List<AtpTypeInfo> typeInfoList = new ArrayList<AtpTypeInfo>(); |
| 125 | 0 | for (AtpType atpType : atpTypes) { |
| 126 | 0 | typeInfoList.add(toAtpTypeInfo(atpType)); |
| 127 | |
} |
| 128 | 0 | 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 | 0 | if (isUpdate) { |
| 137 | 0 | dateRange = dao.fetch(DateRange.class, dateRangeInfo.getId()); |
| 138 | 0 | if (dateRange == null) { |
| 139 | 0 | throw new DoesNotExistException("DateRange does not exist for key: " + dateRangeInfo.getId()); |
| 140 | |
} |
| 141 | 0 | if (!String.valueOf(dateRange.getVersionNumber()).equals(dateRangeInfo.getMetaInfo().getVersionInd())){ |
| 142 | 0 | throw new VersionMismatchException("DateRange to be updated is not the current version"); |
| 143 | |
} |
| 144 | |
} else { |
| 145 | 0 | dateRange = new DateRange(); |
| 146 | |
} |
| 147 | |
|
| 148 | 0 | BeanUtils.copyProperties(dateRangeInfo, dateRange, new String[] { |
| 149 | |
"atpKey", "type", "attributes", "metaInfo","desc" }); |
| 150 | |
|
| 151 | |
|
| 152 | 0 | dateRange.setAttributes(toGenericAttributes(DateRangeAttribute.class, |
| 153 | |
dateRangeInfo.getAttributes(), dateRange, dao)); |
| 154 | |
|
| 155 | |
|
| 156 | 0 | Atp atp = dao.fetch(Atp.class, dateRangeInfo.getAtpId()); |
| 157 | 0 | if (atp == null) { |
| 158 | 0 | throw new InvalidParameterException("Atp does not exist for key: " |
| 159 | |
+ dateRangeInfo.getAtpId()); |
| 160 | |
} |
| 161 | 0 | dateRange.setAtp(atp); |
| 162 | |
|
| 163 | |
|
| 164 | 0 | DateRangeType dateRangeType = dao.fetch(DateRangeType.class, |
| 165 | |
dateRangeInfo.getType()); |
| 166 | 0 | if (dateRangeType == null) { |
| 167 | 0 | throw new InvalidParameterException( |
| 168 | |
"DateRangeType does not exist for key: " |
| 169 | |
+ dateRangeInfo.getType()); |
| 170 | |
} |
| 171 | 0 | dateRange.setType(dateRangeType); |
| 172 | |
|
| 173 | 0 | dateRange.setDescr(toRichText(AtpRichText.class, dateRangeInfo.getDesc())); |
| 174 | |
|
| 175 | 0 | return dateRange; |
| 176 | |
} |
| 177 | |
|
| 178 | |
public static DateRangeInfo toDateRangeInfo(DateRange dateRange) { |
| 179 | |
|
| 180 | 0 | DateRangeInfo dateRangeInfo = new DateRangeInfo(); |
| 181 | |
|
| 182 | 0 | BeanUtils.copyProperties(dateRange, dateRangeInfo, new String[] { |
| 183 | |
"atp", "type", "attributes", "metaInfo", "desc" }); |
| 184 | |
|
| 185 | |
|
| 186 | 0 | dateRangeInfo |
| 187 | |
.setAttributes(toAttributeMap(dateRange.getAttributes())); |
| 188 | 0 | dateRangeInfo.setMetaInfo(toMetaInfo(dateRange.getMeta(), dateRange |
| 189 | |
.getVersionNumber())); |
| 190 | 0 | dateRangeInfo.setType(dateRange.getType().getId()); |
| 191 | 0 | dateRangeInfo.setAtpId(dateRange.getAtp().getId()); |
| 192 | 0 | dateRangeInfo.setDesc(toRichTextInfo(dateRange.getDescr())); |
| 193 | |
|
| 194 | 0 | return dateRangeInfo; |
| 195 | |
} |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
public static List<DateRangeInfo> toDateRangeInfoList( |
| 200 | |
List<DateRange> dateRanges) { |
| 201 | 0 | List<DateRangeInfo> dateRangeInfoList = new ArrayList<DateRangeInfo>(); |
| 202 | 0 | for (DateRange dateRange : dateRanges) { |
| 203 | 0 | dateRangeInfoList.add(toDateRangeInfo(dateRange)); |
| 204 | |
} |
| 205 | 0 | 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 | 0 | if (isUpdate) { |
| 214 | 0 | milestone = dao.fetch(Milestone.class, milestoneInfo.getId()); |
| 215 | 0 | if (milestone == null) { |
| 216 | 0 | throw new DoesNotExistException("Milestone does not exist for key: " + milestoneInfo.getId()); |
| 217 | |
} |
| 218 | 0 | if (!String.valueOf(milestone.getVersionNumber()).equals(milestoneInfo.getMetaInfo().getVersionInd())){ |
| 219 | 0 | throw new VersionMismatchException("Milestone to be updated is not the current version"); |
| 220 | |
} |
| 221 | |
} else { |
| 222 | 0 | milestone = new Milestone(); |
| 223 | |
} |
| 224 | |
|
| 225 | 0 | BeanUtils.copyProperties(milestoneInfo, milestone, new String[] { |
| 226 | |
"atpKey", "type", "attributes", "metaInfo", "desc" }); |
| 227 | |
|
| 228 | |
|
| 229 | 0 | milestone.setAttributes(toGenericAttributes(MilestoneAttribute.class, |
| 230 | |
milestoneInfo.getAttributes(), milestone, dao)); |
| 231 | |
|
| 232 | |
|
| 233 | 0 | Atp atp = dao.fetch(Atp.class, milestoneInfo.getAtpId()); |
| 234 | 0 | if (atp == null) { |
| 235 | 0 | throw new InvalidParameterException("Atp does not exist for key: " |
| 236 | |
+ milestoneInfo.getAtpId()); |
| 237 | |
} |
| 238 | 0 | milestone.setAtp(atp); |
| 239 | |
|
| 240 | |
|
| 241 | 0 | MilestoneType milestoneType = dao.fetch(MilestoneType.class, |
| 242 | |
milestoneInfo.getType()); |
| 243 | 0 | if (milestoneType == null) { |
| 244 | 0 | throw new InvalidParameterException( |
| 245 | |
"MilestoneType does not exist for key: " |
| 246 | |
+ milestoneInfo.getType()); |
| 247 | |
} |
| 248 | 0 | milestone.setType(milestoneType); |
| 249 | |
|
| 250 | 0 | milestone.setDescr(toRichText(AtpRichText.class, milestoneInfo.getDesc())); |
| 251 | |
|
| 252 | 0 | return milestone; |
| 253 | |
|
| 254 | |
} |
| 255 | |
|
| 256 | |
public static MilestoneInfo toMilestoneInfo(Milestone milestone) { |
| 257 | |
|
| 258 | 0 | MilestoneInfo milestoneInfo = new MilestoneInfo(); |
| 259 | |
|
| 260 | 0 | BeanUtils.copyProperties(milestone, milestoneInfo, new String[] { |
| 261 | |
"atp", "type", "attributes", "metaInfo", "desc" }); |
| 262 | |
|
| 263 | |
|
| 264 | 0 | milestoneInfo |
| 265 | |
.setAttributes(toAttributeMap(milestone.getAttributes())); |
| 266 | 0 | milestoneInfo.setMetaInfo(toMetaInfo(milestone.getMeta(), milestone |
| 267 | |
.getVersionNumber())); |
| 268 | 0 | milestoneInfo.setType(milestone.getType().getId()); |
| 269 | 0 | milestoneInfo.setAtpId(milestone.getAtp().getId()); |
| 270 | 0 | milestoneInfo.setDesc(toRichTextInfo(milestone.getDescr())); |
| 271 | |
|
| 272 | 0 | return milestoneInfo; |
| 273 | |
} |
| 274 | |
|
| 275 | |
public static List<MilestoneInfo> toMilestoneInfoList( |
| 276 | |
List<Milestone> milestones) { |
| 277 | 0 | List<MilestoneInfo> milestoneInfoList = new ArrayList<MilestoneInfo>(); |
| 278 | 0 | for (Milestone milestone : milestones) { |
| 279 | 0 | milestoneInfoList.add(toMilestoneInfo(milestone)); |
| 280 | |
} |
| 281 | 0 | return milestoneInfoList; |
| 282 | |
} |
| 283 | |
|
| 284 | |
} |