Coverage Report - org.kuali.student.r2.core.class1.organization.service.impl.OrganizationAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
OrganizationAssembler
0%
0/273
0%
0/84
4.792
 
 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.core.class1.organization.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.entity.Attribute;
 26  
 import org.kuali.student.common.entity.AttributeOwner;
 27  
 import org.kuali.student.common.entity.Meta;
 28  
 import org.kuali.student.common.entity.TimeAmount;
 29  
 import org.kuali.student.common.entity.Type;
 30  
 import org.kuali.student.common.service.impl.BaseAssembler;
 31  
 import org.kuali.student.core.organization.dao.OrganizationDao;
 32  
 import org.kuali.student.core.organization.entity.Org;
 33  
 import org.kuali.student.core.organization.entity.OrgAttribute;
 34  
 import org.kuali.student.core.organization.entity.OrgHierarchy;
 35  
 import org.kuali.student.core.organization.entity.OrgOrgRelation;
 36  
 import org.kuali.student.core.organization.entity.OrgOrgRelationAttribute;
 37  
 import org.kuali.student.core.organization.entity.OrgOrgRelationType;
 38  
 import org.kuali.student.core.organization.entity.OrgPersonRelation;
 39  
 import org.kuali.student.core.organization.entity.OrgPersonRelationAttribute;
 40  
 import org.kuali.student.core.organization.entity.OrgPersonRelationType;
 41  
 import org.kuali.student.core.organization.entity.OrgPositionRestriction;
 42  
 import org.kuali.student.core.organization.entity.OrgPositionRestrictionAttribute;
 43  
 import org.kuali.student.core.organization.entity.OrgType;
 44  
 import org.kuali.student.r2.common.dto.AttributeInfo;
 45  
 import org.kuali.student.r2.common.dto.MetaInfo;
 46  
 import org.kuali.student.r2.common.dto.RichTextInfo;
 47  
 import org.kuali.student.r2.common.dto.TimeAmountInfo;
 48  
 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
 49  
 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
 50  
 import org.kuali.student.r2.common.exceptions.VersionMismatchException;
 51  
 import org.kuali.student.r2.core.organization.dto.OrgHierarchyInfo;
 52  
 import org.kuali.student.r2.core.organization.dto.OrgInfo;
 53  
 import org.kuali.student.r2.core.organization.dto.OrgOrgRelationInfo;
 54  
 import org.kuali.student.r2.core.organization.dto.OrgPersonRelationInfo;
 55  
 import org.kuali.student.r2.core.organization.dto.OrgPositionRestrictionInfo;
 56  
 import org.kuali.student.r2.core.type.dto.TypeInfo;
 57  
 import org.springframework.beans.BeanUtils;
 58  
 
 59  0
 public class OrganizationAssembler {
 60  
     
 61  0
     final static Logger logger = Logger.getLogger(BaseAssembler.class);
 62  
 
 63  
     public static List<OrgHierarchyInfo> toOrgHierarchyInfos(List<OrgHierarchy> orgHierarchys) {
 64  0
         List<OrgHierarchyInfo> orgHierarchyInfos = new ArrayList<OrgHierarchyInfo>(orgHierarchys.size());
 65  0
         for(OrgHierarchy orgHierarchy:orgHierarchys) {
 66  0
             orgHierarchyInfos.add(toOrgHierarchyInfo(orgHierarchy));
 67  
         }
 68  0
         return orgHierarchyInfos;
 69  
     }
 70  
 
 71  
     public static OrgHierarchyInfo toOrgHierarchyInfo(OrgHierarchy orgHierarchy) {
 72  0
         if (orgHierarchy == null) {
 73  0
             return null;
 74  
         }
 75  
         
 76  0
         OrgHierarchyInfo orgHierarchyInfo = new OrgHierarchyInfo();
 77  
         
 78  0
         BeanUtils.copyProperties(orgHierarchy, orgHierarchyInfo, new String[] { "rootOrgId",
 79  
                                                                                 "attributes", 
 80  
                                                                                 "descr" });
 81  
 
 82  
         // copy attributes and RootOrg
 83  0
         orgHierarchyInfo.setAttributes(toAttributeList(orgHierarchy.getAttributes()));
 84  0
         orgHierarchyInfo.setRootOrgId(orgHierarchy.getRootOrg().getId());
 85  0
         RichTextInfo richText = new RichTextInfo();
 86  0
         richText.setFormatted(orgHierarchy.getDescr());
 87  0
         richText.setPlain(orgHierarchy.getDescr());
 88  0
         orgHierarchyInfo.setDescr(richText);
 89  0
         return orgHierarchyInfo;
 90  
     }
 91  
 
 92  
     public static List<OrgInfo> toOrgInfos(List<Org> orgs) {
 93  0
         List<OrgInfo> orgInfos = new ArrayList<OrgInfo>(orgs.size());
 94  0
         for (Org org : orgs) {
 95  0
             orgInfos.add(toOrgInfo(org));
 96  
         }
 97  0
         return orgInfos;
 98  
     }
 99  
     
 100  
     public static OrgInfo toOrgInfo(Org org) {
 101  0
         if (org == null) {
 102  0
             return null;
 103  
         }
 104  
         
 105  0
         OrgInfo orgInfo = new OrgInfo();
 106  
         
 107  0
         BeanUtils.copyProperties(org, orgInfo, new String[] { "type",
 108  
                                                               "attributes", 
 109  
                                                               "meta", 
 110  
                                                               "shortDesc", 
 111  
                                                               "longDesc" });
 112  
         
 113  
         // copy attributes, metadata, and Type
 114  0
         orgInfo.setAttributes(toAttributeList(org.getAttributes()));
 115  0
         orgInfo.setMeta(toMetaInfo(org.getMeta(), org.getVersionNumber()));
 116  0
         orgInfo.setTypeKey(org.getType().getId());
 117  
         
 118  0
         RichTextInfo shortDescr = new RichTextInfo();
 119  0
         shortDescr.setFormatted(org.getShortDesc());
 120  0
         shortDescr.setPlain(org.getShortDesc());
 121  0
         orgInfo.setShortDescr(shortDescr);
 122  
         
 123  0
         RichTextInfo longDescr = new RichTextInfo();
 124  0
         longDescr.setFormatted(org.getLongDesc());
 125  0
         longDescr.setPlain(org.getLongDesc());
 126  0
         orgInfo.setLongDescr(longDescr);
 127  
         
 128  0
         return orgInfo;
 129  
     }
 130  
     
 131  
     public static List<OrgPersonRelationInfo> toOrgPersonRelationInfos(List<OrgPersonRelation> relations) {
 132  0
         List<OrgPersonRelationInfo> relationInfos = new ArrayList<OrgPersonRelationInfo>(relations.size());
 133  0
         for (OrgPersonRelation relation : relations) {
 134  0
             relationInfos.add(toOrgPersonRelationInfo(relation));
 135  
         }
 136  0
         return relationInfos;
 137  
     }
 138  
     
 139  
     public static OrgPersonRelationInfo toOrgPersonRelationInfo(OrgPersonRelation relation) {
 140  0
         OrgPersonRelationInfo relationInfo = new OrgPersonRelationInfo();
 141  
         
 142  0
         BeanUtils.copyProperties(relation, relationInfo, new String[] { "type",
 143  
                                                                         "attributes", 
 144  
                                                                         "meta", 
 145  
                                                                         "orgId"});
 146  
         
 147  0
         relationInfo.setOrgId(relation.getOrg().getId());
 148  0
         relationInfo.setAttributes(toAttributeList(relation.getAttributes()));
 149  0
         relationInfo.setMeta(toMetaInfo(relation.getMeta(), relation.getVersionNumber()));
 150  0
         relationInfo.setTypeKey(relation.getType().getId());
 151  
         //relationInfo.setId(relation.getId());
 152  0
         return relationInfo;
 153  
     }
 154  
     
 155  
     public static List<OrgOrgRelationInfo> toOrgOrgRelationInfos(List<OrgOrgRelation> orgOrgRelations) {
 156  0
         List<OrgOrgRelationInfo> orgOrgRelationInfo = new ArrayList<OrgOrgRelationInfo>();
 157  0
         for(OrgOrgRelation orgOrgRelation:orgOrgRelations){
 158  0
             orgOrgRelationInfo.add(toOrgOrgRelationInfo(orgOrgRelation));
 159  
         }
 160  0
         return orgOrgRelationInfo;
 161  
     }
 162  
     
 163  
     public static OrgOrgRelationInfo toOrgOrgRelationInfo(OrgOrgRelation orgOrgRelation) {
 164  0
         if (orgOrgRelation == null) {
 165  0
             return null;
 166  
         }
 167  
         
 168  0
         OrgOrgRelationInfo orgOrgRelationInfo = new OrgOrgRelationInfo();
 169  
         
 170  0
         BeanUtils.copyProperties(orgOrgRelation, orgOrgRelationInfo, new String[] { "type",
 171  
                                                                                     "attributes", 
 172  
                                                                                     "meta",
 173  
                                                                                     "orgId",
 174  
                                                                                     "relatedOrgId" });
 175  
         
 176  
         // copy attributes, metadata, Type, and related orgs
 177  0
         orgOrgRelationInfo.setAttributes(toAttributeList(orgOrgRelation.getAttributes()));
 178  0
         orgOrgRelationInfo.setMeta(toMetaInfo(orgOrgRelation.getMeta(), orgOrgRelation.getVersionNumber()));
 179  0
         orgOrgRelationInfo.setTypeKey(orgOrgRelation.getType().getId());
 180  0
         orgOrgRelationInfo.setOrgId(orgOrgRelation.getOrg().getId());
 181  0
         orgOrgRelationInfo.setRelatedOrgId(orgOrgRelation.getRelatedOrg().getId());
 182  
         
 183  0
         return orgOrgRelationInfo;
 184  
     }
 185  
     
 186  
     public static OrgPositionRestrictionInfo toOrgPositionRestrictionInfo(OrgPositionRestriction restriction) {
 187  0
         OrgPositionRestrictionInfo restrictionInfo = new OrgPositionRestrictionInfo();
 188  
         
 189  0
         BeanUtils.copyProperties(restriction, restrictionInfo, new String[] { 
 190  
                 "attributes", "meta", "orgId","personRelationType","stdDuration", "descr" });
 191  
         
 192  0
         if (restriction.getStdDuration()!=null) {
 193  0
             restrictionInfo.setStdDuration(new TimeAmountInfo());
 194  0
             BeanUtils.copyProperties(restriction.getStdDuration(), restrictionInfo.getStdDuration());
 195  
         }
 196  
         
 197  0
         restrictionInfo.setOrgId(restriction.getOrg().getId());
 198  0
         restrictionInfo.setAttributes(toAttributeList(restriction.getAttributes()));
 199  0
         restrictionInfo.setMeta(toMetaInfo(restriction.getMeta(), restriction.getVersionNumber()));
 200  0
         restrictionInfo.setOrgPersonRelationTypeKey(restriction.getPersonRelationType().getId());
 201  0
         RichTextInfo richText = new RichTextInfo();
 202  0
         richText.setFormatted(restriction.getDescr());
 203  0
         richText.setPlain(restriction.getDescr());
 204  0
         restrictionInfo.setDescr(richText);
 205  0
         return restrictionInfo;
 206  
         }
 207  
 
 208  
     public static List<OrgPositionRestrictionInfo> toOrgPositionRestrictionInfos(List<OrgPositionRestriction> restrictions) {
 209  0
         List<OrgPositionRestrictionInfo> restrictionInfos = new ArrayList<OrgPositionRestrictionInfo>(restrictions.size());
 210  0
         for (OrgPositionRestriction restriction : restrictions) {
 211  0
             restrictionInfos.add(toOrgPositionRestrictionInfo(restriction));
 212  
         }
 213  0
         return restrictionInfos;
 214  
     }
 215  
     
 216  
     public static TypeInfo toOrgTypeInfo(OrgType orgType) {
 217  0
         return toGenericTypeInfo(orgType);
 218  
     }
 219  
     
 220  
     public static List<TypeInfo> toOrgTypeInfos(List<OrgType> orgTypes) {
 221  0
         List<TypeInfo> orgTypeInfos = new ArrayList<TypeInfo>(orgTypes.size());
 222  0
         for (OrgType orgType : orgTypes) {
 223  0
             orgTypeInfos.add(toOrgTypeInfo(orgType));
 224  
         }
 225  0
         return orgTypeInfos;
 226  
     }
 227  
     
 228  
     public static TypeInfo toOrgPersonRelationTypeInfo(OrgPersonRelationType orgPersonRelationType) {
 229  0
         return toGenericTypeInfo(orgPersonRelationType);
 230  
     }
 231  
     
 232  
     public static List<TypeInfo> toOrgPersonRelationTypeInfos(List<OrgPersonRelationType> orgPersonRelationTypes) {
 233  0
         List<TypeInfo> oprtys = new ArrayList<TypeInfo>(orgPersonRelationTypes.size());
 234  0
         for (OrgPersonRelationType type : orgPersonRelationTypes) {
 235  0
             oprtys.add(toOrgPersonRelationTypeInfo(type));
 236  
         }
 237  0
         return oprtys;
 238  
     }
 239  
     
 240  
     public static TypeInfo toOrgOrgRelationTypeInfo(OrgOrgRelationType orgOrgRelationType) {
 241  0
         return toGenericTypeInfo(orgOrgRelationType);
 242  
     }
 243  
     
 244  
     public static List<TypeInfo> toOrgOrgRelationTypeInfos(List<OrgOrgRelationType> orgOrgRelationTypes) {
 245  0
         List<TypeInfo> orgOrgRelationTypeInfos = new ArrayList<TypeInfo>(orgOrgRelationTypes.size());
 246  0
         for (OrgOrgRelationType orgOrgRelationType : orgOrgRelationTypes) {
 247  0
             orgOrgRelationTypeInfos.add(toOrgOrgRelationTypeInfo(orgOrgRelationType));
 248  
         }
 249  
         
 250  0
         return orgOrgRelationTypeInfos;
 251  
     }
 252  
     
 253  
     public static Org toOrg(boolean isUpdate, OrgInfo orgInfo, OrganizationDao dao)
 254  
         throws InvalidParameterException, DoesNotExistException, VersionMismatchException {
 255  
 
 256  0
         Org org = null;
 257  0
         if (isUpdate) {
 258  
             try {
 259  0
                 org = dao.fetch(Org.class, orgInfo.getId());
 260  0
             } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 261  0
                 throw new DoesNotExistException("Org does not exist for id: " + orgInfo.getId());
 262  0
             }
 263  0
             if (org == null) {
 264  0
                 throw new DoesNotExistException("Org does not exist for id: " + orgInfo.getId());
 265  
             }
 266  0
             if (!String.valueOf(org.getVersionNumber()).equals(orgInfo.getMeta().getVersionInd())){
 267  0
                 throw new VersionMismatchException("Org to be updated is not the current version");
 268  
             }
 269  
         } else {
 270  0
             org = new Org();
 271  
         }
 272  
         
 273  
         // Copy all basic properties
 274  0
         BeanUtils.copyProperties(orgInfo, org, new String[] { "type",
 275  
                                                               "attributes", 
 276  
                                                               "meta", 
 277  
                                                               "longDesc", 
 278  
                                                               "shortDesc" });
 279  
         
 280  
         // Copy Description
 281  0
         org.setLongDesc(orgInfo.getLongDescr().getPlain());
 282  0
         org.setShortDesc(orgInfo.getShortDescr().getPlain());
 283  
         
 284  
         // Copy Attributes
 285  0
         org.setAttributes(toGenericAttributes(OrgAttribute.class, orgInfo.getAttributes(), org, dao));
 286  
         
 287  
         // Search for and copy the type
 288  0
         OrgType orgType = null;
 289  
         try {
 290  0
             orgType = dao.fetch(OrgType.class, orgInfo.getTypeKey());
 291  0
         } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 292  0
             throw new DoesNotExistException("OrgType does not exist for id: " + orgInfo.getTypeKey());
 293  0
         }
 294  0
         if (orgType == null) {
 295  0
             throw new InvalidParameterException("OrgType does not exist for id: " + orgInfo.getTypeKey());
 296  
         }
 297  0
         org.setType(orgType);
 298  
         
 299  0
         return org;
 300  
     }
 301  
     
 302  
     public static OrgOrgRelation toOrgOrgRelation(boolean isUpdate,
 303  
                                                   OrgOrgRelationInfo orgOrgRelationInfo,
 304  
                                                   OrganizationDao dao) 
 305  
         throws DoesNotExistException, VersionMismatchException, InvalidParameterException {
 306  
         
 307  
         OrgOrgRelation orgOrgRelation;
 308  0
         if (isUpdate) {
 309  
             try {
 310  0
                 orgOrgRelation = dao.fetch(OrgOrgRelation.class, orgOrgRelationInfo.getId());
 311  0
             } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 312  0
                 throw new DoesNotExistException("OrgOrgRelation does not exist for id: " + orgOrgRelationInfo.getId());
 313  0
             }
 314  0
             if (orgOrgRelation == null) {
 315  0
                 throw new DoesNotExistException("OrgOrgRelation does not exist for id: " + orgOrgRelationInfo.getId());
 316  
             }
 317  0
             if (!String.valueOf(orgOrgRelation.getVersionNumber()).equals(orgOrgRelationInfo.getMeta().getVersionInd())){
 318  0
                 throw new VersionMismatchException("OrgOrgRelation to be updated is not the current version");
 319  
             }
 320  
         } else {
 321  0
             orgOrgRelation = new OrgOrgRelation();
 322  
         }
 323  
         
 324  
         // Copy all basic properties
 325  0
         BeanUtils.copyProperties(orgOrgRelationInfo, orgOrgRelation, new String[] { "type",
 326  
                                                                                     "attributes", 
 327  
                                                                                     "meta", 
 328  
                                                                                     "org", 
 329  
                                                                                     "relatedOrg" });
 330  
         
 331  
         // Copy Attributes
 332  0
         orgOrgRelation.setAttributes(toGenericAttributes(OrgOrgRelationAttribute.class, orgOrgRelationInfo.getAttributes(), orgOrgRelation, dao));
 333  
         
 334  
         // Search for and copy the org
 335  0
         Org org = null;
 336  
         try {
 337  0
             org = dao.fetch(Org.class, orgOrgRelationInfo.getOrgId());
 338  0
         } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 339  0
             throw new DoesNotExistException("Org does not exist for id: " + orgOrgRelationInfo.getOrgId());
 340  0
         }
 341  0
         if (org == null) {
 342  0
             throw new InvalidParameterException("Org does not exist for id: " + orgOrgRelationInfo.getOrgId());
 343  
         }
 344  0
         orgOrgRelation.setOrg(org);
 345  
         
 346  
         // Search for and copy the related org
 347  0
         Org relatedOrg = null;
 348  
         try {
 349  0
             relatedOrg = dao.fetch(Org.class, orgOrgRelationInfo.getRelatedOrgId());
 350  0
         } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 351  0
             throw new DoesNotExistException("RelatedOrg does not exist for id: " + orgOrgRelationInfo.getRelatedOrgId());
 352  0
         }
 353  0
         if (relatedOrg == null) {
 354  0
             throw new InvalidParameterException("RelatedOrg does not exist for id: " + orgOrgRelationInfo.getRelatedOrgId());
 355  
         }
 356  0
         orgOrgRelation.setRelatedOrg(relatedOrg);
 357  
         
 358  
         // Search for and copy the type
 359  0
         OrgOrgRelationType orgOrgRelationType = null;
 360  
         try {
 361  0
             orgOrgRelationType = dao.fetch(OrgOrgRelationType.class, orgOrgRelationInfo.getTypeKey());
 362  0
         } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 363  0
             throw new DoesNotExistException("OrgOrgRelationType does not exist for id: " + orgOrgRelationInfo.getTypeKey());
 364  0
         }
 365  0
         if (orgOrgRelationType == null) {
 366  0
             throw new InvalidParameterException(
 367  
                                                 "OrgOrgRelationType does not exist for id: " + orgOrgRelationInfo.getTypeKey());
 368  
         }
 369  0
         orgOrgRelation.setType(orgOrgRelationType);
 370  
         
 371  0
         return orgOrgRelation;
 372  
     }
 373  
     
 374  
     public static OrgPersonRelation toOrgPersonRelation(boolean isUpdate,
 375  
                                                         OrgPersonRelationInfo orgPersonRelationInfo,
 376  
                                                         OrganizationDao dao) 
 377  
         throws InvalidParameterException, VersionMismatchException, DoesNotExistException {
 378  
 
 379  0
         OrgPersonRelation orgPersonRelation = null;
 380  0
         if (isUpdate) {
 381  
             try {
 382  0
                 orgPersonRelation = dao.fetch(OrgPersonRelation.class, orgPersonRelationInfo.getId());
 383  0
             } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 384  0
                 throw new DoesNotExistException("OrgOrgRelation does not exist for id: " + orgPersonRelationInfo.getId());
 385  0
             }
 386  0
             if (orgPersonRelation == null) {
 387  0
                 throw new DoesNotExistException("OrgOrgRelation does not exist for id: " + orgPersonRelationInfo.getId());
 388  
             }
 389  0
             if (!String.valueOf(orgPersonRelation.getVersionNumber()).equals(orgPersonRelationInfo.getMeta().getVersionInd())){
 390  0
                 throw new VersionMismatchException("OrgOrgRelation to be updated is not the current version");
 391  
             }
 392  
         } else {
 393  0
             orgPersonRelation = new OrgPersonRelation();
 394  
         }
 395  
         
 396  
         // Copy all basic properties
 397  0
         BeanUtils.copyProperties(orgPersonRelationInfo, orgPersonRelation, new String[] { "type",
 398  
                                                                                           "attributes", "meta", "org", "personId" });
 399  
         
 400  
         // Copy Attributes
 401  0
         orgPersonRelation.setAttributes(toGenericAttributes(OrgPersonRelationAttribute.class, orgPersonRelationInfo.getAttributes(), orgPersonRelation, dao));
 402  
         
 403  
         // Search for and copy the org
 404  0
         Org org = null;
 405  
         try {
 406  0
             org = dao.fetch(Org.class, orgPersonRelationInfo.getOrgId());
 407  0
         } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 408  0
             throw new DoesNotExistException("Org does not exist for id: " + orgPersonRelationInfo.getOrgId());
 409  0
         }
 410  0
         if (org == null) {
 411  0
             throw new InvalidParameterException("Org does not exist for id: " + orgPersonRelationInfo.getOrgId());
 412  
         }
 413  0
         orgPersonRelation.setOrg(org);
 414  
         
 415  
         // Search for and copy the related org
 416  
         //TODO Use the person Service here
 417  
         //                Person person = personService.findPerson(orgPersonRelationInfo.getPersonId());
 418  
         //                if (person == null) {
 419  
         //                        throw new InvalidParameterException(
 420  
         //                                        "Person does not exist for id: " + orgPersonRelationInfo.getPersonId());
 421  
         //                }
 422  
         //                orgPersonRelation.setPersonId(person.getId());
 423  0
         orgPersonRelation.setPersonId(orgPersonRelationInfo.getPersonId());
 424  
         
 425  
         // Search for and copy the type
 426  0
         OrgPersonRelationType orgPersonRelationType = null;
 427  
         try {
 428  0
             orgPersonRelationType = dao.fetch(OrgPersonRelationType.class, orgPersonRelationInfo.getTypeKey());
 429  0
         } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 430  0
             throw new DoesNotExistException("OrgPersonRelationType does not exist for id: " + orgPersonRelationInfo.getTypeKey());
 431  0
         }
 432  0
         if (orgPersonRelationType == null) {
 433  0
             throw new InvalidParameterException("OrgPersonRelationType does not exist for id: " + orgPersonRelationInfo.getTypeKey());
 434  
         }
 435  0
         orgPersonRelation.setType(orgPersonRelationType);
 436  
         
 437  0
         return orgPersonRelation;
 438  
     }
 439  
     
 440  
     public static OrgPositionRestriction toOrgPositionRestriction(boolean isUpdate,
 441  
                                                                   OrgPositionRestrictionInfo orgPositionRestrictionInfo,
 442  
                                                                   OrganizationDao dao) 
 443  
         throws DoesNotExistException, VersionMismatchException, InvalidParameterException {
 444  
         
 445  0
         OrgPositionRestriction orgPositionRestriction = null;
 446  0
         if (isUpdate) {
 447  
             try {
 448  0
                 orgPositionRestriction = dao.fetch(OrgPositionRestriction.class, orgPositionRestrictionInfo.getId());
 449  0
             } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 450  0
                 throw new DoesNotExistException("OrgPositionRestriction does not exist for id: " + orgPositionRestrictionInfo.getId());
 451  0
             }
 452  0
             if (orgPositionRestriction == null) {
 453  0
                 throw new DoesNotExistException("OrgPositionRestriction does not exist for id: " + orgPositionRestrictionInfo.getId());
 454  
             }
 455  0
             if (!String.valueOf(orgPositionRestriction.getVersionNumber()).equals(orgPositionRestrictionInfo.getMeta().getVersionInd())){
 456  0
                 throw new VersionMismatchException("OrgPositionRestriction to be updated is not the current version");
 457  
             }
 458  
         } else {
 459  0
             orgPositionRestriction = new OrgPositionRestriction();
 460  
         }
 461  
         
 462  
         // Copy all basic properties
 463  0
         BeanUtils.copyProperties(orgPositionRestrictionInfo, orgPositionRestriction, new String[] { "personRelationType",
 464  
                                                                                                     "attributes", 
 465  
                                                                                                     "meta", 
 466  
                                                                                                     "org", 
 467  
                                                                                                     "stdDuration", 
 468  
                                                                                                     "descr" });
 469  
         
 470  
         //Copy Description
 471  0
         orgPositionRestriction.setDescr(orgPositionRestrictionInfo.getDescr().getPlain());
 472  
         
 473  
         //Copy std duration
 474  0
         if (orgPositionRestrictionInfo.getStdDuration()!=null) {
 475  0
             orgPositionRestriction.setStdDuration(new TimeAmount());
 476  0
             BeanUtils.copyProperties(orgPositionRestrictionInfo.getStdDuration(), orgPositionRestriction.getStdDuration());
 477  
         }
 478  
         // Copy Attributes
 479  0
         orgPositionRestriction.setAttributes(toGenericAttributes(OrgPositionRestrictionAttribute.class, orgPositionRestrictionInfo.getAttributes(), orgPositionRestriction, dao));
 480  
         
 481  
         // Search for and copy the org
 482  0
         Org org = null;
 483  
         try {
 484  0
             org = dao.fetch(Org.class, orgPositionRestrictionInfo.getOrgId());
 485  0
         } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 486  0
             throw new DoesNotExistException("Org does not exist for id: " + orgPositionRestrictionInfo.getOrgId());
 487  0
         }
 488  0
         if (org == null) {
 489  0
             throw new InvalidParameterException("Org does not exist for id: " + orgPositionRestrictionInfo.getOrgId());
 490  
         }
 491  0
         orgPositionRestriction.setOrg(org);
 492  
         
 493  
         // Search for and copy the type
 494  0
         OrgPersonRelationType orgPersonRelationType = null;
 495  
         try {
 496  0
             orgPersonRelationType = dao.fetch(OrgPersonRelationType.class, orgPositionRestrictionInfo.getOrgPersonRelationTypeKey());
 497  0
         } catch (org.kuali.student.common.exceptions.DoesNotExistException e) {
 498  0
             throw new DoesNotExistException("OrgPersonRelationType does not exist for id: " + orgPositionRestrictionInfo.getOrgPersonRelationTypeKey());
 499  0
         }
 500  0
         if (orgPersonRelationType == null) {
 501  0
             throw new InvalidParameterException("OrgPersonRelationType does not exist for id: " + orgPositionRestrictionInfo.getOrgPersonRelationTypeKey());
 502  
         }
 503  0
         orgPositionRestriction.setPersonRelationType(orgPersonRelationType);
 504  
         
 505  0
         orgPositionRestriction.setDescr(orgPositionRestrictionInfo.getDescr().getPlain());
 506  
         
 507  0
         return orgPositionRestriction;
 508  
     }
 509  
     
 510  
     public static <A extends Attribute<O>, O extends AttributeOwner<A>> List<A> toGenericAttributes(Class<A> attributeClass, List<AttributeInfo> attributeList, O owner, CrudDao dao) throws InvalidParameterException {
 511  
         
 512  0
         List<A> attributes = new ArrayList<A>();
 513  
         
 514  0
         if (owner.getAttributes()==null) {
 515  0
             owner.setAttributes(new ArrayList<A>());
 516  
         }
 517  
         
 518  0
         Map<String, A> currentAttributes = new HashMap<String,A>();
 519  
         
 520  
         // Find all the old attributes(if the owner is not null)
 521  0
         for (A attribute : owner.getAttributes()) {
 522  0
             currentAttributes.put(attribute.getName(), attribute);
 523  
         }
 524  
         
 525  
         //Clear out the attributes
 526  0
         owner.getAttributes().clear();
 527  
         
 528  0
         if (attributeList==null) {
 529  0
             return attributes;
 530  
         }
 531  
         
 532  
         //Update anything that exists, or create a new attribute if it doesn't
 533  0
         for (AttributeInfo attributeEntry : attributeList) {            
 534  
             A attribute;
 535  0
             if (currentAttributes.containsKey(attributeEntry.getKey())) {
 536  0
                 attribute = currentAttributes.remove(attributeEntry.getKey());
 537  
             } else {
 538  
                 try {
 539  0
                     attribute = attributeClass.newInstance();
 540  0
                 } catch(Exception e) {
 541  0
                     throw new RuntimeException("Error copying attributes.",e);
 542  0
                 }
 543  0
                 attribute.setName(attributeEntry.getKey());
 544  0
                 attribute.setOwner(owner);
 545  
             }
 546  0
             attribute.setValue(attributeEntry.getValue());
 547  0
             attributes.add(attribute);
 548  0
         }
 549  
         
 550  
         //Delete leftovers here if behavior is desired
 551  
         
 552  0
         return attributes;
 553  
     }
 554  
     
 555  
         
 556  
     protected static MetaInfo toMetaInfo(Meta meta, Long versionInd) {
 557  
         
 558  0
         MetaInfo metaInfo = new MetaInfo();
 559  
         // If there was a meta passed in then copy the values
 560  0
         if (meta != null) {
 561  0
             BeanUtils.copyProperties(meta, metaInfo);
 562  
         }
 563  0
         if (versionInd==null) {
 564  0
             metaInfo.setVersionInd(null);
 565  
         } else {
 566  0
             metaInfo.setVersionInd(versionInd.toString());
 567  
         }
 568  
         
 569  0
         return metaInfo;
 570  
     }
 571  
     
 572  
     public static List<AttributeInfo> toAttributeList(List<? extends Attribute<?>> attributes) {
 573  
         
 574  0
         List<AttributeInfo> attributeInfos = new ArrayList<AttributeInfo>();
 575  
 
 576  0
         for (Attribute<?> attribute : attributes) {
 577  0
             AttributeInfo attributeInfo = new AttributeInfo();
 578  0
             attributeInfo.setKey(attribute.getName());
 579  0
             attributeInfo.setValue(attribute.getValue());
 580  0
             attributeInfos.add(attributeInfo);
 581  0
         }
 582  
         
 583  0
         return attributeInfos;
 584  
     }
 585  
     
 586  
     public static <S extends Type<?>> TypeInfo toGenericTypeInfo(S typeEntity) {
 587  0
         if (typeEntity == null) {
 588  0
             return null;
 589  
         }
 590  
         
 591  
         TypeInfo typeInfo;
 592  
         try {
 593  
             // Create a new TypeInfo based on the <T> class and copy the
 594  
             // properties
 595  0
             typeInfo = new TypeInfo();
 596  0
             BeanUtils.copyProperties(typeEntity, typeInfo,
 597  
                                      new String[] { "attributes", "descr" });
 598  
             
 599  0
             typeInfo.setKey(typeEntity.getId());
 600  
             
 601  
             // Copy the attributes
 602  0
             typeInfo.setAttributes(toAttributeList(typeEntity.getAttributes()));
 603  
             
 604  
             //Copy the description
 605  0
             RichTextInfo richText = new RichTextInfo();
 606  0
             richText.setFormatted(typeEntity.getDescr());
 607  0
             richText.setPlain(typeEntity.getDescr());
 608  0
             typeInfo.setDescr(richText);
 609  
             
 610  0
             return typeInfo;
 611  
 
 612  0
         } catch (Exception e) {
 613  0
             logger.error("Exception occured: ", e);
 614  
         }
 615  
 
 616  0
         return null;
 617  
     }
 618  
 }