001    package org.kuali.student.lum.program.server;
002    
003    import java.util.List;
004    import java.util.Map;
005    
006    import org.kuali.student.common.dto.DtoConstants;
007    import org.kuali.student.common.exceptions.InvalidParameterException;
008    import org.kuali.student.common.exceptions.OperationFailedException;
009    import org.kuali.student.common.ui.server.gwt.AbstractDataService;
010    import org.kuali.student.common.validation.dto.ValidationResultInfo;
011    import org.kuali.student.lum.lu.service.LuService;
012    import org.kuali.student.lum.program.client.ProgramClientConstants;
013    import org.kuali.student.lum.program.dto.CredentialProgramInfo;
014    import org.kuali.student.lum.program.dto.MajorDisciplineInfo;
015    import org.kuali.student.lum.program.service.ProgramService;
016    
017    /**
018     * @author Igor
019     */
020    public class CredentialProgramDataService extends AbstractDataService {
021    
022        private static final long serialVersionUID = 1L;
023        
024        private ProgramService programService;
025        private LuService luService;
026        
027        @Override
028        protected String getDefaultWorkflowDocumentType() {
029            return null;
030        }
031    
032        @Override
033        protected String getDefaultMetaDataState() {
034            return null;
035        }
036    
037        @Override
038        protected Object get(String id) throws Exception {
039            if (ProgramClientConstants.CREDENTIAL_PROGRAM_TYPES.contains(id)){
040                List<String> credIds = luService.getCluIdsByLuType(id, DtoConstants.STATE_ACTIVE);
041                if (null == credIds || credIds.size() != 1) {
042                    throw new OperationFailedException("A single credential program of type " + id + " is required; database contains " +
043                                                        (null == credIds ? "0" : credIds.size() +
044                                                        "."));
045                }
046                return programService.getCredentialProgram(credIds.get(0));
047            } else {
048                    return programService.getCredentialProgram(id);
049            }
050        }
051    
052        @Override
053        protected Object save(Object dto, Map<String, Object> properties) throws Exception {
054            if (dto instanceof CredentialProgramInfo) {
055                CredentialProgramInfo cpInfo = (CredentialProgramInfo) dto;
056                if (cpInfo.getId() == null && cpInfo.getVersionInfo() != null) {
057                    String credentialVersionIndId = cpInfo.getVersionInfo().getVersionIndId();
058                    cpInfo = programService.createNewCredentialProgramVersion(credentialVersionIndId, "New credential program version");
059                } else if (cpInfo.getId() == null) {
060                    cpInfo = programService.createCredentialProgram(cpInfo);
061                } else {
062                    cpInfo = programService.updateCredentialProgram(cpInfo);
063                }
064                return cpInfo;
065            } else {
066                throw new InvalidParameterException("Only persistence of CredentialProgram is supported by this DataService implementation.");
067            }
068        }
069    
070        @Override
071            protected List<ValidationResultInfo> validate(Object dto) throws Exception {
072                    return programService.validateCredentialProgram("OBJECT", (CredentialProgramInfo)dto);
073            }
074        
075        @Override
076        protected Class<?> getDtoClass() {
077            return CredentialProgramInfo.class;
078        }
079    
080        public void setProgramService(ProgramService programService) {
081            this.programService = programService;
082        }
083    
084        public void setLuService(LuService luService) {
085            this.luService = luService;
086        }
087    }