View Javadoc

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.r2.lum.lo.service.impl;
17  
18  
19  import org.kuali.student.r2.core.service.assembly.BaseAssembler;
20  import org.kuali.student.r2.lum.lo.dao.LoDao;
21  import org.kuali.student.r2.lum.lo.entity.Lo;
22  import org.kuali.student.r2.lum.lo.entity.LoAttribute;
23  import org.kuali.student.r2.lum.lo.entity.LoCategory;
24  import org.kuali.student.r2.lum.lo.entity.LoCategoryAttribute;
25  import org.kuali.student.r2.lum.lo.entity.LoCategoryType;
26  import org.kuali.student.r2.lum.lo.entity.LoLoRelation;
27  import org.kuali.student.r2.lum.lo.entity.LoLoRelationAttribute;
28  import org.kuali.student.r2.lum.lo.entity.LoLoRelationType;
29  import org.kuali.student.r2.lum.lo.entity.LoRepository;
30  import org.kuali.student.r2.lum.lo.entity.LoRichText;
31  import org.kuali.student.r2.lum.lo.entity.LoType;
32  import org.kuali.student.r1.common.dao.CrudDao;
33  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
34  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
35  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
36  import org.kuali.student.r2.lum.lo.dto.LoCategoryInfo;
37  import org.kuali.student.r2.lum.lo.dto.LoInfo;
38  import org.kuali.student.r2.lum.lo.dto.LoLoRelationInfo;
39  import org.kuali.student.r2.lum.lo.dto.LoRepositoryInfo;
40  import org.springframework.beans.BeanUtils;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  
45  public class LearningObjectiveServiceAssembler extends BaseAssembler {
46  
47      public static Lo toLo(boolean isUpdate, LoInfo dto, CrudDao dao) throws InvalidParameterException, DoesNotExistException, VersionMismatchException {
48          return toLo(isUpdate, new Lo(), dto, dao);
49      }
50      
51      public static Lo toLo(boolean isUpdate, Lo entity, LoInfo dto, CrudDao dao) throws InvalidParameterException, DoesNotExistException, VersionMismatchException {
52          if(null == dto) {
53              return null;
54          }
55          Lo lo;
56  
57          if (isUpdate) {
58              lo = dao.fetch(Lo.class, dto.getId());
59              if (lo == null) {
60                  throw new DoesNotExistException((new StringBuilder()).append("Lo does not exist for id: ").append(dto.getId()).toString());
61              }
62              if ( ! String.valueOf(lo.getVersionNumber()).equals(dto.getMeta().getVersionInd()) ) {
63                  throw new VersionMismatchException("Lo to be updated is not the current version");
64              }
65          } else {
66              lo = new Lo();
67          }
68  
69          BeanUtils.copyProperties(dto, lo, new String[] { "descr", "loRepositoryKey", "typeKey", "attributes", "meta" });
70          
71          lo.setAttributes(toGenericAttributes(LoAttribute.class, dto.getAttributes(), lo, dao));
72          lo.setDescr(toRichText(LoRichText.class, dto.getDescr()));
73          lo.setState(dto.getStateKey());
74  
75          LoRepository repository = dao.fetch(LoRepository.class, dto.getLoRepositoryKey());
76          if(null == repository) {
77              throw new InvalidParameterException((new StringBuilder()).append("LoRepository does not exist for id: ").append(dto.getLoRepositoryKey()).toString());
78          }
79          lo.setLoRepository(repository);
80          
81          LoType type = dao.fetch(LoType.class, dto.getTypeKey());
82          if(null == type) {
83              throw new InvalidParameterException((new StringBuilder()).append("LoType does not exist for id: ").append(dto.getTypeKey()).toString());
84          }
85          lo.setLoType(type);
86          
87          return lo;
88      }
89  
90      public static LoInfo toLoInfo(Lo entity) {
91          LoInfo dto = new LoInfo();
92  
93          BeanUtils.copyProperties(entity, dto,
94                  new String[] { "descr", "attributes", "loType", "meta", "type", "state"  });
95          dto.setDescr(toRichTextInfo(entity.getDescr()));
96          dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
97          dto.setAttributes(toAttributeList(entity.getAttributes()));
98          dto.setTypeKey(entity.getLoType().getId());
99          dto.setStateKey(entity.getState());
100         dto.setLoRepositoryKey(entity.getLoRepository() == null? null: entity.getLoRepository().getId());
101         return dto;
102     }
103 
104     public static LoCategory toLoCategory(LoCategoryInfo dto, LoDao dao) throws InvalidParameterException, DoesNotExistException {
105         return toLoCategory(new LoCategory(), dto, dao);
106     }
107     
108     public static LoCategory toLoCategory(LoCategory entity, LoCategoryInfo dto, LoDao dao) throws InvalidParameterException, DoesNotExistException {
109         if(entity == null)
110             entity = new LoCategory();
111         BeanUtils.copyProperties(dto, entity,
112                 new String[] { "descr", "attributes", "meta", "loRepositoryKey", "typeKey", "id", "stateKey"});
113         entity.setDesc(toRichText(LoRichText.class, dto.getDescr()));
114         entity.setAttributes(toGenericAttributes(LoCategoryAttribute.class, dto.getAttributes(), entity, dao));
115         entity.setLoRepository(dao.fetch(LoRepository.class, dto.getLoRepositoryKey()));
116         entity.setLoCategoryType(dao.fetch(LoCategoryType.class, dto.getTypeKey()));
117         entity.setState(dto.getStateKey());
118         return entity;
119     }
120 
121     public static LoCategoryInfo toLoCategoryInfo(LoCategory entity) {
122         LoCategoryInfo dto = new LoCategoryInfo();
123 
124         BeanUtils.copyProperties(entity, dto,
125                 new String[] {"descr", "attributes", "loRepository", "loCategoryType", "state", "meta" });
126         dto.setDescr(toRichTextInfo(entity.getDescr()));
127         dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
128         dto.setAttributes(toAttributeList(entity.getAttributes()));
129         dto.setLoRepositoryKey(entity.getLoRepository().getId());
130         dto.setStateKey(entity.getState());
131         dto.setTypeKey(entity.getLoCategoryType().getId());
132         return dto;
133     }
134     
135     public static LoRepositoryInfo toLoRepositoryInfo(LoRepository entity) {
136         LoRepositoryInfo dto = new LoRepositoryInfo();
137         
138         BeanUtils.copyProperties(entity, dto,
139                 new String[] { "descr", "attributes", "rootLo" });
140         dto.setKey(entity.getId());
141         dto.setDescr(toRichTextInfo(entity.getDescr()));
142         dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
143         dto.setAttributes(toAttributeList(entity.getAttributes()));
144         dto.setRootLoId(entity.getRootLo() == null? null :entity.getRootLo().getId());
145         return dto;
146     }
147     
148     public static List<LoInfo> toLoInfos(List<Lo> los) {
149         List<LoInfo> list = new ArrayList<LoInfo>();
150         for (Lo lo : los) {
151             list.add(toLoInfo(lo));
152         }
153         return list;
154     }
155 
156     public static List<LoCategoryInfo> toLoCategoryInfos(List<LoCategory> categories) {
157     	if (null == categories) {
158     		return new ArrayList<LoCategoryInfo>(0);
159     	}
160         List<LoCategoryInfo> list = new ArrayList<LoCategoryInfo>(categories.size());
161         for (LoCategory loCategory : categories) {
162             list.add(toLoCategoryInfo(loCategory));
163         }
164         return list;
165     }
166 
167     public static List<LoRepositoryInfo> toLoRepositoryInfos(List<LoRepository> repositories) {
168         List<LoRepositoryInfo> list = new ArrayList<LoRepositoryInfo>();
169         for (LoRepository loRepository : repositories) {
170             list.add(toLoRepositoryInfo(loRepository));
171         }
172         return list;
173     }
174 	
175 	public static LoLoRelation toLoLoRelation(boolean isUpdate, LoLoRelationInfo dto, CrudDao dao) throws DoesNotExistException, VersionMismatchException, InvalidParameterException {
176 		return toLoLoRelation(isUpdate, null, dto, dao);
177 	}
178 	
179 	public static LoLoRelation toLoLoRelation(boolean isUpdate, LoLoRelation entity, LoLoRelationInfo dto, CrudDao dao) throws DoesNotExistException, VersionMismatchException, InvalidParameterException {
180         if(null == dto) {
181             return null;
182         }
183         LoLoRelation llRelation;
184 
185         if (isUpdate) {
186             llRelation = dao.fetch(LoLoRelation.class, dto.getId());
187             if (llRelation == null) {
188                 throw new DoesNotExistException((new StringBuilder()).append("LoLoRelation does not exist for id: ").append(dto.getId()).toString());
189             }
190             if ( ! String.valueOf(llRelation.getVersionNumber()).equals(dto.getMeta().getVersionInd()) ) {
191                 throw new VersionMismatchException("LoLoRelation to be updated is not the current version");
192             }
193         } else {
194             llRelation = new LoLoRelation();
195         }
196 
197         BeanUtils.copyProperties(dto, llRelation, new String[] { "loId", "relatedLoId", "attributes", "meta" });
198         
199         llRelation.setAttributes(toGenericAttributes(LoLoRelationAttribute.class, dto.getAttributes(), llRelation, dao));
200 
201         Lo lo = null;
202         Lo relatedLo = null;
203         LoLoRelationType relationType = null;
204         try {
205 	        lo = dao.fetch(Lo.class, dto.getLoId());
206 	        relatedLo = dao.fetch(Lo.class, dto.getRelatedLoId());
207 	        relationType = dao.fetch(LoLoRelationType.class, dto.getTypeKey());
208         } catch (DoesNotExistException dnee) {
209         	throw new DoesNotExistException((null == lo ? "Lo" : (null == relatedLo ? "Related Lo" : "Lo-Lo relation type")) +
210         									" does not exist when creating LoLoRelation", dnee);
211         }
212         
213         llRelation.setLo(lo);
214         llRelation.setRelatedLo(relatedLo);
215         llRelation.setLoLoRelationType(relationType);
216         llRelation.setState(dto.getStateKey());
217         
218         return llRelation;
219 	}
220 	
221 	public static LoLoRelationInfo toLoLoRelationInfo(LoLoRelation entity) {
222 		LoLoRelationInfo dto = new LoLoRelationInfo();
223 		
224         BeanUtils.copyProperties(entity, dto,
225                 new String[] {"meta", "lo", "relatedLo", "type", "attributes", "state" });
226         dto.setLoId(entity.getLo().getId());
227         dto.setRelatedLoId(entity.getRelatedLo().getId());
228         dto.setTypeKey(entity.getLoLoRelationType().getId());
229         dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
230         dto.setAttributes(toAttributeList(entity.getAttributes()));
231         dto.setStateKey(entity.getState());
232         return dto;
233 	}
234 
235 	public static List<LoLoRelationInfo> toLoLoRelationInfos( List<LoLoRelation> llRelations) {
236 		List<LoLoRelationInfo> llRelInfos = new ArrayList<LoLoRelationInfo>();
237 		for (LoLoRelation llRelation : llRelations) {
238 			llRelInfos.add(toLoLoRelationInfo(llRelation));
239 		}
240 		return llRelInfos;
241 	}
242 }