Coverage Report - org.kuali.student.core.organization.assembly.OrgPositionRestrictionAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
OrgPositionRestrictionAssembler
0%
0/103
0%
0/28
3
 
 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.organization.assembly;
 17  
 
 18  
 import static org.kuali.student.common.assembly.util.AssemblerUtils.addVersionIndicator;
 19  
 import static org.kuali.student.common.assembly.util.AssemblerUtils.getVersionIndicator;
 20  
 import static org.kuali.student.common.assembly.util.AssemblerUtils.isCreated;
 21  
 import static org.kuali.student.common.assembly.util.AssemblerUtils.isDeleted;
 22  
 import static org.kuali.student.common.assembly.util.AssemblerUtils.isModified;
 23  
 import static org.kuali.student.common.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.assembly.data.AssemblyException;
 31  
 import org.kuali.student.common.assembly.data.Data;
 32  
 import org.kuali.student.common.assembly.data.Metadata;
 33  
 import org.kuali.student.common.assembly.data.QueryPath;
 34  
 import org.kuali.student.common.assembly.data.Data.Property;
 35  
 import org.kuali.student.common.assembly.old.Assembler;
 36  
 import org.kuali.student.common.assembly.old.data.SaveResult;
 37  
 import org.kuali.student.common.assembly.util.AssemblerUtils;
 38  
 import org.kuali.student.common.dto.MetaInfo;
 39  
 import org.kuali.student.common.dto.StatusInfo;
 40  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 41  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 42  
 import org.kuali.student.common.ui.client.mvc.DataModelDefinition;
 43  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 44  
 import org.kuali.student.core.organization.assembly.data.server.org.OrgHelper;
 45  
 import org.kuali.student.core.organization.assembly.data.server.org.OrgPositionHelper;
 46  
 import org.kuali.student.core.organization.dto.OrgPositionRestrictionInfo;
 47  
 import org.kuali.student.core.organization.service.OrganizationService;
 48  
 
 49  0
 public class OrgPositionRestrictionAssembler implements Assembler<Data, OrgPositionHelper>{
 50  0
         final Logger LOG = Logger.getLogger(OrgPositionRestrictionAssembler.class);
 51  
     private OrganizationService orgService;
 52  
     private Metadata metadata;
 53  
     public static final String POSITION_PATH                  = "OrgPositionRestrictionInfo";
 54  0
     private DataModel orgPositionModel = new DataModel();
 55  
     public void setMetaData(Metadata metadata){
 56  0
         this.metadata=metadata;
 57  0
     }
 58  
     
 59  
     @Override
 60  
     public Data assemble(OrgPositionHelper input) throws AssemblyException {
 61  
         // TODO Neerav Agrawal - THIS METHOD NEEDS JAVADOCS
 62  0
         return null;
 63  
     }
 64  
 
 65  
     @Override
 66  
     public OrgPositionHelper disassemble(Data input) throws AssemblyException {
 67  
         // TODO Neerav Agrawal - THIS METHOD NEEDS JAVADOCS
 68  0
         return null;
 69  
     }
 70  
 
 71  
     @Override
 72  
     public Data get(String id) throws AssemblyException {
 73  0
         List<OrgPositionRestrictionInfo> positions = new ArrayList<OrgPositionRestrictionInfo>();
 74  0
         Data orgPositionMap = null;
 75  
         try{
 76  0
             positions = orgService.getPositionRestrictionsByOrg(id);
 77  0
             orgPositionMap = buildOrgPositionMap(positions);
 78  
         }
 79  0
         catch(DoesNotExistException dnee){
 80  0
             return null;
 81  
             
 82  
         }
 83  0
         catch(Exception e){
 84  0
             LOG.error(e);
 85  0
         }
 86  0
         return orgPositionMap;
 87  
     }
 88  
 
 89  
     @Override
 90  
     public Metadata getMetadata(String idType, String id, String type, String state) throws AssemblyException {
 91  
         // TODO Neerav Agrawal - THIS METHOD NEEDS JAVADOCS
 92  0
         return null;
 93  
     }
 94  
 
 95  
     public void setOrgService(OrganizationService service){
 96  0
         orgService = service;
 97  0
     }
 98  
     
 99  
     @Override
 100  
     public SaveResult<Data> save(Data input) throws AssemblyException {
 101  0
         addPositionRestriction(input);
 102  0
         SaveResult<Data> result = new SaveResult<Data>();
 103  0
         List<ValidationResultInfo> validationResults = validate(input);
 104  0
         result.setValue(input);
 105  0
         return result;
 106  
     }
 107  
 
 108  
     @Override
 109  
     public List<ValidationResultInfo> validate(Data input) throws AssemblyException {
 110  
         // TODO Neerav Agrawal - THIS METHOD NEEDS JAVADOCS
 111  0
         return null;
 112  
     }
 113  
     
 114  
     private void addPositionRestriction(Data input) throws AssemblyException{
 115  0
         if (input == null) {
 116  0
             return;
 117  
         }
 118  0
         DataModelDefinition def = new DataModelDefinition(metadata);
 119  0
         orgPositionModel.setDefinition(def);
 120  0
         QueryPath metaPath = QueryPath.concat(null, POSITION_PATH);
 121  0
         Metadata orgPersonMeta =orgPositionModel.getMetadata(metaPath);
 122  
         
 123  0
         for (Iterator<Property> propIter=((Data)input.get(POSITION_PATH)).iterator();propIter.hasNext();) {
 124  0
                 Property p = propIter.next(); 
 125  0
             OrgPositionHelper orgPositionHelper=  OrgPositionHelper.wrap((Data)p.getValue());
 126  0
             if (isUpdated(orgPositionHelper.getData())) {
 127  0
                 if (orgPersonMeta.isCanEdit()) {
 128  0
                     OrgPositionRestrictionInfo orgPositionRestrictionInfo = buildOrgPositionRestrictionInfo(orgPositionHelper);
 129  0
                     orgPositionRestrictionInfo.setId(orgPositionHelper.getId());
 130  
                     try {
 131  0
                         OrgPositionRestrictionInfo result = orgService.updatePositionRestrictionForOrg(orgPositionRestrictionInfo.getOrgId(), orgPositionRestrictionInfo.getOrgPersonRelationTypeKey(), orgPositionRestrictionInfo);
 132  0
                         addVersionIndicator(orgPositionHelper.getData(), OrgPositionRestrictionInfo.class.getName(), result.getId(), result.getMetaInfo().getVersionInd());
 133  0
                     } catch (Exception e) {
 134  0
                         throw new AssemblyException();
 135  0
                     }
 136  
                     //Clear flag to avoid multiple updates
 137  0
                     AssemblerUtils.setUpdated(orgPositionHelper.getData(),false);
 138  0
                 }
 139  
             }
 140  0
             else if(isDeleted(orgPositionHelper.getData())&&orgPositionHelper.getId()!=null){
 141  
                 try{
 142  0
                     if(orgPositionHelper.getId()!=null){
 143  0
                         StatusInfo  result = orgService.removePositionRestrictionFromOrg(orgPositionHelper.getOrgId(), orgPositionHelper.getPersonRelationType());
 144  0
                         propIter.remove();
 145  
                     }
 146  
                 }
 147  0
                 catch(Exception e ){
 148  0
                     LOG.error(e);
 149  0
                     throw(new AssemblyException());
 150  0
                 }
 151  
             }
 152  0
             else if(isCreated(orgPositionHelper.getData())){
 153  0
                 orgPositionHelper.setOrgId((OrgHelper.wrap((Data)input.get("orgInfo")).getId()));
 154  0
                 OrgPositionRestrictionInfo orgPositionRestrictionInfo = buildOrgPositionRestrictionInfo(orgPositionHelper);
 155  
                 try{
 156  0
                     OrgPositionRestrictionInfo  result = orgService.addPositionRestrictionToOrg(orgPositionHelper.getOrgId(), 
 157  
                             orgPositionHelper.getPersonRelationType(), orgPositionRestrictionInfo);
 158  0
                     orgPositionHelper.setId(result.getId());
 159  0
                     addVersionIndicator(orgPositionHelper.getData(),OrgPositionRestrictionInfo.class.getName(),result.getId(),result.getMetaInfo().getVersionInd());
 160  
                 }
 161  0
                 catch(Exception e ){
 162  0
                     LOG.error(e);
 163  0
                     throw new AssemblyException();
 164  0
                 }
 165  
                 //Clear flag to avoid multiple creates
 166  0
                 AssemblerUtils.setCreated(orgPositionHelper.getData(),false);
 167  
             }
 168  
            
 169  
           
 170  0
         }
 171  
         
 172  0
     }
 173  
     
 174  
     
 175  
     private Data buildOrgPositionMap( List<OrgPositionRestrictionInfo> positions){
 176  0
         Data orgPositions = new Data();
 177  0
         int count =0;
 178  0
         for(OrgPositionRestrictionInfo position:positions){
 179  0
             Data positionMap = new Data();
 180  0
             OrgPositionHelper orgPositionHelper = OrgPositionHelper.wrap(positionMap);
 181  0
             orgPositionHelper.setId(position.getId());
 182  0
             orgPositionHelper.setOrgId(position.getOrgId());
 183  0
             orgPositionHelper.setPersonRelationType(position.getOrgPersonRelationTypeKey());
 184  0
             orgPositionHelper.setTitle(position.getTitle());
 185  0
             orgPositionHelper.setDesc(position.getTitle());
 186  0
             orgPositionHelper.setMinNumRelations(position.getMinNumRelations());
 187  0
             orgPositionHelper.setMaxNumRelations(position.getMaxNumRelations());
 188  0
             addVersionIndicator(orgPositionHelper.getData(),OrgPositionRestrictionInfo.class.getName(),position.getId(),position.getMetaInfo().getVersionInd());
 189  0
             orgPositions.set(count,orgPositionHelper.getData());
 190  0
             count = count +1;
 191  0
         }
 192  0
         return orgPositions;
 193  
     }
 194  
     
 195  
     private OrgPositionRestrictionInfo buildOrgPositionRestrictionInfo(OrgPositionHelper orgPositionHelper){
 196  0
         OrgPositionRestrictionInfo orgPositionRestrictionInfo = new OrgPositionRestrictionInfo();
 197  0
         orgPositionRestrictionInfo.setOrgPersonRelationTypeKey(orgPositionHelper.getPersonRelationType());
 198  0
         orgPositionRestrictionInfo.setTitle(orgPositionHelper.getTitle());
 199  0
         orgPositionRestrictionInfo.setDesc(orgPositionHelper.getDesc());
 200  0
         orgPositionRestrictionInfo.setMinNumRelations(orgPositionHelper.getMinNumRelations());
 201  0
         orgPositionRestrictionInfo.setMaxNumRelations(orgPositionHelper.getMaxNumRelations());
 202  0
         orgPositionRestrictionInfo.setOrgId(orgPositionHelper.getOrgId());
 203  0
         if (isModified(orgPositionHelper.getData())) {
 204  0
             if (isUpdated(orgPositionHelper.getData())) {
 205  0
                 MetaInfo metaInfo = new MetaInfo();
 206  0
                 orgPositionRestrictionInfo.setMetaInfo(metaInfo);
 207  0
                 orgPositionRestrictionInfo.setId(orgPositionHelper.getId());
 208  0
             }
 209  0
             else if (isDeleted(orgPositionHelper.getData())) {
 210  
             }
 211  0
             else if (isCreated(orgPositionHelper.getData())) {
 212  
             } 
 213  
         }
 214  0
         if(orgPositionRestrictionInfo.getMetaInfo()!=null){
 215  0
             orgPositionRestrictionInfo.getMetaInfo().setVersionInd(getVersionIndicator(orgPositionHelper.getData()));
 216  
         }
 217  0
         return orgPositionRestrictionInfo;
 218  
     }
 219  
 
 220  
         @Override
 221  
         public Metadata getDefaultMetadata() throws AssemblyException {
 222  
                 // TODO Auto-generated method stub
 223  0
                 return null;
 224  
         }
 225  
 
 226  
 }