| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.core.organization.assembly; | 
  | 17 |  |   | 
  | 18 |  |  import static org.kuali.student.core.assembly.util.AssemblerUtils.addVersionIndicator; | 
  | 19 |  |  import static org.kuali.student.core.assembly.util.AssemblerUtils.getVersionIndicator; | 
  | 20 |  |  import static org.kuali.student.core.assembly.util.AssemblerUtils.isCreated; | 
  | 21 |  |  import static org.kuali.student.core.assembly.util.AssemblerUtils.isDeleted; | 
  | 22 |  |  import static org.kuali.student.core.assembly.util.AssemblerUtils.isModified; | 
  | 23 |  |  import static org.kuali.student.core.assembly.util.AssemblerUtils.isUpdated; | 
  | 24 |  |   | 
  | 25 |  |  import java.util.ArrayList; | 
  | 26 |  |  import java.util.Iterator; | 
  | 27 |  |  import java.util.List; | 
  | 28 |  |   | 
  | 29 |  |  import org.apache.log4j.Logger; | 
  | 30 |  |  import org.kuali.student.common.ui.client.mvc.DataModel; | 
  | 31 |  |  import org.kuali.student.common.ui.client.mvc.DataModelDefinition; | 
  | 32 |  |  import org.kuali.student.core.assembly.data.AssemblyException; | 
  | 33 |  |  import org.kuali.student.core.assembly.data.Data; | 
  | 34 |  |  import org.kuali.student.core.assembly.data.Metadata; | 
  | 35 |  |  import org.kuali.student.core.assembly.data.QueryPath; | 
  | 36 |  |  import org.kuali.student.core.assembly.data.Data.Property; | 
  | 37 |  |  import org.kuali.student.core.assembly.old.Assembler; | 
  | 38 |  |  import org.kuali.student.core.assembly.old.data.SaveResult; | 
  | 39 |  |  import org.kuali.student.core.assembly.util.AssemblerUtils; | 
  | 40 |  |  import org.kuali.student.core.dto.MetaInfo; | 
  | 41 |  |  import org.kuali.student.core.exceptions.DoesNotExistException; | 
  | 42 |  |  import org.kuali.student.core.organization.assembly.data.server.org.OrgHelper; | 
  | 43 |  |  import org.kuali.student.core.organization.assembly.data.server.org.OrgPersonHelper; | 
  | 44 |  |  import org.kuali.student.core.organization.dto.OrgPersonRelationInfo; | 
  | 45 |  |  import org.kuali.student.core.organization.service.OrganizationService; | 
  | 46 |  |  import org.kuali.student.core.validation.dto.ValidationResultInfo; | 
  | 47 |  |   | 
  | 48 | 0 |  public class OrgPersonRelationAssembler implements Assembler<Data, OrgPersonHelper>{ | 
  | 49 | 0 |          final Logger LOG = Logger.getLogger(OrgPersonRelationAssembler.class); | 
  | 50 |  |      private OrganizationService orgService; | 
  | 51 |  |      private Metadata metadata; | 
  | 52 | 0 |      private DataModel orgPersonModel = new DataModel(); | 
  | 53 |  |      public static final String PERSON_PATH                  = "orgPersonRelationInfo"; | 
  | 54 |  |       | 
  | 55 |  |      public void setMetaData(Metadata metadata){ | 
  | 56 | 0 |          this.metadata=metadata; | 
  | 57 |  |           | 
  | 58 | 0 |      } | 
  | 59 |  |   | 
  | 60 |  |      public void setOrgService(OrganizationService service){ | 
  | 61 | 0 |          orgService = service; | 
  | 62 | 0 |      } | 
  | 63 |  |      @Override | 
  | 64 |  |      public Data assemble(OrgPersonHelper input) throws AssemblyException { | 
  | 65 |  |           | 
  | 66 | 0 |          return null; | 
  | 67 |  |      } | 
  | 68 |  |   | 
  | 69 |  |      @Override | 
  | 70 |  |      public OrgPersonHelper disassemble(Data input) throws AssemblyException { | 
  | 71 |  |           | 
  | 72 | 0 |          return null; | 
  | 73 |  |      } | 
  | 74 |  |   | 
  | 75 |  |      @Override | 
  | 76 |  |      public Data get(String id) throws AssemblyException { | 
  | 77 | 0 |          List<OrgPersonRelationInfo> relations = new ArrayList<OrgPersonRelationInfo>(); | 
  | 78 | 0 |          Data orgRelationMap = null; | 
  | 79 |  |          try{ | 
  | 80 | 0 |              relations = orgService.getOrgPersonRelationsByOrg(id); | 
  | 81 | 0 |              orgRelationMap = buildOrgPersonRelationMap(relations); | 
  | 82 |  |          } | 
  | 83 | 0 |          catch(DoesNotExistException dnee){ | 
  | 84 | 0 |              return null; | 
  | 85 |  |               | 
  | 86 |  |          } | 
  | 87 | 0 |          catch(Exception e){ | 
  | 88 | 0 |                  LOG.error(e); | 
  | 89 | 0 |          } | 
  | 90 | 0 |          return orgRelationMap; | 
  | 91 |  |      } | 
  | 92 |  |   | 
  | 93 |  |   | 
  | 94 |  |   | 
  | 95 |  |      @Override | 
  | 96 |  |      public SaveResult<Data> save(Data input) throws AssemblyException { | 
  | 97 | 0 |              SaveResult<Data> result = new SaveResult<Data>(); | 
  | 98 | 0 |          result.setValue(input); | 
  | 99 | 0 |          List<ValidationResultInfo> validationResults = validate(input); | 
  | 100 | 0 |          result.setValidationResults(validationResults); | 
  | 101 |  |           | 
  | 102 | 0 |          updatePersonRelations(input); | 
  | 103 | 0 |          return result; | 
  | 104 |  |      } | 
  | 105 |  |   | 
  | 106 |  |   | 
  | 107 |  |   | 
  | 108 |  |      @Override | 
  | 109 |  |      public List<ValidationResultInfo> validate(Data input) throws AssemblyException { | 
  | 110 |  |           | 
  | 111 | 0 |          return null; | 
  | 112 |  |      } | 
  | 113 |  |       | 
  | 114 |  |      private void updatePersonRelations(Data input) throws AssemblyException{ | 
  | 115 | 0 |          if (input == null) { | 
  | 116 | 0 |              return; | 
  | 117 |  |          } | 
  | 118 | 0 |          DataModelDefinition def = new DataModelDefinition(metadata); | 
  | 119 | 0 |          orgPersonModel.setDefinition(def); | 
  | 120 |  |           | 
  | 121 | 0 |          QueryPath metaPath = QueryPath.concat(null, PERSON_PATH); | 
  | 122 | 0 |          Metadata orgPersonMeta =orgPersonModel.getMetadata(metaPath); | 
  | 123 |  |           | 
  | 124 | 0 |          for (Iterator<Property> propertyIter = ((Data)input.get("orgPersonRelationInfo")).iterator();propertyIter.hasNext();) { | 
  | 125 | 0 |                  Property p = propertyIter.next(); | 
  | 126 | 0 |              OrgPersonHelper orgPersonHelper=  OrgPersonHelper.wrap((Data)p.getValue()); | 
  | 127 | 0 |              if (isUpdated(orgPersonHelper.getData())) { | 
  | 128 | 0 |                  if (orgPersonMeta.isCanEdit()) { | 
  | 129 | 0 |                      OrgPersonRelationInfo orgPersonRelationInfo = buildOrgPersonRelationInfo(orgPersonHelper); | 
  | 130 | 0 |                      orgPersonRelationInfo.setId(orgPersonHelper.getId()); | 
  | 131 |  |                      try { | 
  | 132 | 0 |                          OrgPersonRelationInfo result = orgService.updateOrgPersonRelation(orgPersonHelper.getId(), orgPersonRelationInfo); | 
  | 133 | 0 |                          addVersionIndicator(orgPersonHelper.getData(), OrgPersonRelationInfo.class.getName(), result.getId(), result.getMetaInfo().getVersionInd()); | 
  | 134 | 0 |                      } catch (Exception e) { | 
  | 135 | 0 |                          throw new AssemblyException(); | 
  | 136 | 0 |                      } | 
  | 137 | 0 |                      AssemblerUtils.setUpdated(orgPersonHelper.getData(), false); | 
  | 138 | 0 |                  } | 
  | 139 |  |              } | 
  | 140 | 0 |              else if(isDeleted(orgPersonHelper.getData())&&orgPersonHelper.getId()!=null){ | 
  | 141 |  |                  try{ | 
  | 142 | 0 |                      orgService.removeOrgPersonRelation(orgPersonHelper.getId()); | 
  | 143 | 0 |                      propertyIter.remove(); | 
  | 144 |  |                  } | 
  | 145 | 0 |                  catch(Exception e ){ | 
  | 146 | 0 |                          LOG.error(e); | 
  | 147 | 0 |                      throw(new AssemblyException()); | 
  | 148 | 0 |                  } | 
  | 149 |  |              } | 
  | 150 | 0 |              else if(isCreated(orgPersonHelper.getData())){ | 
  | 151 | 0 |                  orgPersonHelper.setOrgId((OrgHelper.wrap((Data)input.get("orgInfo")).getId())); | 
  | 152 | 0 |                  OrgPersonRelationInfo orgPersonRelationInfo = buildOrgPersonRelationInfo(orgPersonHelper); | 
  | 153 |  |                  try{ | 
  | 154 | 0 |                      OrgPersonRelationInfo  result = orgService.createOrgPersonRelation(orgPersonHelper.getOrgId(), orgPersonHelper.getPersonId(), orgPersonHelper.getTypeKey(), orgPersonRelationInfo); | 
  | 155 | 0 |                      orgPersonHelper.setId(result.getId()); | 
  | 156 | 0 |                      addVersionIndicator(orgPersonHelper.getData(),OrgPersonRelationInfo.class.getName(),result.getId(),result.getMetaInfo().getVersionInd()); | 
  | 157 |  |                  } | 
  | 158 | 0 |                  catch(Exception e ){ | 
  | 159 | 0 |                          LOG.error(e); | 
  | 160 | 0 |                      throw new AssemblyException(); | 
  | 161 | 0 |                  } | 
  | 162 | 0 |                  AssemblerUtils.setCreated(orgPersonHelper.getData(), false); | 
  | 163 |  |              } | 
  | 164 |  |              | 
  | 165 |  |             | 
  | 166 | 0 |          } | 
  | 167 | 0 |      } | 
  | 168 |  |       | 
  | 169 |  |      private OrgPersonRelationInfo buildOrgPersonRelationInfo(OrgPersonHelper orgPersonHelper){ | 
  | 170 | 0 |          OrgPersonRelationInfo orgPersonRelationInfo = new OrgPersonRelationInfo(); | 
  | 171 | 0 |          orgPersonRelationInfo.setOrgId(orgPersonHelper.getOrgId());       | 
  | 172 | 0 |          orgPersonRelationInfo.setPersonId(orgPersonHelper.getPersonId()); | 
  | 173 | 0 |          orgPersonRelationInfo.setType(orgPersonHelper.getTypeKey()); | 
  | 174 | 0 |          orgPersonRelationInfo.setEffectiveDate(orgPersonHelper.getEffectiveDate()); | 
  | 175 | 0 |          orgPersonRelationInfo.setExpirationDate(orgPersonHelper.getExpirationDate()); | 
  | 176 | 0 |          if (orgPersonHelper.getState()!=null) | 
  | 177 | 0 |                  orgPersonRelationInfo.setState(orgPersonHelper.getState()); | 
  | 178 |  |          else | 
  | 179 | 0 |                  orgPersonHelper.setState("Active"); | 
  | 180 |  |                   | 
  | 181 |  |          | 
  | 182 | 0 |          if (isModified(orgPersonHelper.getData())) { | 
  | 183 | 0 |              if (isUpdated(orgPersonHelper.getData())||isDeleted(orgPersonHelper.getData())) { | 
  | 184 | 0 |                  MetaInfo metaInfo = new MetaInfo(); | 
  | 185 | 0 |                  orgPersonRelationInfo.setMetaInfo(metaInfo); | 
  | 186 | 0 |                  orgPersonRelationInfo.setId(orgPersonHelper.getId()); | 
  | 187 |  |              } | 
  | 188 |  |          } | 
  | 189 | 0 |          if(orgPersonRelationInfo.getMetaInfo()!=null){ | 
  | 190 | 0 |              orgPersonRelationInfo.getMetaInfo().setVersionInd(getVersionIndicator(orgPersonHelper.getData())); | 
  | 191 |  |          } | 
  | 192 | 0 |          return orgPersonRelationInfo; | 
  | 193 |  |      } | 
  | 194 |  |       | 
  | 195 |  |      private Data buildOrgPersonRelationMap( List<OrgPersonRelationInfo> relations){ | 
  | 196 | 0 |          Data orgRelations = new Data(); | 
  | 197 |  |          try { | 
  | 198 | 0 |          int count =0; | 
  | 199 |  |   | 
  | 200 | 0 |          DataModelDefinition def = new DataModelDefinition(metadata); | 
  | 201 | 0 |          orgPersonModel.setDefinition(def); | 
  | 202 |  |           | 
  | 203 | 0 |          QueryPath metaPath = QueryPath.concat(null, PERSON_PATH); | 
  | 204 | 0 |          Metadata orgPersonMeta =orgPersonModel.getMetadata(metaPath); | 
  | 205 | 0 |          for(OrgPersonRelationInfo relation:relations){ | 
  | 206 | 0 |              Data relationMap = new Data(); | 
  | 207 | 0 |              OrgPersonHelper orgPersonHelper = OrgPersonHelper.wrap(relationMap); | 
  | 208 | 0 |              orgPersonHelper.setId(relation.getId()); | 
  | 209 | 0 |              orgPersonHelper.setOrgId(relation.getOrgId()); | 
  | 210 | 0 |              if(!orgPersonMeta.isCanEdit()){ | 
  | 211 |  |                   | 
  | 212 | 0 |                      orgPersonHelper.setTypeKey(orgService.getOrgPersonRelationType(relation.getType()).getName()); | 
  | 213 |  |                  | 
  | 214 |  |              } | 
  | 215 |  |              else{ | 
  | 216 | 0 |                  orgPersonHelper.setTypeKey(relation.getType()); | 
  | 217 |  |              } | 
  | 218 |  |               | 
  | 219 | 0 |              orgPersonHelper.setPersonId(relation.getPersonId()); | 
  | 220 | 0 |              orgPersonHelper.setEffectiveDate(relation.getEffectiveDate()); | 
  | 221 | 0 |              orgPersonHelper.setExpirationDate(relation.getExpirationDate()); | 
  | 222 | 0 |              addVersionIndicator(orgPersonHelper.getData(),OrgPersonRelationInfo.class.getName(),relation.getId(),relation.getMetaInfo().getVersionInd()); | 
  | 223 | 0 |              orgRelations.set(count,orgPersonHelper.getData()); | 
  | 224 | 0 |              count = count +1; | 
  | 225 | 0 |          } | 
  | 226 |  |          } | 
  | 227 | 0 |          catch(Exception e){ | 
  | 228 | 0 |                  LOG.error(e); | 
  | 229 | 0 |          } | 
  | 230 | 0 |          return orgRelations; | 
  | 231 |  |      } | 
  | 232 |  |      @Override | 
  | 233 |  |      public Metadata getMetadata(String idType, String id, String type, String state) throws AssemblyException { | 
  | 234 |  |           | 
  | 235 | 0 |          return null; | 
  | 236 |  |      } | 
  | 237 |  |   | 
  | 238 |  |          @Override | 
  | 239 |  |          public Metadata getDefaultMetadata() throws AssemblyException { | 
  | 240 |  |                   | 
  | 241 | 0 |                  return null; | 
  | 242 |  |          } | 
  | 243 |  |   | 
  | 244 |  |  } |