| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.common.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.common.dao.CrudDao; |
| 25 | |
import org.kuali.student.common.dto.MetaInfo; |
| 26 | |
import org.kuali.student.common.dto.RichTextInfo; |
| 27 | |
import org.kuali.student.common.dto.TypeInfo; |
| 28 | |
import org.kuali.student.common.entity.Attribute; |
| 29 | |
import org.kuali.student.common.entity.AttributeOwner; |
| 30 | |
import org.kuali.student.common.entity.Meta; |
| 31 | |
import org.kuali.student.common.entity.MetaEntity; |
| 32 | |
import org.kuali.student.common.entity.RichText; |
| 33 | |
import org.kuali.student.common.entity.Type; |
| 34 | |
import org.kuali.student.common.entity.Version; |
| 35 | |
import org.kuali.student.common.exceptions.InvalidParameterException; |
| 36 | |
import org.kuali.student.common.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 | |
|
| 67 | 0 | for (A attribute : owner.getAttributes()) { |
| 68 | 0 | currentAttributes.put(attribute.getName(), attribute); |
| 69 | |
|
| 70 | |
} |
| 71 | |
|
| 72 | |
|
| 73 | 0 | owner.getAttributes().clear(); |
| 74 | |
|
| 75 | |
|
| 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 | |
|
| 95 | |
|
| 96 | 0 | return attributes; |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public static <T extends TypeInfo, S extends Type<?>> T toGenericTypeInfo( |
| 111 | |
Class<T> typeInfoClass, S typeEntity) { |
| 112 | 0 | if (typeEntity == null) { |
| 113 | 0 | return null; |
| 114 | |
} |
| 115 | |
|
| 116 | |
T typeInfo; |
| 117 | |
try { |
| 118 | |
|
| 119 | |
|
| 120 | 0 | typeInfo = typeInfoClass.newInstance(); |
| 121 | 0 | BeanUtils.copyProperties(typeEntity, typeInfo, |
| 122 | |
new String[] { "attributes" }); |
| 123 | |
|
| 124 | |
|
| 125 | 0 | typeInfo.setAttributes(toAttributeMap(typeEntity.getAttributes())); |
| 126 | |
|
| 127 | |
|
| 128 | 0 | typeInfo.setDescr(typeEntity.getDescr()); |
| 129 | |
|
| 130 | 0 | return typeInfo; |
| 131 | |
|
| 132 | 0 | } catch (Exception e) { |
| 133 | 0 | logger.error("Exception occured: ", e); |
| 134 | |
} |
| 135 | 0 | return null; |
| 136 | |
|
| 137 | |
} |
| 138 | |
|
| 139 | |
public static <T extends TypeInfo, S extends Type<?>> List<T> toGenericTypeInfoList( |
| 140 | |
Class<T> typeInfoClass, List<S> typeEntities) { |
| 141 | 0 | List<T> typeInfoList = new ArrayList<T>(); |
| 142 | 0 | if(typeEntities!=null){ |
| 143 | 0 | for (S typeEntity : typeEntities) { |
| 144 | 0 | typeInfoList.add(toGenericTypeInfo(typeInfoClass, typeEntity)); |
| 145 | |
} |
| 146 | |
} |
| 147 | 0 | return typeInfoList; |
| 148 | |
} |
| 149 | |
|
| 150 | |
public static List<String> toGenericTypeKeyList( List<? extends Type<?>> typeEntities){ |
| 151 | 0 | List<String> typeKeys = new ArrayList<String>(); |
| 152 | 0 | if(typeEntities!=null){ |
| 153 | 0 | for(Type<?> typeEntity:typeEntities){ |
| 154 | 0 | typeKeys.add(typeEntity.getId()); |
| 155 | |
} |
| 156 | |
} |
| 157 | 0 | return typeKeys; |
| 158 | |
} |
| 159 | |
|
| 160 | |
protected static MetaInfo toMetaInfo(MetaEntity metaEntity) { |
| 161 | 0 | if(metaEntity == null){ |
| 162 | 0 | return null; |
| 163 | |
} |
| 164 | 0 | return toMetaInfo(metaEntity.getMeta(), metaEntity.getVersionNumber()); |
| 165 | |
} |
| 166 | |
|
| 167 | |
protected static MetaInfo toMetaInfo(Meta meta, Long versionInd) { |
| 168 | |
|
| 169 | 0 | MetaInfo metaInfo = new MetaInfo(); |
| 170 | |
|
| 171 | 0 | if (meta != null) { |
| 172 | 0 | BeanUtils.copyProperties(meta, metaInfo); |
| 173 | |
} |
| 174 | 0 | if(versionInd==null){ |
| 175 | 0 | metaInfo.setVersionInd(null); |
| 176 | |
}else{ |
| 177 | 0 | metaInfo.setVersionInd(versionInd.toString()); |
| 178 | |
} |
| 179 | |
|
| 180 | 0 | return metaInfo; |
| 181 | |
} |
| 182 | |
|
| 183 | |
public static <T extends RichText> T toRichText(Class<T> richTextClass, RichTextInfo richTextInfo) { |
| 184 | 0 | if(richTextInfo == null){ |
| 185 | 0 | return null; |
| 186 | |
} |
| 187 | |
|
| 188 | 0 | T richText = null; |
| 189 | |
|
| 190 | |
try { |
| 191 | 0 | richText = richTextClass.newInstance(); |
| 192 | 0 | BeanUtils.copyProperties(richTextInfo, richText); |
| 193 | 0 | } catch (Exception e) { |
| 194 | 0 | throw new RuntimeException(e); |
| 195 | 0 | } |
| 196 | |
|
| 197 | 0 | return richText; |
| 198 | |
} |
| 199 | |
|
| 200 | |
public static RichTextInfo toRichTextInfo(RichText entity) { |
| 201 | 0 | if(entity==null){ |
| 202 | 0 | return null; |
| 203 | |
} |
| 204 | |
|
| 205 | 0 | RichTextInfo dto = new RichTextInfo(); |
| 206 | 0 | BeanUtils.copyProperties(entity, dto, new String[] { "id" }); |
| 207 | |
|
| 208 | 0 | return dto; |
| 209 | |
} |
| 210 | |
|
| 211 | |
public static VersionInfo toVersionInfo(Version version) { |
| 212 | 0 | if(version==null){ |
| 213 | 0 | return null; |
| 214 | |
} |
| 215 | 0 | VersionInfo versionInfo = new VersionInfo(); |
| 216 | 0 | versionInfo.setCurrentVersionStart(version.getCurrentVersionStart()); |
| 217 | 0 | versionInfo.setCurrentVersionEnd(version.getCurrentVersionEnd()); |
| 218 | 0 | versionInfo.setSequenceNumber(version.getSequenceNumber()); |
| 219 | 0 | versionInfo.setVersionComment(version.getVersionComment()); |
| 220 | 0 | versionInfo.setVersionIndId(version.getVersionIndId()); |
| 221 | 0 | versionInfo.setVersionedFromId(version.getVersionedFromId()); |
| 222 | |
|
| 223 | 0 | return versionInfo; |
| 224 | |
} |
| 225 | |
} |