|  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.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.isModified;  | 
  |  21 |     | 
   import static org.kuali.student.common.assembly.util.AssemblerUtils.setCreated;  | 
  |  22 |     | 
   import static org.kuali.student.common.assembly.util.AssemblerUtils.setUpdated;  | 
  |  23 |     | 
     | 
  |  24 |     | 
   import java.util.ArrayList;  | 
  |  25 |     | 
   import java.util.List;  | 
  |  26 |     | 
     | 
  |  27 |     | 
   import org.apache.log4j.Logger;  | 
  |  28 |     | 
   import org.kuali.rice.kim.bo.types.dto.AttributeSet;  | 
  |  29 |     | 
   import org.kuali.student.common.assembly.data.AssemblyException;  | 
  |  30 |     | 
   import org.kuali.student.common.assembly.data.Data;  | 
  |  31 |     | 
   import org.kuali.student.common.assembly.data.Metadata;  | 
  |  32 |     | 
   import org.kuali.student.common.assembly.data.QueryPath;  | 
  |  33 |     | 
   import org.kuali.student.common.assembly.old.BaseAssembler;  | 
  |  34 |     | 
   import org.kuali.student.common.assembly.old.data.SaveResult;  | 
  |  35 |     | 
   import org.kuali.student.common.dto.MetaInfo;  | 
  |  36 |     | 
   import org.kuali.student.common.exceptions.AlreadyExistsException;  | 
  |  37 |     | 
   import org.kuali.student.common.exceptions.DataValidationErrorException;  | 
  |  38 |     | 
   import org.kuali.student.common.exceptions.DoesNotExistException;  | 
  |  39 |     | 
   import org.kuali.student.common.exceptions.InvalidParameterException;  | 
  |  40 |     | 
   import org.kuali.student.common.exceptions.MissingParameterException;  | 
  |  41 |     | 
   import org.kuali.student.common.exceptions.OperationFailedException;  | 
  |  42 |     | 
   import org.kuali.student.common.exceptions.PermissionDeniedException;  | 
  |  43 |     | 
   import org.kuali.student.common.exceptions.VersionMismatchException;  | 
  |  44 |     | 
   import org.kuali.student.common.ui.client.mvc.DataModel;  | 
  |  45 |     | 
   import org.kuali.student.common.ui.client.mvc.DataModelDefinition;  | 
  |  46 |     | 
   import org.kuali.student.common.validation.dto.ValidationResultInfo;  | 
  |  47 |     | 
   import org.kuali.student.core.organization.assembly.data.server.OrgInfoData;  | 
  |  48 |     | 
   import org.kuali.student.core.organization.assembly.data.server.OrgInfoData.ModificationState;  | 
  |  49 |     | 
   import org.kuali.student.core.organization.assembly.data.server.org.OrgHelper;  | 
  |  50 |     | 
   import org.kuali.student.core.organization.dto.OrgInfo;  | 
  |  51 |     | 
   import org.kuali.student.core.organization.dto.OrgOrgRelationInfo;  | 
  |  52 |     | 
   import org.kuali.student.core.organization.dto.OrgPositionRestrictionInfo;  | 
  |  53 |     | 
   import org.kuali.student.core.organization.service.OrganizationService;  | 
  |  54 |     | 
   import org.springframework.transaction.annotation.Transactional;  | 
  |  55 |     | 
     | 
  |  56 |    0 |    @Transactional(readOnly=true,rollbackFor={Throwable.class}) | 
  |  57 |     | 
   public class OrgProposalAssembler extends BaseAssembler<Data, OrgHelper>{ | 
  |  58 |    0 |            final Logger LOG = Logger.getLogger(OrgProposalAssembler.class);  | 
  |  59 |     | 
       private OrganizationService orgService;  | 
  |  60 |    0 |        public static  String PROPOSAL_TYPE_CREATE_ORG = "kuali.proposal.type.org.create";  | 
  |  61 |    0 |        public static  String ORG_PROPOSAL_DATA_TYPE = "OrgProposal";  | 
  |  62 |     | 
       public static final String ORG_INFO_PATH                  = "orgInfo";  | 
  |  63 |     | 
       public static final String QUALIFICATION_ORG_ID                 = "orgId";  | 
  |  64 |     | 
       public static final String PERM_DTO_NAME                 = "Organization";  | 
  |  65 |     | 
       private Metadata metadata;  | 
  |  66 |    0 |        private DataModel orgProposalModel = new DataModel();  | 
  |  67 |    0 |        public OrgProposalAssembler(){ | 
  |  68 |     | 
     | 
  |  69 |    0 |        }  | 
  |  70 |     | 
     | 
  |  71 |     | 
     | 
  |  72 |     | 
     | 
  |  73 |     | 
     | 
  |  74 |     | 
         | 
  |  75 |     | 
       private void setMetadata(String orgId){ | 
  |  76 |     | 
           try { | 
  |  77 |    0 |                this.metadata=getMetadata(QUALIFICATION_ORG_ID, orgId, null, null);  | 
  |  78 |    0 |            } catch (AssemblyException e) { | 
  |  79 |    0 |                LOG.error(e);  | 
  |  80 |    0 |            }  | 
  |  81 |    0 |        }  | 
  |  82 |     | 
       @Override  | 
  |  83 |     | 
       public Data assemble(OrgHelper input) throws AssemblyException { | 
  |  84 |    0 |            if (input == null) { | 
  |  85 |    0 |                return null;  | 
  |  86 |     | 
           }  | 
  |  87 |    0 |            Data result = new Data();  | 
  |  88 |     | 
             | 
  |  89 |    0 |            return result;  | 
  |  90 |     | 
       }  | 
  |  91 |     | 
     | 
  |  92 |     | 
       @Override  | 
  |  93 |     | 
       public OrgHelper disassemble(Data input) throws AssemblyException { | 
  |  94 |    0 |            if (input == null) { | 
  |  95 |    0 |                return null;  | 
  |  96 |     | 
           }  | 
  |  97 |     | 
     | 
  |  98 |     | 
     | 
  |  99 |    0 |            return null;  | 
  |  100 |     | 
       }  | 
  |  101 |     | 
     | 
  |  102 |     | 
       @Override  | 
  |  103 |     | 
       public Data get(String id) throws AssemblyException { | 
  |  104 |    0 |            if(metadata==null){ | 
  |  105 |    0 |                setMetadata(id);  | 
  |  106 |     | 
           }  | 
  |  107 |    0 |            OrgInfo orgInfo = new OrgInfo();  | 
  |  108 |    0 |            List<OrgPositionRestrictionInfo> positions = new ArrayList<OrgPositionRestrictionInfo>();  | 
  |  109 |    0 |            List<OrgOrgRelationInfo> relations = new ArrayList<OrgOrgRelationInfo>();  | 
  |  110 |    0 |            Data result = new Data();  | 
  |  111 |     | 
     | 
  |  112 |     | 
           try{ | 
  |  113 |    0 |                orgInfo = orgService.getOrganization(id);  | 
  |  114 |    0 |                OrgInfoData orgInfoData = new OrgInfoData();  | 
  |  115 |    0 |                orgInfoData.setOrgInfo(orgInfo);  | 
  |  116 |    0 |                OrgHelper resultOrg = buildOrgDataMap(orgInfoData);  | 
  |  117 |    0 |                OrgOrgRelationAssembler orgOrgRelationAssembler = new OrgOrgRelationAssembler();  | 
  |  118 |    0 |                orgOrgRelationAssembler.setMetaData(metadata);  | 
  |  119 |    0 |                orgOrgRelationAssembler.setOrgService(orgService);  | 
  |  120 |    0 |                OrgPositionRestrictionAssembler orgPositionRestrictionAssembler= new OrgPositionRestrictionAssembler();  | 
  |  121 |    0 |                orgPositionRestrictionAssembler.setOrgService(orgService);  | 
  |  122 |    0 |                orgPositionRestrictionAssembler.setMetaData(metadata);  | 
  |  123 |    0 |                OrgPersonRelationAssembler orgPersonRelationAssembler = new OrgPersonRelationAssembler();  | 
  |  124 |    0 |                orgPersonRelationAssembler.setMetaData(metadata);  | 
  |  125 |    0 |                orgPersonRelationAssembler.setOrgService(orgService);  | 
  |  126 |    0 |                Data orgOrgRelationMap = orgOrgRelationAssembler.get(id);  | 
  |  127 |    0 |                Data orgPositionMap = orgPositionRestrictionAssembler.get(id);  | 
  |  128 |    0 |                Data orgRelationMap = orgPersonRelationAssembler.get(id);  | 
  |  129 |    0 |                result.set("orgInfo", resultOrg.getData()); | 
  |  130 |    0 |                result.set("orgOrgRelationInfo", orgOrgRelationMap); | 
  |  131 |    0 |                result.set("OrgPositionRestrictionInfo", orgPositionMap); | 
  |  132 |    0 |                result.set("orgPersonRelationInfo", orgRelationMap); | 
  |  133 |     | 
                 | 
  |  134 |     | 
           }  | 
  |  135 |    0 |            catch(Exception e){ | 
  |  136 |    0 |                    LOG.error(e);  | 
  |  137 |    0 |            }  | 
  |  138 |     | 
             | 
  |  139 |    0 |            return result;  | 
  |  140 |     | 
       }  | 
  |  141 |     | 
     | 
  |  142 |     | 
     | 
  |  143 |     | 
       @Override  | 
  |  144 |     | 
           @Transactional(readOnly=false,noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class}) | 
  |  145 |     | 
       public SaveResult<Data> save(Data input) throws AssemblyException { | 
  |  146 |     | 
             | 
  |  147 |    0 |            OrgHelper orgHelper = OrgHelper.wrap((Data)input.get("orgInfo")); | 
  |  148 |    0 |            if(metadata==null){ | 
  |  149 |    0 |                setMetadata(orgHelper.getId());  | 
  |  150 |     | 
           }  | 
  |  151 |    0 |            OrgInfoData orgInfoData = buildOrgInfo(orgHelper);  | 
  |  152 |    0 |            SaveResult<Data> result = new SaveResult<Data>();  | 
  |  153 |    0 |            List<ValidationResultInfo> validationResults = validate(input);  | 
  |  154 |     | 
     | 
  |  155 |     | 
           try { | 
  |  156 |    0 |                saveOrg(orgInfoData);     | 
  |  157 |     | 
     | 
  |  158 |     | 
     | 
  |  159 |     | 
     | 
  |  160 |     | 
     | 
  |  161 |    0 |                String orgId = orgInfoData.getOrgInfo().getId();  | 
  |  162 |    0 |                addVersionIndicator(orgHelper.getData(),OrgInfo.class.getName(),orgId,orgInfoData.getOrgInfo().getMetaInfo().getVersionInd());  | 
  |  163 |     | 
                 | 
  |  164 |    0 |                orgHelper.setId(orgId);  | 
  |  165 |    0 |                if(orgId!=null&&input.get("orgOrgRelationInfo")!=null){ | 
  |  166 |     | 
     | 
  |  167 |    0 |                    OrgOrgRelationAssembler orgOrgRelationAssembler = new OrgOrgRelationAssembler();  | 
  |  168 |    0 |                    orgOrgRelationAssembler.setMetaData(metadata);  | 
  |  169 |    0 |                    orgOrgRelationAssembler.setOrgService(orgService);  | 
  |  170 |    0 |                    Data relationData = orgOrgRelationAssembler.save(input).getValue();  | 
  |  171 |     | 
     | 
  |  172 |     | 
               }  | 
  |  173 |    0 |                if(orgId!=null&&input.get("OrgPositionRestrictionInfo")!=null){ | 
  |  174 |    0 |                    OrgPositionRestrictionAssembler orgPositionRestrictionAssembler= new OrgPositionRestrictionAssembler();  | 
  |  175 |    0 |                    orgPositionRestrictionAssembler.setMetaData(metadata);  | 
  |  176 |    0 |                    orgPositionRestrictionAssembler.setOrgService(orgService);  | 
  |  177 |    0 |                    Data positionData = orgPositionRestrictionAssembler.save(input).getValue();  | 
  |  178 |     | 
               }  | 
  |  179 |    0 |                if(orgId!=null&&input.get("orgPersonRelationInfo")!=null){ | 
  |  180 |    0 |                    OrgPersonRelationAssembler orgPersonRelationAssembler= new OrgPersonRelationAssembler();  | 
  |  181 |    0 |                    orgPersonRelationAssembler.setMetaData(metadata);  | 
  |  182 |    0 |                    orgPersonRelationAssembler.setOrgService(orgService);  | 
  |  183 |    0 |                    Data relationData = orgPersonRelationAssembler.save(input).getValue();  | 
  |  184 |     | 
               }  | 
  |  185 |     | 
                 | 
  |  186 |    0 |                result.setValue((orgId == null) ? null : get(orgId));  | 
  |  187 |     | 
                 | 
  |  188 |    0 |            } catch (Exception e) { | 
  |  189 |    0 |                LOG.error(e);  | 
  |  190 |    0 |                throw(new AssemblyException());  | 
  |  191 |    0 |            }  | 
  |  192 |     | 
             | 
  |  193 |    0 |            return result;  | 
  |  194 |     | 
       }  | 
  |  195 |     | 
         | 
  |  196 |     | 
       public void setOrgService(OrganizationService service){ | 
  |  197 |    0 |            orgService = service;  | 
  |  198 |    0 |        }  | 
  |  199 |     | 
         | 
  |  200 |     | 
         | 
  |  201 |     | 
       private OrgInfoData buildOrgInfo(OrgHelper org){ | 
  |  202 |    0 |            OrgInfo orgInfo = new OrgInfo();  | 
  |  203 |    0 |            OrgInfoData result = new OrgInfoData();  | 
  |  204 |     | 
                     | 
  |  205 |    0 |            orgInfo.setType(org.getType());  | 
  |  206 |    0 |            orgInfo.setLongDesc(org.getDescription());  | 
  |  207 |    0 |            orgInfo.setShortName(org.getAbbreviation());  | 
  |  208 |    0 |            orgInfo.setLongName(org.getName());  | 
  |  209 |    0 |            orgInfo.setEffectiveDate(org.getEffectiveDate());  | 
  |  210 |    0 |            orgInfo.setExpirationDate(org.getExpirationDate());  | 
  |  211 |    0 |            if(org.getId()!=null){ | 
  |  212 |    0 |                orgInfo.setId(org.getId());  | 
  |  213 |     | 
           }  | 
  |  214 |     | 
             | 
  |  215 |    0 |            if (isModified(org.getData())) { | 
  |  216 |     | 
     | 
  |  217 |    0 |                    MetaInfo metaInfo = new MetaInfo();  | 
  |  218 |    0 |                    orgInfo.setMetaInfo(metaInfo);  | 
  |  219 |    0 |                    result.setModificationState(ModificationState.UPDATED);  | 
  |  220 |     | 
     | 
  |  221 |     | 
     | 
  |  222 |     | 
     | 
  |  223 |    0 |            }  | 
  |  224 |     | 
           else{ | 
  |  225 |    0 |                setCreated(org.getData(), true);  | 
  |  226 |    0 |                result.setModificationState(ModificationState.CREATED);  | 
  |  227 |     | 
           }  | 
  |  228 |    0 |            if(orgInfo.getMetaInfo()!=null){ | 
  |  229 |    0 |                orgInfo.getMetaInfo().setVersionInd(getVersionIndicator(org.getData()));  | 
  |  230 |     | 
           }  | 
  |  231 |     | 
     | 
  |  232 |    0 |            result.setOrgInfo(orgInfo);  | 
  |  233 |    0 |            return result;  | 
  |  234 |     | 
       }  | 
  |  235 |     | 
     | 
  |  236 |     | 
       private void saveOrg(OrgInfoData input) throws AlreadyExistsException, DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, DoesNotExistException, VersionMismatchException { | 
  |  237 |    0 |            OrgInfo result = null;  | 
  |  238 |    0 |            OrgInfo orgInfo = input.getOrgInfo();  | 
  |  239 |    0 |            DataModelDefinition def = new DataModelDefinition(metadata);  | 
  |  240 |    0 |            orgProposalModel.setDefinition(def);  | 
  |  241 |    0 |            QueryPath metaPath = QueryPath.concat(null, ORG_INFO_PATH);  | 
  |  242 |    0 |            Metadata orgProposalMeta =orgProposalModel.getMetadata(metaPath);  | 
  |  243 |    0 |            if (input.getModificationState() != null) { | 
  |  244 |    0 |                if (orgProposalMeta.isCanEdit()) { | 
  |  245 |    0 |                    switch (input.getModificationState()) { | 
  |  246 |     | 
                       case CREATED:  | 
  |  247 |    0 |                            result = orgService.createOrganization(orgInfo.getType(), orgInfo);  | 
  |  248 |    0 |                            break;  | 
  |  249 |     | 
                       case UPDATED:  | 
  |  250 |    0 |                            result = orgService.updateOrganization(orgInfo.getId(), orgInfo);  | 
  |  251 |     | 
                       default:  | 
  |  252 |     | 
                   }  | 
  |  253 |     | 
               }  | 
  |  254 |     | 
           }  | 
  |  255 |    0 |            if (result != null) { | 
  |  256 |    0 |                input.setOrgInfo(result);  | 
  |  257 |     | 
           }  | 
  |  258 |    0 |        }  | 
  |  259 |     | 
     | 
  |  260 |     | 
       @Override  | 
  |  261 |     | 
       public List<ValidationResultInfo> validate(Data input) throws AssemblyException { | 
  |  262 |     | 
             | 
  |  263 |    0 |            return null;  | 
  |  264 |     | 
       }  | 
  |  265 |     | 
     | 
  |  266 |     | 
       private <T extends Data> T copyIfExists(Data source, T target, String key) { | 
  |  267 |    0 |            Data d = source.get(key);  | 
  |  268 |    0 |            if (d == null) { | 
  |  269 |    0 |                return null;  | 
  |  270 |     | 
           } else { | 
  |  271 |    0 |                d.copy(target, false);  | 
  |  272 |    0 |                source.set(key, target);  | 
  |  273 |    0 |                return target;  | 
  |  274 |     | 
           }  | 
  |  275 |     | 
       }  | 
  |  276 |     | 
         | 
  |  277 |     | 
       private OrgHelper buildOrgDataMap(OrgInfoData orgInfoData){ | 
  |  278 |    0 |            OrgHelper org =  OrgHelper.wrap(new Data());  | 
  |  279 |    0 |            org.setId(orgInfoData.getOrgInfo().getId());  | 
  |  280 |    0 |            org.setType(orgInfoData.getOrgInfo().getType());  | 
  |  281 |    0 |            org.setName(orgInfoData.getOrgInfo().getLongName());  | 
  |  282 |    0 |            org.setAbbreviation(orgInfoData.getOrgInfo().getShortName());  | 
  |  283 |    0 |            org.setDescription(orgInfoData.getOrgInfo().getLongDesc());  | 
  |  284 |    0 |            org.setEffectiveDate(orgInfoData.getOrgInfo().getEffectiveDate());  | 
  |  285 |    0 |            org.setExpirationDate(orgInfoData.getOrgInfo().getExpirationDate());  | 
  |  286 |    0 |            setUpdated(org.getData(), true);  | 
  |  287 |    0 |            addVersionIndicator(org.getData(),OrgInfo.class.getName(),orgInfoData.getOrgInfo().getId(),orgInfoData.getOrgInfo().getMetaInfo().getVersionInd());  | 
  |  288 |     | 
     | 
  |  289 |    0 |            return org;  | 
  |  290 |     | 
       }  | 
  |  291 |     | 
         | 
  |  292 |     | 
           @Override  | 
  |  293 |     | 
       protected String getDataType() { | 
  |  294 |    0 |            return ORG_PROPOSAL_DATA_TYPE;  | 
  |  295 |     | 
       }  | 
  |  296 |     | 
     | 
  |  297 |     | 
       @Override  | 
  |  298 |     | 
       protected String getDocumentPropertyName() { | 
  |  299 |    0 |            return "course";  | 
  |  300 |     | 
       }  | 
  |  301 |     | 
     | 
  |  302 |     | 
       @Override  | 
  |  303 |     | 
       protected String getDtoName() { | 
  |  304 |    0 |            return PERM_DTO_NAME;  | 
  |  305 |     | 
       }  | 
  |  306 |     | 
     | 
  |  307 |     | 
       @Override  | 
  |  308 |     | 
       protected AttributeSet getQualification(String idType, String id) { | 
  |  309 |    0 |            AttributeSet qualification = null;  | 
  |  310 |    0 |            if(id!=null&&!id.isEmpty()){ | 
  |  311 |    0 |             qualification = new AttributeSet();  | 
  |  312 |     | 
             | 
  |  313 |     | 
     | 
  |  314 |     | 
     | 
  |  315 |    0 |            qualification.put(idType, id);  | 
  |  316 |     | 
           }  | 
  |  317 |     | 
             | 
  |  318 |    0 |            return qualification;  | 
  |  319 |     | 
       }  | 
  |  320 |     | 
        | 
  |  321 |     | 
   }  |