Coverage Report - org.kuali.student.core.service.impl.BaseAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseAssembler
0%
0/82
0%
0/34
3.8
 
 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.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 org.apache.log4j.Logger;
 24  
 import org.kuali.student.core.dao.CrudDao;
 25  
 import org.kuali.student.core.dto.MetaInfo;
 26  
 import org.kuali.student.core.dto.RichTextInfo;
 27  
 import org.kuali.student.core.dto.TypeInfo;
 28  
 import org.kuali.student.core.entity.Attribute;
 29  
 import org.kuali.student.core.entity.AttributeOwner;
 30  
 import org.kuali.student.core.entity.Meta;
 31  
 import org.kuali.student.core.entity.MetaEntity;
 32  
 import org.kuali.student.core.entity.RichText;
 33  
 import org.kuali.student.core.entity.Type;
 34  
 import org.kuali.student.core.entity.Version;
 35  
 import org.kuali.student.core.exceptions.InvalidParameterException;
 36  
 import org.kuali.student.core.versionmanagement.dto.VersionInfo;
 37  
 import org.springframework.beans.BeanUtils;
 38  
 
 39  0
 public class BaseAssembler {
 40  
     
 41  0
     final static Logger logger = Logger.getLogger(BaseAssembler.class);
 42  
 
 43  
         public static Map<String, String> toAttributeMap(
 44  
                         List<? extends Attribute<?>> attributes) {
 45  
 
 46  0
                 Map<String, String> attributeInfos = new HashMap<String, String>();
 47  
 
 48  0
                 for (Attribute<?> attribute : attributes) {
 49  0
                         attributeInfos.put(attribute.getName(), attribute.getValue());
 50  
                 }
 51  
 
 52  0
                 return attributeInfos;
 53  
         }
 54  
 
 55  
         public static <A extends Attribute<O>, O extends AttributeOwner<A>> List<A> toGenericAttributes(
 56  
                         Class<A> attributeClass, Map<String, String> attributeMap, O owner,
 57  
                         CrudDao dao) throws InvalidParameterException {
 58  0
                 List<A> attributes = new ArrayList<A>();
 59  
 
 60  0
                 if(owner.getAttributes()==null){
 61  0
                         owner.setAttributes(new ArrayList<A>());
 62  
                 }
 63  
                 
 64  0
                 Map<String, A> currentAttributes = new HashMap<String,A>();
 65  
                 
 66  
                 // Find all the old attributes(if the owner is not null)
 67  0
                 for (A attribute : owner.getAttributes()) {
 68  0
                         currentAttributes.put(attribute.getName(), attribute);
 69  
                         
 70  
                 }
 71  
                 
 72  
                 //Clear out the attributes
 73  0
                 owner.getAttributes().clear();
 74  
         
 75  
                 //Update anything that exists, or create a new attribute if it doesn't
 76  0
                 for (Map.Entry<String, String> attributeEntry : attributeMap.entrySet()) {
 77  
                         
 78  
                         A attribute;
 79  0
                         if(currentAttributes.containsKey(attributeEntry.getKey())){
 80  0
                                 attribute = currentAttributes.remove(attributeEntry.getKey());
 81  
                         }else{
 82  
                                 try{
 83  0
                                         attribute = attributeClass.newInstance();
 84  0
                                 }catch(Exception e){
 85  0
                                         throw new RuntimeException("Error copying attributes.",e);
 86  0
                                 }
 87  0
                                 attribute.setName(attributeEntry.getKey());
 88  0
                                 attribute.setOwner(owner);
 89  
                         }
 90  0
                         attribute.setValue(attributeEntry.getValue());
 91  0
                         attributes.add(attribute);
 92  0
                 }
 93  
                 
 94  
                 //Delete leftovers
 95  0
                 for(A attribute:currentAttributes.values()){
 96  0
                         dao.delete(attribute);
 97  
                 }
 98  
 
 99  0
                 return attributes;
 100  
         }
 101  
 
 102  
         /**
 103  
          * @param <T>
 104  
          *            TypeInfo class
 105  
          * @param <S>
 106  
          *            Type Class
 107  
          * @param typeInfoClass
 108  
          *            the class of the resulting typeInfo object
 109  
          * @param typeEntity
 110  
          *            the typeEntity to copy from
 111  
          * @return a new TypeInfo
 112  
          */
 113  
         public static <T extends TypeInfo, S extends Type<?>> T toGenericTypeInfo(
 114  
                         Class<T> typeInfoClass, S typeEntity) {
 115  0
                 if (typeEntity == null) {
 116  0
                         return null;
 117  
                 }
 118  
 
 119  
                 T typeInfo;
 120  
                 try {
 121  
                         // Create a new TypeInfo based on the <T> class and copy the
 122  
                         // properties
 123  0
                         typeInfo = typeInfoClass.newInstance();
 124  0
                         BeanUtils.copyProperties(typeEntity, typeInfo,
 125  
                                         new String[] { "attributes" });
 126  
 
 127  
                         // Copy the attributes
 128  0
                         typeInfo.setAttributes(toAttributeMap(typeEntity.getAttributes()));
 129  
 
 130  
                         //Copy the description
 131  0
                         typeInfo.setDescr(typeEntity.getDescr());
 132  
                         
 133  0
                         return typeInfo;
 134  
 
 135  0
                 } catch (Exception e) {
 136  0
                         logger.error("Exception occured: ", e);
 137  
                 }
 138  0
                 return null;
 139  
 
 140  
         }
 141  
 
 142  
         public static <T extends TypeInfo, S extends Type<?>> List<T> toGenericTypeInfoList(
 143  
                         Class<T> typeInfoClass, List<S> typeEntities) {
 144  0
                 List<T> typeInfoList = new ArrayList<T>();
 145  0
                 if(typeEntities!=null){
 146  0
                         for (S typeEntity : typeEntities) {
 147  0
                                 typeInfoList.add(toGenericTypeInfo(typeInfoClass, typeEntity));
 148  
                         }
 149  
                 }
 150  0
                 return typeInfoList;
 151  
         }
 152  
         
 153  
         public static List<String> toGenericTypeKeyList( List<? extends Type<?>> typeEntities){
 154  0
                 List<String> typeKeys = new ArrayList<String>();
 155  0
                 if(typeEntities!=null){
 156  0
                         for(Type<?> typeEntity:typeEntities){
 157  0
                                 typeKeys.add(typeEntity.getId());
 158  
                         }
 159  
                 }
 160  0
                 return typeKeys;
 161  
         }
 162  
 
 163  
         protected static MetaInfo toMetaInfo(MetaEntity metaEntity) {
 164  0
                 if(metaEntity == null){
 165  0
                         return null;
 166  
                 }
 167  0
                 return toMetaInfo(metaEntity.getMeta(), metaEntity.getVersionNumber());
 168  
         }
 169  
         
 170  
         protected static MetaInfo toMetaInfo(Meta meta, Long versionInd) {
 171  
 
 172  0
                 MetaInfo metaInfo = new MetaInfo();
 173  
                 // If there was a meta passed in then copy the values
 174  0
                 if (meta != null) {
 175  0
                         BeanUtils.copyProperties(meta, metaInfo);
 176  
                 }
 177  0
                 if(versionInd==null){
 178  0
                         metaInfo.setVersionInd(null);
 179  
                 }else{
 180  0
                         metaInfo.setVersionInd(versionInd.toString());
 181  
                 }
 182  
 
 183  0
                 return metaInfo;
 184  
         }
 185  
 
 186  
         public static <T extends RichText> T toRichText(Class<T> richTextClass, RichTextInfo richTextInfo) {
 187  0
                 if(richTextInfo == null){
 188  0
                         return null;
 189  
                 }
 190  
                 
 191  0
                 T richText = null;
 192  
                 
 193  
                 try {
 194  0
                     richText = richTextClass.newInstance();
 195  0
                     BeanUtils.copyProperties(richTextInfo, richText);
 196  0
             } catch (Exception e) {
 197  0
             throw new RuntimeException(e);
 198  0
         }
 199  
             
 200  0
                 return richText;
 201  
         }
 202  
 
 203  
         public static RichTextInfo toRichTextInfo(RichText entity) {
 204  0
         if(entity==null){
 205  0
             return null;
 206  
         }
 207  
         
 208  0
         RichTextInfo dto = new RichTextInfo();
 209  0
         BeanUtils.copyProperties(entity, dto, new String[] { "id" });
 210  
         
 211  0
         return dto;
 212  
     }
 213  
         
 214  
         public static VersionInfo toVersionInfo(Version version) {
 215  0
                 if(version==null){
 216  0
                         return null;
 217  
                 }
 218  0
                 VersionInfo versionInfo = new VersionInfo();
 219  0
                 versionInfo.setCurrentVersionStart(version.getCurrentVersionStart());
 220  0
                 versionInfo.setCurrentVersionEnd(version.getCurrentVersionEnd());
 221  0
                 versionInfo.setSequenceNumber(version.getSequenceNumber());
 222  0
                 versionInfo.setVersionComment(version.getVersionComment());
 223  0
                 versionInfo.setVersionIndId(version.getVersionIndId());
 224  0
                 versionInfo.setVersionedFromId(version.getVersionedFromId());
 225  
                 
 226  0
                 return versionInfo;
 227  
         }
 228  
 }