| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.core.document.service.impl; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.ArrayList; | 
  | 19 |  |  import java.util.List; | 
  | 20 |  |   | 
  | 21 |  |  import org.kuali.student.core.document.dao.DocumentDao; | 
  | 22 |  |  import org.kuali.student.core.document.dto.DocumentBinaryInfo; | 
  | 23 |  |  import org.kuali.student.core.document.dto.DocumentCategoryInfo; | 
  | 24 |  |  import org.kuali.student.core.document.dto.DocumentInfo; | 
  | 25 |  |  import org.kuali.student.core.document.dto.RefDocRelationInfo; | 
  | 26 |  |  import org.kuali.student.core.document.entity.Document; | 
  | 27 |  |  import org.kuali.student.core.document.entity.DocumentAttribute; | 
  | 28 |  |  import org.kuali.student.core.document.entity.DocumentCategory; | 
  | 29 |  |  import org.kuali.student.core.document.entity.DocumentRichText; | 
  | 30 |  |  import org.kuali.student.core.document.entity.RefDocRelation; | 
  | 31 |  |  import org.kuali.student.core.document.entity.RefDocRelationAttribute; | 
  | 32 |  |  import org.kuali.student.core.document.entity.RefDocRelationType; | 
  | 33 |  |  import org.kuali.student.core.document.entity.RefObjectSubType; | 
  | 34 |  |  import org.kuali.student.core.document.entity.RefObjectType; | 
  | 35 |  |  import org.kuali.student.core.exceptions.DoesNotExistException; | 
  | 36 |  |  import org.kuali.student.core.exceptions.InvalidParameterException; | 
  | 37 |  |  import org.kuali.student.core.exceptions.OperationFailedException; | 
  | 38 |  |  import org.kuali.student.core.service.impl.BaseAssembler; | 
  | 39 |  |  import org.springframework.beans.BeanUtils; | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |   | 
  | 43 |  |   | 
  | 44 |  |   | 
  | 45 |  |   | 
  | 46 |  |   | 
  | 47 | 0 |  public class DocumentServiceAssembler extends BaseAssembler { | 
  | 48 |  |   | 
  | 49 |  |      public static DocumentInfo toDocumentInfo(Document entity) { | 
  | 50 | 0 |          DocumentInfo dto = new DocumentInfo(); | 
  | 51 |  |   | 
  | 52 | 0 |          BeanUtils.copyProperties(entity, dto, | 
  | 53 |  |                  new String[] { "desc", "attributes", "metaInfo","type", "categoryList", "document" }); | 
  | 54 | 0 |          dto.setDesc(toRichTextInfo(entity.getDescr())); | 
  | 55 | 0 |          dto.setMetaInfo(toMetaInfo(entity)); | 
  | 56 | 0 |          dto.setAttributes(toAttributeMap(entity.getAttributes())); | 
  | 57 | 0 |          dto.setType(entity.getType().getId()); | 
  | 58 | 0 |          dto.setDocumentBinaryInfo(new DocumentBinaryInfo()); | 
  | 59 | 0 |          dto.getDocumentBinaryInfo().setBinary(entity.getDocument()); | 
  | 60 | 0 |          return dto; | 
  | 61 |  |      } | 
  | 62 |  |       | 
  | 63 |  |      public static List<DocumentInfo> toDocumentInfos(List<Document> entities) { | 
  | 64 | 0 |          List<DocumentInfo> dtos = new ArrayList<DocumentInfo>(); | 
  | 65 | 0 |          if(entities!=null){ | 
  | 66 | 0 |                  for (Document entity : entities) { | 
  | 67 | 0 |                          dtos.add(toDocumentInfo(entity)); | 
  | 68 |  |                  } | 
  | 69 |  |                 } | 
  | 70 | 0 |          return dtos; | 
  | 71 |  |      } | 
  | 72 |  |   | 
  | 73 |  |      public static Document toDocument(Document entity, DocumentInfo dto, DocumentDao dao) throws InvalidParameterException { | 
  | 74 | 0 |          BeanUtils.copyProperties(dto, entity, | 
  | 75 |  |                  new String[] { "desc", "attributes", "metaInfo", "type", "id", "documentBinaryInfo" }); | 
  | 76 | 0 |          entity.setDescr(toRichText(DocumentRichText.class, dto.getDesc())); | 
  | 77 | 0 |          entity.setDocument(dto.getDocumentBinaryInfo().getBinary()); | 
  | 78 | 0 |          entity.setAttributes(toGenericAttributes(DocumentAttribute.class, dto.getAttributes(), entity, dao)); | 
  | 79 | 0 |          return entity; | 
  | 80 |  |      } | 
  | 81 |  |   | 
  | 82 |  |      public static DocumentCategoryInfo toDocumentCategoryInfo(DocumentCategory entity) { | 
  | 83 | 0 |          DocumentCategoryInfo dto = new DocumentCategoryInfo(); | 
  | 84 |  |   | 
  | 85 | 0 |          BeanUtils.copyProperties(entity, dto, | 
  | 86 |  |                  new String[] { "desc", "attributes", "type", "documents" }); | 
  | 87 | 0 |          dto.setDesc(toRichTextInfo(entity.getDescr())); | 
  | 88 | 0 |          dto.setAttributes(toAttributeMap(entity.getAttributes())); | 
  | 89 | 0 |          return dto; | 
  | 90 |  |      } | 
  | 91 |  |   | 
  | 92 |  |      public static List<DocumentCategoryInfo> toDocumentCategoryInfos(List<DocumentCategory> entities) { | 
  | 93 | 0 |          List<DocumentCategoryInfo> dtos = new ArrayList<DocumentCategoryInfo>(); | 
  | 94 | 0 |          if(entities!=null){ | 
  | 95 | 0 |                  for (DocumentCategory entity : entities) { | 
  | 96 | 0 |                          dtos.add(toDocumentCategoryInfo(entity)); | 
  | 97 |  |                  } | 
  | 98 |  |          } | 
  | 99 | 0 |          return dtos; | 
  | 100 |  |      } | 
  | 101 |  |   | 
  | 102 |  |          public static RefDocRelationInfo toRefDocRelationInfo(RefDocRelation entity) { | 
  | 103 | 0 |                  RefDocRelationInfo dto = new RefDocRelationInfo(); | 
  | 104 |  |                   | 
  | 105 | 0 |                  dto.setAttributes(toAttributeMap(entity.getAttributes())); | 
  | 106 | 0 |                  dto.setDesc(toRichTextInfo(entity.getDescr())); | 
  | 107 | 0 |                  dto.setDocumentId(entity.getDocument().getId()); | 
  | 108 | 0 |                  dto.setEffectiveDate(entity.getEffectiveDate()); | 
  | 109 | 0 |                  dto.setExpirationDate(entity.getExpirationDate()); | 
  | 110 | 0 |                  dto.setId(entity.getId()); | 
  | 111 | 0 |                  dto.setMetaInfo(toMetaInfo(entity)); | 
  | 112 | 0 |                  dto.setRefObjectId(entity.getRefObjectId()); | 
  | 113 | 0 |                  dto.setRefObjectTypeKey(entity.getRefObjectType().getId()); | 
  | 114 | 0 |                  dto.setState(entity.getState()); | 
  | 115 | 0 |                  dto.setTitle(entity.getTitle()); | 
  | 116 | 0 |                  dto.setType(entity.getRefDocRelationType().getId()); | 
  | 117 |  |                   | 
  | 118 | 0 |                  return dto; | 
  | 119 |  |          } | 
  | 120 |  |   | 
  | 121 |  |          public static List<RefDocRelationInfo> toRefDocRelationInfos( | 
  | 122 |  |                          List<RefDocRelation> entities) { | 
  | 123 | 0 |                  List<RefDocRelationInfo> dtos = new ArrayList<RefDocRelationInfo>(); | 
  | 124 | 0 |                  if(entities!=null){ | 
  | 125 | 0 |                          for(RefDocRelation entity:entities){ | 
  | 126 | 0 |                                  dtos.add(toRefDocRelationInfo(entity)); | 
  | 127 |  |                          } | 
  | 128 |  |                  } | 
  | 129 | 0 |                  return dtos; | 
  | 130 |  |          } | 
  | 131 |  |   | 
  | 132 |  |          public static RefDocRelation toRefDocRelation(RefDocRelation entity, | 
  | 133 |  |                          RefDocRelationInfo dto, DocumentDao dao) throws InvalidParameterException, OperationFailedException { | 
  | 134 |  |           | 
  | 135 |  |                   | 
  | 136 | 0 |                  RefObjectType refObjectType = null;  | 
  | 137 |  |          RefDocRelationType refDocRelationType; | 
  | 138 |  |          Document document; | 
  | 139 |  |           | 
  | 140 |  |           | 
  | 141 |  |          try{ | 
  | 142 | 0 |                  refDocRelationType = dao.fetch(RefDocRelationType.class, dto.getType()); | 
  | 143 | 0 |          }catch(DoesNotExistException e){ | 
  | 144 | 0 |                  throw new InvalidParameterException("RefDocRelationType does not exist for key:" + dto.getType()); | 
  | 145 | 0 |          }  | 
  | 146 |  |           | 
  | 147 |  |           | 
  | 148 | 0 |          if(refDocRelationType.getRefObjectSubTypes()==null){ | 
  | 149 | 0 |                  throw new OperationFailedException("No RefObjectSubTypes are defined for this relation Type"); | 
  | 150 |  |          } | 
  | 151 |  |           | 
  | 152 |  |           | 
  | 153 | 0 |          for(RefObjectSubType subType:refDocRelationType.getRefObjectSubTypes()){ | 
  | 154 | 0 |                  if(dto.getRefObjectTypeKey().equals(subType.getRefObjectType().getId())){ | 
  | 155 | 0 |                          refObjectType = subType.getRefObjectType(); | 
  | 156 | 0 |                          break; | 
  | 157 |  |                  } | 
  | 158 |  |          } | 
  | 159 | 0 |          if(refObjectType == null){ | 
  | 160 | 0 |                  throw new InvalidParameterException("RefObjectType: " + dto.getRefObjectTypeKey()+ | 
  | 161 |  |                                  " does not exist or is invalid for relation type: " + dto.getType()); | 
  | 162 |  |          } | 
  | 163 |  |           | 
  | 164 |  |           | 
  | 165 |  |          try{ | 
  | 166 | 0 |                  document = dao.fetch(Document.class, dto.getDocumentId()); | 
  | 167 | 0 |          }catch(DoesNotExistException e){ | 
  | 168 | 0 |                  throw new InvalidParameterException("Document does not exist for key:" + dto.getDocumentId()); | 
  | 169 | 0 |          }  | 
  | 170 |  |           | 
  | 171 |  |           | 
  | 172 | 0 |          entity.setAttributes(toGenericAttributes(RefDocRelationAttribute.class, dto.getAttributes(), entity, dao)); | 
  | 173 | 0 |          entity.setDescr(toRichText(DocumentRichText.class, dto.getDesc())); | 
  | 174 | 0 |          entity.setDocument(document); | 
  | 175 | 0 |          entity.setEffectiveDate(dto.getEffectiveDate()); | 
  | 176 | 0 |          entity.setExpirationDate(dto.getExpirationDate()); | 
  | 177 | 0 |          entity.setId(dto.getId()); | 
  | 178 | 0 |          entity.setRefDocRelationType(refDocRelationType); | 
  | 179 | 0 |          entity.setRefObjectId(dto.getRefObjectId()); | 
  | 180 | 0 |          entity.setRefObjectType(refObjectType); | 
  | 181 | 0 |          entity.setState(dto.getState()); | 
  | 182 | 0 |          entity.setTitle(dto.getTitle()); | 
  | 183 |  |           | 
  | 184 | 0 |          return entity; | 
  | 185 |  |          } | 
  | 186 |  |   | 
  | 187 |  |  } |