Search in sources :

Example 1 with KoskiPersonState

use of fi.otavanopisto.pyramus.domainmodel.koski.KoskiPersonState in project pyramus by otavanopisto.

the class KoskiClient method updatePersonToKoski.

private boolean updatePersonToKoski(Oppija oppija, Person person, String personOid) {
    try {
        String uri = String.format("%s/oppija", getBaseUrl());
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(uri);
        Builder request = target.request(MediaType.APPLICATION_JSON_TYPE).header("Authorization", "Basic " + getAuth());
        ObjectMapper mapper = new ObjectMapper();
        StringWriter writer = new StringWriter();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        mapper.writeValue(writer, oppija);
        String requestStr = writer.toString();
        Response response = request.put(Entity.json(requestStr));
        if (response.getStatus() == 200) {
            String ret = response.readEntity(String.class);
            // For test environment the oid's are not saved
            if (!settings.isTestEnvironment()) {
                OppijaReturnVal oppijaReturnVal = mapper.readValue(ret, OppijaReturnVal.class);
                String servedPersonOid = oppijaReturnVal.getHenkilo().getOid();
                if (StringUtils.isEmpty(personOid)) {
                    // If the oid was empty in db, save the given one
                    personVariableDAO.setPersonVariable(person, KOSKI_HENKILO_OID, servedPersonOid);
                } else {
                    // Validate the oid is the same
                    if (!StringUtils.equals(personOid, servedPersonOid))
                        throw new RuntimeException("Returned person oid doesn't match the saved oid");
                }
                Set<SourceSystemId> invalidatedSourceSystemIds = oppija.getOpiskeluoikeudet().stream().filter(opiskeluoikeus -> opiskeluoikeus.getTila().getOpiskeluoikeusjaksot().stream().anyMatch(opiskeluoikeusjakso -> opiskeluoikeusjakso.getTila().getValue() == OpiskeluoikeudenTila.mitatoity)).map(opiskeluoikeus -> parseSource(opiskeluoikeus.getLahdejarjestelmanId().getId())).collect(Collectors.toSet());
                for (OpiskeluoikeusReturnVal opiskeluoikeus : oppijaReturnVal.getOpiskeluoikeudet()) {
                    if (opiskeluoikeus.getLahdejarjestelmanId() != null && opiskeluoikeus.getLahdejarjestelmanId().getLahdejarjestelma() != null && StringUtils.equals(opiskeluoikeus.getLahdejarjestelmanId().getLahdejarjestelma().getKoodiarvo(), "pyramus")) {
                        SourceSystemId sourceSystemId = parseSource(opiskeluoikeus.getLahdejarjestelmanId().getId());
                        if (sourceSystemId != null) {
                            Student reportedStudent = studentDAO.findById(sourceSystemId.getStudentId());
                            KoskiStudentHandler handler = koskiController.getStudentHandler(sourceSystemId.getHandler());
                            if (reportedStudent.getArchived() || invalidatedSourceSystemIds.contains(sourceSystemId)) {
                                // For archived or invalidated student the studypermission oid is cleared as Koski doesn't want to receive this id ever again
                                handler.removeOid(sourceSystemId.getHandler(), reportedStudent, opiskeluoikeus.getOid());
                            } else {
                                handler.saveOrValidateOid(sourceSystemId.getHandler(), reportedStudent, opiskeluoikeus.getOid());
                            }
                        } else {
                            logger.log(Level.WARNING, String.format("Could not update student oid because returned source system id couldn't be parsed (Person %d).", person.getId()));
                        }
                    } else {
                        logger.log(Level.WARNING, String.format("Could not update student oid because returned source system was not defined or isn't this system."));
                    }
                }
            }
            // Log successful event
            koskiPersonLogDAO.create(person, KoskiPersonState.SUCCESS, new Date());
            logger.info(String.format("KoskiClient: successfully updated person %d.", person.getId()));
            return true;
        } else {
            String ret = response.readEntity(String.class);
            String errorMessage = errorResponseMessage(ret);
            // Log failed event
            koskiPersonLogDAO.create(person, KoskiPersonState.SERVER_FAILURE, new Date(), errorMessage);
            logger.log(Level.SEVERE, String.format("Koski server returned %d when trying to create person %d. Content %s", response.getStatus(), person.getId(), ret));
            return false;
        }
    } catch (Exception ex) {
        try {
            KoskiPersonState reason = KoskiPersonState.UNKNOWN_FAILURE;
            if (ex instanceof KoskiException) {
                reason = ((KoskiException) ex).getState();
            }
            if (ex instanceof ProcessingException) {
                ProcessingException pe = (ProcessingException) ex;
                if (pe.getCause() instanceof ConnectException) {
                    reason = KoskiPersonState.SERVER_UNAVAILABLE;
                }
            }
            // Log failed event
            koskiPersonLogDAO.create(person, reason, new Date(), ex.getMessage());
        } catch (Exception e) {
        }
        logger.log(Level.SEVERE, String.format("Unknown failure while updating person %d", person.getId()), ex);
        return false;
    }
}
Also used : Opiskeluoikeus(fi.otavanopisto.pyramus.koski.model.Opiskeluoikeus) Arrays(java.util.Arrays) KoskiPersonState(fi.otavanopisto.pyramus.domainmodel.koski.KoskiPersonState) Date(java.util.Date) PersonVariableDAO(fi.otavanopisto.pyramus.dao.users.PersonVariableDAO) Client(javax.ws.rs.client.Client) SimpleDateFormat(java.text.SimpleDateFormat) SettingKeyDAO(fi.otavanopisto.pyramus.dao.system.SettingKeyDAO) KoskiErrorMessageBody(fi.otavanopisto.pyramus.koski.model.result.KoskiErrorMessageBody) StringUtils(org.apache.commons.lang3.StringUtils) Level(java.util.logging.Level) Inject(javax.inject.Inject) ClientBuilder(javax.ws.rs.client.ClientBuilder) Lahdejarjestelma(fi.otavanopisto.pyramus.koski.koodisto.Lahdejarjestelma) MediaType(javax.ws.rs.core.MediaType) Builder(javax.ws.rs.client.Invocation.Builder) Student(fi.otavanopisto.pyramus.domainmodel.students.Student) CollectionUtils(org.apache.commons.collections.CollectionUtils) OpiskeluoikeusReturnVal(fi.otavanopisto.pyramus.koski.model.result.OpiskeluoikeusReturnVal) KoskiPersonLogDAO(fi.otavanopisto.pyramus.dao.koski.KoskiPersonLogDAO) Oppija(fi.otavanopisto.pyramus.koski.model.Oppija) ConnectException(java.net.ConnectException) KoskiPersonLog(fi.otavanopisto.pyramus.domainmodel.koski.KoskiPersonLog) SettingKey(fi.otavanopisto.pyramus.domainmodel.system.SettingKey) NoLatestStudentException(fi.otavanopisto.pyramus.koski.exception.NoLatestStudentException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Person(fi.otavanopisto.pyramus.domainmodel.base.Person) SettingDAO(fi.otavanopisto.pyramus.dao.system.SettingDAO) Setting(fi.otavanopisto.pyramus.domainmodel.system.Setting) OpiskeluoikeudenTila(fi.otavanopisto.pyramus.koski.koodisto.OpiskeluoikeudenTila) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Entity(javax.ws.rs.client.Entity) OppijaReturnVal(fi.otavanopisto.pyramus.koski.model.result.OppijaReturnVal) Objects(java.util.Objects) List(java.util.List) OpiskeluoikeusJakso(fi.otavanopisto.pyramus.koski.model.OpiskeluoikeusJakso) Response(javax.ws.rs.core.Response) ProcessingException(javax.ws.rs.ProcessingException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) WebTarget(javax.ws.rs.client.WebTarget) StudentDAO(fi.otavanopisto.pyramus.dao.students.StudentDAO) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) OppijaReturnVal(fi.otavanopisto.pyramus.koski.model.result.OppijaReturnVal) Student(fi.otavanopisto.pyramus.domainmodel.students.Student) KoskiPersonState(fi.otavanopisto.pyramus.domainmodel.koski.KoskiPersonState) Date(java.util.Date) ConnectException(java.net.ConnectException) NoLatestStudentException(fi.otavanopisto.pyramus.koski.exception.NoLatestStudentException) ProcessingException(javax.ws.rs.ProcessingException) Response(javax.ws.rs.core.Response) StringWriter(java.io.StringWriter) OpiskeluoikeusReturnVal(fi.otavanopisto.pyramus.koski.model.result.OpiskeluoikeusReturnVal) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) SimpleDateFormat(java.text.SimpleDateFormat) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ProcessingException(javax.ws.rs.ProcessingException) ConnectException(java.net.ConnectException)

Aggregations

TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 KoskiPersonLogDAO (fi.otavanopisto.pyramus.dao.koski.KoskiPersonLogDAO)1 StudentDAO (fi.otavanopisto.pyramus.dao.students.StudentDAO)1 SettingDAO (fi.otavanopisto.pyramus.dao.system.SettingDAO)1 SettingKeyDAO (fi.otavanopisto.pyramus.dao.system.SettingKeyDAO)1 PersonVariableDAO (fi.otavanopisto.pyramus.dao.users.PersonVariableDAO)1 Person (fi.otavanopisto.pyramus.domainmodel.base.Person)1 KoskiPersonLog (fi.otavanopisto.pyramus.domainmodel.koski.KoskiPersonLog)1 KoskiPersonState (fi.otavanopisto.pyramus.domainmodel.koski.KoskiPersonState)1 Student (fi.otavanopisto.pyramus.domainmodel.students.Student)1 Setting (fi.otavanopisto.pyramus.domainmodel.system.Setting)1 SettingKey (fi.otavanopisto.pyramus.domainmodel.system.SettingKey)1 NoLatestStudentException (fi.otavanopisto.pyramus.koski.exception.NoLatestStudentException)1 Lahdejarjestelma (fi.otavanopisto.pyramus.koski.koodisto.Lahdejarjestelma)1 OpiskeluoikeudenTila (fi.otavanopisto.pyramus.koski.koodisto.OpiskeluoikeudenTila)1 Opiskeluoikeus (fi.otavanopisto.pyramus.koski.model.Opiskeluoikeus)1 OpiskeluoikeusJakso (fi.otavanopisto.pyramus.koski.model.OpiskeluoikeusJakso)1 Oppija (fi.otavanopisto.pyramus.koski.model.Oppija)1 KoskiErrorMessageBody (fi.otavanopisto.pyramus.koski.model.result.KoskiErrorMessageBody)1